Skip to content

Commit 4236fb0

Browse files
Merge branch 'main' into co2
2 parents b45cd54 + 2401812 commit 4236fb0

File tree

10 files changed

+133
-236
lines changed

10 files changed

+133
-236
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: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +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)
46+
continue
4547
}
46-
job := jobs[0]
48+
CreateCommitStatusForRunJobs(ctx, job.Run, job)
49+
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
50+
}
51+
52+
if job := jobs[0]; job.Run != nil && job.Run.Repo != nil {
4753
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
4854
}
4955
}
@@ -208,7 +214,10 @@ func CancelAbandonedJobs(ctx context.Context) error {
208214
log.Warn("cancel abandoned job %v: %v", job.ID, err)
209215
// go on
210216
}
211-
CreateCommitStatus(ctx, job)
217+
if job.Run == nil || job.Run.Repo == nil {
218+
continue // error occurs during loading attributes, the following code that depends on "Run.Repo" will fail, so ignore and skip
219+
}
220+
CreateCommitStatusForRunJobs(ctx, job.Run, job)
212221
if updated {
213222
updatedJobs = append(updatedJobs, job)
214223
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)

services/actions/commit_status.go

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,62 @@ import (
88
"errors"
99
"fmt"
1010
"path"
11+
"strconv"
1112

1213
actions_model "code.gitea.io/gitea/models/actions"
1314
"code.gitea.io/gitea/models/db"
1415
git_model "code.gitea.io/gitea/models/git"
16+
repo_model "code.gitea.io/gitea/models/repo"
1517
user_model "code.gitea.io/gitea/models/user"
1618
actions_module "code.gitea.io/gitea/modules/actions"
1719
"code.gitea.io/gitea/modules/commitstatus"
18-
git "code.gitea.io/gitea/modules/git"
1920
"code.gitea.io/gitea/modules/log"
2021
webhook_module "code.gitea.io/gitea/modules/webhook"
2122
commitstatus_service "code.gitea.io/gitea/services/repository/commitstatus"
2223

2324
"github.com/nektos/act/pkg/jobparser"
2425
)
2526

26-
// CreateCommitStatus creates a commit status for the given job.
27+
// CreateCommitStatusForRunJobs creates a commit status for the given job if it has a supported event and related commit.
2728
// 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) {
29+
func CreateCommitStatusForRunJobs(ctx context.Context, run *actions_model.ActionRun, jobs ...*actions_model.ActionRunJob) {
30+
// don't create commit status for cron job
31+
if run.ScheduleID != 0 {
32+
return
33+
}
34+
35+
event, commitID, err := getCommitStatusEventNameAndCommitID(run)
36+
if err != nil {
37+
log.Error("GetCommitStatusEventNameAndSHA: %v", err)
38+
}
39+
if event == "" || commitID == "" {
40+
return // unsupported event, or no commit id, or error occurs, do nothing
41+
}
42+
43+
if err = run.LoadAttributes(ctx); err != nil {
44+
log.Error("run.LoadAttributes: %v", err)
45+
return
46+
}
47+
2948
for _, job := range jobs {
30-
if err := createCommitStatus(ctx, job); err != nil {
49+
if err = createCommitStatus(ctx, run.Repo, event, commitID, run, job); err != nil {
3150
log.Error("Failed to create commit status for job %d: %v", job.ID, err)
3251
}
3352
}
3453
}
3554

36-
func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) error {
37-
if err := job.LoadAttributes(ctx); err != nil {
38-
return fmt.Errorf("load run: %w", err)
39-
}
40-
41-
run := job.Run
42-
43-
var (
44-
sha string
45-
event string
46-
)
55+
func getCommitStatusEventNameAndCommitID(run *actions_model.ActionRun) (event, commitID string, _ error) {
4756
switch run.Event {
4857
case webhook_module.HookEventPush:
4958
event = "push"
5059
payload, err := run.GetPushEventPayload()
5160
if err != nil {
52-
return fmt.Errorf("GetPushEventPayload: %w", err)
61+
return "", "", fmt.Errorf("GetPushEventPayload: %w", err)
5362
}
5463
if payload.HeadCommit == nil {
55-
return errors.New("head commit is missing in event payload")
64+
return "", "", errors.New("head commit is missing in event payload")
5665
}
57-
sha = payload.HeadCommit.ID
66+
commitID = payload.HeadCommit.ID
5867
case // pull_request
5968
webhook_module.HookEventPullRequest,
6069
webhook_module.HookEventPullRequestSync,
@@ -69,32 +78,33 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
6978
}
7079
payload, err := run.GetPullRequestEventPayload()
7180
if err != nil {
72-
return fmt.Errorf("GetPullRequestEventPayload: %w", err)
81+
return "", "", fmt.Errorf("GetPullRequestEventPayload: %w", err)
7382
}
7483
if payload.PullRequest == nil {
75-
return errors.New("pull request is missing in event payload")
84+
return "", "", errors.New("pull request is missing in event payload")
7685
} else if payload.PullRequest.Head == nil {
77-
return errors.New("head of pull request is missing in event payload")
86+
return "", "", errors.New("head of pull request is missing in event payload")
7887
}
79-
sha = payload.PullRequest.Head.Sha
88+
commitID = payload.PullRequest.Head.Sha
8089
case webhook_module.HookEventRelease:
8190
event = string(run.Event)
82-
sha = run.CommitSHA
83-
default:
84-
return nil
91+
commitID = run.CommitSHA
92+
default: // do nothing, return empty
8593
}
94+
return event, commitID, nil
95+
}
8696

87-
repo := run.Repo
97+
func createCommitStatus(ctx context.Context, repo *repo_model.Repository, event, commitID string, run *actions_model.ActionRun, job *actions_model.ActionRunJob) error {
8898
// TODO: store workflow name as a field in ActionRun to avoid parsing
8999
runName := path.Base(run.WorkflowID)
90100
if wfs, err := jobparser.Parse(job.WorkflowPayload); err == nil && len(wfs) > 0 {
91101
runName = wfs[0].Name
92102
}
93-
ctxname := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event)
103+
ctxName := fmt.Sprintf("%s / %s (%s)", runName, job.Name, event)
94104
state := toCommitStatus(job.Status)
95-
if statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll); err == nil {
105+
if statuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, commitID, db.ListOptionsAll); err == nil {
96106
for _, v := range statuses {
97-
if v.Context == ctxname {
107+
if v.Context == ctxName {
98108
if v.State == state {
99109
// no need to update
100110
return nil
@@ -106,7 +116,7 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
106116
return fmt.Errorf("GetLatestCommitStatus: %w", err)
107117
}
108118

109-
description := ""
119+
var description string
110120
switch job.Status {
111121
// TODO: if we want support description in different languages, we need to support i18n placeholders in it
112122
case actions_model.StatusSuccess:
@@ -123,6 +133,8 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
123133
description = "Waiting to run"
124134
case actions_model.StatusBlocked:
125135
description = "Blocked by required conditions"
136+
default:
137+
description = "Unknown status: " + strconv.Itoa(int(job.Status))
126138
}
127139

128140
index, err := getIndexOfJob(ctx, job)
@@ -131,20 +143,16 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
131143
}
132144

133145
creator := user_model.NewActionsUser()
134-
commitID, err := git.NewIDFromString(sha)
135-
if err != nil {
136-
return fmt.Errorf("HashTypeInterfaceFromHashString: %w", err)
137-
}
138146
status := git_model.CommitStatus{
139-
SHA: sha,
147+
SHA: commitID,
140148
TargetURL: fmt.Sprintf("%s/jobs/%d", run.Link(), index),
141149
Description: description,
142-
Context: ctxname,
150+
Context: ctxName,
143151
CreatorID: creator.ID,
144152
State: state,
145153
}
146154

147-
return commitstatus_service.CreateCommitStatus(ctx, repo, creator, commitID.String(), &status)
155+
return commitstatus_service.CreateCommitStatus(ctx, repo, creator, commitID, &status)
148156
}
149157

150158
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/notifier_helper.go

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ import (
2727
api "code.gitea.io/gitea/modules/structs"
2828
webhook_module "code.gitea.io/gitea/modules/webhook"
2929
"code.gitea.io/gitea/services/convert"
30-
notify_service "code.gitea.io/gitea/services/notify"
3130

32-
"github.com/nektos/act/pkg/jobparser"
3331
"github.com/nektos/act/pkg/model"
3432
)
3533

@@ -346,65 +344,10 @@ func handleWorkflows(
346344

347345
run.NeedApproval = need
348346

349-
if err := run.LoadAttributes(ctx); err != nil {
350-
log.Error("LoadAttributes: %v", err)
347+
if err := PrepareRunAndInsert(ctx, dwf.Content, run, nil); err != nil {
348+
log.Error("PrepareRunAndInsert: %v", err)
351349
continue
352350
}
353-
354-
vars, err := actions_model.GetVariablesOfRun(ctx, run)
355-
if err != nil {
356-
log.Error("GetVariablesOfRun: %v", err)
357-
continue
358-
}
359-
360-
wfRawConcurrency, err := jobparser.ReadWorkflowRawConcurrency(dwf.Content)
361-
if err != nil {
362-
log.Error("ReadWorkflowRawConcurrency: %v", err)
363-
continue
364-
}
365-
if wfRawConcurrency != nil {
366-
err = EvaluateRunConcurrencyFillModel(ctx, run, wfRawConcurrency, vars)
367-
if err != nil {
368-
log.Error("EvaluateRunConcurrencyFillModel: %v", err)
369-
continue
370-
}
371-
}
372-
373-
giteaCtx := GenerateGiteaContext(run, nil)
374-
375-
jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
376-
if err != nil {
377-
log.Error("jobparser.Parse: %v", err)
378-
continue
379-
}
380-
381-
if len(jobs) > 0 && jobs[0].RunName != "" {
382-
run.Title = jobs[0].RunName
383-
}
384-
385-
if err := InsertRun(ctx, run, jobs); err != nil {
386-
log.Error("InsertRun: %v", err)
387-
continue
388-
}
389-
390-
alljobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
391-
if err != nil {
392-
log.Error("FindRunJobs: %v", err)
393-
continue
394-
}
395-
CreateCommitStatus(ctx, alljobs...)
396-
if len(alljobs) > 0 {
397-
job := alljobs[0]
398-
err := job.LoadRun(ctx)
399-
if err != nil {
400-
log.Error("LoadRun: %v", err)
401-
continue
402-
}
403-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
404-
}
405-
for _, job := range alljobs {
406-
notify_service.WorkflowJobStatusUpdate(ctx, input.Repo, input.Doer, job, nil)
407-
}
408351
}
409352
return nil
410353
}
@@ -559,24 +502,6 @@ func handleSchedules(
559502
Content: dwf.Content,
560503
}
561504

562-
vars, err := actions_model.GetVariablesOfRun(ctx, run.ToActionRun())
563-
if err != nil {
564-
log.Error("GetVariablesOfRun: %v", err)
565-
continue
566-
}
567-
568-
giteaCtx := GenerateGiteaContext(run.ToActionRun(), nil)
569-
570-
jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
571-
if err != nil {
572-
log.Error("jobparser.Parse: %v", err)
573-
continue
574-
}
575-
576-
if len(jobs) > 0 && jobs[0].RunName != "" {
577-
run.Title = jobs[0].RunName
578-
}
579-
580505
crons = append(crons, run)
581506
}
582507

0 commit comments

Comments
 (0)