Skip to content

Commit e712ff0

Browse files
committed
fix
1 parent 1afd23d commit e712ff0

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

routers/api/v1/repo/actions_run.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
package repo
55

66
import (
7+
"errors"
8+
79
actions_model "code.gitea.io/gitea/models/actions"
10+
"code.gitea.io/gitea/modules/util"
811
"code.gitea.io/gitea/routers/common"
912
"code.gitea.io/gitea/services/context"
1013
)
@@ -40,19 +43,22 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
4043
// "$ref": "#/responses/notFound"
4144

4245
jobID := ctx.PathParamInt64("job_id")
43-
if jobID == 0 {
44-
ctx.APIError(400, "invalid job id")
45-
return
46-
}
4746
curJob, err := actions_model.GetRunJobByID(ctx, jobID)
4847
if err != nil {
4948
ctx.APIErrorInternal(err)
5049
return
5150
}
52-
if err := curJob.LoadRepo(ctx); err != nil {
51+
if err = curJob.LoadRepo(ctx); err != nil {
5352
ctx.APIErrorInternal(err)
5453
return
5554
}
5655

57-
common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob)
56+
err = common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob)
57+
if err != nil {
58+
if errors.Is(err, util.ErrNotExist) {
59+
ctx.APIErrorNotFound(err)
60+
} else {
61+
ctx.APIErrorInternal(err)
62+
}
63+
}
5864
}

routers/common/actions.go

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,54 @@ package common
55

66
import (
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
}

routers/web/repo/actions/view.go

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

480-
common.DownloadActionsRunJobLogsWithIndex(ctx.Base, ctx.Repo.Repository, run.ID, jobIndex)
480+
if err = common.DownloadActionsRunJobLogsWithIndex(ctx.Base, ctx.Repo.Repository, run.ID, jobIndex); err != nil {
481+
ctx.NotFoundOrServerError("DownloadActionsRunJobLogsWithIndex", func(err error) bool {
482+
return errors.Is(err, util.ErrNotExist)
483+
}, err)
484+
}
481485
}
482486

483487
func Cancel(ctx *context_module.Context) {

0 commit comments

Comments
 (0)