Skip to content

Commit 5debe79

Browse files
committed
fix escaping and error handling
1 parent 5b395f7 commit 5debe79

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

routers/api/v1/repo/action.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,11 @@ func ActionsGetWorkflow(ctx *context.APIContext) {
663663
workflowID := ctx.PathParam("workflow_id")
664664
workflow, err := actions_service.GetActionWorkflow(ctx, workflowID)
665665
if err != nil {
666-
ctx.Error(http.StatusInternalServerError, "GetActionWorkflow", err)
667-
return
668-
}
669-
670-
if workflow == nil {
671-
ctx.Error(http.StatusNotFound, "GetActionWorkflow", err)
666+
if errors.Is(err, util.ErrNotExist) {
667+
ctx.Error(http.StatusNotFound, "GetActionWorkflow", err)
668+
} else {
669+
ctx.Error(http.StatusInternalServerError, "GetActionWorkflow", err)
670+
}
672671
return
673672
}
674673

@@ -712,7 +711,11 @@ func ActionsDisableWorkflow(ctx *context.APIContext) {
712711
workflowID := ctx.PathParam("workflow_id")
713712
err := actions_service.DisableActionWorkflow(ctx, workflowID)
714713
if err != nil {
715-
ctx.Error(http.StatusInternalServerError, "DisableActionWorkflow", err)
714+
if errors.Is(err, util.ErrNotExist) {
715+
ctx.Error(http.StatusNotFound, "DisableActionWorkflow", err)
716+
} else {
717+
ctx.Error(http.StatusInternalServerError, "DisableActionWorkflow", err)
718+
}
716719
return
717720
}
718721

@@ -842,7 +845,11 @@ func ActionsEnableWorkflow(ctx *context.APIContext) {
842845
workflowID := ctx.PathParam("workflow_id")
843846
err := actions_service.EnableActionWorkflow(ctx, workflowID)
844847
if err != nil {
845-
ctx.Error(http.StatusInternalServerError, "EnableActionWorkflow", err)
848+
if errors.Is(err, util.ErrNotExist) {
849+
ctx.Error(http.StatusNotFound, "EnableActionWorkflow", err)
850+
} else {
851+
ctx.Error(http.StatusInternalServerError, "EnableActionWorkflow", err)
852+
}
846853
return
847854
}
848855

services/actions/workflow.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"fmt"
88
"net/http"
9+
"net/url"
910
"path"
1011
"strings"
1112

@@ -29,9 +30,9 @@ import (
2930

3031
func getActionWorkflowPath(commit *git.Commit) string {
3132
paths := []string{".gitea/workflows", ".github/workflows"}
32-
for _, path := range paths {
33-
if _, err := commit.SubTree(path); err == nil {
34-
return path
33+
for _, treePath := range paths {
34+
if _, err := commit.SubTree(treePath); err == nil {
35+
return treePath
3536
}
3637
}
3738
return ""
@@ -43,9 +44,9 @@ func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder
4344

4445
defaultBranch, _ := commit.GetBranchName()
4546

46-
URL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), entry.Name())
47-
HTMLURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), defaultBranch, folder, entry.Name())
48-
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), entry.Name(), ctx.Repo.Repository.DefaultBranch)
47+
workflowURL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), url.PathEscape(entry.Name()))
48+
workflowRepoURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), util.PathEscapeSegments(defaultBranch), util.PathEscapeSegments(folder), url.PathEscape(entry.Name()))
49+
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), url.PathEscape(entry.Name()), url.QueryEscape(ctx.Repo.Repository.DefaultBranch))
4950

5051
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
5152
// State types:
@@ -74,8 +75,8 @@ func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder
7475
State: state,
7576
CreatedAt: createdAt,
7677
UpdatedAt: updatedAt,
77-
URL: URL,
78-
HTMLURL: HTMLURL,
78+
URL: workflowURL,
79+
HTMLURL: workflowRepoURL,
7980
BadgeURL: badgeURL,
8081
}
8182
}
@@ -133,7 +134,7 @@ func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionW
133134
}
134135
}
135136

136-
return nil, fmt.Errorf("workflow '%s' not found", workflowID)
137+
return nil, util.NewNotExistErrorf("workflow %q not found", workflowID)
137138
}
138139

139140
func DisableActionWorkflow(ctx *context.APIContext, workflowID string) error {

0 commit comments

Comments
 (0)