Skip to content

Commit 9768960

Browse files
authored
fix: cross-repo create-pull-request checkout ignores triggering branch ref (#28395)
1 parent 8359733 commit 9768960

4 files changed

Lines changed: 64 additions & 6 deletions

File tree

.changeset/patch-fix-cross-repo-checkout-ref.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/smoke-create-cross-repo-pr.lock.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/workflow/compiler_safe_outputs_steps.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,18 @@ func (c *Compiler) buildSharedPRCheckoutSteps(data *WorkflowData) []string {
7272
// targeting.  Then a lot of this gnarly event code will be only on the "front end" (prepping the
7373
// coding agent) not the "backend" (applying the safe outputs)
7474
const baseBranchFallbackExpr = "${{ github.base_ref || github.event.pull_request.base.ref || github.ref_name || github.event.repository.default_branch }}"
75+
// Cross-repo fallback omits github.ref_name because it refers to the branch in the triggering repository,
76+
// which may not exist in the target repository (e.g., when triggered via workflow_dispatch from a feature branch).
77+
const crossRepoFallbackExpr = "${{ github.base_ref || github.event.pull_request.base.ref || github.event.repository.default_branch }}"
7578
var checkoutRef string
7679
if data.SafeOutputs.CreatePullRequests != nil && data.SafeOutputs.CreatePullRequests.BaseBranch != "" {
7780
checkoutRef = data.SafeOutputs.CreatePullRequests.BaseBranch
7881
consolidatedSafeOutputsStepsLog.Printf("Using custom base-branch from create-pull-request for checkout ref: %s", checkoutRef)
82+
} else if targetRepoSlug != "" {
83+
// Cross-repo checkout: avoid github.ref_name which refers to the triggering branch,
84+
// not a branch in the target repository.
85+
checkoutRef = crossRepoFallbackExpr
86+
consolidatedSafeOutputsStepsLog.Printf("Using cross-repo fallback base branch expression for checkout ref (no github.ref_name)")
7987
} else {
8088
checkoutRef = baseBranchFallbackExpr
8189
consolidatedSafeOutputsStepsLog.Printf("Using fallback base branch expression for checkout ref")

pkg/workflow/compiler_safe_outputs_steps_test.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
// TestBuildSharedPRCheckoutSteps tests shared PR checkout step generation
1414
func TestBuildSharedPRCheckoutSteps(t *testing.T) {
1515
tests := []struct {
16-
name string
17-
safeOutputs *SafeOutputsConfig
18-
trialMode bool
19-
trialRepo string
20-
checkContains []string
16+
name string
17+
safeOutputs *SafeOutputsConfig
18+
trialMode bool
19+
trialRepo string
20+
checkContains []string
21+
checkNotContains []string
2122
}{
2223
{
2324
name: "create pull request only",
@@ -120,6 +121,46 @@ func TestBuildSharedPRCheckoutSteps(t *testing.T) {
120121
"token: ${{ secrets.GH_AW_CROSS_REPO_PAT }}",
121122
"GIT_TOKEN: ${{ secrets.GH_AW_CROSS_REPO_PAT }}",
122123
`REPO_NAME: "org/target-repo"`,
124+
// Cross-repo checkout must not use github.ref_name
125+
"ref: ${{ github.base_ref || github.event.pull_request.base.ref || github.event.repository.default_branch }}",
126+
},
127+
},
128+
{
129+
name: "cross-repo without base-branch uses safe ref omitting github.ref_name",
130+
safeOutputs: &SafeOutputsConfig{
131+
CreatePullRequests: &CreatePullRequestsConfig{
132+
TargetRepoSlug: "org/other-repo",
133+
},
134+
},
135+
checkContains: []string{
136+
"ref: ${{ github.base_ref || github.event.pull_request.base.ref || github.event.repository.default_branch }}",
137+
},
138+
checkNotContains: []string{
139+
"github.ref_name",
140+
},
141+
},
142+
{
143+
name: "trial mode cross-repo omits github.ref_name from checkout ref",
144+
trialMode: true,
145+
trialRepo: "org/trial-repo",
146+
safeOutputs: &SafeOutputsConfig{
147+
CreatePullRequests: &CreatePullRequestsConfig{},
148+
},
149+
checkContains: []string{
150+
"repository: org/trial-repo",
151+
"ref: ${{ github.base_ref || github.event.pull_request.base.ref || github.event.repository.default_branch }}",
152+
},
153+
},
154+
{
155+
name: "cross-repo with explicit base-branch uses base-branch not cross-repo fallback",
156+
safeOutputs: &SafeOutputsConfig{
157+
CreatePullRequests: &CreatePullRequestsConfig{
158+
TargetRepoSlug: "org/other-repo",
159+
BaseBranch: "develop",
160+
},
161+
},
162+
checkContains: []string{
163+
"ref: develop",
123164
},
124165
},
125166
{
@@ -212,6 +253,10 @@ func TestBuildSharedPRCheckoutSteps(t *testing.T) {
212253
for _, expected := range tt.checkContains {
213254
assert.Contains(t, stepsContent, expected, "Expected to find: "+expected)
214255
}
256+
257+
for _, notExpected := range tt.checkNotContains {
258+
assert.NotContains(t, stepsContent, notExpected, "Expected NOT to find: "+notExpected)
259+
}
215260
})
216261
}
217262
}

0 commit comments

Comments
 (0)