0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-13 06:46:03 +02:00

fix: restore ctx in URL construction and correct load strategies

- Remove spurious context.ReferencesGitRepo(true) from /actions route group
- ToActionTask: restore targeted loads (LoadJob/LoadRun/LoadRepo) instead
  of LoadAttributes which unnecessarily loads task steps; use
  httplib.MakeAbsoluteURL(ctx, ...) for the URL field
- ToActionWorkflowRun: replace run.LoadAttributes with direct repo
  assignment + LoadTriggerUser to avoid redundant language-stats query;
  restore attempt.Run = run before attempt.LoadAttributes to prevent
  redundant DB re-fetch; restore ctx to APIURL/HTMLURL calls
- ToActionWorkflowJob: restore ctx to APIURL and HTMLURL calls

Co-Authored-By: Claude <claude-sonnet-4-6@anthropic.com>
This commit is contained in:
Ross Golder 2026-05-04 17:06:56 +07:00
parent ee37427d38
commit e38010e532
No known key found for this signature in database
GPG Key ID: 253A7E508D2D59CD
2 changed files with 23 additions and 13 deletions

View File

@ -1278,7 +1278,7 @@ func Routes() *web.Router {
m.Delete("", reqRepoWriter(unit.TypeActions), repo.DeleteArtifact) m.Delete("", reqRepoWriter(unit.TypeActions), repo.DeleteArtifact)
}) })
m.Get("/artifacts/{artifact_id}/zip", repo.DownloadArtifact) m.Get("/artifacts/{artifact_id}/zip", repo.DownloadArtifact)
}, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true)) }, reqRepoReader(unit.TypeActions))
m.Group("/keys", func() { m.Group("/keys", func() {
m.Combo("").Get(repo.ListDeployKeys). m.Combo("").Get(repo.ListDeployKeys).
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey) Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)

View File

@ -29,6 +29,7 @@ import (
"code.gitea.io/gitea/modules/actions" "code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -222,13 +223,18 @@ func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
} }
} }
// ToActionTask convert a actions_model.ActionTask to an api.ActionTask // ToActionTask convert an actions_model.ActionTask to an api.ActionTask
func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.ActionTask, error) { func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.ActionTask, error) {
if err := t.LoadAttributes(ctx); err != nil { // don't need Steps here, only need to load job and its run
if err := t.LoadJob(ctx); err != nil {
return nil, err
}
if err := t.Job.LoadRun(ctx); err != nil {
return nil, err
}
if err := t.Job.Run.LoadRepo(ctx); err != nil {
return nil, err return nil, err
} }
url := strings.TrimSuffix(setting.AppURL, "/") + t.GetRunLink()
return &api.ActionTask{ return &api.ActionTask{
ID: t.ID, ID: t.ID,
@ -240,7 +246,7 @@ func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.Action
DisplayTitle: t.Job.Run.Title, DisplayTitle: t.Job.Run.Title,
Status: t.Status.String(), Status: t.Status.String(),
WorkflowID: t.Job.Run.WorkflowID, WorkflowID: t.Job.Run.WorkflowID,
URL: url, URL: httplib.MakeAbsoluteURL(ctx, t.GetRunLink()),
CreatedAt: t.Created.AsLocalTime(), CreatedAt: t.Created.AsLocalTime(),
UpdatedAt: t.Updated.AsLocalTime(), UpdatedAt: t.Updated.AsLocalTime(),
RunStartedAt: t.Started.AsLocalTime(), RunStartedAt: t.Started.AsLocalTime(),
@ -248,7 +254,10 @@ func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.Action
} }
func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run *actions_model.ActionRun, attempt *actions_model.ActionRunAttempt) (*api.ActionWorkflowRun, error) { func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run *actions_model.ActionRun, attempt *actions_model.ActionRunAttempt) (*api.ActionWorkflowRun, error) {
if err := run.LoadAttributes(ctx); err != nil { if run.Repo == nil {
run.Repo = repo
}
if err := run.LoadTriggerUser(ctx); err != nil {
return nil, err return nil, err
} }
@ -272,6 +281,7 @@ func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run *
var previousAttemptURL *string var previousAttemptURL *string
if attempt != nil { if attempt != nil {
attempt.Run = run
if err := attempt.LoadAttributes(ctx); err != nil { if err := attempt.LoadAttributes(ctx); err != nil {
return nil, err return nil, err
} }
@ -281,16 +291,16 @@ func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run *
completedAt = attempt.Stopped.AsLocalTime() completedAt = attempt.Stopped.AsLocalTime()
triggerUser = attempt.TriggerUser triggerUser = attempt.TriggerUser
if attempt.Attempt > 1 { if attempt.Attempt > 1 {
url := fmt.Sprintf("%s/actions/runs/%d/attempts/%d", repo.APIURL(), run.ID, attempt.Attempt-1) url := fmt.Sprintf("%s/actions/runs/%d/attempts/%d", repo.APIURL(ctx), run.ID, attempt.Attempt-1)
previousAttemptURL = &url previousAttemptURL = &url
} }
} }
return &api.ActionWorkflowRun{ return &api.ActionWorkflowRun{
ID: run.ID, ID: run.ID,
URL: fmt.Sprintf("%s/actions/runs/%d", repo.APIURL(), run.ID), URL: fmt.Sprintf("%s/actions/runs/%d", repo.APIURL(ctx), run.ID),
PreviousAttemptURL: previousAttemptURL, PreviousAttemptURL: previousAttemptURL,
HTMLURL: run.HTMLURL(), HTMLURL: run.HTMLURL(ctx),
RunNumber: run.Index, RunNumber: run.Index,
RunAttempt: runAttempt, RunAttempt: runAttempt,
CreatedAt: run.Created.AsLocalTime(), CreatedAt: run.Created.AsLocalTime(),
@ -401,11 +411,11 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task
return &api.ActionWorkflowJob{ return &api.ActionWorkflowJob{
ID: job.ID, ID: job.ID,
// missing api endpoint for this location // missing api endpoint for this location
URL: fmt.Sprintf("%s/actions/jobs/%d", repo.APIURL(), job.ID), URL: fmt.Sprintf("%s/actions/jobs/%d", repo.APIURL(ctx), job.ID),
HTMLURL: fmt.Sprintf("%s/jobs/%d", job.Run.HTMLURL(), job.ID), HTMLURL: fmt.Sprintf("%s/jobs/%d", job.Run.HTMLURL(ctx), job.ID),
RunID: job.RunID, RunID: job.RunID,
// Missing api endpoint for this location, artifacts are available under a nested url // Missing api endpoint for this location, artifacts are available under a nested url
RunURL: fmt.Sprintf("%s/actions/runs/%d", repo.APIURL(), job.RunID), RunURL: fmt.Sprintf("%s/actions/runs/%d", repo.APIURL(ctx), job.RunID),
Name: job.Name, Name: job.Name,
Labels: job.RunsOn, Labels: job.RunsOn,
RunAttempt: job.Attempt, RunAttempt: job.Attempt,