From 032ecff2cad2587b669862a80b7196c578b1d845 Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Mon, 13 Nov 2023 16:27:31 -0500 Subject: [PATCH] Add extra logic to handle tag refs in GithubActions --- .../Agents/GitHubActionsTests.cs | 9 ++++++-- .../Agents/GitHubActions.cs | 23 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs index 9472bb1104..ad85f36e11 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/GitHubActionsTests.cs @@ -18,6 +18,7 @@ public void SetUp() this.environment = sp.GetRequiredService(); this.buildServer = sp.GetRequiredService(); this.environment.SetEnvironmentVariable(GitHubActions.EnvironmentVariableName, "true"); + this.environment.SetEnvironmentVariable("GITHUB_REF_TYPE", "branch"); this.githubSetEnvironmentTempFilePath = Path.GetTempFileName(); this.environment.SetEnvironmentVariable(GitHubActions.GitHubSetEnvTempFileEnvironmentVariableName, this.githubSetEnvironmentTempFilePath); @@ -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] diff --git a/src/GitVersion.BuildAgents/Agents/GitHubActions.cs b/src/GitVersion.BuildAgents/Agents/GitHubActions.cs index 380d216862..8038b1facb 100644 --- a/src/GitVersion.BuildAgents/Agents/GitHubActions.cs +++ b/src/GitVersion.BuildAgents/Agents/GitHubActions.cs @@ -50,7 +50,28 @@ public override void WriteIntegration(Action 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; }