Skip to content

Commit 3edc110

Browse files
authored
Merge pull request #906 from simonejsing/simon/uniqueprnuget
Fix tag-number-pattern and useBranchName configuration options for ContinuousDeployment mode
2 parents c503f89 + 959e84a commit 3edc110

File tree

4 files changed

+100
-22
lines changed

4 files changed

+100
-22
lines changed

docs/configuration.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Configuration
1+
# Configuration
22
GitVersion 3.0 is mainly powered by configuration and no longer has branching strategies hard coded.
33

44
## Configuration tool
@@ -79,8 +79,20 @@ The options in here are:
7979
- **`prevent-increment-of-merged-branch-version:`** When `release-2.0.0` is merged into master, we want master to build `2.0.0`.
8080
If `release-2.0.0` is merged into develop we want it to build `2.1.0`, this option prevents incrementing after a versioned branch is merged
8181

82-
- **`tag-number-pattern:`** Pull requests require us to pull the pre-release number out of the branch name so `refs/pulls/534/merge` builds as `PullRequest.534`.
83-
This is a regex with a named capture group called `number`
82+
- **`tag-number-pattern:`** Pull requests require us to extract the pre-release number out of the branch name so `refs/pulls/534/merge` builds as `PullRequest.534`.
83+
This is a regex with a named capture group called `number`
84+
If the branch mode is set to ContinuousDeployment, then the extracted `number` is appended to the name of the pre-release tag and the number portion is the number of commits since the last tag.
85+
This enables consecutive commits to the pull request branch to generate unique full semantic version numbers when the branch is configured to use ContinuousDeployment mode.
86+
Example usage:
87+
```yaml
88+
branches:
89+
(pull|pull\-requests|pr)[/-]:
90+
mode: ContinuousDeployment
91+
tag: PullRequest
92+
increment: Inherit
93+
track-merge-target: true
94+
tag-name-pattern: '[/-](?<number>\d+)[-/]'
95+
```
8496

8597
- **`track-merge-target:`** Strategy which will look for tagged merge commits directly off the current branch. For example `develop` → `release/1.0.0` → merge into `master` and tag `1.0.0`. The tag is *not* on develop, but develop should be version `1.0.0` now.
8698

src/GitVersionCore.Tests/VariableProviderTests.cs

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

153153
JsonOutputFormatter.ToJson(vars).ShouldMatchApproved(c => c.SubFolder("Approved"));
154154
}
155+
156+
[Test]
157+
public void ProvidesVariablesInContinuousDeploymentModeWithTagNamePattern()
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, tagNumberPattern: @"[/-](?<number>\d+)[-/]");
173+
var vars = VariableProvider.GetVariablesFor(semVer, config, false);
174+
175+
vars.FullSemVer.ShouldBe("1.2.3-PullRequest0002.5");
176+
}
177+
178+
[Test]
179+
public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToUseBranchName()
180+
{
181+
var semVer = new SemanticVersion
182+
{
183+
Major = 1,
184+
Minor = 2,
185+
Patch = 3,
186+
BuildMetaData = "5.Branch.develop"
187+
};
188+
189+
semVer.BuildMetaData.Branch = "feature";
190+
semVer.BuildMetaData.Sha = "commitSha";
191+
semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z");
192+
193+
var config = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, tag: "useBranchName");
194+
var vars = VariableProvider.GetVariablesFor(semVer, config, false);
195+
196+
vars.FullSemVer.ShouldBe("1.2.3-feature.5");
197+
}
155198
}

src/GitVersionCore/OutputVariables/VariableProvider.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{
33
using System;
44
using System.ComponentModel;
5+
using System.Text.RegularExpressions;
6+
using GitVersion.VersionCalculation;
57

68
public static class VariableProvider
79
{
@@ -13,7 +15,22 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion,
1315
// Continuous Deployment always requires a pre-release tag unless the commit is tagged
1416
if (!semanticVersion.PreReleaseTag.HasTag())
1517
{
16-
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
18+
semanticVersion.PreReleaseTag.Name = NextVersionCalculator.GetBranchSpecificTag(config, semanticVersion.BuildMetaData.Branch, null);
19+
if (string.IsNullOrEmpty(semanticVersion.PreReleaseTag.Name))
20+
{
21+
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
22+
}
23+
}
24+
25+
// Evaluate tag number pattern and append to prerelease tag, preserving build metadata
26+
if (!string.IsNullOrEmpty(config.TagNumberPattern))
27+
{
28+
var match = Regex.Match(semanticVersion.BuildMetaData.Branch, config.TagNumberPattern);
29+
var numberGroup = match.Groups["number"];
30+
if (numberGroup.Success)
31+
{
32+
semanticVersion.PreReleaseTag.Name += numberGroup.Value.PadLeft(config.BuildMetaDataPadding, '0');
33+
}
1734
}
1835

1936
// For continuous deployment the commits since tag gets promoted to the pre-release number

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,7 @@ public SemanticVersion FindVersion(GitVersionContext context)
6969

7070
void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVersion, string branchNameOverride)
7171
{
72-
var tagToUse = context.Configuration.Tag;
73-
if (tagToUse == "useBranchName")
74-
{
75-
tagToUse = "{BranchName}";
76-
}
77-
if (tagToUse.Contains("{BranchName}"))
78-
{
79-
Logger.WriteInfo("Using branch name to calculate version tag");
80-
81-
var branchName = branchNameOverride ?? context.CurrentBranch.FriendlyName;
82-
if (!string.IsNullOrWhiteSpace(context.Configuration.BranchPrefixToTrim))
83-
{
84-
branchName = branchName.RegexReplace(context.Configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
85-
}
86-
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
87-
88-
tagToUse = tagToUse.Replace("{BranchName}", branchName);
89-
}
72+
var tagToUse = GetBranchSpecificTag(context.Configuration, context.CurrentBranch.FriendlyName, branchNameOverride);
9073

9174
int? number = null;
9275
if (!string.IsNullOrEmpty(context.Configuration.TagNumberPattern))
@@ -119,6 +102,29 @@ void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVers
119102
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number);
120103
}
121104

105+
public static string GetBranchSpecificTag(EffectiveConfiguration configuration, string branchFriendlyName, string branchNameOverride)
106+
{
107+
var tagToUse = configuration.Tag;
108+
if (tagToUse == "useBranchName")
109+
{
110+
tagToUse = "{BranchName}";
111+
}
112+
if (tagToUse.Contains("{BranchName}"))
113+
{
114+
Logger.WriteInfo("Using branch name to calculate version tag");
115+
116+
var branchName = branchNameOverride ?? branchFriendlyName;
117+
if (!string.IsNullOrWhiteSpace(configuration.BranchPrefixToTrim))
118+
{
119+
branchName = branchName.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
120+
}
121+
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
122+
123+
tagToUse = tagToUse.Replace("{BranchName}", branchName);
124+
}
125+
return tagToUse;
126+
}
127+
122128
static bool MajorMinorPatchEqual(SemanticVersion lastTag, SemanticVersion baseVersion)
123129
{
124130
return lastTag.Major == baseVersion.Major &&

0 commit comments

Comments
 (0)