diff --git a/src/GitVersion.sln.DotSettings b/src/GitVersion.sln.DotSettings index 6f32af444a..1ea5318cf3 100644 --- a/src/GitVersion.sln.DotSettings +++ b/src/GitVersion.sln.DotSettings @@ -485,7 +485,7 @@ II.2.12 <HandlesEvent /> </Patterns> CustomLayout - True + False True True False diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs new file mode 100644 index 0000000000..e587b1fada --- /dev/null +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -0,0 +1,167 @@ +using System; +using GitVersion; +using GitVersion.BuildServers; +using GitVersion.Logging; +using NUnit.Framework; +using Shouldly; +using System.Collections.Generic; +using Environment = System.Environment; + +namespace GitVersionCore.Tests.BuildServers +{ + + [TestFixture] + public class GitHubActionsTests : TestBase + { + private IEnvironment environment; + private ILog log; + + [SetUp] + public void SetUp() + { + log = new NullLog(); + + environment = new TestEnvironment(); + environment.SetEnvironmentVariable("GITHUB_ACTION", Guid.NewGuid().ToString()); + } + + [TearDown] + public void TearDown() + { + environment.SetEnvironmentVariable("GITHUB_ACTION", null); + } + + [Test] + public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.CanApplyToCurrentContext(); + + // Assert + result.ShouldBeTrue(); + } + + [Test] + public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet() + { + // Arrange + environment.SetEnvironmentVariable("GITHUB_ACTION", ""); + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.CanApplyToCurrentContext(); + + // Assert + result.ShouldBeFalse(); + } + + [Test] + public void GetCurrentBranchShouldGetBranchIfSet() + { + // Arrange + const string expected = "actionsBranch"; + + environment.SetEnvironmentVariable("GITHUB_REF", $"refs/heads/{expected}"); + + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBe(expected); + } + + [Test] + public void GetCurrentBranchShouldNotMatchTag() + { + // Arrange + environment.SetEnvironmentVariable("GITHUB_REF", $"refs/tags/v1.0.0"); + + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBeNull(); + } + + [TestCase("Something", "1.0.0", + "::set-env name=GitVersion_Something::1.0.0")] + public void GetSetParameterMessage(string key, string value, string expectedResult) + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + // Assert + environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace(); + + // Act + var result = buildServer.GenerateSetParameterMessage(key, value); + + // Assert + result.ShouldContain(s => true, 1); + result.ShouldBeEquivalentTo(new[] { expectedResult }); + } + + [Test] + public void SkipEmptySetParameterMessage() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GenerateSetParameterMessage("Hello", string.Empty); + + // Assert + result.ShouldBeEquivalentTo(new string[0]); + } + + [Test] + public void ShouldWriteIntegration() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + var vars = new TestableVersionVariables("1.0.0"); + + var list = new List(); + + // Assert + environment.GetEnvironmentVariable("GitVersion_Major").ShouldBeNullOrWhiteSpace(); + + // Act + buildServer.WriteIntegration(s => { list.Add(s); }, vars); + + // Assert + var expected = new List + { + "Executing GenerateSetVersionMessage for 'GitHubActions'.", + "", + "Executing GenerateBuildLogOutput for 'GitHubActions'.", + "::set-env name=GitVersion_Major::1.0.0" + }; + + string.Join(Environment.NewLine, list) + .ShouldBe(string.Join(Environment.NewLine, expected)); + } + + [Test] + public void GetEmptyGenerateSetVersionMessage() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + var vars = new TestableVersionVariables("1.0.0"); + + // Act + var message = buildServer.GenerateSetVersionMessage(vars); + + // Assert + message.ShouldBeEmpty(); + } + } +} diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs new file mode 100644 index 0000000000..f04dbc486b --- /dev/null +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -0,0 +1,65 @@ +using GitVersion.Logging; +using GitVersion.OutputVariables; + +namespace GitVersion.BuildServers +{ + public class GitHubActions: BuildServerBase + { + // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables + + public GitHubActions(IEnvironment environment, ILog log) : base(environment, log) + { + } + + public const string EnvironmentVariableName = "GITHUB_ACTION"; + protected override string EnvironmentVariable { get; } = EnvironmentVariableName; + + public override string GenerateSetVersionMessage(VersionVariables variables) + { + // There is no equivalent function in GitHub Actions. + + return string.Empty; + } + + public override string[] GenerateSetParameterMessage(string name, string value) + { + // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env + // Example + // echo "::set-env name=action_state::yellow" + + if (!string.IsNullOrWhiteSpace(value)) + { + var key = $"GitVersion_{name}"; + + return new[] + { + $"::set-env name={key}::{value}" + }; + } + + return new string[0]; + } + + public override string GetCurrentBranch(bool usingDynamicRepos) + { + // GITHUB_REF + // The branch or tag ref that triggered the workflow. + // For example, refs/heads/feature-branch-1. If neither a branch or + // tag is available for the event type, the variable will not exist. + + var value = Environment.GetEnvironmentVariable("GITHUB_REF"); + if (!string.IsNullOrWhiteSpace(value)) + { + const string refsHeads = "refs/heads/"; + + if (value.StartsWith(refsHeads)) + { + value = value.Substring(refsHeads.Length); + return value; + } + } + + return base.GetCurrentBranch(usingDynamicRepos); + } + } +} diff --git a/src/GitVersionCore/GitVersionCoreModule.cs b/src/GitVersionCore/GitVersionCoreModule.cs index 22a67d8031..49a3b60866 100644 --- a/src/GitVersionCore/GitVersionCoreModule.cs +++ b/src/GitVersionCore/GitVersionCoreModule.cs @@ -57,6 +57,7 @@ private static void RegisterBuildServers(IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } private static void RegisterVersionStrategies(IServiceCollection services)