@@ -30,17 +30,21 @@ import (
3030
3131// CommitStatus holds a single Status of a single Commit
3232type CommitStatus struct {
33- ID int64 `xorm:"pk autoincr"`
34- Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
35- RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
36- Repo * repo_model.Repository `xorm:"-"`
37- State commitstatus.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
38- SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
39- TargetURL string `xorm:"TEXT"`
40- Description string `xorm:"TEXT"`
41- ContextHash string `xorm:"VARCHAR(64) index"`
42- Context string `xorm:"TEXT"`
43- Creator * user_model.User `xorm:"-"`
33+ ID int64 `xorm:"pk autoincr"`
34+ Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
35+ RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
36+ Repo * repo_model.Repository `xorm:"-"`
37+ State commitstatus.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
38+ SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
39+
40+ // TargetURL points to the commit status page reported by a CI system
41+ // If Gitea Actions is used, it is a relative like "{RepoLink}/actions/runs/{RunID}/jobs{JobID}"
42+ TargetURL string `xorm:"TEXT"`
43+
44+ Description string `xorm:"TEXT"`
45+ ContextHash string `xorm:"VARCHAR(64) index"`
46+ Context string `xorm:"TEXT"`
47+ Creator * user_model.User `xorm:"-"`
4448 CreatorID int64
4549
4650 CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
@@ -211,26 +215,45 @@ func (status *CommitStatus) LocaleString(lang translation.Locale) string {
211215
212216// HideActionsURL set `TargetURL` to an empty string if the status comes from Gitea Actions
213217func (status * CommitStatus ) HideActionsURL (ctx context.Context ) {
214- if status .CreatedByGiteaActions (ctx ) {
218+ if _ , ok := status .cutTargetURLGiteaActionsPrefix (ctx ); ok {
215219 status .TargetURL = ""
216220 }
217221}
218222
219- // CreatedByGiteaActions returns true if the commit status is created by Gitea Actions
220- func (status * CommitStatus ) CreatedByGiteaActions (ctx context.Context ) bool {
223+ func (status * CommitStatus ) cutTargetURLGiteaActionsPrefix (ctx context.Context ) (string , bool ) {
221224 if status .RepoID == 0 {
222- return false
225+ return "" , false
223226 }
224227
225228 if status .Repo == nil {
226229 if err := status .loadRepository (ctx ); err != nil {
227230 log .Error ("loadRepository: %v" , err )
228- return false
231+ return "" , false
229232 }
230233 }
231234
232235 prefix := status .Repo .Link () + "/actions"
233- return strings .HasPrefix (status .TargetURL , prefix )
236+ return strings .CutPrefix (prefix , status .TargetURL )
237+ }
238+
239+ // ParseGiteaActionsTargetURL parses the commit status target URL as Gitea Actions link
240+ func (status * CommitStatus ) ParseGiteaActionsTargetURL (ctx context.Context ) (runID , jobID int64 , ok bool ) {
241+ s , ok := status .cutTargetURLGiteaActionsPrefix (ctx )
242+ if ! ok {
243+ return 0 , 0 , false
244+ }
245+
246+ parts := strings .Split (s , "/" ) // expect: /runs/{runID}/jobs/{jobID}
247+ if len (parts ) < 5 || parts [1 ] != "runs" || parts [3 ] != "jobs" {
248+ return 0 , 0 , false
249+ }
250+
251+ runID , err1 := strconv .ParseInt (parts [2 ], 10 , 64 )
252+ jobID , err2 := strconv .ParseInt (parts [4 ], 10 , 64 )
253+ if err1 != nil || err2 != nil {
254+ return 0 , 0 , false
255+ }
256+ return runID , jobID , true
234257}
235258
236259// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
0 commit comments