Skip to content

Commit 8271be5

Browse files
committed
Extract {base_,}{sha,ref} and EventName logic
1 parent 31e8ad3 commit 8271be5

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

models/actions/run.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,38 @@ func (run *ActionRun) Link() string {
7272
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.Index)
7373
}
7474

75+
func (run *ActionRun) RefShaBaseRefAndHeadRef() (string, string, string, string) {
76+
var ref, sha, baseRef, headRef string
77+
78+
ref = run.Ref
79+
sha = run.CommitSHA
80+
81+
if pullPayload, err := run.GetPullRequestEventPayload(); err == nil && pullPayload.PullRequest != nil && pullPayload.PullRequest.Base != nil && pullPayload.PullRequest.Head != nil {
82+
baseRef = pullPayload.PullRequest.Base.Ref
83+
headRef = pullPayload.PullRequest.Head.Ref
84+
85+
// if the TriggerEvent is pull_request_target, ref and sha need to be set according to the base of pull request
86+
// In GitHub's documentation, ref should be the branch or tag that triggered workflow. But when the TriggerEvent is pull_request_target,
87+
// the ref will be the base branch.
88+
if run.TriggerEvent == "pull_request_target" {
89+
ref = git.BranchPrefix + pullPayload.PullRequest.Base.Name
90+
sha = pullPayload.PullRequest.Base.Sha
91+
}
92+
}
93+
return ref, sha, baseRef, headRef
94+
}
95+
96+
func (run *ActionRun) EventName() string {
97+
// TriggerEvent is added in https://github.com/go-gitea/gitea/pull/25229
98+
// This fallback is for the old ActionRun that doesn't have the TriggerEvent field
99+
// and should be removed in 1.22
100+
eventName := run.TriggerEvent
101+
if eventName == "" {
102+
eventName = run.Event.Event()
103+
}
104+
return eventName
105+
}
106+
75107
// RefLink return the url of run's ref
76108
func (run *ActionRun) RefLink() string {
77109
refName := git.RefName(run.Ref)

routers/api/actions/runner/utils.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,8 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
117117
event := map[string]any{}
118118
_ = json.Unmarshal([]byte(t.Job.Run.EventPayload), &event)
119119

120-
// TriggerEvent is added in https://github.com/go-gitea/gitea/pull/25229
121-
// This fallback is for the old ActionRun that doesn't have the TriggerEvent field
122-
// and should be removed in 1.22
123-
eventName := t.Job.Run.TriggerEvent
124-
if eventName == "" {
125-
eventName = t.Job.Run.Event.Event()
126-
}
127-
128-
baseRef := ""
129-
headRef := ""
130-
ref := t.Job.Run.Ref
131-
sha := t.Job.Run.CommitSHA
132-
if pullPayload, err := t.Job.Run.GetPullRequestEventPayload(); err == nil && pullPayload.PullRequest != nil && pullPayload.PullRequest.Base != nil && pullPayload.PullRequest.Head != nil {
133-
baseRef = pullPayload.PullRequest.Base.Ref
134-
headRef = pullPayload.PullRequest.Head.Ref
135-
136-
// if the TriggerEvent is pull_request_target, ref and sha need to be set according to the base of pull request
137-
// In GitHub's documentation, ref should be the branch or tag that triggered workflow. But when the TriggerEvent is pull_request_target,
138-
// the ref will be the base branch.
139-
if t.Job.Run.TriggerEvent == actions_module.GithubEventPullRequestTarget {
140-
ref = git.BranchPrefix + pullPayload.PullRequest.Base.Name
141-
sha = pullPayload.PullRequest.Base.Sha
142-
}
143-
}
144-
120+
eventName := t.Job.Run.EventName()
121+
ref, sha, baseRef, headRef := t.Job.Run.RefShaBaseRefAndHeadRef()
145122
refName := git.RefName(ref)
146123

147124
taskContext, err := structpb.NewStruct(map[string]any{

0 commit comments

Comments
 (0)