Skip to content

Commit

Permalink
Fix ref to trigger Actions (#22679)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wolfogre committed Jan 31, 2023
1 parent fd29071 commit 707ecec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
20 changes: 20 additions & 0 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -63,6 +65,24 @@ 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" {
return run.Repo.Link() + "/pulls/" + refName.ShortName()
}
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" {
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 {
Expand Down
13 changes: 10 additions & 3 deletions services/actions/notifier_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type notifyInput struct {
func newNotifyInput(repo *repo_model.Repository, doer *user_model.User, event webhook_module.HookEventType) *notifyInput {
return &notifyInput{
Repo: repo,
Ref: repo.DefaultBranch,
Doer: doer,
Event: event,
}
Expand All @@ -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 = pr.GetGitRefName()
}
return input
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion templates/repo/actions/runs_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
<div class="issue-item-main f1 fc df">
<div class="issue-item-top-row">
<a class="index ml-0 mr-2" href="{{if .HTMLURL}}{{.HTMLURL}}{{else}}{{$.Link}}/{{.Index}}{{end}}">
{{.Title}} <span class="ui label">{{RefShortName .Ref}}</span>
{{.Title}}
</a>
<span class="ui label">
{{if .RefLink}}
<a href="{{.RefLink}}">{{.PrettyRef}}</a>
{{else}}
{{.PrettyRef}}
{{end}}
</span>
</div>
<div class="desc issue-item-bottom-row df ac fw my-1">
<b>{{if not $.CurWorkflow}}{{.WorkflowID}} {{end}}#{{.Index}}</b>: {{$.locale.Tr "actions.runs.commit"}}
Expand Down

0 comments on commit 707ecec

Please sign in to comment.