Skip to content

Commit 5471212

Browse files
authored
Enable correct PR versioning for Buildkite (#3013)
* Enable correct PR versioning for Buildkite * Fix formatting issues
1 parent b0649e1 commit 5471212

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/GitVersion.Core.Tests/BuildAgents/BuildKiteTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void GetCurrentBranchShouldHandleBranches()
5252
{
5353
// Arrange
5454
this.environment.SetEnvironmentVariable("BUILDKITE_BRANCH", MainBranch);
55+
this.environment.SetEnvironmentVariable("BUILDKITE_PULL_REQUEST", "false");
5556

5657
// Act
5758
var result = this.buildServer.GetCurrentBranch(false);
@@ -60,6 +61,20 @@ public void GetCurrentBranchShouldHandleBranches()
6061
result.ShouldBe(MainBranch);
6162
}
6263

64+
[Test]
65+
public void GetCurrentBranchShouldHandlePullRequests()
66+
{
67+
// Arrange
68+
this.environment.SetEnvironmentVariable("BUILDKITE_BRANCH", "feature/new");
69+
this.environment.SetEnvironmentVariable("BUILDKITE_PULL_REQUEST", "55");
70+
71+
// Act
72+
var result = this.buildServer.GetCurrentBranch(false);
73+
74+
// Assert
75+
result.ShouldBe("refs/pull/55/head");
76+
}
77+
6378
[Test]
6479
public void GetSetParameterMessageShouldReturnEmptyArray()
6580
{

src/GitVersion.Core/BuildAgents/BuildKite.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ public override string GenerateSetVersionMessage(VersionVariables variables) =>
2121
public override string[] GenerateSetParameterMessage(string name, string value) =>
2222
Array.Empty<string>(); // There is no equivalent function in BuildKite.
2323

24-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILDKITE_BRANCH");
24+
public override string? GetCurrentBranch(bool usingDynamicRepos)
25+
{
26+
var pullRequest = Environment.GetEnvironmentVariable("BUILDKITE_PULL_REQUEST");
27+
if (string.IsNullOrEmpty(pullRequest) || pullRequest == "false")
28+
{
29+
return Environment.GetEnvironmentVariable("BUILDKITE_BRANCH");
30+
}
31+
else
32+
{
33+
// For pull requests BUILDKITE_BRANCH refers to the head, so adjust the
34+
// branch name for pull request versioning to function as expected
35+
return string.Format("refs/pull/{0}/head", pullRequest);
36+
}
37+
}
2538

2639
public override bool PreventFetch() => true;
2740
}

0 commit comments

Comments
 (0)