refactor(actions): small cleanups across models, services and routers (#39722)

Collection of small, behaviour-preserving cleanups in the Actions code.

- Fix log and error prefixes that named non-existent or wrong functions,
and wrap errors with `%w` so `errors.Is` keeps working.
- Remove unused code.
- Rely on `ctx.ServerError` / `ctx.APIErrorAuto`, which already map
`util.ErrNotExist` and friends, instead of equivalent hand-written
switches.
- Load runs referenced by commit statuses with one query instead of one
per run (used on every PR view); the result order is now stable.
- Encode an event's payload once instead of up to three times.
- Share the duplicated runner-token "get or create" logic and the
"notify and emit" call pair.
- Simplify `ToActionArtifact` / `ToActionRunner` signatures.

---------

Signed-off-by: bircni <bircni@icloud.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-10 12:55:28 +00:00
1 parent c344004e53
commit c30ddc3e14
26 files changed
+113 -223

No files matched your search

+1 -1
View File
@@ -664,7 +664,7 @@ func (data *actionRunListData) prepareCommon(ctx *context.Context, workflows []W
return false
}
if err := loadIsRefDeleted(ctx, ctx.Repo.Repository.ID, data.ActionRuns); err != nil {
log.Error("LoadIsRefDeleted", err)
log.Error("loadIsRefDeleted: %v", err)
}
if !data.processActionRuns(ctx) {
+13 -37
View File
@@ -252,9 +252,7 @@ func ViewWorkflowFile(ctx *context_module.Context) {
commit, err := ctx.Repo.GitRepo.GetCommit(ctx, run.CommitSHA)
if err != nil {
ctx.NotFoundOrServerError("GetCommit", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetCommit", err)
return
}
rpath, entries, err := actions.ListWorkflows(ctx, ctx.Repo.GitRepo, commit)
@@ -1037,9 +1035,7 @@ func Logs(ctx *context_module.Context) {
jobID := ctx.PathParamInt64("job")
if err := common.DownloadActionsRunJobLogsWithID(ctx.Base, ctx.Repo.Repository, run.ID, jobID); err != nil {
ctx.NotFoundOrServerError("DownloadActionsRunJobLogsWithID", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("DownloadActionsRunJobLogsWithID", err)
}
}
@@ -1065,9 +1061,7 @@ func Approve(ctx *context_module.Context) {
return
}
if _, err := actions_service.ApproveRuns(ctx, ctx.Repo.Repository, ctx.Doer, []int64{run.ID}); err != nil {
ctx.NotFoundOrServerError("ApproveRuns", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("ApproveRuns", err)
return
}
@@ -1115,9 +1109,7 @@ func getCurrentRunJobsByPathParam(ctx *context_module.Context) (*actions_model.A
jobID := ctx.PathParamInt64("job")
selectedJob, err = actions_model.GetRunJobByRunAndID(ctx, run.ID, jobID)
if err != nil {
ctx.NotFoundOrServerError("GetRunJobByRepoAndID", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetRunJobByRunAndID", err)
return nil, nil, nil
}
}
@@ -1132,27 +1124,21 @@ func getCurrentRunJobsByPathParam(ctx *context_module.Context) (*actions_model.A
// Explicit attempt number in the URL — user is viewing a historical attempt.
attempt, err = actions_model.GetRunAttemptByRunIDAndAttemptNum(ctx, run.ID, attemptNum)
if err != nil {
ctx.NotFoundOrServerError("GetRunAttemptByRunIDAndAttempt", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetRunAttemptByRunIDAndAttemptNum", err)
return nil, nil, nil
}
case selectedJob != nil && selectedJob.RunAttemptID > 0:
// No explicit attempt in the URL, but the requested job belongs to a known attempt — resolve via the job.
attempt, err = actions_model.GetRunAttemptByRepoAndID(ctx, selectedJob.RepoID, selectedJob.RunAttemptID)
if err != nil {
ctx.NotFoundOrServerError("GetRunAttemptByRepoAndID", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetRunAttemptByRepoAndID", err)
return nil, nil, nil
}
default:
// No attempt context at all — show the latest attempt (nil for legacy runs).
attempt, _, err = run.GetLatestAttempt(ctx)
if err != nil {
ctx.NotFoundOrServerError("GetLatestAttempt", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetLatestAttempt", err)
return nil, nil, nil
}
}
@@ -1201,9 +1187,7 @@ func ArtifactsDeleteView(ctx *context_module.Context) {
}
resolvedAttemptID, err := resolveArtifactAttemptIDFromQuery(ctx, run)
if err != nil {
ctx.NotFoundOrServerError("resolveArtifactAttemptIDFromQuery", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("resolveArtifactAttemptIDFromQuery", err)
return
}
artifactName := ctx.PathParam("artifact_name")
@@ -1221,9 +1205,7 @@ func ArtifactsDownloadView(ctx *context_module.Context) {
}
resolvedAttemptID, err := resolveArtifactAttemptIDFromQuery(ctx, run)
if err != nil {
ctx.NotFoundOrServerError("resolveArtifactAttemptIDFromQuery", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("resolveArtifactAttemptIDFromQuery", err)
return
}
artifactName := ctx.PathParam("artifact_name")
@@ -1308,7 +1290,7 @@ func ApproveAllChecks(ctx *context_module.Context) {
ctx.ServerError("GetLatestCommitStatus", err)
return
}
runs, err := actions_service.GetRunsFromCommitStatuses(ctx, commitStatuses)
runs, err := actions_service.GetRunsFromCommitStatuses(ctx, repo.ID, commitStatuses)
if err != nil {
ctx.ServerError("GetRunsFromCommitStatuses", err)
return
@@ -1327,9 +1309,7 @@ func ApproveAllChecks(ctx *context_module.Context) {
}
if _, err := actions_service.ApproveRuns(ctx, repo, ctx.Doer, runIDs); err != nil {
ctx.NotFoundOrServerError("ApproveRuns", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("ApproveRuns", err)
return
}
@@ -1442,9 +1422,7 @@ func Run(ctx *context_module.Context) {
func viewScopedWorkflowFile(ctx *context_module.Context, run *actions_model.ActionRun) {
sourceRepo, err := repo_model.GetRepositoryByID(ctx, run.WorkflowRepoID)
if err != nil {
ctx.NotFoundOrServerError("GetRepositoryByID", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetRepositoryByID", err)
return
}
@@ -1467,9 +1445,7 @@ func viewScopedWorkflowFile(ctx *context_module.Context, run *actions_model.Acti
commit, err := sourceGitRepo.GetCommit(ctx, run.WorkflowCommitSHA)
if err != nil {
ctx.NotFoundOrServerError("GetCommit", func(err error) bool {
return errors.Is(err, util.ErrNotExist)
}, err)
ctx.ServerError("GetCommit", err)
return
}
rpath, entries, err := actions.ListScopedWorkflows(ctx, sourceGitRepo, commit)
+1 -1
View File
@@ -438,7 +438,7 @@ func (prInfo *pullRequestViewInfo) prepareMergeBoxStatusCheckData(ctx *context.C
// so the status-check section must render when there are any required contexts, not only when enableStatusCheck is on.
data.ShowStatusCheck = data.enableStatusCheck || data.hasRequiredStatusContexts || len(statusCheckData.PullCommitStatuses) > 0
runs, err := actions_service.GetRunsFromCommitStatuses(ctx, commitStatuses)
runs, err := actions_service.GetRunsFromCommitStatuses(ctx, ctx.Repo.Repository.ID, commitStatuses)
if err != nil {
log.Error("GetRunsFromCommitStatuses: %v", err)
}
+3 -10
View File
@@ -139,16 +139,9 @@ func Runners(ctx *context.Context) {
}
// ownid=0,repo_id=0,means this token is used for global
var token *actions_model.ActionRunnerToken
token, err = actions_model.GetLatestRunnerToken(ctx, opts.OwnerID, opts.RepoID)
if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
token, err = actions_model.NewRunnerToken(ctx, opts.OwnerID, opts.RepoID)
if err != nil {
ctx.ServerError("CreateRunnerToken", err)
return
}
} else if err != nil {
ctx.ServerError("GetLatestRunnerToken", err)
token, err := actions_model.GetOrCreateActiveRunnerToken(ctx, opts.OwnerID, opts.RepoID)
if err != nil {
ctx.ServerError("GetOrCreateActiveRunnerToken", err)
return
}