44package repo
55
66import (
7- "errors"
8- "fmt"
9- "net/http"
10- "strings"
11-
127 actions_model "code.gitea.io/gitea/models/actions"
13- "code.gitea.io/gitea/modules/actions"
14- "code.gitea.io/gitea/modules/util"
8+ "code.gitea.io/gitea/routers/common"
159 "code.gitea.io/gitea/services/context"
1610)
1711
18- func getRunIndex (ctx * context.APIContext ) int64 {
12+ func getRunID (ctx * context.APIContext ) int64 {
1913 // if run param is "latest", get the latest run index
20- if ctx .PathParam ("run_id " ) == "latest" {
14+ if ctx .PathParam ("run " ) == "latest" {
2115 if run , _ := actions_model .GetLatestRun (ctx , ctx .Repo .Repository .ID ); run != nil {
22- return run .Index
23- }
24- }
25- return ctx .PathParamInt64 ("run_id" )
26- }
27-
28- // getRunJobs gets the jobs of runIndex, and returns jobs[jobIndex], jobs.
29- // Any error will be written to the ctx.
30- // It never returns a nil job of an empty jobs, if the jobIndex is out of range, it will be treated as 0.
31- func getRunJobs (ctx * context.APIContext , runIndex , jobIndex int64 ) (* actions_model.ActionRunJob , []* actions_model.ActionRunJob ) {
32- run , err := actions_model .GetRunByIndex (ctx , ctx .Repo .Repository .ID , runIndex )
33- if err != nil {
34- if errors .Is (err , util .ErrNotExist ) {
35- ctx .HTTPError (http .StatusNotFound , err .Error ())
36- return nil , nil
16+ return run .ID
3717 }
38- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
39- return nil , nil
40- }
41- run .Repo = ctx .Repo .Repository
42- jobs , err := actions_model .GetRunJobsByRunID (ctx , run .ID )
43- if err != nil {
44- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
45- return nil , nil
4618 }
47- if len (jobs ) == 0 {
48- ctx .HTTPError (http .StatusNotFound )
49- return nil , nil
50- }
51-
52- for _ , v := range jobs {
53- v .Run = run
54- }
55-
56- if jobIndex >= 0 && jobIndex < int64 (len (jobs )) {
57- return jobs [jobIndex ], jobs
58- }
59- return jobs [0 ], jobs
19+ return ctx .PathParamInt64 ("run" )
6020}
6121
62- func DownloadActionsRunLogs (ctx * context.APIContext ) {
63- // swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run_id }/jobs/{job}/logs repository downloadActionsRunLogs
22+ func DownloadActionsRunJobLogs (ctx * context.APIContext ) {
23+ // swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run }/jobs/{job}/logs repository downloadActionsRunJobLogs
6424 // ---
6525 // summary: Downloads the logs for a workflow run redirects to blob url
6626 // produces:
@@ -76,7 +36,7 @@ func DownloadActionsRunLogs(ctx *context.APIContext) {
7636 // description: name of the repository
7737 // type: string
7838 // required: true
79- // - name: run_id
39+ // - name: run
8040 // in: path
8141 // description: id of the run, this could be latest
8242 // type: integer
@@ -87,57 +47,14 @@ func DownloadActionsRunLogs(ctx *context.APIContext) {
8747 // type: integer
8848 // required: true
8949 // responses:
90- // "302 ":
91- // description: redirect to the blob download
50+ // "200 ":
51+ // description: output blob content
9252 // "400":
9353 // "$ref": "#/responses/error"
9454 // "404":
9555 // "$ref": "#/responses/notFound"
9656
97- runIndex := getRunIndex (ctx )
57+ runID := getRunID (ctx )
9858 jobIndex := ctx .PathParamInt64 ("job" )
99-
100- job , _ := getRunJobs (ctx , runIndex , jobIndex )
101- if ctx .Written () {
102- return
103- }
104- if job .TaskID == 0 {
105- ctx .HTTPError (http .StatusNotFound , "job is not started" )
106- return
107- }
108-
109- err := job .LoadRun (ctx )
110- if err != nil {
111- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
112- return
113- }
114-
115- task , err := actions_model .GetTaskByID (ctx , job .TaskID )
116- if err != nil {
117- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
118- return
119- }
120- if task .LogExpired {
121- ctx .HTTPError (http .StatusNotFound , "logs have been cleaned up" )
122- return
123- }
124-
125- reader , err := actions .OpenLogs (ctx , task .LogInStorage , task .LogFilename )
126- if err != nil {
127- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
128- return
129- }
130- defer reader .Close ()
131-
132- workflowName := job .Run .WorkflowID
133- if p := strings .Index (workflowName , "." ); p > 0 {
134- workflowName = workflowName [0 :p ]
135- }
136- ctx .ServeContent (reader , & context.ServeHeaderOptions {
137- Filename : fmt .Sprintf ("%v-%v-%v.log" , workflowName , job .Name , task .ID ),
138- ContentLength : & task .LogSize ,
139- ContentType : "text/plain" ,
140- ContentTypeCharset : "utf-8" ,
141- Disposition : "attachment" ,
142- })
59+ common .DownloadActionsRunJobLogs (ctx .Base , ctx .Repo .Repository , runID , jobIndex )
14360}
0 commit comments