-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
318d171
Download actions logs from API
lunny 14b6ce7
Fix swagger
lunny 3d45dc9
Merge branch 'main' into lunny/actions_log_api
lunny 9ec87c7
Add test for API actions log download
lunny 5628233
Extract actions log download function
lunny 49f1d35
Merge branch 'main' into lunny/actions_log_api
lunny bf5ccf8
Update swagger documentation
lunny 75cab36
Fix bug and test
lunny 24b215a
Merge branch 'main' into lunny/actions_log_api
lunny 940bfe9
Use job_id for the api endpoint
lunny e654bd0
Merge branch 'main' into lunny/actions_log_api
lunny 898ffff
Remove unused function
lunny 470d05e
Update routers/api/v1/repo/actions_run.go
lunny 520e732
Update models/actions/run_job_list.go
lunny 43208e4
Merge branch 'main' into lunny/actions_log_api
lunny b5d9696
Merge branch 'lunny/actions_log_api' of github.com:lunny/gitea into l…
lunny 8cbf5f2
Fix lint
lunny 1afd23d
Merge branch 'main' into lunny/actions_log_api
wxiaoguang e712ff0
fix
wxiaoguang 08638a6
Merge branch 'main' into lunny/actions_log_api
GiteaBot 8bb8e17
Merge branch 'main' into lunny/actions_log_api
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
actions_model "code.gitea.io/gitea/models/actions" | ||
"code.gitea.io/gitea/modules/actions" | ||
"code.gitea.io/gitea/modules/util" | ||
"code.gitea.io/gitea/services/context" | ||
) | ||
|
||
func getRunIndex(ctx *context.APIContext) int64 { | ||
// if run param is "latest", get the latest run index | ||
if ctx.PathParam("run_id") == "latest" { | ||
if run, _ := actions_model.GetLatestRun(ctx, ctx.Repo.Repository.ID); run != nil { | ||
return run.Index | ||
} | ||
} | ||
return ctx.PathParamInt64("run_id") | ||
} | ||
|
||
// getRunJobs gets the jobs of runIndex, and returns jobs[jobIndex], jobs. | ||
// Any error will be written to the ctx. | ||
// It never returns a nil job of an empty jobs, if the jobIndex is out of range, it will be treated as 0. | ||
func getRunJobs(ctx *context.APIContext, runIndex, jobIndex int64) (*actions_model.ActionRunJob, []*actions_model.ActionRunJob) { | ||
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) | ||
if err != nil { | ||
if errors.Is(err, util.ErrNotExist) { | ||
ctx.HTTPError(http.StatusNotFound, err.Error()) | ||
return nil, nil | ||
} | ||
ctx.HTTPError(http.StatusInternalServerError, err.Error()) | ||
return nil, nil | ||
} | ||
run.Repo = ctx.Repo.Repository | ||
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID) | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
ctx.HTTPError(http.StatusInternalServerError, err.Error()) | ||
return nil, nil | ||
} | ||
if len(jobs) == 0 { | ||
ctx.HTTPError(http.StatusNotFound) | ||
return nil, nil | ||
} | ||
|
||
for _, v := range jobs { | ||
v.Run = run | ||
} | ||
|
||
if jobIndex >= 0 && jobIndex < int64(len(jobs)) { | ||
return jobs[jobIndex], jobs | ||
} | ||
return jobs[0], jobs | ||
} | ||
|
||
func DownloadActionsRunLogs(ctx *context.APIContext) { | ||
// swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs/{job}/logs repository downloadActionsRunLogs | ||
// --- | ||
// summary: Downloads the logs for a workflow run redirects to blob url | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: owner | ||
// in: path | ||
// description: name of the owner | ||
// type: string | ||
// required: true | ||
// - name: repo | ||
// in: path | ||
// description: name of the repository | ||
// type: string | ||
// required: true | ||
// - name: run_id | ||
// in: path | ||
// description: id of the run, this could be latest | ||
// type: integer | ||
// required: true | ||
// - name: job | ||
// in: path | ||
// description: id of the job | ||
// type: integer | ||
// required: true | ||
// responses: | ||
// "302": | ||
// description: redirect to the blob download | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// "400": | ||
// "$ref": "#/responses/error" | ||
// "404": | ||
// "$ref": "#/responses/notFound" | ||
|
||
runIndex := getRunIndex(ctx) | ||
jobIndex := ctx.PathParamInt64("job") | ||
|
||
job, _ := getRunJobs(ctx, runIndex, jobIndex) | ||
if ctx.Written() { | ||
return | ||
} | ||
if job.TaskID == 0 { | ||
ctx.HTTPError(http.StatusNotFound, "job is not started") | ||
return | ||
} | ||
|
||
err := job.LoadRun(ctx) | ||
if err != nil { | ||
ctx.HTTPError(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
task, err := actions_model.GetTaskByID(ctx, job.TaskID) | ||
if err != nil { | ||
ctx.HTTPError(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
if task.LogExpired { | ||
ctx.HTTPError(http.StatusNotFound, "logs have been cleaned up") | ||
return | ||
} | ||
|
||
reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename) | ||
if err != nil { | ||
ctx.HTTPError(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
defer reader.Close() | ||
|
||
workflowName := job.Run.WorkflowID | ||
if p := strings.Index(workflowName, "."); p > 0 { | ||
workflowName = workflowName[0:p] | ||
} | ||
ctx.ServeContent(reader, &context.ServeHeaderOptions{ | ||
Filename: fmt.Sprintf("%v-%v-%v.log", workflowName, job.Name, task.ID), | ||
ContentLength: &task.LogSize, | ||
ContentType: "text/plain", | ||
ContentTypeCharset: "utf-8", | ||
Disposition: "attachment", | ||
}) | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.