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