Skip to content

Commit a7ca32f

Browse files
Fix for GITHUB_HEAD_REF having empty value (#779)
* Fix for GITHUB_HEAD_REF having empty value * Fix test
1 parent 24edffa commit a7ca32f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/main/java/pl/allegro/tech/build/axion/release/infrastructure/git/GitRepository.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ private String branchName() {
343343
*/
344344
private Optional<String> branchNameFromGithubEnvVariable() {
345345
if (env("GITHUB_ACTIONS").isPresent()) {
346-
return env("GITHUB_HEAD_REF");
346+
return env("GITHUB_HEAD_REF")
347+
// GitHub violates its own documentation and sets this variable always. For pull_request and
348+
// pull_request_target it contains actual value, for every other event it's just empty string
349+
.filter(it -> !it.isBlank());
347350
}
348351
return Optional.empty();
349352
}

src/test/groovy/pl/allegro/tech/build/axion/release/infrastructure/git/GitRepositoryTest.groovy

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import static pl.allegro.tech.build.axion.release.domain.scm.ScmPropertiesBuilde
3030

3131
class GitRepositoryTest extends Specification {
3232

33-
public static final String MASTER_BRANCH = "master"
3433
File repositoryDir
3534

3635
File remoteRepositoryDir
@@ -43,6 +42,8 @@ class GitRepositoryTest extends Specification {
4342

4443
GitRepository repository
4544

45+
String defaultBranch;
46+
4647
void setup() {
4748
remoteRepositoryDir = File.createTempDir('axion-release', 'tmp')
4849
Map remoteRepositories = GitProjectBuilder.gitProject(remoteRepositoryDir).withInitialCommit().build()
@@ -54,6 +55,9 @@ class GitRepositoryTest extends Specification {
5455

5556
rawRepository = repositories[Grgit]
5657
repository = repositories[GitRepository]
58+
59+
assert rawRepository.branch.current().name == remoteRawRepository.branch.current().name
60+
defaultBranch = rawRepository.branch.current().name
5761
}
5862

5963
def "should throw unavailable exception when initializing in unexisitng repository"() {
@@ -592,7 +596,7 @@ class GitRepositoryTest extends Specification {
592596
git.checkout().setName(secondBranchName).call()
593597
commitFile('second/aa', 'foo')
594598
commitFile('b/ba', 'bar')
595-
git.checkout().setName(MASTER_BRANCH).call()
599+
git.checkout().setName(defaultBranch).call()
596600
git.merge().include(git.repository.resolve(secondBranchName)).setCommit(true).setMessage("unintresting").setFastForward(MergeCommand.FastForwardMode.NO_FF).call()
597601

598602
commitFile('after/aa', 'after')
@@ -631,7 +635,6 @@ class GitRepositoryTest extends Specification {
631635

632636
@WithEnvironment([
633637
'GITHUB_ACTIONS=true',
634-
'GITHUB_EVENT_NAME=pull_request',
635638
'GITHUB_HEAD_REF=pr-source-branch'
636639
])
637640
def "should get branch name on Github Actions if pull_request triggered the workflow"() {
@@ -644,15 +647,14 @@ class GitRepositoryTest extends Specification {
644647

645648
@WithEnvironment([
646649
'GITHUB_ACTIONS=true',
647-
'GITHUB_EVENT_NAME=pull_request_target',
648-
'GITHUB_HEAD_REF=pr-source-branch'
650+
'GITHUB_HEAD_REF='
649651
])
650-
def "should get branch name on Github Actions if pull_request_target triggered the workflow"() {
652+
def "should ignore GITHUB_HEAD_REF variable if it has empty value"() {
651653
when:
652654
ScmPosition position = repository.currentPosition()
653655

654656
then:
655-
position.branch == 'pr-source-branch'
657+
position.branch == defaultBranch
656658
}
657659

658660
private void commitFile(String subDir, String fileName) {

src/test/groovy/pl/allegro/tech/build/axion/release/util/WithEnvironmentExtension.groovy

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ class WithEnvironmentExtension implements IAnnotationDrivenExtension<WithEnviron
1515
feature.getFeatureMethod().addInterceptor { invocation ->
1616
List<Pair<String, String>> envVarDefinitions = annotation.value().toList().stream()
1717
.map { it.split("=") }
18-
.map { Pair.of(it[0], it[1]) }
18+
.map { parts ->
19+
switch (parts.length) {
20+
case 2: return Pair.of(parts[0], parts[1])
21+
case 1: return Pair.of(parts[0], "")
22+
default: return null
23+
}
24+
}
25+
.filter { it != null }
1926
.collect(toList())
2027

2128
envVarDefinitions.forEach { setEnvVariable(it.first(), it.second()) }

0 commit comments

Comments
 (0)