Skip to content

Commit 3951ba8

Browse files
committed
call getCommitStatusEventNameAndSHA once for a run
1 parent 9f600e1 commit 3951ba8

File tree

7 files changed

+53
-66
lines changed

7 files changed

+53
-66
lines changed

routers/api/actions/runner/runner.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ func (s *Service) UpdateTask(
217217
return nil, status.Errorf(codes.Internal, "load run: %v", err)
218218
}
219219

220-
// don't create commit status for cron job
221-
if task.Job.Run.ScheduleID == 0 {
222-
actions_service.CreateCommitStatus(ctx, task.Job)
223-
}
220+
actions_service.CreateCommitStatusForRunJobs(ctx, task.Job.Run, task.Job)
224221

225222
if task.Status.IsDone() {
226223
notify_service.WorkflowJobStatusUpdate(ctx, task.Job.Run.Repo, task.Job.Run.TriggerUser, task.Job, task)

routers/web/repo/actions/view.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shou
541541
return err
542542
}
543543

544-
actions_service.CreateCommitStatus(ctx, job)
544+
actions_service.CreateCommitStatusForRunJobs(ctx, job.Run, job)
545545
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
546546

547547
return nil
@@ -569,7 +569,7 @@ func Logs(ctx *context_module.Context) {
569569
func Cancel(ctx *context_module.Context) {
570570
runIndex := getRunIndex(ctx)
571571

572-
_, jobs := getRunJobs(ctx, runIndex, -1)
572+
firstJob, jobs := getRunJobs(ctx, runIndex, -1)
573573
if ctx.Written() {
574574
return
575575
}
@@ -588,7 +588,7 @@ func Cancel(ctx *context_module.Context) {
588588
return
589589
}
590590

591-
actions_service.CreateCommitStatus(ctx, jobs...)
591+
actions_service.CreateCommitStatusForRunJobs(ctx, firstJob.Run, jobs...)
592592
actions_service.EmitJobsIfReadyByJobs(updatedJobs)
593593

594594
for _, job := range updatedJobs {
@@ -642,7 +642,7 @@ func Approve(ctx *context_module.Context) {
642642
return
643643
}
644644

645-
actions_service.CreateCommitStatus(ctx, jobs...)
645+
actions_service.CreateCommitStatusForRunJobs(ctx, current.Run, jobs...)
646646

647647
if len(updatedJobs) > 0 {
648648
job := updatedJobs[0]

services/actions/clear_tasks.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ func StopEndlessTasks(ctx context.Context) error {
3737
}
3838

3939
func notifyWorkflowJobStatusUpdate(ctx context.Context, jobs []*actions_model.ActionRunJob) {
40-
if len(jobs) > 0 {
41-
CreateCommitStatus(ctx, jobs...)
42-
for _, job := range jobs {
43-
_ = job.LoadAttributes(ctx)
44-
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
40+
if len(jobs) == 0 {
41+
return
42+
}
43+
for _, job := range jobs {
44+
if err := job.LoadAttributes(ctx); err != nil {
45+
log.Error("Failed to load job attributes: %v", err)
4546
}
46-
job := jobs[0]
47-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
47+
CreateCommitStatusForRunJobs(ctx, job.Run, job)
48+
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
4849
}
50+
51+
job := jobs[0]
52+
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
4953
}
5054

5155
func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
@@ -208,7 +212,10 @@ func CancelAbandonedJobs(ctx context.Context) error {
208212
log.Warn("cancel abandoned job %v: %v", job.ID, err)
209213
// go on
210214
}
211-
CreateCommitStatus(ctx, job)
215+
if job.Run == nil {
216+
continue // error occurs during loading attributes, the following code that depends on "Run" will fail, so ignore and skip
217+
}
218+
CreateCommitStatusForRunJobs(ctx, job.Run, job)
212219
if updated {
213220
updatedJobs = append(updatedJobs, job)
214221
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)

services/actions/commit_status.go

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,39 @@ import (
1212
actions_model "code.gitea.io/gitea/models/actions"
1313
"code.gitea.io/gitea/models/db"
1414
git_model "code.gitea.io/gitea/models/git"
15+
repo_model "code.gitea.io/gitea/models/repo"
1516
user_model "code.gitea.io/gitea/models/user"
1617
actions_module "code.gitea.io/gitea/modules/actions"
1718
"code.gitea.io/gitea/modules/commitstatus"
18-
git "code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/log"
2020
webhook_module "code.gitea.io/gitea/modules/webhook"
2121
commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
2222

2323
"github.com/nektos/act/pkg/jobparser"
2424
)
2525

26-
// CreateCommitStatus creates a commit status for the given job.
26+
// CreateCommitStatusForRunJobs creates a commit status for the given job if it has a supported event and related commit.
2727
// It won't return an error failed, but will log it, because it's not critical.
28-
func CreateCommitStatus(ctx context.Context, jobs ...*actions_model.ActionRunJob) {
28+
func CreateCommitStatusForRunJobs(ctx context.Context, run *actions_model.ActionRun, jobs ...*actions_model.ActionRunJob) {
29+
// don't create commit status for cron job
30+
if run.ScheduleID != 0 {
31+
return
32+
}
33+
34+
event, commitID, err := getCommitStatusEventNameAndSHA(run)
35+
if err != nil {
36+
log.Error("GetCommitStatusEventNameAndSHA: %v", err)
37+
} else if event == "" || commitID == "" {
38+
return // Unsupported event, do nothing
39+
}
40+
41+
if err = run.LoadAttributes(ctx); err != nil {
42+
log.Error("run.LoadAttributes: %v", err)
43+
return
44+
}
45+
2946
for _, job := range jobs {
30-
if err := createCommitStatus(ctx, job); err != nil {
47+
if err = createCommitStatus(ctx, run.Repo, event, commitID, run, job); err != nil {
3148
log.Error("Failed to create commit status for job %d: %v", job.ID, err)
3249
}
3350
}
@@ -76,46 +93,17 @@ func getCommitStatusEventNameAndSHA(run *actions_model.ActionRun) (event, sha st
7693
return event, sha, nil
7794
}
7895

79-
// ShouldCreateCommitStatus checks whether we should create a commit status for the given run.
80-
// It returns true if the event is supported and the SHA is not empty.
81-
func ShouldCreateCommitStatus(run *actions_model.ActionRun) bool {
82-
event, sha, err := getCommitStatusEventNameAndSHA(run)
83-
if err != nil {
84-
log.Error("ShouldCreateCommitStatus: %s", err.Error())
85-
return false
86-
} else if event == "" || sha == "" {
87-
// Unsupported event, do nothing
88-
return false
89-
}
90-
return true
91-
}
92-
93-
func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) error {
94-
if err := job.LoadAttributes(ctx); err != nil {
95-
return fmt.Errorf("load run: %w", err)
96-
}
97-
98-
run := job.Run
99-
100-
event, sha, err := getCommitStatusEventNameAndSHA(run)
101-
if err != nil {
102-
return fmt.Errorf("getCommitStatusEventNameAndSHA: %w", err)
103-
} else if event == "" || sha == "" {
104-
// Unsupported event, do nothing
105-
return nil
106-
}
107-
108-
repo := run.Repo
96+
func createCommitStatus(ctx context.Context, repo *repo_model.Repository, event, commitID string, run *actions_model.ActionRun, job *actions_model.ActionRunJob) error {
10997
// TODO: store workflow name as a field in ActionRun to avoid parsing
11098
runName := path.Base(run.WorkflowID)
11199
if wfs, err := jobparser.Parse(job.WorkflowPayload); err == nil && len(wfs) > 0 {
112100
runName = wfs[0].Name
113101
}
114-
ctxname := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event)
102+
ctxName := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event)
115103
state := toCommitStatus(job.Status)
116-
if statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll); err == nil {
104+
if statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, commitID, db.ListOptionsAll); err == nil {
117105
for _, v := range statuses {
118-
if v.Context == ctxname {
106+
if v.Context == ctxName {
119107
if v.State == state {
120108
// no need to update
121109
return nil
@@ -144,6 +132,8 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
144132
description = "Waiting to run"
145133
case actions_model.StatusBlocked:
146134
description = "Blocked by required conditions"
135+
default:
136+
description = "Unknown status: " + job.Status.String()
147137
}
148138

149139
index, err := getIndexOfJob(ctx, job)
@@ -152,20 +142,16 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
152142
}
153143

154144
creator := user_model.NewActionsUser()
155-
commitID, err := git.NewIDFromString(sha)
156-
if err != nil {
157-
return fmt.Errorf("HashTypeInterfaceFromHashString: %w", err)
158-
}
159145
status := git_model.CommitStatus{
160-
SHA: sha,
146+
SHA: commitID,
161147
TargetURL: fmt.Sprintf("%s/jobs/%d", run.Link(), index),
162148
Description: description,
163-
Context: ctxname,
149+
Context: ctxName,
164150
CreatorID: creator.ID,
165151
State: state,
166152
}
167153

168-
return commitstatus_service.CreateCommitStatus(ctx, repo, creator, commitID.String(), &status)
154+
return commitstatus_service.CreateCommitStatus(ctx, repo, creator, commitID, &status)
169155
}
170156

171157
func toCommitStatus(status actions_model.Status) commitstatus.CommitStatusState {

services/actions/job_emitter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func checkJobsByRunID(ctx context.Context, runID int64) error {
8989
}); err != nil {
9090
return err
9191
}
92-
CreateCommitStatus(ctx, jobs...)
92+
CreateCommitStatusForRunJobs(ctx, run, jobs...)
9393
for _, job := range updatedJobs {
9494
_ = job.LoadAttributes(ctx)
9595
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)

services/actions/run.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ func PrepareRunAndInsert(ctx context.Context, content []byte, run *actions_model
6262
return fmt.Errorf("FindRunJob: %w", err)
6363
}
6464

65-
// Create commit status for all jobs if needed
66-
if ShouldCreateCommitStatus(run) {
67-
CreateCommitStatus(ctx, allJobs...)
68-
}
65+
CreateCommitStatusForRunJobs(ctx, run, allJobs...)
6966

7067
notify_service.WorkflowRunStatusUpdate(ctx, run.Repo, run.TriggerUser, run)
7168
for _, job := range allJobs {

services/actions/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func PickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv
9797
return nil, false, nil
9898
}
9999

100-
CreateCommitStatus(ctx, job)
100+
CreateCommitStatusForRunJobs(ctx, job.Run, job)
101101
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, actionTask)
102102

103103
return task, true, nil

0 commit comments

Comments
 (0)