Skip to content

Commit a02af29

Browse files
author
Simon Ejsing
committed
Support for tagging prerelease based on branch ref spec when running in ContinuousDeployment mode. This enables pull request branches to produce unique version numbers when the pull request branch is set to ContinuousDeployment mode.
1 parent c503f89 commit a02af29

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

src/GitVersionCore.Tests/VariableProviderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,27 @@ public void ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommi
152152

153153
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
154154
}
155+
156+
[Test]
157+
public void ProvidesVariablesInContinuousDeploymentModeForTagNumber()
158+
{
159+
var semVer = new SemanticVersion
160+
{
161+
Major = 1,
162+
Minor = 2,
163+
Patch = 3,
164+
PreReleaseTag = "PullRequest",
165+
BuildMetaData = "5.Branch.develop"
166+
};
167+
168+
semVer.BuildMetaData.Branch = "pull/2/merge";
169+
semVer.BuildMetaData.Sha = "commitSha";
170+
semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z");
171+
172+
var config = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, tagNamePattern: @"[/-](?<number>\d+)[-/]");
173+
var vars = VariableProvider.GetVariablesFor(semVer, config, false);
174+
175+
vars.FullSemVer.ShouldBe("1.2.3-PullRequest2.5");
176+
}
177+
155178
}

src/GitVersionCore/Configuration/BranchConfig.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public BranchConfig(BranchConfig branchConfiguration)
1414
Tag = branchConfiguration.Tag;
1515
Increment = branchConfiguration.Increment;
1616
PreventIncrementOfMergedBranchVersion = branchConfiguration.PreventIncrementOfMergedBranchVersion;
17+
TagNamePattern = branchConfiguration.TagNamePattern;
1718
TagNumberPattern = branchConfiguration.TagNumberPattern;
1819
TrackMergeTarget = branchConfiguration.TrackMergeTarget;
1920
CommitMessageIncrementing = branchConfiguration.CommitMessageIncrementing;
@@ -34,6 +35,9 @@ public BranchConfig(BranchConfig branchConfiguration)
3435
[YamlMember(Alias = "prevent-increment-of-merged-branch-version")]
3536
public bool? PreventIncrementOfMergedBranchVersion { get; set; }
3637

38+
[YamlMember(Alias = "tag-name-pattern")]
39+
public string TagNamePattern { get; set; }
40+
3741
[YamlMember(Alias = "tag-number-pattern")]
3842
public string TagNumberPattern { get; set; }
3943

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ public static void ApplyBranchDefaults(Config config,
114114
bool defaultPreventIncrement = false,
115115
VersioningMode? defaultVersioningMode = null, // Looked up from main config
116116
bool defaultTrackMergeTarget = false,
117-
string defaultTagNumberPattern = null)
117+
string defaultTagNumberPattern = null,
118+
string defaultTagNamePattern = null)
118119
{
119120
branchConfig.Tag = branchConfig.Tag ?? defaultTag;
121+
branchConfig.TagNamePattern = branchConfig.TagNamePattern ?? defaultTagNamePattern;
120122
branchConfig.TagNumberPattern = branchConfig.TagNumberPattern ?? defaultTagNumberPattern;
121123
branchConfig.Increment = branchConfig.Increment ?? defaultIncrementStrategy;
122124
branchConfig.PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion ?? defaultPreventIncrement;

src/GitVersionCore/EffectiveConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public EffectiveConfiguration(
1515
string tag, string nextVersion, IncrementStrategy increment,
1616
string branchPrefixToTrim,
1717
bool preventIncrementForMergedBranchVersion,
18+
string tagNamePattern,
1819
string tagNumberPattern,
1920
string continuousDeploymentFallbackTag,
2021
bool trackMergeTarget,
@@ -37,6 +38,7 @@ IEnumerable<IVersionFilter> versionFilters
3738
Increment = increment;
3839
BranchPrefixToTrim = branchPrefixToTrim;
3940
PreventIncrementForMergedBranchVersion = preventIncrementForMergedBranchVersion;
41+
TagNamePattern = tagNamePattern;
4042
TagNumberPattern = tagNumberPattern;
4143
ContinuousDeploymentFallbackTag = continuousDeploymentFallbackTag;
4244
TrackMergeTarget = trackMergeTarget;
@@ -73,6 +75,8 @@ IEnumerable<IVersionFilter> versionFilters
7375

7476
public bool PreventIncrementForMergedBranchVersion { get; private set; }
7577

78+
public string TagNamePattern { get; private set; }
79+
7680
public string TagNumberPattern { get; private set; }
7781

7882
public string ContinuousDeploymentFallbackTag { get; private set; }

src/GitVersionCore/GitVersionContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void CalculateEffectiveConfiguration()
9696

9797
var versioningMode = currentBranchConfig.Value.VersioningMode.Value;
9898
var tag = currentBranchConfig.Value.Tag;
99+
var tagNamePattern = currentBranchConfig.Value.TagNamePattern;
99100
var tagNumberPattern = currentBranchConfig.Value.TagNumberPattern;
100101
var incrementStrategy = currentBranchConfig.Value.Increment.Value;
101102
var preventIncrementForMergedBranchVersion = currentBranchConfig.Value.PreventIncrementOfMergedBranchVersion.Value;
@@ -116,6 +117,7 @@ void CalculateEffectiveConfiguration()
116117
assemblyVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix,
117118
tag, nextVersion, incrementStrategy, currentBranchConfig.Key,
118119
preventIncrementForMergedBranchVersion,
120+
tagNamePattern,
119121
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
120122
trackMergeTarget,
121123
majorMessage, minorMessage, patchMessage,

src/GitVersionCore/OutputVariables/VariableProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.ComponentModel;
5+
using System.Text.RegularExpressions;
56

67
public static class VariableProvider
78
{
@@ -16,6 +17,17 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion,
1617
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
1718
}
1819

20+
// Evaluate tag name pattern and append to prerelease tag
21+
if (!string.IsNullOrEmpty(config.TagNamePattern))
22+
{
23+
var match = Regex.Match(semanticVersion.BuildMetaData.Branch, config.TagNamePattern);
24+
var numberGroup = match.Groups["number"];
25+
if (numberGroup.Success)
26+
{
27+
semanticVersion.PreReleaseTag.Name += numberGroup.Value;
28+
}
29+
}
30+
1931
// For continuous deployment the commits since tag gets promoted to the pre-release number
2032
semanticVersion.PreReleaseTag.Number = semanticVersion.BuildMetaData.CommitsSinceTag;
2133
semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag ?? 0;

0 commit comments

Comments
 (0)