@@ -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
150158func toCommitStatus (status actions_model.Status ) commitstatus.CommitStatusState {
0 commit comments