Skip to content

Commit 66ae02b

Browse files
authored
Merge pull request #3687 from CasperWSchmidt/main
Implements regular expression for determining version label
2 parents 6ffb6e1 + 9ede034 commit 66ae02b

File tree

16 files changed

+77
-52
lines changed

16 files changed

+77
-52
lines changed

BREAKING_CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* A new configuration property with name `version-in-branch-pattern` has been introduced. This setting only applies on branches where the option `is-release-branch` is set to `true`. Please notice that the branch name needs to be defined after the version number by default (instead of `support/lts-2.0.0` please name the branch like `support/2.0.0-lts`).
2929
* The `is-release-branch` property of the `hotfix` branch setting has been changed from `false` to `true`. If present the hotfix number will be considered now by default.
3030
* In the GitHub and the Git Flow workflows the `label` property is by default set to an empty string on the `main` branch. This yields to a pre-release version on `main` with an empty tag. Instead of for instance `1.0.1+46` GitVersion generates the full semantic version `1.0.1-46` instead. This behavior can be changed to generate only stable versions (no pre-release version) with setting the label to `null` (Please keep in mind that the `label` property on root needs to be set to `null` as well, otherwise the fallback applies). This change is caused by issue #2347.
31+
* The `useBranchName` magic string has been removed. Instead use `{BranchName}` for `label`.
32+
* The `BranchPrefixToTrim` configuration property has been removed. `RegularExpression` is now used to capture named groups instead.
33+
* Default `RegularExpression` for feature branches is changed from `^features?[/-]` to `^features?[/-](?<BranchName>.+)` to support using `{BranchName}` out-of-the-box
34+
* Default `RegularExpression` for unknown branches is changed from `.*` to `(?<BranchName>.*)` to support using `{BranchName}` out-of-the-box
3135
3236
## v5.0.0
3337

docs/input/docs/reference/configuration.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,9 @@ values, but here they are if you need to:
484484
### regex
485485

486486
This is the regex which is used to match the current branch to the correct
487-
branch configuration.
487+
branch configuration.
488+
489+
[Named groups](https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#named-matched-subexpressions) can be used to dynamically label pre-releases based on the branch name, or parts of it. See [Label](#label) for more details and examples.
488490

489491
### source-branches
490492

@@ -572,11 +574,13 @@ Same as for the [global configuration, explained above](#mode).
572574

573575
### label
574576

575-
The pre release label to use for this branch. Use the value `useBranchName` to use
576-
the branch name instead. For example `feature/foo` would become a pre-release
577-
label of `foo` with this value. Use the value `{BranchName}` as a placeholder to
578-
insert the branch name. For example `feature/foo` would become a pre-release label
579-
of `alpha.foo` with the value of `alpha.{BranchName}`.
577+
The pre-release label to use for this branch. Use the value `{BranchName}` as a placeholder to
578+
insert the value of the named group `BranchName` from the [regular expression](#regex).
579+
580+
For example: branch `feature/foo` would become a pre-release label
581+
of `alpha.foo` with `label: 'alpha.{BranchName}'` and `regex: '^features?[/-](?<BranchName>.+)'`.
582+
583+
Another example: branch `features/sc-12345/some-description` would become a pre-release label of `sc-12345` with `label: '{StoryNo}'` and `regex: '^features?[/-](?<StoryNo>sc-\d+)[-/].+'`.
580584

581585
**Note:** To clear a default use an empty string: `label: ''`
582586

schemas/6.0/GitVersion.configuration.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
}
266266
},
267267
"string": {
268-
"description": "The label to use for this branch. Can be \u0027useBranchName\u0027 to extract the label from the branch name. Use the value {BranchName} as a placeholder to insert the branch name.",
268+
"description": "The label to use for this branch. Use the value {BranchName} or similar as a placeholder to insert a named capture group from RegularExpression (fx. the branch name).",
269269
"type": "string"
270270
},
271271
"string1": {
@@ -557,4 +557,4 @@
557557
}
558558
}
559559
}
560-
}
560+
}

src/GitVersion.Core.Tests/Configuration/ConfigurationExtensionsTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,20 @@ public void EnsureIsReleaseBranchWithReferenceNameWorksAsExpected(string branchN
5757
actual.IsTag.ShouldBe(expectedIsTag);
5858
isReleaseBranch.ShouldBe(expectedIsReleaseBranch);
5959
}
60+
61+
[TestCase("feature/sc-1000/Description", "^features?[/-](?<BranchName>.+)", "{BranchName}", "sc-1000-Description")]
62+
[TestCase("feature/sc-1000/Description", "^features?[/-](?<StoryNo>sc-\\d+)[-/].+", "{StoryNo}", "sc-1000")]
63+
public void EnsureGetBranchSpecificLabelWorksAsExpected(string branchName, string regularExpression, string label, string expectedLabel)
64+
{
65+
var configuration = GitFlowConfigurationBuilder.New
66+
.WithoutBranches()
67+
.WithBranch(branchName, builder => builder
68+
.WithLabel(label)
69+
.WithRegularExpression(regularExpression))
70+
.Build();
71+
72+
var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName(branchName));
73+
var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName(branchName), null);
74+
actual.ShouldBe(expectedLabel);
75+
}
6076
}

src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ branches:
5959
mode: ContinuousDelivery
6060
label: '{BranchName}'
6161
increment: Inherit
62-
regex: ^features?[/-]
62+
regex: ^features?[/-](?<BranchName>.+)
6363
source-branches:
6464
- develop
6565
- main
@@ -114,7 +114,7 @@ branches:
114114
mode: ContinuousDelivery
115115
label: '{BranchName}'
116116
increment: Inherit
117-
regex: .*
117+
regex: (?<BranchName>.+)
118118
source-branches:
119119
- main
120120
- develop

src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public TestEffectiveConfiguration(
1616
string tagPrefix = ConfigurationConstants.DefaultTagPrefix,
1717
string label = "ci",
1818
string? nextVersion = null,
19-
string branchPrefixToTrim = "",
19+
string regularExpression = "",
2020
bool preventIncrementOfMergedBranchVersion = false,
2121
string? labelNumberPattern = null,
2222
bool trackMergeTarget = false,
@@ -41,7 +41,7 @@ public TestEffectiveConfiguration(
4141
label,
4242
nextVersion,
4343
IncrementStrategy.Patch,
44-
branchPrefixToTrim,
44+
regularExpression,
4545
preventIncrementOfMergedBranchVersion,
4646
labelNumberPattern,
4747
trackMergeTarget,

src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,15 @@ public void CanUseBranchNameOffAReleaseBranch()
183183
fixture.AssertFullSemver("0.3.0-PROJ-1.1+3", configuration);
184184
}
185185

186-
[TestCase("alpha", "JIRA-123", "alpha")]
187-
[TestCase("useBranchName", "JIRA-123", "JIRA-123")]
188-
[TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")]
189-
public void ShouldUseConfiguredTag(string tag, string featureName, string preReleaseTagName)
186+
[TestCase("alpha", "JIRA-123", "^features?[/-](?<BranchName>.+)", "alpha")]
187+
[TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "^features?[/-](?<BranchName>.+)", "alpha.JIRA-123")]
188+
[TestCase("{BranchName}-of-task-number-{TaskNumber}", "4711_this-is-a-feature", "^features?[/-](?<TaskNumber>\\d+)_(?<BranchName>.+)", "this-is-a-feature-of-task-number-4711")]
189+
public void ShouldUseConfiguredLabel(string label, string featureName, string regularExpression, string preReleaseLabelName)
190190
{
191191
var configuration = GitFlowConfigurationBuilder.New
192-
.WithBranch("feature", builder => builder.WithLabel(tag))
192+
.WithBranch("feature", builder => builder
193+
.WithLabel(label)
194+
.WithRegularExpression(regularExpression))
193195
.Build();
194196

195197
using var fixture = new EmptyRepositoryFixture();
@@ -198,7 +200,7 @@ public void ShouldUseConfiguredTag(string tag, string featureName, string preRel
198200
fixture.BranchTo(featureBranchName);
199201
fixture.Repository.MakeCommits(5);
200202

201-
var expectedFullSemVer = $"1.0.1-{preReleaseTagName}.1+5";
203+
var expectedFullSemVer = $"1.0.1-{preReleaseLabelName}.1+5";
202204
fixture.AssertFullSemver(expectedFullSemVer, configuration);
203205
}
204206

src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ShouldOnlyConsiderTagsMatchingOfCurrentBranch()
3434
public void CanTakeVersionFromReleaseBranch()
3535
{
3636
var configuration = GitFlowConfigurationBuilder.New
37-
.WithBranch("release", _ => _.WithLabel("{BranchName}"))
37+
.WithBranch("release", _ => _.WithLabel("{BranchName}").WithRegularExpression("(?<BranchName>.*)"))
3838
.Build();
3939

4040
using var fixture = new EmptyRepositoryFixture();
@@ -52,7 +52,7 @@ public void CanTakeVersionFromReleaseBranch()
5252
public void CanTakeVersionFromHotfixBranch()
5353
{
5454
var configuration = GitFlowConfigurationBuilder.New
55-
.WithBranch("hotfix", _ => _.WithLabel("{BranchName}"))
55+
.WithBranch("hotfix", _ => _.WithLabel("{BranchName}").WithRegularExpression("(?<BranchName>.*)"))
5656
.Build();
5757

5858
using var fixture = new EmptyRepositoryFixture();
@@ -83,7 +83,7 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged()
8383
{
8484
// * 1c08923 54 minutes ago (HEAD -> develop)
8585
// | * 03dd6d5 56 minutes ago (tag: 1.0.1-feature.1, feature)
86-
// |/
86+
// |/
8787
// * e2ff13b 58 minutes ago (tag: 1.0.0-unstable.0, main)
8888

8989
var configuration = GitFlowConfigurationBuilder.New
@@ -106,14 +106,13 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged()
106106
}
107107

108108
[TestCase("alpha", "JIRA-123", "alpha")]
109-
[TestCase("useBranchName", "JIRA-123", "JIRA-123")]
110109
[TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")]
111110
public void LabelIsBranchNameForBranchesWithoutPrefixedBranchName(string label, string branchName, string preReleaseTagName)
112111
{
113112
var configuration = GitFlowConfigurationBuilder.New
114113
.WithBranch("other", builder => builder
115114
.WithIncrement(IncrementStrategy.Patch)
116-
.WithRegularExpression(".*")
115+
.WithRegularExpression("(?<BranchName>.*)")
117116
.WithSourceBranches()
118117
.WithLabel(label))
119118
.Build();

src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void ProvidesVariablesInContinuousDeploymentModeWithTagNamePattern()
182182
}
183183

184184
[Test]
185-
public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToUseBranchName()
185+
public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToBranchName()
186186
{
187187
var semanticVersion = new SemanticVersion
188188
{

src/GitVersion.Core/Configuration/BranchConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal record BranchConfiguration : IBranchConfiguration
1111
public VersioningMode? VersioningMode { get; internal set; }
1212

1313
[JsonPropertyName("label")]
14-
[JsonPropertyDescription("The label to use for this branch. Can be 'useBranchName' to extract the label from the branch name. Use the value {BranchName} as a placeholder to insert the branch name.")]
14+
[JsonPropertyDescription("The label to use for this branch. Use the value {BranchName} or similar as a placeholder to insert a named capture group from RegularExpression (fx. the branch name).")]
1515
public string? Label { get; internal set; }
1616

1717
[JsonPropertyName("increment")]

0 commit comments

Comments
 (0)