Skip to content

Commit 707ecec

Browse files
authored
Fix ref to trigger Actions (#22679)
If triggered by PR, the ref should be `pull/<index>/head` instead of `repo.DefaultBranch`. And improve UI: <img width="493" alt="image" src="https://user-images.githubusercontent.com/9418365/215731280-312564f2-2450-45d0-b986-1accb0670976.png"> Related to #21937.
1 parent fd29071 commit 707ecec

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

models/actions/run.go

+20
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package actions
66
import (
77
"context"
88
"fmt"
9+
"strings"
910
"time"
1011

1112
"code.gitea.io/gitea/models/db"
1213
repo_model "code.gitea.io/gitea/models/repo"
1314
user_model "code.gitea.io/gitea/models/user"
15+
"code.gitea.io/gitea/modules/git"
1416
"code.gitea.io/gitea/modules/json"
1517
api "code.gitea.io/gitea/modules/structs"
1618
"code.gitea.io/gitea/modules/timeutil"
@@ -63,6 +65,24 @@ func (run *ActionRun) Link() string {
6365
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.Index)
6466
}
6567

68+
// RefLink return the url of run's ref
69+
func (run *ActionRun) RefLink() string {
70+
refName := git.RefName(run.Ref)
71+
if refName.RefGroup() == "pull" {
72+
return run.Repo.Link() + "/pulls/" + refName.ShortName()
73+
}
74+
return git.RefURL(run.Repo.Link(), run.Ref)
75+
}
76+
77+
// PrettyRef return #id for pull ref or ShortName for others
78+
func (run *ActionRun) PrettyRef() string {
79+
refName := git.RefName(run.Ref)
80+
if refName.RefGroup() == "pull" {
81+
return "#" + strings.TrimSuffix(strings.TrimPrefix(run.Ref, git.PullPrefix), "/head")
82+
}
83+
return refName.ShortName()
84+
}
85+
6686
// LoadAttributes load Repo TriggerUser if not loaded
6787
func (run *ActionRun) LoadAttributes(ctx context.Context) error {
6888
if run == nil {

services/actions/notifier_helper.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ type notifyInput struct {
6767
func newNotifyInput(repo *repo_model.Repository, doer *user_model.User, event webhook_module.HookEventType) *notifyInput {
6868
return &notifyInput{
6969
Repo: repo,
70-
Ref: repo.DefaultBranch,
7170
Doer: doer,
7271
Event: event,
7372
}
@@ -90,6 +89,9 @@ func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput {
9089

9190
func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput {
9291
input.PullRequest = pr
92+
if input.Ref == "" {
93+
input.Ref = pr.GetGitRefName()
94+
}
9395
return input
9496
}
9597

@@ -124,8 +126,13 @@ func notify(ctx context.Context, input *notifyInput) error {
124126
}
125127
defer gitRepo.Close()
126128

129+
ref := input.Ref
130+
if ref == "" {
131+
ref = input.Repo.DefaultBranch
132+
}
133+
127134
// Get the commit object for the ref
128-
commit, err := gitRepo.GetCommit(input.Ref)
135+
commit, err := gitRepo.GetCommit(ref)
129136
if err != nil {
130137
return fmt.Errorf("gitRepo.GetCommit: %w", err)
131138
}
@@ -152,7 +159,7 @@ func notify(ctx context.Context, input *notifyInput) error {
152159
OwnerID: input.Repo.OwnerID,
153160
WorkflowID: id,
154161
TriggerUserID: input.Doer.ID,
155-
Ref: input.Ref,
162+
Ref: ref,
156163
CommitSHA: commit.ID.String(),
157164
IsForkPullRequest: input.PullRequest != nil && input.PullRequest.IsFromFork(),
158165
Event: input.Event,

templates/repo/actions/runs_list.tmpl

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
<div class="issue-item-main f1 fc df">
88
<div class="issue-item-top-row">
99
<a class="index ml-0 mr-2" href="{{if .HTMLURL}}{{.HTMLURL}}{{else}}{{$.Link}}/{{.Index}}{{end}}">
10-
{{.Title}} <span class="ui label">{{RefShortName .Ref}}</span>
10+
{{.Title}}
1111
</a>
12+
<span class="ui label">
13+
{{if .RefLink}}
14+
<a href="{{.RefLink}}">{{.PrettyRef}}</a>
15+
{{else}}
16+
{{.PrettyRef}}
17+
{{end}}
18+
</span>
1219
</div>
1320
<div class="desc issue-item-bottom-row df ac fw my-1">
1421
<b>{{if not $.CurWorkflow}}{{.WorkflowID}} {{end}}#{{.Index}}</b>: {{$.locale.Tr "actions.runs.commit"}}

0 commit comments

Comments
 (0)