diff --git a/.gitignore b/.gitignore index 762c2b2..765df35 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ testResults.xml coverage.xml coverage.cobertura.xml *.trx +*.DotSettings.user diff --git a/src/SimpleVersion.Abstractions/Environment/BaseVersionEnvironment.cs b/src/SimpleVersion.Abstractions/Environment/BaseVersionEnvironment.cs index 35656f4..c0d66a2 100644 --- a/src/SimpleVersion.Abstractions/Environment/BaseVersionEnvironment.cs +++ b/src/SimpleVersion.Abstractions/Environment/BaseVersionEnvironment.cs @@ -1,7 +1,7 @@ // Licensed under the MIT license. See https://kieranties.mit-license.org/ for full license information. using System; -using SimpleVersion.Pipeline; +using System.Text.RegularExpressions; namespace SimpleVersion.Environment { @@ -10,20 +10,30 @@ namespace SimpleVersion.Environment /// public abstract class BaseVersionEnvironment : IVersionEnvironment { + /// + /// Use to normalize branch names from a canonical name. + /// + protected static readonly Regex CanonicalBranchTrimmer = new Regex(@"^refs\/(heads\/)?", RegexOptions.IgnoreCase); + /// /// Initializes a new instance of the class. /// /// The accessor for environment variables. - public BaseVersionEnvironment(IEnvironmentVariableAccessor accessor) + protected BaseVersionEnvironment(IEnvironmentVariableAccessor accessor) { Variables = accessor ?? throw new ArgumentNullException(nameof(accessor)); + CanonicalBranchName = Variables.GetVariable("simpleversion.sourcebranch"); + if (CanonicalBranchName != null) + { + BranchName = CanonicalBranchTrimmer.Replace(CanonicalBranchName, string.Empty); + } } /// - public abstract string? CanonicalBranchName { get; } + public string? CanonicalBranchName { get; protected set; } /// - public abstract string? BranchName { get; } + public string? BranchName { get; protected set; } /// public abstract bool IsValid { get; } diff --git a/src/SimpleVersion.Core/Environment/AzureDevopsEnvironment.cs b/src/SimpleVersion.Core/Environment/AzureDevopsEnvironment.cs index 0679a66..b0272bf 100644 --- a/src/SimpleVersion.Core/Environment/AzureDevopsEnvironment.cs +++ b/src/SimpleVersion.Core/Environment/AzureDevopsEnvironment.cs @@ -10,8 +10,6 @@ namespace SimpleVersion.Environment /// public class AzureDevopsEnvironment : BaseVersionEnvironment { - private static readonly Regex _trim = new Regex(@"^refs\/(heads\/)?", RegexOptions.IgnoreCase); - /// /// Initializes a new instance of the class. /// @@ -19,19 +17,16 @@ public class AzureDevopsEnvironment : BaseVersionEnvironment public AzureDevopsEnvironment(IEnvironmentVariableAccessor accessor) : base(accessor) { - CanonicalBranchName = Variables.GetVariable("BUILD_SOURCEBRANCH"); - if (CanonicalBranchName != null) + if (string.IsNullOrWhiteSpace(CanonicalBranchName)) { - BranchName = _trim.Replace(CanonicalBranchName, string.Empty); + CanonicalBranchName = Variables.GetVariable("BUILD_SOURCEBRANCH"); + if (CanonicalBranchName != null) + { + BranchName = CanonicalBranchTrimmer.Replace(CanonicalBranchName, string.Empty); + } } } - /// - public override string? CanonicalBranchName { get; } - - /// - public override string? BranchName { get; } - /// public override bool IsValid => Variables.GetVariable("TF_BUILD").ToBool(); } diff --git a/src/SimpleVersion.Core/Environment/DefaultVersionEnvironment.cs b/src/SimpleVersion.Core/Environment/DefaultVersionEnvironment.cs index 405073a..4146791 100644 --- a/src/SimpleVersion.Core/Environment/DefaultVersionEnvironment.cs +++ b/src/SimpleVersion.Core/Environment/DefaultVersionEnvironment.cs @@ -15,12 +15,6 @@ public DefaultVersionEnvironment(IEnvironmentVariableAccessor accessor) : base(a { } - /// - public override string? CanonicalBranchName => null; - - /// - public override string? BranchName => null; - /// public override bool IsValid => true; } diff --git a/src/SimpleVersion.Docs/site/articles/environments.md b/src/SimpleVersion.Docs/site/articles/environments.md new file mode 100644 index 0000000..c4c82ba --- /dev/null +++ b/src/SimpleVersion.Docs/site/articles/environments.md @@ -0,0 +1,26 @@ +Environments +============ + +SimpleVersion attempts to identify git information from the local environment. +When running on Continuous Integration (CI) systems you may need to prepare the +environment to cater for the CI systems specific approach to builds. + +Azure Devops +------------ + +Azure Devops is identified as the build environment when the environment variable `TF_BUILD` is set. + +When identified, the canonical and friendly branch names are derived from the `BUILD_SOURCEBRANCH` environment variable. + +Fallback Environment +-------------------- + +For all environments, the build branch can be overridden by setting `simpleversion.sourcebranch` to the _full canonical branch name_. When this variable is set it will override the resolution from any CI system as well as the local repository branch name. + +### TeamCity + +When building with TeamCity you can lift the branch name of the build to an environment variable by doing the following: + +1. Open your build configuration, and select Parameters +1. Add a new paramter called `env.simpleversion.sourcebranch` +1. Set the value to `%teamcity.build.vcs.branch.{vcsid}%` where `{vcsid}` is the VCS Root of your repo diff --git a/src/SimpleVersion.Docs/site/articles/toc.yml b/src/SimpleVersion.Docs/site/articles/toc.yml index f89124f..af722e1 100644 --- a/src/SimpleVersion.Docs/site/articles/toc.yml +++ b/src/SimpleVersion.Docs/site/articles/toc.yml @@ -2,5 +2,7 @@ href: intro.md - name: Configuration href: configuration.md +- name: Environments + href: environments.md - name: Results href: results.md diff --git a/test/SimpleVersion.Abstractions.Tests/Environment/BaseVersionEnvironmentFixture.cs b/test/SimpleVersion.Abstractions.Tests/Environment/BaseVersionEnvironmentFixture.cs index 7a7b6a9..11b5c63 100644 --- a/test/SimpleVersion.Abstractions.Tests/Environment/BaseVersionEnvironmentFixture.cs +++ b/test/SimpleVersion.Abstractions.Tests/Environment/BaseVersionEnvironmentFixture.cs @@ -34,16 +34,28 @@ public void Ctor_WithAccessor_SetsProperty() instance.VariablesWrapper.Should().BeSameAs(accessor); } + [Fact] + public void BranchName_OverrideSet_ReturnsOverride() + { + // Arrange + var expectedName = "OVERRIDE"; + var accessor = Substitute.For(); + accessor.GetVariable("simpleversion.sourcebranch").Returns(expectedName); + var sut = new StubVersionEnvironment(accessor); + + // Act + var result = sut.BranchName; + + // Assert + result.Should().Be(expectedName); + } + private class StubVersionEnvironment : BaseVersionEnvironment { public StubVersionEnvironment(IEnvironmentVariableAccessor accessor) : base(accessor) { } - public override string CanonicalBranchName => throw new NotImplementedException("Should not be tested"); - - public override string BranchName => throw new NotImplementedException("Should not be tested"); - public override bool IsValid => throw new NotImplementedException("Should not be tested"); public IEnvironmentVariableAccessor VariablesWrapper => Variables; diff --git a/test/SimpleVersion.Core.Tests/Environment/AzureDevopsEnvironmentFixture.cs b/test/SimpleVersion.Core.Tests/Environment/AzureDevopsEnvironmentFixture.cs index 8028245..d48ab77 100644 --- a/test/SimpleVersion.Core.Tests/Environment/AzureDevopsEnvironmentFixture.cs +++ b/test/SimpleVersion.Core.Tests/Environment/AzureDevopsEnvironmentFixture.cs @@ -102,5 +102,25 @@ public void BranchName_ReadsCorrectVariable(string canonical, string branch) // Assert result.Should().Be(branch); } + + [Theory] + [InlineData("refs/heads/master")] + [InlineData("refs/heads/release/1.0/master")] + [InlineData("refs/pull/10/merge")] + public void BranchName_OverrideSet_ReturnsOverride(string canonical) + { + // Arrange + var expectedName = "override"; + _env.GetVariable("simpleversion.sourcebranch").Returns(expectedName); + _env.GetVariable("BUILD_SOURCEBRANCH").Returns(canonical); + + var sut = new AzureDevopsEnvironment(_env); + + // Act + var result = sut.BranchName; + + // Assert + result.Should().Be(expectedName); + } } } diff --git a/test/SimpleVersion.Core.Tests/Environment/DefaultVersionEnvironmentFixture.cs b/test/SimpleVersion.Core.Tests/Environment/DefaultVersionEnvironmentFixture.cs index f8f8f8d..fb8dc06 100644 --- a/test/SimpleVersion.Core.Tests/Environment/DefaultVersionEnvironmentFixture.cs +++ b/test/SimpleVersion.Core.Tests/Environment/DefaultVersionEnvironmentFixture.cs @@ -15,6 +15,7 @@ public class DefaultVersionEnvironmentFixture public DefaultVersionEnvironmentFixture() { _env = Substitute.For(); + _env.GetVariable("simpleversion.sourcebranch").Returns((string)null); } [Fact] @@ -66,5 +67,20 @@ public void BranchName_ReturnsNull() // Assert result.Should().BeNull(); } + + [Fact] + public void BranchName_OverrideSet_ReturnsOverride() + { + // Arrange + var expectedName = "OVERRIDE"; + _env.GetVariable("simpleversion.sourcebranch").Returns(expectedName); + var sut = new DefaultVersionEnvironment(_env); + + // Act + var result = sut.BranchName; + + // Assert + result.Should().Be(expectedName); + } } }