Skip to content

Download actions job logs from API #33858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions routers/api/v1/repo/actions_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package repo

import (
"errors"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/common"
"code.gitea.io/gitea/services/context"
)
Expand Down Expand Up @@ -40,19 +43,22 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"

jobID := ctx.PathParamInt64("job_id")
if jobID == 0 {
ctx.APIError(400, "invalid job id")
return
}
curJob, err := actions_model.GetRunJobByID(ctx, jobID)
if err != nil {
ctx.APIErrorInternal(err)
return
}
if err := curJob.LoadRepo(ctx); err != nil {
if err = curJob.LoadRepo(ctx); err != nil {
ctx.APIErrorInternal(err)
return
}

common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob)
err = common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
}
}
56 changes: 17 additions & 39 deletions routers/common/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,54 @@ package common

import (
"fmt"
"net/http"
"strings"

actions_model "code.gitea.io/gitea/models/actions"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/context"
)

func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) {
if runID == 0 {
ctx.HTTPError(http.StatusBadRequest, "invalid run id")
return
}

func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) error {
runJobs, err := actions_model.GetRunJobsByRunID(ctx, runID)
if err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
return fmt.Errorf("GetRunJobsByRunID: %w", err)
}
if len(runJobs) == 0 {
ctx.HTTPError(http.StatusNotFound)
return
if err = runJobs.LoadRepos(ctx); err != nil {
return fmt.Errorf("LoadRepos: %w", err)
}
if err := runJobs.LoadRepos(ctx); err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
}

var curJob *actions_model.ActionRunJob
if jobIndex >= 0 && jobIndex < int64(len(runJobs)) {
curJob = runJobs[jobIndex]
if 0 < jobIndex || jobIndex >= int64(len(runJobs)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caused #34038, It should be jobIndex < 0. Fixed in #34041

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't see this, my workflow that I tested was to short.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I made this typo. But why tests didn't catch it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why tests didn't catch it?

The reason was, this test only verifies that downloading a job log with index 0 downloads successfully.

Now this "bug" only appears for the second,third,nth job.

I have commented on the fix PR a patch to the test that expands the test to check, which I cannot push with my privilege here directly into that fix.

return util.NewNotExistErrorf("job index is out of range: %d", jobIndex)
}
if curJob == nil {
ctx.HTTPError(http.StatusNotFound)
return
}

DownloadActionsRunJobLogs(ctx, ctxRepo, curJob)
return DownloadActionsRunJobLogs(ctx, ctxRepo, runJobs[jobIndex])
}

func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, curJob *actions_model.ActionRunJob) {
func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, curJob *actions_model.ActionRunJob) error {
if curJob.Repo.ID != ctxRepo.ID {
ctx.HTTPError(http.StatusNotFound)
return
return util.NewNotExistErrorf("job not found")
}

if curJob.TaskID == 0 {
ctx.HTTPError(http.StatusNotFound, "job is not started")
return
return util.NewNotExistErrorf("job not started")
}

if err := curJob.LoadRun(ctx); err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
return fmt.Errorf("LoadRun: %w", err)
}

task, err := actions_model.GetTaskByID(ctx, curJob.TaskID)
if err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
return fmt.Errorf("GetTaskByID: %w", err)
}

if task.LogExpired {
ctx.HTTPError(http.StatusNotFound, "logs have been cleaned up")
return
return util.NewNotExistErrorf("logs have been cleaned up")
}

reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename)
if err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
return fmt.Errorf("OpenLogs: %w", err)
}
defer reader.Close()

Expand All @@ -90,4 +67,5 @@ func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository
ContentTypeCharset: "utf-8",
Disposition: "attachment",
})
return nil
}
6 changes: 5 additions & 1 deletion routers/web/repo/actions/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,11 @@ func Logs(ctx *context_module.Context) {
return
}

common.DownloadActionsRunJobLogsWithIndex(ctx.Base, ctx.Repo.Repository, run.ID, jobIndex)
if err = common.DownloadActionsRunJobLogsWithIndex(ctx.Base, ctx.Repo.Repository, run.ID, jobIndex); err != nil {
ctx.NotFoundOrServerError("DownloadActionsRunJobLogsWithIndex", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
}
}

func Cancel(ctx *context_module.Context) {
Expand Down