Skip to content

Commit 51b884a

Browse files
committed
align BuildAgentBase implementations
1 parent 721a338 commit 51b884a

File tree

8 files changed

+107
-17
lines changed

8 files changed

+107
-17
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,43 @@ public void AzurePipelinesBuildNumberWithSemVer(string buildNumberFormat, string
8080
var logMessage = this.buildServer.GenerateSetVersionMessage(vars);
8181
logMessage.ShouldBe(logPrefix + expectedBuildNumber);
8282
}
83+
84+
[Test]
85+
public void GetCurrentBranchShouldHandleBranches()
86+
{
87+
// Arrange
88+
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", $"refs/heads/{MainBranch}");
89+
90+
// Act
91+
var result = this.buildServer.GetCurrentBranch(false);
92+
93+
// Assert
94+
result.ShouldBe($"refs/heads/{MainBranch}");
95+
}
96+
97+
[Test]
98+
public void GetCurrentBranchShouldHandleTags()
99+
{
100+
// Arrange
101+
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", "refs/tags/1.0.0");
102+
103+
// Act
104+
var result = this.buildServer.GetCurrentBranch(false);
105+
106+
// Assert
107+
result.ShouldBeNull();
108+
}
109+
110+
[Test]
111+
public void GetCurrentBranchShouldHandlePullRequests()
112+
{
113+
// Arrange
114+
this.environment.SetEnvironmentVariable("BUILD_SOURCEBRANCH", "refs/pull/1/merge");
115+
116+
// Act
117+
var result = this.buildServer.GetCurrentBranch(false);
118+
119+
// Assert
120+
result.ShouldBeNull();
121+
}
83122
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
7272
var result = this.buildServer.GetCurrentBranch(false);
7373

7474
// Assert
75-
result.ShouldBe("refs/pull/55/head");
75+
result.ShouldBeNull();
7676
}
7777

7878
[Test]

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GitVersion.BuildAgents;
22
using GitVersion.Core.Tests.Helpers;
3+
using GitVersion.Helpers;
34
using GitVersion.VersionCalculation;
45
using Microsoft.Extensions.DependencyInjection;
56
using NUnit.Framework;
@@ -54,7 +55,8 @@ public void PicksUpBranchNameFromEnvironmentFromWebHook()
5455
public void WriteAllVariablesToTheTextWriter()
5556
{
5657
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
57-
var f = Path.Combine(assemblyLocation, "codebuild_this_file_should_be_deleted.properties");
58+
assemblyLocation.ShouldNotBeNull();
59+
var f = PathHelper.Combine(assemblyLocation, "codebuild_this_file_should_be_deleted.properties");
5860

5961
try
6062
{
@@ -68,7 +70,7 @@ public void WriteAllVariablesToTheTextWriter()
6870

6971
private void AssertVariablesAreWrittenToFile(string file)
7072
{
71-
var writes = new List<string>();
73+
var writes = new List<string?>();
7274
var semanticVersion = new SemanticVersion
7375
{
7476
Major = 1,
@@ -100,4 +102,30 @@ private void AssertVariablesAreWrittenToFile(string file)
100102
props.ShouldContain("GitVersion_Major=1");
101103
props.ShouldContain("GitVersion_Minor=2");
102104
}
105+
106+
[Test]
107+
public void GetCurrentBranchShouldHandleTags()
108+
{
109+
// Arrange
110+
this.environment.SetEnvironmentVariable("CODEBUILD_WEBHOOK_HEAD_REF", "refs/tags/1.0.0");
111+
112+
// Act
113+
var result = this.buildServer.GetCurrentBranch(false);
114+
115+
// Assert
116+
result.ShouldBeNull();
117+
}
118+
119+
[Test]
120+
public void GetCurrentBranchShouldHandlePullRequests()
121+
{
122+
// Arrange
123+
this.environment.SetEnvironmentVariable("CODEBUILD_SOURCE_VERSION", "refs/pull/1/merge");
124+
125+
// Act
126+
var result = this.buildServer.GetCurrentBranch(false);
127+
128+
// Assert
129+
result.ShouldBeNull();
130+
}
103131
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using GitVersion;
21
using GitVersion.BuildAgents;
32
using GitVersion.Core.Tests.Helpers;
43
using Microsoft.Extensions.DependencyInjection;
54
using NUnit.Framework;
65
using Shouldly;
76

8-
namespace GitVersionCore.Tests.BuildAgents;
7+
namespace GitVersion.Core.Tests.BuildAgents;
98

109
[TestFixture]
1110
public class SpaceAutomationTests : TestBase
@@ -71,7 +70,7 @@ public void GetCurrentBranchShouldHandleTags()
7170
var result = this.buildServer.GetCurrentBranch(false);
7271

7372
// Assert
74-
result.ShouldBe("refs/tags/1.0.0");
73+
result.ShouldBeNull();
7574
}
7675

7776
[Test]
@@ -84,7 +83,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
8483
var result = this.buildServer.GetCurrentBranch(false);
8584

8685
// Assert
87-
result.ShouldBe("refs/pull/1/merge");
86+
result.ShouldBeNull();
8887
}
8988

9089
[Test]

src/GitVersion.Core/BuildAgents/AzurePipelines.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2121
$"##vso[task.setvariable variable=GitVersion.{name};isOutput=true]{value}"
2222
};
2323

24-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
24+
public override string? GetCurrentBranch(bool usingDynamicRepos)
25+
{
26+
// https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables
27+
// BUILD_SOURCEBRANCH does not contain the branch name if the build was triggered by a tag or pull request.
28+
string? branchName = Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
29+
if (branchName != null && branchName.StartsWith("refs/heads/"))
30+
{
31+
return branchName;
32+
}
33+
return null;
34+
}
2535

2636
public override bool PreventFetch() => true;
2737

src/GitVersion.Core/BuildAgents/BuildKite.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3030
}
3131
else
3232
{
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);
33+
// To align the behavior with the other BuildAgent implementations
34+
// we return here also null.
35+
return null;
3636
}
3737
}
3838

src/GitVersion.Core/BuildAgents/CodeBuild.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,24 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2525

2626
public override string? GetCurrentBranch(bool usingDynamicRepos)
2727
{
28-
var currentBranch = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
29-
30-
if (currentBranch.IsNullOrEmpty())
28+
string? branchName = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
29+
if (string.IsNullOrEmpty(branchName))
3130
{
32-
return Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
31+
branchName = Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
3332
}
3433

35-
return currentBranch;
34+
if (branchName != null && branchName.StartsWith("refs/heads/"))
35+
{
36+
return branchName;
37+
}
38+
return null;
3639
}
3740

3841
public override void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)
3942
{
43+
if (this.file is null)
44+
return;
45+
4046
base.WriteIntegration(writer, variables, updateBuildNumber);
4147
writer($"Outputting variables to '{this.file}' ... ");
4248
File.WriteAllLines(this.file, GenerateBuildLogOutput(variables));

src/GitVersion.Core/BuildAgents/SpaceAutomation.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ public SpaceAutomation(IEnvironment environment, ILog log) : base(environment, l
1313

1414
protected override string EnvironmentVariable => EnvironmentVariableName;
1515

16-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
16+
public override string? GetCurrentBranch(bool usingDynamicRepos)
17+
{
18+
string? branchName = Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
19+
if (branchName != null && branchName.StartsWith("refs/heads/"))
20+
{
21+
return branchName;
22+
}
23+
return null;
24+
}
1725

1826
public override string[] GenerateSetParameterMessage(string name, string value) => Array.Empty<string>();
1927

0 commit comments

Comments
 (0)