Skip to content

Commit 940bfe9

Browse files
committed
Use job_id for the api endpoint
1 parent 24b215a commit 940bfe9

File tree

7 files changed

+86
-62
lines changed

7 files changed

+86
-62
lines changed

models/actions/run_job.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ func (job *ActionRunJob) LoadRun(ctx context.Context) error {
6060
return nil
6161
}
6262

63+
func (job *ActionRunJob) LoadRepo(ctx context.Context) error {
64+
if job.Repo == nil {
65+
repo, err := repo_model.GetRepositoryByID(ctx, job.RepoID)
66+
if err != nil {
67+
return err
68+
}
69+
job.Repo = repo
70+
}
71+
return nil
72+
}
73+
6374
// LoadAttributes load Run if not loaded
6475
func (job *ActionRunJob) LoadAttributes(ctx context.Context) error {
6576
if job == nil {

routers/api/v1/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,8 +1168,8 @@ func Routes() *web.Router {
11681168
m.Post("/{workflow_id}/dispatches", reqRepoWriter(unit.TypeActions), bind(api.CreateActionWorkflowDispatch{}), repo.ActionsDispatchWorkflow)
11691169
}, context.ReferencesGitRepo(), reqToken(), reqRepoReader(unit.TypeActions))
11701170

1171-
m.Group("/actions/runs", func() {
1172-
m.Get("/{run}/jobs/{job}/logs", repo.DownloadActionsRunJobLogs)
1171+
m.Group("/actions/jobs", func() {
1172+
m.Get("/{job_id}/logs", repo.DownloadActionsRunJobLogs)
11731173
}, reqToken(), reqRepoReader(unit.TypeActions))
11741174

11751175
m.Group("/hooks/git", func() {

routers/api/v1/repo/actions_run.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func getRunID(ctx *context.APIContext) int64 {
2020
}
2121

2222
func DownloadActionsRunJobLogs(ctx *context.APIContext) {
23-
// swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run}/jobs/{job}/logs repository downloadActionsRunJobLogs
23+
// swagger:operation GET /repos/{owner}/{repo}/actions/jobs/{job_id}/logs repository downloadActionsRunJobLogs
2424
// ---
2525
// summary: Downloads the logs for a workflow run redirects to blob url
2626
// produces:
@@ -36,12 +36,7 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
3636
// description: name of the repository
3737
// type: string
3838
// required: true
39-
// - name: run
40-
// in: path
41-
// description: id of the run, this could be latest
42-
// type: integer
43-
// required: true
44-
// - name: job
39+
// - name: job_id
4540
// in: path
4641
// description: id of the job
4742
// type: integer
@@ -54,7 +49,20 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
5449
// "404":
5550
// "$ref": "#/responses/notFound"
5651

57-
runID := getRunID(ctx)
58-
jobIndex := ctx.PathParamInt64("job")
59-
common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, runID, jobIndex)
52+
jobID := ctx.PathParamInt64("job_id")
53+
if jobID == 0 {
54+
ctx.APIError(400, "invalid job id")
55+
return
56+
}
57+
curJob, err := actions_model.GetRunJobByID(ctx, jobID)
58+
if err != nil {
59+
ctx.APIErrorInternal(err)
60+
return
61+
}
62+
if err := curJob.LoadRepo(ctx); err != nil {
63+
ctx.APIErrorInternal(err)
64+
return
65+
}
66+
67+
common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob)
6068
}

routers/common/actions.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"code.gitea.io/gitea/services/context"
1515
)
1616

17-
func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) {
17+
func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) {
1818
if runID == 0 {
1919
ctx.HTTPError(http.StatusBadRequest, "invalid run id")
2020
return
@@ -33,10 +33,6 @@ func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository
3333
ctx.HTTPError(http.StatusInternalServerError, err.Error())
3434
return
3535
}
36-
if runJobs[0].Repo.ID != ctxRepo.ID {
37-
ctx.HTTPError(http.StatusNotFound)
38-
return
39-
}
4036

4137
var curJob *actions_model.ActionRunJob
4238
if jobIndex >= 0 && jobIndex < int64(len(runJobs)) {
@@ -47,6 +43,15 @@ func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository
4743
return
4844
}
4945

46+
DownloadActionsRunJobLogs(ctx, ctxRepo, curJob)
47+
}
48+
49+
func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, curJob *actions_model.ActionRunJob) {
50+
if curJob.Repo.ID != ctxRepo.ID {
51+
ctx.HTTPError(http.StatusNotFound)
52+
return
53+
}
54+
5055
if curJob.TaskID == 0 {
5156
ctx.HTTPError(http.StatusNotFound, "job is not started")
5257
return

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func Logs(ctx *context_module.Context) {
477477
return
478478
}
479479

480-
common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, run.ID, jobIndex)
480+
common.DownloadActionsRunJobLogsWithIndex(ctx.Base, ctx.Repo.Repository, run.ID, jobIndex)
481481
}
482482

483483
func Cancel(ctx *context_module.Context) {

templates/swagger/v1_json.tmpl

Lines changed: 34 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/actions_log_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
actions_model "code.gitea.io/gitea/models/actions"
1516
auth_model "code.gitea.io/gitea/models/auth"
1617
repo_model "code.gitea.io/gitea/models/repo"
1718
"code.gitea.io/gitea/models/unittest"
@@ -36,7 +37,7 @@ func TestDownloadTaskLogs(t *testing.T) {
3637
{
3738
treePath: ".gitea/workflows/download-task-logs-zstd.yml",
3839
fileContent: `name: download-task-logs-zstd
39-
on:
40+
on:
4041
push:
4142
paths:
4243
- '.gitea/workflows/download-task-logs-zstd.yml'
@@ -68,7 +69,7 @@ jobs:
6869
{
6970
treePath: ".gitea/workflows/download-task-logs-no-zstd.yml",
7071
fileContent: `name: download-task-logs-no-zstd
71-
on:
72+
on:
7273
push:
7374
paths:
7475
- '.gitea/workflows/download-task-logs-no-zstd.yml'
@@ -151,8 +152,14 @@ jobs:
151152
}
152153

153154
runID, _ := strconv.ParseInt(task.Context.GetFields()["run_id"].GetStringValue(), 10, 64)
155+
156+
jobs, err := actions_model.GetRunJobsByRunID(t.Context(), runID)
157+
assert.NoError(t, err)
158+
assert.Len(t, jobs, 1)
159+
jobID := jobs[0].ID
160+
154161
// download task logs from API and check content
155-
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/actions/runs/%d/jobs/0/logs", user2.Name, repo.Name, runID)).
162+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/actions/jobs/%d/logs", user2.Name, repo.Name, jobID)).
156163
AddTokenAuth(token)
157164
resp = MakeRequest(t, req, http.StatusOK)
158165
logTextLines = strings.Split(strings.TrimSpace(resp.Body.String()), "\n")

0 commit comments

Comments
 (0)