From 0c75f134d6744c8d9631eec759e202ffcc45f0e9 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sun, 29 Jan 2023 14:00:36 +0800 Subject: [PATCH 1/4] fix: ref to notify --- services/actions/notifier_helper.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 44df568cbe9a..4d371c87ccd2 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -67,7 +67,6 @@ type notifyInput struct { func newNotifyInput(repo *repo_model.Repository, doer *user_model.User, event webhook_module.HookEventType) *notifyInput { return ¬ifyInput{ Repo: repo, - Ref: repo.DefaultBranch, Doer: doer, Event: event, } @@ -90,6 +89,9 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput { func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput { input.PullRequest = pr + if input.Ref == "" { + input.Ref = fmt.Sprintf("pull/%d/head", pr.Index) + } return input } @@ -124,8 +126,13 @@ func notify(ctx context.Context, input *notifyInput) error { } defer gitRepo.Close() + ref := input.Ref + if ref == "" { + ref = input.Repo.DefaultBranch + } + // Get the commit object for the ref - commit, err := gitRepo.GetCommit(input.Ref) + commit, err := gitRepo.GetCommit(ref) if err != nil { return fmt.Errorf("gitRepo.GetCommit: %w", err) } @@ -152,7 +159,7 @@ func notify(ctx context.Context, input *notifyInput) error { OwnerID: input.Repo.OwnerID, WorkflowID: id, TriggerUserID: input.Doer.ID, - Ref: input.Ref, + Ref: ref, CommitSHA: commit.ID.String(), IsForkPullRequest: input.PullRequest != nil && input.PullRequest.IsFromFork(), Event: input.Event, From b81bbfbb7ce4b0f6fbd70844bd24753e76da643c Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 31 Jan 2023 18:12:05 +0800 Subject: [PATCH 2/4] feat: link to PR --- models/actions/run.go | 23 +++++++++++++++++++++++ services/actions/notifier_helper.go | 2 +- templates/repo/actions/runs_list.tmpl | 9 ++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 2b748bb0d563..c997c0d49699 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -6,11 +6,13 @@ package actions import ( "context" "fmt" + "strings" "time" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -63,6 +65,27 @@ func (run *ActionRun) Link() string { return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.Index) } +func (run *ActionRun) RefLink() string { + refName := git.RefName(run.Ref) + switch refName.RefGroup() { + case "heads": + return run.Repo.Link() + "/src/branch/" + refName.ShortName() + case "tags": + return run.Repo.Link() + "/src/tag/" + refName.ShortName() + case "pull": + return run.Repo.Link() + "/pulls/" + refName.ShortName() + } + return "" +} + +func (run *ActionRun) PrettyRef() string { + refName := git.RefName(run.Ref) + if refName.RefGroup() == "pull" { + return "#" + strings.TrimSuffix(strings.TrimPrefix(run.Ref, git.PullPrefix), "/head") + } + return refName.ShortName() +} + // LoadAttributes load Repo TriggerUser if not loaded func (run *ActionRun) LoadAttributes(ctx context.Context) error { if run == nil { diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 4d371c87ccd2..9c9fc644cb49 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -90,7 +90,7 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput { func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput { input.PullRequest = pr if input.Ref == "" { - input.Ref = fmt.Sprintf("pull/%d/head", pr.Index) + input.Ref = pr.GetGitRefName() } return input } diff --git a/templates/repo/actions/runs_list.tmpl b/templates/repo/actions/runs_list.tmpl index 2e2e037de02e..d3eb12430f28 100644 --- a/templates/repo/actions/runs_list.tmpl +++ b/templates/repo/actions/runs_list.tmpl @@ -7,8 +7,15 @@
- {{.Title}} {{RefShortName .Ref}} + {{.Title}} + + {{if .RefLink}} + {{.PrettyRef}} + {{else}} + {{.PrettyRef}} + {{end}} +
{{if not $.CurWorkflow}}{{.WorkflowID}} {{end}}#{{.Index}}: {{$.locale.Tr "actions.runs.commit"}} From 111c436c5b80ddaddc18c78fbdc6ed3585b0e0f1 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 31 Jan 2023 18:32:17 +0800 Subject: [PATCH 3/4] fix: use git.RefURL --- models/actions/run.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index c997c0d49699..e956d2575362 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -67,15 +67,10 @@ func (run *ActionRun) Link() string { func (run *ActionRun) RefLink() string { refName := git.RefName(run.Ref) - switch refName.RefGroup() { - case "heads": - return run.Repo.Link() + "/src/branch/" + refName.ShortName() - case "tags": - return run.Repo.Link() + "/src/tag/" + refName.ShortName() - case "pull": + if refName.RefGroup() == "pull" { return run.Repo.Link() + "/pulls/" + refName.ShortName() } - return "" + return git.RefURL(run.Repo.Link(), run.Ref) } func (run *ActionRun) PrettyRef() string { From a403670b3767a17fda6520d4122c60b9ff77fa67 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 31 Jan 2023 18:34:33 +0800 Subject: [PATCH 4/4] chore: add comments --- models/actions/run.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/models/actions/run.go b/models/actions/run.go index e956d2575362..14d191c814e3 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -65,6 +65,7 @@ func (run *ActionRun) Link() string { return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.Index) } +// RefLink return the url of run's ref func (run *ActionRun) RefLink() string { refName := git.RefName(run.Ref) if refName.RefGroup() == "pull" { @@ -73,6 +74,7 @@ func (run *ActionRun) RefLink() string { return git.RefURL(run.Repo.Link(), run.Ref) } +// PrettyRef return #id for pull ref or ShortName for others func (run *ActionRun) PrettyRef() string { refName := git.RefName(run.Ref) if refName.RefGroup() == "pull" {