Skip to content

Commit

Permalink
Add extra logic to handle tag refs in GithubActions
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Nov 13, 2023
1 parent afd3e0a commit 032ecff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void SetUp()
this.environment = sp.GetRequiredService<IEnvironment>();
this.buildServer = sp.GetRequiredService<GitHubActions>();
this.environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true");
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "branch");

this.githubSetEnvironmentTempFilePath = Path.GetTempFileName();
this.environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, this.githubSetEnvironmentTempFilePath);
Expand Down Expand Up @@ -75,26 +76,30 @@ public void GetCurrentBranchShouldHandleBranches()
public void GetCurrentBranchShouldHandleTags()
{
// Arrange
this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "tag");
this.environment.SetEnvironmentVariable("GITHUB_REF", "refs/tags/1.0.0");

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

// Assert
result.ShouldBe("refs/tags/1.0.0");
result.ShouldBeNull();
}

[Test]
public void GetCurrentBranchShouldHandlePullRequests()
{
// Arrange
this.environment.SetEnvironmentVariable("GITHUB_EVENT_NAME", "pull_request");
this.environment.SetEnvironmentVariable("GITHUB_HEAD_REF", "some-branch");
this.environment.SetEnvironmentVariable("GITHUB_BASE_REF", MainBranch);
this.environment.SetEnvironmentVariable("GITHUB_REF", "refs/pull/1/merge");

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

// Assert
result.ShouldBe("refs/pull/1/merge");
result.ShouldBe("some-branch");
}

[Test]
Expand Down
23 changes: 22 additions & 1 deletion src/GitVersion.BuildAgents/Agents/GitHubActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,28 @@ public override void WriteIntegration(Action<string?> writer, GitVersionVariable
}
}

public override string? GetCurrentBranch(bool usingDynamicRepos) => Environment.GetEnvironmentVariable("GITHUB_REF");

public override string? GetCurrentBranch(bool usingDynamicRepos)
{
var refType = Environment.GetEnvironmentVariable("GITHUB_REF_TYPE") ?? "";
var eventName = Environment.GetEnvironmentVariable("GITHUB_EVENT_NAME") ?? "";
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
// GITHUB_REF must be used only for "real" branches, not for tags.
// Bug fix for https://github.com/GitTools/GitVersion/issues/2838

// pull_request or pull_request_target
if (eventName.StartsWith("pull_request", StringComparison.OrdinalIgnoreCase))
{
return Environment.GetEnvironmentVariable("GITHUB_HEAD_REF");
}

if (refType.Equals("tag", StringComparison.OrdinalIgnoreCase))
{
return null;
}

return Environment.GetEnvironmentVariable("GITHUB_REF");
}

public override bool PreventFetch() => true;
}

0 comments on commit 032ecff

Please sign in to comment.