Skip to content

Commit b17d33e

Browse files
authored
Merge pull request #3085 from GitTools/revert-3033-main
Revert "Use GITHUB_REF only for CI builds on branches"
2 parents 3892733 + 4cdea84 commit b17d33e

11 files changed

+17
-120
lines changed

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

-39
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,4 @@ 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-
}
12283
}

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

+1-1
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.ShouldBeNull();
75+
result.ShouldBe("refs/pull/55/head");
7676
}
7777

7878
[Test]

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

-26
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,4 @@ private void AssertVariablesAreWrittenToFile(string file)
102102
props.ShouldContain("GitVersion_Major=1");
103103
props.ShouldContain("GitVersion_Minor=2");
104104
}
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-
}
131105
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void GetCurrentBranchShouldHandleTags()
8383
var result = this.buildServer.GetCurrentBranch(false);
8484

8585
// Assert
86-
result.ShouldBeNull();
86+
result.ShouldBe("refs/tags/1.0.0");
8787
}
8888

8989
[Test]
@@ -96,7 +96,7 @@ public void GetCurrentBranchShouldHandlePullRequests()
9696
var result = this.buildServer.GetCurrentBranch(false);
9797

9898
// Assert
99-
result.ShouldBeNull();
99+
result.ShouldBe("refs/pull/1/merge");
100100
}
101101

102102
[Test]

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using GitVersion;
12
using GitVersion.BuildAgents;
23
using GitVersion.Core.Tests.Helpers;
34
using Microsoft.Extensions.DependencyInjection;
45
using NUnit.Framework;
56
using Shouldly;
67

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

910
[TestFixture]
1011
public class SpaceAutomationTests : TestBase
@@ -70,7 +71,7 @@ public void GetCurrentBranchShouldHandleTags()
7071
var result = this.buildServer.GetCurrentBranch(false);
7172

7273
// Assert
73-
result.ShouldBeNull();
74+
result.ShouldBe("refs/tags/1.0.0");
7475
}
7576

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

8586
// Assert
86-
result.ShouldBeNull();
87+
result.ShouldBe("refs/pull/1/merge");
8788
}
8889

8990
[Test]

src/GitVersion.Core/BuildAgents/AzurePipelines.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ 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)
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-
}
24+
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
3525

3626
public override bool PreventFetch() => true;
3727

src/GitVersion.Core/BuildAgents/BuildKite.cs

+3-3
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-
// To align the behavior with the other BuildAgent implementations
34-
// we return here also null.
35-
return null;
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);
3636
}
3737
}
3838

src/GitVersion.Core/BuildAgents/CodeBuild.cs

+3-11
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2525

2626
public override string? GetCurrentBranch(bool usingDynamicRepos)
2727
{
28-
string? branchName = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
29-
if (string.IsNullOrEmpty(branchName))
30-
{
31-
branchName = Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName);
32-
}
33-
34-
if (branchName != null && branchName.StartsWith("refs/heads/"))
35-
{
36-
return branchName;
37-
}
38-
return null;
28+
var currentBranch = Environment.GetEnvironmentVariable(WebHookEnvironmentVariableName);
29+
30+
return currentBranch.IsNullOrEmpty() ? Environment.GetEnvironmentVariable(SourceVersionEnvironmentVariableName) : currentBranch;
3931
}
4032

4133
public override void WriteIntegration(Action<string?> writer, VersionVariables variables, bool updateBuildNumber = true)

src/GitVersion.Core/BuildAgents/GitHubActions.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,7 @@ public override void WriteIntegration(Action<string?> writer, VersionVariables v
5050
}
5151
}
5252

53-
public override string? GetCurrentBranch(bool usingDynamicRepos)
54-
{
55-
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
56-
// GITHUB_REF must be used only for "real" branches, not for tags and pull requests.
57-
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838
58-
string? githubRef = Environment.GetEnvironmentVariable("GITHUB_REF");
59-
if (githubRef != null && githubRef.StartsWith("refs/heads/"))
60-
{
61-
return githubRef;
62-
}
63-
return null;
64-
}
53+
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GITHUB_REF");
6554

6655
public override bool PreventFetch() => true;
6756
}

src/GitVersion.Core/BuildAgents/GitLabCi.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2222
$"GitVersion_{name}={value}"
2323
};
2424

25-
// According to https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
26-
// the CI_COMMIT_BRANCH environment variable must be used.
27-
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_BRANCH");
25+
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");
2826

2927
public override bool PreventFetch() => true;
3028

src/GitVersion.Core/BuildAgents/SpaceAutomation.cs

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

1414
protected override string EnvironmentVariable => EnvironmentVariableName;
1515

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-
}
16+
public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("JB_SPACE_GIT_BRANCH");
2517

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

0 commit comments

Comments
 (0)