@@ -5,77 +5,54 @@ package common
55
66import (
77 "fmt"
8- "net/http"
98 "strings"
109
1110 actions_model "code.gitea.io/gitea/models/actions"
1211 repo_model "code.gitea.io/gitea/models/repo"
1312 "code.gitea.io/gitea/modules/actions"
13+ "code.gitea.io/gitea/modules/util"
1414 "code.gitea.io/gitea/services/context"
1515)
1616
17- func DownloadActionsRunJobLogsWithIndex (ctx * context.Base , ctxRepo * repo_model.Repository , runID , jobIndex int64 ) {
18- if runID == 0 {
19- ctx .HTTPError (http .StatusBadRequest , "invalid run id" )
20- return
21- }
22-
17+ func DownloadActionsRunJobLogsWithIndex (ctx * context.Base , ctxRepo * repo_model.Repository , runID , jobIndex int64 ) error {
2318 runJobs , err := actions_model .GetRunJobsByRunID (ctx , runID )
2419 if err != nil {
25- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
26- return
20+ return fmt .Errorf ("GetRunJobsByRunID: %w" , err )
2721 }
28- if len (runJobs ) == 0 {
29- ctx .HTTPError (http .StatusNotFound )
30- return
22+ if err = runJobs .LoadRepos (ctx ); err != nil {
23+ return fmt .Errorf ("LoadRepos: %w" , err )
3124 }
32- if err := runJobs .LoadRepos (ctx ); err != nil {
33- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
34- return
35- }
36-
37- var curJob * actions_model.ActionRunJob
38- if jobIndex >= 0 && jobIndex < int64 (len (runJobs )) {
39- curJob = runJobs [jobIndex ]
25+ if 0 < jobIndex || jobIndex >= int64 (len (runJobs )) {
26+ return util .NewNotExistErrorf ("job index is out of range: %d" , jobIndex )
4027 }
41- if curJob == nil {
42- ctx .HTTPError (http .StatusNotFound )
43- return
44- }
45-
46- DownloadActionsRunJobLogs (ctx , ctxRepo , curJob )
28+ return DownloadActionsRunJobLogs (ctx , ctxRepo , runJobs [jobIndex ])
4729}
4830
49- func DownloadActionsRunJobLogs (ctx * context.Base , ctxRepo * repo_model.Repository , curJob * actions_model.ActionRunJob ) {
31+ func DownloadActionsRunJobLogs (ctx * context.Base , ctxRepo * repo_model.Repository , curJob * actions_model.ActionRunJob ) error {
5032 if curJob .Repo .ID != ctxRepo .ID {
51- ctx .HTTPError (http .StatusNotFound )
52- return
33+ return util .NewNotExistErrorf ("job not found" )
5334 }
5435
5536 if curJob .TaskID == 0 {
56- ctx .HTTPError (http .StatusNotFound , "job is not started" )
57- return
37+ return util .NewNotExistErrorf ("job not started" )
5838 }
5939
6040 if err := curJob .LoadRun (ctx ); err != nil {
61- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
62- return
41+ return fmt .Errorf ("LoadRun: %w" , err )
6342 }
6443
6544 task , err := actions_model .GetTaskByID (ctx , curJob .TaskID )
6645 if err != nil {
67- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
68- return
46+ return fmt .Errorf ("GetTaskByID: %w" , err )
6947 }
48+
7049 if task .LogExpired {
71- ctx .HTTPError (http .StatusNotFound , "logs have been cleaned up" )
72- return
50+ return util .NewNotExistErrorf ("logs have been cleaned up" )
7351 }
7452
7553 reader , err := actions .OpenLogs (ctx , task .LogInStorage , task .LogFilename )
7654 if err != nil {
77- ctx .HTTPError (http .StatusInternalServerError , err .Error ())
78- return
55+ return fmt .Errorf ("OpenLogs: %w" , err )
7956 }
8057 defer reader .Close ()
8158
@@ -90,4 +67,5 @@ func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository
9067 ContentTypeCharset : "utf-8" ,
9168 Disposition : "attachment" ,
9269 })
70+ return nil
9371}
0 commit comments