Skip to content

Commit a42a838

Browse files
authored
Fix ref for workflows triggered by pull_request_target (#25743)
Follow #25229 At present, when the trigger event is `pull_request_target`, the `ref` and `sha` of `ActionRun` are set according to the base branch of the pull request. This makes it impossible for us to find the head branch of the `ActionRun` directly. In this PR, the `ref` and `sha` will always be set to the head branch and they will be changed to the base branch when generating the task context.
1 parent 811fc9d commit a42a838

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

modules/actions/workflows.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
type DetectedWorkflow struct {
2424
EntryName string
2525
TriggerEvent string
26-
Commit *git.Commit
27-
Ref string
2826
Content []byte
2927
}
3028

@@ -120,7 +118,6 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
120118
dwf := &DetectedWorkflow{
121119
EntryName: entry.Name(),
122120
TriggerEvent: evt.Name,
123-
Commit: commit,
124121
Content: content,
125122
}
126123
workflows = append(workflows, dwf)

routers/api/actions/runner/utils.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,22 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
129129

130130
baseRef := ""
131131
headRef := ""
132+
ref := t.Job.Run.Ref
133+
sha := t.Job.Run.CommitSHA
132134
if pullPayload, err := t.Job.Run.GetPullRequestEventPayload(); err == nil && pullPayload.PullRequest != nil && pullPayload.PullRequest.Base != nil && pullPayload.PullRequest.Head != nil {
133135
baseRef = pullPayload.PullRequest.Base.Ref
134136
headRef = pullPayload.PullRequest.Head.Ref
137+
138+
// if the TriggerEvent is pull_request_target, ref and sha need to be set according to the base of pull request
139+
// In GitHub's documentation, ref should be the branch or tag that triggered workflow. But when the TriggerEvent is pull_request_target,
140+
// the ref will be the base branch.
141+
if t.Job.Run.TriggerEvent == actions_module.GithubEventPullRequestTarget {
142+
ref = git.BranchPrefix + pullPayload.PullRequest.Base.Name
143+
sha = pullPayload.PullRequest.Base.Sha
144+
}
135145
}
136146

137-
refName := git.RefName(t.Job.Run.Ref)
147+
refName := git.RefName(ref)
138148

139149
taskContext, err := structpb.NewStruct(map[string]any{
140150
// standard contexts, see https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
@@ -153,7 +163,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
153163
"graphql_url": "", // string, The URL of the GitHub GraphQL API.
154164
"head_ref": headRef, // string, The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target.
155165
"job": fmt.Sprint(t.JobID), // string, The job_id of the current job.
156-
"ref": t.Job.Run.Ref, // string, The fully-formed ref of the branch or tag that triggered the workflow run. For workflows triggered by push, this is the branch or tag ref that was pushed. For workflows triggered by pull_request, this is the pull request merge branch. For workflows triggered by release, this is the release tag created. For other triggers, this is the branch or tag ref that triggered the workflow run. This is only set if a branch or tag is available for the event type. The ref given is fully-formed, meaning that for branches the format is refs/heads/<branch_name>, for pull requests it is refs/pull/<pr_number>/merge, and for tags it is refs/tags/<tag_name>. For example, refs/heads/feature-branch-1.
166+
"ref": ref, // string, The fully-formed ref of the branch or tag that triggered the workflow run. For workflows triggered by push, this is the branch or tag ref that was pushed. For workflows triggered by pull_request, this is the pull request merge branch. For workflows triggered by release, this is the release tag created. For other triggers, this is the branch or tag ref that triggered the workflow run. This is only set if a branch or tag is available for the event type. The ref given is fully-formed, meaning that for branches the format is refs/heads/<branch_name>, for pull requests it is refs/pull/<pr_number>/merge, and for tags it is refs/tags/<tag_name>. For example, refs/heads/feature-branch-1.
157167
"ref_name": refName.ShortName(), // string, The short ref name of the branch or tag that triggered the workflow run. This value matches the branch or tag name shown on GitHub. For example, feature-branch-1.
158168
"ref_protected": false, // boolean, true if branch protections are configured for the ref that triggered the workflow run.
159169
"ref_type": refName.RefType(), // string, The type of ref that triggered the workflow run. Valid values are branch or tag.
@@ -167,7 +177,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
167177
"run_attempt": fmt.Sprint(t.Job.Attempt), // string, A unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run.
168178
"secret_source": "Actions", // string, The source of a secret used in a workflow. Possible values are None, Actions, Dependabot, or Codespaces.
169179
"server_url": setting.AppURL, // string, The URL of the GitHub server. For example: https://github.com.
170-
"sha": t.Job.Run.CommitSHA, // string, The commit SHA that triggered the workflow. The value of this commit SHA depends on the event that triggered the workflow. For more information, see "Events that trigger workflows." For example, ffac537e6cbbf934b08745a378932722df287a53.
180+
"sha": sha, // string, The commit SHA that triggered the workflow. The value of this commit SHA depends on the event that triggered the workflow. For more information, see "Events that trigger workflows." For example, ffac537e6cbbf934b08745a378932722df287a53.
171181
"token": t.Token, // string, A token to authenticate on behalf of the GitHub App installed on your repository. This is functionally equivalent to the GITHUB_TOKEN secret. For more information, see "Automatic token authentication."
172182
"triggering_actor": "", // string, The username of the user that initiated the workflow run. If the workflow run is a re-run, this value may differ from github.actor. Any workflow re-runs will use the privileges of github.actor, even if the actor initiating the re-run (github.triggering_actor) has different privileges.
173183
"workflow": t.Job.Run.WorkflowID, // string, The name of the workflow. If the workflow file doesn't specify a name, the value of this property is the full path of the workflow file in the repository.

services/actions/notifier_helper.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ func notify(ctx context.Context, input *notifyInput) error {
152152
} else {
153153
for _, wf := range workflows {
154154
if wf.TriggerEvent != actions_module.GithubEventPullRequestTarget {
155-
wf.Ref = ref
156155
detectedWorkflows = append(detectedWorkflows, wf)
157156
}
158157
}
@@ -174,7 +173,6 @@ func notify(ctx context.Context, input *notifyInput) error {
174173
} else {
175174
for _, wf := range baseWorkflows {
176175
if wf.TriggerEvent == actions_module.GithubEventPullRequestTarget {
177-
wf.Ref = baseRef
178176
detectedWorkflows = append(detectedWorkflows, wf)
179177
}
180178
}
@@ -212,8 +210,8 @@ func notify(ctx context.Context, input *notifyInput) error {
212210
OwnerID: input.Repo.OwnerID,
213211
WorkflowID: dwf.EntryName,
214212
TriggerUserID: input.Doer.ID,
215-
Ref: dwf.Ref,
216-
CommitSHA: dwf.Commit.ID.String(),
213+
Ref: ref,
214+
CommitSHA: commit.ID.String(),
217215
IsForkPullRequest: isForkPullRequest,
218216
Event: input.Event,
219217
EventPayload: string(p),

tests/integration/actions_trigger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestPullRequestTargetEvent(t *testing.T) {
138138

139139
// load and compare ActionRun
140140
actionRun := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: baseRepo.ID})
141-
assert.Equal(t, addWorkflowToBaseResp.Commit.SHA, actionRun.CommitSHA)
141+
assert.Equal(t, addFileToForkedResp.Commit.SHA, actionRun.CommitSHA)
142142
assert.Equal(t, actions_module.GithubEventPullRequestTarget, actionRun.TriggerEvent)
143143
})
144144
}

0 commit comments

Comments
 (0)