Skip to content

Commit 4e6bc4a

Browse files
committed
Try another change
1 parent b9cad33 commit 4e6bc4a

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/functionalTest/kotlin/com/figure/gradle/semver/specs/CrossRepositoryPullRequestSpec.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
})

src/main/kotlin/com/figure/gradle/semver/internal/command/Branch.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ class Branch(
4141
}
4242

4343
// Handle cross-repo PRs where the branch doesn't exist locally
44-
// but we still want to use the branch name from GITHUB_HEAD_REF
45-
return if (Env.isCI && Env.githubHeadRef != null && refName == Env.githubHeadRef) {
44+
// This happens when GitHub Actions checks out the target repository but the
45+
// branch name from GITHUB_HEAD_REF (from forked repo) doesn't exist locally
46+
return if (Env.isCI && Env.githubHeadRef != null) {
4647
// Create a synthetic ref object that preserves the original branch name
47-
SyntheticRef(name = "refs/heads/$refName", target = headRef.objectId)
48+
// This ensures version calculation uses the actual feature branch name
49+
// instead of falling back to HEAD or the merge ref
50+
SyntheticRef(name = "refs/heads/${Env.githubHeadRef}", target = headRef.objectId)
4851
} else {
49-
headRef.takeIf { !it.target.name.startsWith("refs/heads/") }
50-
?: headRef
52+
// For non-CI environments or when not in a PR, use HEAD
53+
headRef
5154
}
5255
}
5356

0 commit comments

Comments
 (0)