Skip to content

Commit becd15f

Browse files
authored
Fix Actions API (#35204)
Fixed a nil pointer error. Related #34337
1 parent 920d62c commit becd15f

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

routers/api/v1/repo/action.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,18 +1132,23 @@ func GetWorkflowRun(ctx *context.APIContext) {
11321132
// "$ref": "#/responses/notFound"
11331133

11341134
runID := ctx.PathParamInt64("run")
1135-
job, _, err := db.GetByID[actions_model.ActionRun](ctx, runID)
1135+
job, has, err := db.GetByID[actions_model.ActionRun](ctx, runID)
1136+
if err != nil {
1137+
ctx.APIErrorInternal(err)
1138+
return
1139+
}
11361140

1137-
if err != nil || job.RepoID != ctx.Repo.Repository.ID {
1138-
ctx.APIError(http.StatusNotFound, util.ErrNotExist)
1141+
if !has || job.RepoID != ctx.Repo.Repository.ID {
1142+
ctx.APIErrorNotFound(util.ErrNotExist)
1143+
return
11391144
}
11401145

1141-
convertedArtifact, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job)
1146+
convertedRun, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job)
11421147
if err != nil {
11431148
ctx.APIErrorInternal(err)
11441149
return
11451150
}
1146-
ctx.JSON(http.StatusOK, convertedArtifact)
1151+
ctx.JSON(http.StatusOK, convertedRun)
11471152
}
11481153

11491154
// ListWorkflowRunJobs Lists all jobs for a workflow run.
@@ -1237,10 +1242,15 @@ func GetWorkflowJob(ctx *context.APIContext) {
12371242
// "$ref": "#/responses/notFound"
12381243

12391244
jobID := ctx.PathParamInt64("job_id")
1240-
job, _, err := db.GetByID[actions_model.ActionRunJob](ctx, jobID)
1245+
job, has, err := db.GetByID[actions_model.ActionRunJob](ctx, jobID)
1246+
if err != nil {
1247+
ctx.APIErrorInternal(err)
1248+
return
1249+
}
12411250

1242-
if err != nil || job.RepoID != ctx.Repo.Repository.ID {
1243-
ctx.APIError(http.StatusNotFound, util.ErrNotExist)
1251+
if !has || job.RepoID != ctx.Repo.Repository.ID {
1252+
ctx.APIErrorNotFound(util.ErrNotExist)
1253+
return
12441254
}
12451255

12461256
convertedWorkflowJob, err := convert.ToActionWorkflowJob(ctx, ctx.Repo.Repository, nil, job)
@@ -1251,7 +1261,7 @@ func GetWorkflowJob(ctx *context.APIContext) {
12511261
ctx.JSON(http.StatusOK, convertedWorkflowJob)
12521262
}
12531263

1254-
// GetArtifacts Lists all artifacts for a repository.
1264+
// GetArtifactsOfRun Lists all artifacts for a repository.
12551265
func GetArtifactsOfRun(ctx *context.APIContext) {
12561266
// swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run}/artifacts repository getArtifactsOfRun
12571267
// ---
@@ -1354,7 +1364,7 @@ func DeleteActionRun(ctx *context.APIContext) {
13541364
runID := ctx.PathParamInt64("run")
13551365
run, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID)
13561366
if errors.Is(err, util.ErrNotExist) {
1357-
ctx.APIError(http.StatusNotFound, err)
1367+
ctx.APIErrorNotFound(err)
13581368
return
13591369
} else if err != nil {
13601370
ctx.APIErrorInternal(err)

tests/integration/api_actions_delete_run_test.go renamed to tests/integration/api_actions_run_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,44 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
)
2020

21+
func TestAPIActionsGetWorkflowRun(t *testing.T) {
22+
defer prepareTestEnvActionsArtifacts(t)()
23+
24+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
25+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
26+
session := loginUser(t, user.Name)
27+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
28+
29+
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/runs/802802", repo.FullName())).
30+
AddTokenAuth(token)
31+
MakeRequest(t, req, http.StatusNotFound)
32+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/runs/802", repo.FullName())).
33+
AddTokenAuth(token)
34+
MakeRequest(t, req, http.StatusNotFound)
35+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/runs/803", repo.FullName())).
36+
AddTokenAuth(token)
37+
MakeRequest(t, req, http.StatusOK)
38+
}
39+
40+
func TestAPIActionsGetWorkflowJob(t *testing.T) {
41+
defer prepareTestEnvActionsArtifacts(t)()
42+
43+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
44+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
45+
session := loginUser(t, user.Name)
46+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
47+
48+
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/jobs/198198", repo.FullName())).
49+
AddTokenAuth(token)
50+
MakeRequest(t, req, http.StatusNotFound)
51+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/jobs/198", repo.FullName())).
52+
AddTokenAuth(token)
53+
MakeRequest(t, req, http.StatusOK)
54+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/jobs/196", repo.FullName())).
55+
AddTokenAuth(token)
56+
MakeRequest(t, req, http.StatusNotFound)
57+
}
58+
2159
func TestAPIActionsDeleteRunCheckPermission(t *testing.T) {
2260
defer prepareTestEnvActionsArtifacts(t)()
2361

0 commit comments

Comments
 (0)