Skip to content

Commit 96edae9

Browse files
committed
refactor TargetURL
1 parent 6df3821 commit 96edae9

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

models/git/commit_status.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,21 @@ import (
3030

3131
// CommitStatus holds a single Status of a single Commit
3232
type 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
213217
func (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

services/actions/commit_status.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"path"
11-
"regexp"
1211
"strconv"
1312

1413
actions_model "code.gitea.io/gitea/models/actions"
@@ -57,14 +56,11 @@ func CreateCommitStatusForRunJobs(ctx context.Context, run *actions_model.Action
5756
func GetRunsFromCommitStatuses(ctx context.Context, statuses []*git_model.CommitStatus) ([]*actions_model.ActionRun, error) {
5857
runMap := make(map[int64]*actions_model.ActionRun)
5958
for _, status := range statuses {
60-
if !status.CreatedByGiteaActions(ctx) {
59+
runIndex, _, ok := status.ParseGiteaActionsTargetURL(ctx)
60+
if !ok {
6161
continue
6262
}
63-
runIndex, _, err := getActionRunAndJobIndexFromCommitStatus(status)
64-
if err != nil {
65-
return nil, fmt.Errorf("getActionRunAndJobIndexFromCommitStatus: %w", err)
66-
}
67-
_, ok := runMap[runIndex]
63+
_, ok = runMap[runIndex]
6864
if !ok {
6965
run, err := actions_model.GetRunByIndex(ctx, status.RepoID, runIndex)
7066
if err != nil {
@@ -84,27 +80,6 @@ func GetRunsFromCommitStatuses(ctx context.Context, statuses []*git_model.Commit
8480
return runs, nil
8581
}
8682

87-
func getActionRunAndJobIndexFromCommitStatus(status *git_model.CommitStatus) (int64, int64, error) {
88-
// status.TargetURL should be like "<repo_link>/actions/runs/<run_index>/jobs/<job_index>"
89-
re := regexp.MustCompile(regexp.QuoteMeta(status.Repo.Link()+"/actions/runs/") + `(\d+)/jobs/(\d+)$`)
90-
matches := re.FindStringSubmatch(status.TargetURL)
91-
92-
if len(matches) != 3 {
93-
return 0, 0, fmt.Errorf("%s is not a Gitea Actions link", status.TargetURL)
94-
}
95-
96-
runIndex, err := strconv.ParseInt(matches[1], 10, 64)
97-
if err != nil {
98-
return 0, 0, fmt.Errorf("parse run index: %w", err)
99-
}
100-
jobIndex, err := strconv.ParseInt(matches[2], 10, 64)
101-
if err != nil {
102-
return 0, 0, fmt.Errorf("parse job index: %w", err)
103-
}
104-
105-
return runIndex, jobIndex, nil
106-
}
107-
10883
func getCommitStatusEventNameAndCommitID(run *actions_model.ActionRun) (event, commitID string, _ error) {
10984
switch run.Event {
11085
case webhook_module.HookEventPush:

0 commit comments

Comments
 (0)