From e38010e5327e4793d5ad3c07bc89b7d3b1908ae1 Mon Sep 17 00:00:00 2001 From: Ross Golder Date: Mon, 4 May 2026 17:06:56 +0700 Subject: [PATCH] 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 --- routers/api/v1/api.go | 2 +- services/convert/convert.go | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 3fce86cda9..86823d918b 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1278,7 +1278,7 @@ func Routes() *web.Router { m.Delete("", reqRepoWriter(unit.TypeActions), repo.DeleteArtifact) }) m.Get("/artifacts/{artifact_id}/zip", repo.DownloadArtifact) - }, reqRepoReader(unit.TypeActions), context.ReferencesGitRepo(true)) + }, reqRepoReader(unit.TypeActions)) m.Group("/keys", func() { m.Combo("").Get(repo.ListDeployKeys). Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey) diff --git a/services/convert/convert.go b/services/convert/convert.go index 7c0dd55530..e5d5e58781 100644 --- a/services/convert/convert.go +++ b/services/convert/convert.go @@ -29,6 +29,7 @@ import ( "code.gitea.io/gitea/modules/actions" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" 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) { - 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 } - - url := strings.TrimSuffix(setting.AppURL, "/") + t.GetRunLink() return &api.ActionTask{ ID: t.ID, @@ -240,7 +246,7 @@ func ToActionTask(ctx context.Context, t *actions_model.ActionTask) (*api.Action DisplayTitle: t.Job.Run.Title, Status: t.Status.String(), WorkflowID: t.Job.Run.WorkflowID, - URL: url, + URL: httplib.MakeAbsoluteURL(ctx, t.GetRunLink()), CreatedAt: t.Created.AsLocalTime(), UpdatedAt: t.Updated.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) { - if err := run.LoadAttributes(ctx); err != nil { + if run.Repo == nil { + run.Repo = repo + } + if err := run.LoadTriggerUser(ctx); err != nil { return nil, err } @@ -272,6 +281,7 @@ func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run * var previousAttemptURL *string if attempt != nil { + attempt.Run = run if err := attempt.LoadAttributes(ctx); err != nil { return nil, err } @@ -281,16 +291,16 @@ func ToActionWorkflowRun(ctx context.Context, repo *repo_model.Repository, run * completedAt = attempt.Stopped.AsLocalTime() triggerUser = attempt.TriggerUser 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 } } return &api.ActionWorkflowRun{ 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, - HTMLURL: run.HTMLURL(), + HTMLURL: run.HTMLURL(ctx), RunNumber: run.Index, RunAttempt: runAttempt, CreatedAt: run.Created.AsLocalTime(), @@ -401,11 +411,11 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task return &api.ActionWorkflowJob{ ID: job.ID, // missing api endpoint for this location - URL: fmt.Sprintf("%s/actions/jobs/%d", repo.APIURL(), job.ID), - HTMLURL: fmt.Sprintf("%s/jobs/%d", job.Run.HTMLURL(), job.ID), + URL: fmt.Sprintf("%s/actions/jobs/%d", repo.APIURL(ctx), job.ID), + HTMLURL: fmt.Sprintf("%s/jobs/%d", job.Run.HTMLURL(ctx), job.ID), RunID: job.RunID, // 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, Labels: job.RunsOn, RunAttempt: job.Attempt,