@@ -71,4 +71,57 @@ class CrossRepositoryPullRequestSpec : FunSpec({
7171 projects.versions shouldOnlyHave " 1.0.1-fix-builds-fail-cross-repo.0"
7272 }
7373 }
74+
75+ test("should handle cross-repo PR with both GITHUB_HEAD_REF and GITHUB_REF_NAME set") {
76+ withEnvironment(
77+ environment = mapOf(
78+ Env .CI to "true",
79+ Env .GITHUB_HEAD_REF to forkedFeatureBranch,
80+ Env .GITHUB_REF_NAME to "123/merge", // Typical PR merge ref
81+ ),
82+ mode = OverrideMode .SetOrOverride ,
83+ ) {
84+ // Given: Similar setup but with both environment variables set
85+ projects.git {
86+ initialBranch = mainBranch
87+ actions = actions {
88+ commit(message = "1 commit on $mainBranch", tag = "1.0.0")
89+ checkout(mainBranch) // Stay on main to simulate cross-repo PR checkout
90+ }
91+ }
92+
93+ // When: Building should prioritize GITHUB_HEAD_REF over GITHUB_REF_NAME
94+ projects.build(GradleVersion .current())
95+
96+ // Then: Should use the forked branch name, not the merge ref
97+ projects.versions shouldOnlyHave " 1.0.1-fix-builds-fail-cross-repo.0"
98+ }
99+ }
100+
101+ test("should fallback to GITHUB_REF_NAME when GITHUB_HEAD_REF is empty") {
102+ withEnvironment(
103+ environment = mapOf(
104+ Env .CI to "true",
105+ Env .GITHUB_HEAD_REF to "", // Empty but present
106+ Env .GITHUB_REF_NAME to "feature-branch-fallback",
107+ ),
108+ mode = OverrideMode .SetOrOverride ,
109+ ) {
110+ // Given: GITHUB_HEAD_REF is empty (not a PR) but GITHUB_REF_NAME is set
111+ projects.git {
112+ initialBranch = mainBranch
113+ actions = actions {
114+ commit(message = "1 commit on $mainBranch", tag = "1.0.0")
115+ checkout("feature-branch-fallback")
116+ commit(message = "1 commit on feature branch")
117+ }
118+ }
119+
120+ // When: Building should use GITHUB_REF_NAME since GITHUB_HEAD_REF is empty
121+ projects.build(GradleVersion .current())
122+
123+ // Then: Should use the ref name from GITHUB_REF_NAME with correct commit count
124+ projects.versions shouldOnlyHave " 1.0.1-feature-branch-fallback.1"
125+ }
126+ }
74127})
0 commit comments