mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-12 13:22:55 +02:00
actions: reuse preview path helpers in devtest mock
The mock had its own normalizeMockArtifactPath / chooseMockArtifactPath that drifted from the production helpers (silent fallback to first file on invalid input, instead of "" — the new no-fallback semantics). Export GetRequestedPreviewPath and ChoosePreviewPath and call them from the mock so behavior stays in sync. Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>
This commit is contained in:
parent
0b3f92cd9a
commit
5fdb61540f
@ -157,24 +157,12 @@ end_of_record
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeMockArtifactPath(path string) string {
|
func mockArtifactFilePaths(files []mockArtifactFile) []string {
|
||||||
path = util.PathJoinRelX(path)
|
paths := make([]string, len(files))
|
||||||
if path == "." {
|
for i, file := range files {
|
||||||
return ""
|
paths[i] = file.Path
|
||||||
}
|
}
|
||||||
return path
|
return paths
|
||||||
}
|
|
||||||
|
|
||||||
func chooseMockArtifactPath(files []mockArtifactFile, requestedPath string) string {
|
|
||||||
if len(files) == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
for _, file := range files {
|
|
||||||
if file.Path == requestedPath {
|
|
||||||
return requestedPath
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return files[0].Path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type generateMockStepsLogOptions struct {
|
type generateMockStepsLogOptions struct {
|
||||||
@ -534,11 +522,7 @@ func MockActionsArtifactPreview(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedPath := normalizeMockArtifactPath(strings.TrimPrefix(ctx.PathParam("*"), "/"))
|
selectedPath := actions.ChoosePreviewPath(mockArtifactFilePaths(files), actions.GetRequestedPreviewPath(ctx))
|
||||||
if selectedPath == "" {
|
|
||||||
selectedPath = normalizeMockArtifactPath(ctx.Req.URL.Query().Get("path"))
|
|
||||||
}
|
|
||||||
selectedPath = chooseMockArtifactPath(files, selectedPath)
|
|
||||||
previewFiles := make([]actions.ArtifactPreviewFile, 0, len(files))
|
previewFiles := make([]actions.ArtifactPreviewFile, 0, len(files))
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
previewFiles = append(previewFiles, actions.ArtifactPreviewFile{
|
previewFiles = append(previewFiles, actions.ArtifactPreviewFile{
|
||||||
@ -568,11 +552,7 @@ func MockActionsArtifactPreviewRaw(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedPath := normalizeMockArtifactPath(strings.TrimPrefix(ctx.PathParam("*"), "/"))
|
selectedPath := actions.ChoosePreviewPath(mockArtifactFilePaths(files), actions.GetRequestedPreviewPath(ctx))
|
||||||
if selectedPath == "" {
|
|
||||||
selectedPath = normalizeMockArtifactPath(ctx.Req.URL.Query().Get("path"))
|
|
||||||
}
|
|
||||||
selectedPath = chooseMockArtifactPath(files, selectedPath)
|
|
||||||
if selectedPath == "" {
|
if selectedPath == "" {
|
||||||
ctx.NotFound(nil)
|
ctx.NotFound(nil)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1017,7 +1017,10 @@ func normalizeArtifactPreviewPath(path string) string {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRequestedPreviewPath(ctx *context_module.Context) string {
|
// GetRequestedPreviewPath reads the requested artifact preview path from a
|
||||||
|
// request, accepting either the trailing `/preview/raw/*` path segment or a
|
||||||
|
// `?path=` query parameter, and normalizes it to a safe relative path.
|
||||||
|
func GetRequestedPreviewPath(ctx *context_module.Context) string {
|
||||||
path := strings.TrimPrefix(ctx.PathParam("*"), "/")
|
path := strings.TrimPrefix(ctx.PathParam("*"), "/")
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = ctx.Req.URL.Query().Get("path")
|
path = ctx.Req.URL.Query().Get("path")
|
||||||
@ -1033,10 +1036,10 @@ func artifactPreviewFallbackPath(artifact *actions_model.ActionArtifact) string
|
|||||||
return artifact.ArtifactName
|
return artifact.ArtifactName
|
||||||
}
|
}
|
||||||
|
|
||||||
// choosePreviewPath resolves the preview path to render.
|
// ChoosePreviewPath resolves the preview path to render.
|
||||||
// An empty `requested` means no path was specified, so the first file is selected as a default.
|
// An empty `requested` means no path was specified, so the first file is selected as a default.
|
||||||
// A non-empty `requested` that is not present in `paths` returns "" so callers can 404 instead of silently swapping to a different file.
|
// A non-empty `requested` that is not present in `paths` returns "" so callers can 404 instead of silently swapping to a different file.
|
||||||
func choosePreviewPath(paths []string, requested string) string {
|
func ChoosePreviewPath(paths []string, requested string) string {
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -1249,7 +1252,7 @@ func ArtifactsPreviewView(ctx *context_module.Context) {
|
|||||||
ctx.ServerError("listPreviewPaths", err)
|
ctx.ServerError("listPreviewPaths", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
selectedPath := choosePreviewPath(paths, getRequestedPreviewPath(ctx))
|
selectedPath := ChoosePreviewPath(paths, GetRequestedPreviewPath(ctx))
|
||||||
|
|
||||||
previewFiles := make([]ArtifactPreviewFile, 0, len(paths))
|
previewFiles := make([]ArtifactPreviewFile, 0, len(paths))
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
@ -1289,7 +1292,7 @@ func ArtifactsPreviewRawView(ctx *context_module.Context) {
|
|||||||
ctx.ServerError("listPreviewPaths", err)
|
ctx.ServerError("listPreviewPaths", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
selectedPath := choosePreviewPath(paths, getRequestedPreviewPath(ctx))
|
selectedPath := ChoosePreviewPath(paths, GetRequestedPreviewPath(ctx))
|
||||||
if selectedPath == "" {
|
if selectedPath == "" {
|
||||||
ctx.HTTPError(http.StatusNotFound, "artifact file not found")
|
ctx.HTTPError(http.StatusNotFound, "artifact file not found")
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user