Skip to content

Commit c6b57b5

Browse files
fix(actions): use base branch ref for pull_request_target context
Fixes an issue where pull_request_target workflows set 'ref' in gitea/github context to 'refs/heads/owner:branch' instead of 'refs/heads/branch'. PRBranchInfo.Name contains the full label string (e.g. 'owner:branch'), whereas PRBranchInfo.Ref contains the git branch reference name (e.g. 'branch'). Signed-off-by: Sudhanshu Singh <sudhanshuwriterblc@gmail.com>
1 parent 39cc4db commit c6b57b5

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

services/actions/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GenerateGiteaContext(ctx context.Context, run *actions_model.ActionRun, att
4949
// In GitHub's documentation, ref should be the branch or tag that triggered workflow. But when the TriggerEvent is pull_request_target,
5050
// the ref will be the base branch.
5151
if run.TriggerEvent == actions_module.GithubEventPullRequestTarget {
52-
ref = git.BranchPrefix + pullPayload.PullRequest.Base.Name
52+
ref = git.BranchPrefix + pullPayload.PullRequest.Base.Ref
5353
sha = pullPayload.PullRequest.Base.Sha
5454
}
5555
}

services/actions/context_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import (
99

1010
actions_model "gitea.dev/models/actions"
1111
"gitea.dev/models/db"
12+
repo_model "gitea.dev/models/repo"
1213
"gitea.dev/models/unittest"
14+
user_model "gitea.dev/models/user"
15+
actions_module "gitea.dev/modules/actions"
1316
"gitea.dev/modules/json"
1417
api "gitea.dev/modules/structs"
18+
webhook_module "gitea.dev/modules/webhook"
1519

1620
act_model "gitea.com/gitea/runner/act/model"
1721
"github.com/stretchr/testify/assert"
@@ -318,3 +322,35 @@ func TestFindTaskNeeds(t *testing.T) {
318322
assert.Equal(t, "abc", ret["job1"].Outputs["output_a"])
319323
assert.Equal(t, "bbb", ret["job1"].Outputs["output_b"])
320324
}
325+
326+
func TestGenerateGiteaContextPullRequestTarget(t *testing.T) {
327+
payload := api.PullRequestPayload{
328+
PullRequest: &api.PullRequest{
329+
Base: &api.PRBranchInfo{
330+
Name: "owner:main",
331+
Ref: "main",
332+
Sha: "1234567890abcdef",
333+
},
334+
Head: &api.PRBranchInfo{
335+
Name: "fork:feature",
336+
Ref: "feature",
337+
Sha: "fedcba0987654321",
338+
},
339+
},
340+
}
341+
payloadBytes, err := json.Marshal(payload)
342+
assert.NoError(t, err)
343+
344+
run := &actions_model.ActionRun{
345+
Event: webhook_module.HookEventPullRequest,
346+
TriggerEvent: string(actions_module.GithubEventPullRequestTarget),
347+
EventPayload: string(payloadBytes),
348+
TriggerUser: &user_model.User{Name: "test-user"},
349+
Repo: &repo_model.Repository{Name: "test-repo", OwnerName: "test-owner"},
350+
}
351+
352+
giteaCtx := GenerateGiteaContext(t.Context(), run, nil, nil)
353+
354+
assert.Equal(t, "refs/heads/main", giteaCtx["ref"])
355+
assert.Equal(t, "main", giteaCtx["ref_name"])
356+
}

0 commit comments

Comments
 (0)