Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ namespace GitVersion.Agents.Tests;
[TestFixture]
public class GitLabCiTests : TestBase
{
private GitLabCi buildServer;
private IServiceProvider sp;
private GitLabCi buildServer;
private IEnvironment environment;

[SetUp]
public void SetUp()
{
this.sp = ConfigureServices(services => services.AddSingleton<GitLabCi>());
this.buildServer = this.sp.GetRequiredService<GitLabCi>();
this.environment = this.sp.GetRequiredService<IEnvironment>();
this.environment.SetEnvironmentVariable(GitLabCi.EnvironmentVariableName, "true");
}

[TearDown]
public void TearDown() => this.environment.SetEnvironmentVariable(GitLabCi.EnvironmentVariableName, null);

[Test]
public void GenerateSetVersionMessageReturnsVersionAsIsAlthoughThisIsNotUsedByJenkins()
{
Expand All @@ -34,6 +40,55 @@ public void GenerateMessageTest()
generatedParameterMessages[0].ShouldBe("GitVersion_name=value");
}

[TestCase("main", "main")]
[TestCase("dev", "dev")]
[TestCase("development", "development")]
[TestCase("my_cool_feature", "my_cool_feature")]
[TestCase("#3-change_projectname", "#3-change_projectname")]
public void GetCurrentBranchShouldHandleBranches(string branchName, string expectedResult)
{
this.environment.SetEnvironmentVariable("CI_COMMIT_REF_NAME", branchName);

var result = this.buildServer.GetCurrentBranch(false);

result.ShouldBe(expectedResult);
}

[TestCase("main", "", "main")]
[TestCase("v1.0.0", "v1.0.0", null)]
[TestCase("development", "", "development")]
[TestCase("v1.2.1", "v1.2.1", null)]
public void GetCurrentBranchShouldHandleTags(string branchName, string commitTag, string? expectedResult)
{
this.environment.SetEnvironmentVariable("CI_COMMIT_REF_NAME", branchName);
this.environment.SetEnvironmentVariable("CI_COMMIT_TAG", commitTag); // only set in pipelines for tags

var result = this.buildServer.GetCurrentBranch(false);

if (!string.IsNullOrEmpty(expectedResult))
{
result.ShouldBe(expectedResult);
}
else
{
result.ShouldBeNull();
}
}

[TestCase("main", "main")]
[TestCase("dev", "dev")]
[TestCase("development", "development")]
[TestCase("my_cool_feature", "my_cool_feature")]
[TestCase("#3-change_projectname", "#3-change_projectname")]
public void GetCurrentBranchShouldHandlePullRequests(string branchName, string expectedResult)
{
this.environment.SetEnvironmentVariable("CI_COMMIT_REF_NAME", branchName);

var result = this.buildServer.GetCurrentBranch(false);

result.ShouldBe(expectedResult);
}

[Test]
public void WriteAllVariablesToTheTextWriter()
{
Expand Down
13 changes: 12 additions & 1 deletion src/GitVersion.BuildAgents/Agents/GitLabCi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ public override string[] GenerateSetParameterMessage(string name, string? value)
$"GitVersion_{name}={value}"
];

public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");
public override string? GetCurrentBranch(bool usingDynamicRepos)
{
// CI_COMMIT_REF_NAME can contain either the branch or the tag
// See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

// CI_COMMIT_TAG is only available in tag pipelines,
// so we can exit if CI_COMMIT_REF_NAME would return the tag
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI_COMMIT_TAG")))
return null;

return Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");
}

public override bool PreventFetch() => true;

Expand Down