Skip to content

Commit

Permalink
Sync release/0.1 with master (#59)
Browse files Browse the repository at this point in the history
* Initial token rule implementation (#52)

* Enable {branchname} and {shortsha}

Token replacement to contribute towards #13

* Add shortbranchname token (#53)

* #54 - Better error handling for malformed JSON (#56)

* Fix #55 - Implement branchnamesuffix (#57)

* Feature/overrides (#58)

* Base model for overrides

* Enable branch override configuration

Contributes to #29

* Sample for branch overrides

* Use new versioning overrides - bump label
  • Loading branch information
Kieranties authored Mar 12, 2019
1 parent af1d471 commit 070978c
Show file tree
Hide file tree
Showing 31 changed files with 1,118 additions and 106 deletions.
11 changes: 11 additions & 0 deletions .simpleversion.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
"^refs/heads/master$",
"^refs/heads/preview/.+$",
"^refs/heads/release/.+$"
],
"overrides": [
{
"match": "^refs/heads/feature/.+$",
"metadata": [ "{shortbranchname}" ]
},
{
"match": "^refs/heads/release/.+$",
"label": [],
"metadata": [ "*" ]
}
]
}
}
22 changes: 16 additions & 6 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ All of the following are accepted values:
"Version" : "1.2.3.4"
```

> You may also specify '*' in place of a number to insert the generated height.
Label
-----

Expand All @@ -33,7 +31,6 @@ The `Label` property specifies an array of labels to be included in the version.
"Label" : ["alpha1"]
"Label" : ["alpha1", "test"]
```
> You may also specify '*' in place of a value to insert the generated height.

MetaData
--------
Expand All @@ -49,8 +46,6 @@ in the final version.
"MetaData" : ["demo", "sprint1"]
```

> You may also specify '*' in place of a value to insert the generated height.
OffSet
------

Expand Down Expand Up @@ -98,4 +93,19 @@ Semver2 verison of `0.1.0-alpha2.5` when there are five commits.

All other branches will append the short sha, generating a Semver2 version of
`0.1.0-alpha2.5.c903782` when there are five commits and the sha begins with
_903782_
_903782_

Replacement Tokens
------------------

SimpleVersion allows specific _tokens_ to be used in some properties to allow
substitution of values during invocation. The following tokens may be used:


| Name | Token | Where | Description |
| ------------------ | -------------------- | ------------------------------ | -------------------------------------------------------------------------- |
| Height | `*` | `Version`, `Label`, `Metadata` | Inserts the calculated height |
| Branch Name | `{branchname}` | `Label`, `Metadata` | Inserts the canonical branch name, stripped of non-alphanumeric characters |
| Short Branch Name | `{shortbranchname}` | `Label`, `Metadata` | Inserts the friendly branch name, stripped of non-alphanumeric characters |
| Branch Name Suffix | `{branchnamesuffix}` | `Label`, `Metadata` | Inserts the last segment of the canonical name of a branch |
| Short Sha | `{shortsha}` | `Label`, `Metadata` | Inserts the first seven characters of the commit sha, prefixed with `c` |
1 change: 1 addition & 0 deletions docs/Results.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following is an example from invoking the command line tool:
"Height": 18,
"HeightPadded": "0018",
"Sha": "ebc8f22ae83bfa3c1e36d6bf70c2a383ae30c9dd",
"CanonicalBranchName": "refs/heads/preview/test",
"BranchName": "preview/test",
"Formats": {
"Semver1": "0.1.0-alpha2-0018",
Expand Down
28 changes: 28 additions & 0 deletions docs/samples/.simpleversion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// An example branch strategy
{
// Specify the base version number
"version": "1.0.0",
// Specify the base label - Use the current branchname, height appended by default
"label": [ "{branchname}" ],
"branches": {
// The following branches will build packages to be released
"release": [
"master$",
"release/.+"
],
"overrides" : [
{
// The master branch will always label as rc1 with sha metadata
"match": "master$",
"label": ["rc1"],
"metadata" : ["{shortsha}"]
},
{
// The release branch will have no label and put height in the metadata
"match": "release/.*",
"label": [],
"metadata" : ["*"]
}
]
}
}
62 changes: 60 additions & 2 deletions integration/SimpleVersion.Command.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Describe 'SimpleVersion.Command'{
}
}

It 'Returns base values for initial commit'{
It 'Returns base values for initial commit' {
Copy-Item $PSScriptRoot\assets\.simpleversion.json -Destination $pwd
git add *
git commit -m 'Initial commit'
Expand Down Expand Up @@ -130,7 +130,7 @@ Describe 'SimpleVersion.Command'{
git commit -m 'empty' --allow-empty
$json = Get-Content $pwd\.simpleversion.json -Raw | ConvertFrom-Json
$json.Version = "1.0.0"
Set-Content $pwd\.simpleversion.json (ConvertTo-Json $json)
Set-Content $pwd\.simpleversion.json (ConvertTo-Json $json -Depth 100)
git add *
git commit -m 'Updated version'
git commit -m 'empty' --allow-empty
Expand All @@ -154,4 +154,62 @@ Describe 'SimpleVersion.Command'{
}
}
}

Context 'Branch Overrides' {
Context 'Override Matches' {

BeforeAll {
$dir = New-Item "${TestDrive}\$(Get-Random)" -ItemType Directory
Push-Location $dir
git init
git config user.email "[email protected]"
git config user.name "Simple Version"

Copy-Item $PSScriptRoot\assets\.simpleversion.json -Destination $pwd
git add *
git commit -m 'Initial commit'

$sha = git rev-parse HEAD
$expectedSha = "c$($sha.Substring(0, 7))"
}

AfterAll {
Pop-Location
Remove-Item $dir -Recurse -Force
}

It 'Returns override label and meta if provided' {

git checkout -b test/feature
$result = Invoke
Validate $result -AsSuccess {
$json.BranchName | Should -Be 'test/feature'
$json.Formats.Semver1 | Should -Be '0.1.0-testfeature-0001'
$json.Formats.Semver2 | Should -Be '0.1.0-testfeature.1+internal'
}
}

It 'Returns override label only if provided' {

git checkout -b test/hotfix
$result = Invoke
Validate $result -AsSuccess {
$json.BranchName | Should -Be 'test/hotfix'
$json.Formats.Semver1 | Should -Be "0.1.0-$expectedSha-0001"
$json.Formats.Semver2 | Should -Be "0.1.0-$expectedSha.1"
}
}

It 'Returns override label only if provided' {

git checkout -b test/release
$result = Invoke
Validate $result -AsSuccess {
$json.BranchName | Should -Be 'test/release'
$json.Formats.Semver1 | Should -Be "0.1.0-alpha1-0001"
$json.Formats.Semver2 | Should -Be "0.1.0-alpha1.1+1.$expectedSha"
}
}
}
}
}
18 changes: 17 additions & 1 deletion integration/assets/.simpleversion.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
"release": [
"^refs/heads/master$",
"^refs/heads/preview/.+$",
"^refs/heads/release/.+$"
"^refs/heads/release/.+$",
"^refs/heads/test/.+$"
],
"overrides" : [
{
"match": "test/feature",
"label": ["{branchname}"],
"metadata" : ["internal"]
},
{
"match": "test/release",
"metadata": ["*", "{shortsha}"]
},
{
"match": "test/hotfix",
"label": ["{shortsha}"]
}
]
}
}
13 changes: 13 additions & 0 deletions src/SimpleVersion.Abstractions/Model/BranchConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace SimpleVersion.Model
{
public class BranchConfiguration
{
public string Match { get; set; } = string.Empty;

public List<string> Label { get; set; } = null;

public List<string> MetaData { get; set; } = null;
}
}
2 changes: 2 additions & 0 deletions src/SimpleVersion.Abstractions/Model/BranchInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ namespace SimpleVersion.Model
public class BranchInfo
{
public List<string> Release { get; } = new List<string>();

public List<BranchConfiguration> Overrides { get; } = new List<BranchConfiguration>();
}
}
13 changes: 13 additions & 0 deletions src/SimpleVersion.Abstractions/Model/LabelConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleVersion.Model
{
public class LabelConfiguration
{
public List<string> Label { get; } = new List<string>();
}
}
14 changes: 14 additions & 0 deletions src/SimpleVersion.Abstractions/Rules/IRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SimpleVersion.Pipeline;
using System.Collections.Generic;

namespace SimpleVersion.Rules
{
public interface IRule<T>
{
string Token { get; }

T Resolve(VersionContext context, T value);

IEnumerable<T> Apply(VersionContext context, IEnumerable<T> value);
}
}
43 changes: 11 additions & 32 deletions src/SimpleVersion.Core/Pipeline/Formatting/Semver1FormatProcess.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using SimpleVersion.Rules;

namespace SimpleVersion.Pipeline.Formatting
{
public class Semver1FormatProcess : ICalculatorProcess
{
public void Apply(VersionContext context)
{
var labelParts = new List<string>(context.Configuration.Label);

if (!context.Configuration.Version.Contains("*"))
{
// if we have a label, ensure height is included
if (labelParts.Count != 0 && !labelParts.Contains("*"))
labelParts.Add("*");
}

// add short sha if required
if (labelParts.Count > 0)
var rules = new IRule<string>[]
{
var addShortSha = true;
foreach (var pattern in context.Configuration.Branches.Release)
{
if (Regex.IsMatch(context.Result.CanonicalBranchName, pattern))
{
addShortSha = false;
break;
}
}

if (addShortSha)
{
var shortSha = context.Result.Sha.Substring(0, 7);
labelParts.Add($"c{shortSha}");
}
}

var label = string.Join("-", labelParts);
label = label.Replace("*", context.Result.HeightPadded);
new HeightRule(true),
ShortShaRule.Instance,
BranchNameRule.Instance,
ShortBranchNameRule.Instance,
BranchNameSuffixRule.Instance
};

var labelParts = context.Configuration.Label.ApplyRules(context, rules);
var label = string.Join("-", labelParts).ResolveRules(context, rules);

var format = context.Result.Version;

Expand Down
47 changes: 13 additions & 34 deletions src/SimpleVersion.Core/Pipeline/Formatting/Semver2FormatProcess.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using SimpleVersion.Rules;

namespace SimpleVersion.Pipeline.Formatting
{
public class Semver2FormatProcess : ICalculatorProcess
{
public void Apply(VersionContext context)
{
var labelParts = new List<string>(context.Configuration.Label);
var metaParts = new List<string>(context.Configuration.MetaData);

if (!context.Configuration.Version.Contains("*"))
{
// if we have a label, ensure height is included
if (labelParts.Count != 0 && !labelParts.Contains("*"))
labelParts.Add("*");
}

// add short sha if required
var addShortSha = true;
foreach (var pattern in context.Configuration.Branches.Release)
{
if (Regex.IsMatch(context.Result.CanonicalBranchName, pattern))
{
addShortSha = false;
break;
}
}

if (addShortSha)
var rules = new IRule<string>[]
{
var shortSha = context.Result.Sha.Substring(0, 7);
labelParts.Add($"c{shortSha}");
}

var label = string.Join(".", labelParts);
label = label.Replace("*", context.Result.Height.ToString());

var meta = string.Join(".", metaParts);
meta = meta.Replace("*", context.Result.Height.ToString());

HeightRule.Instance,
ShortShaRule.Instance,
BranchNameRule.Instance,
ShortBranchNameRule.Instance,
BranchNameSuffixRule.Instance
};

var labelParts = context.Configuration.Label.ApplyRules(context, rules);
var label = string.Join(".", labelParts).ResolveRules(context, rules);
var meta = string.Join(".", context.Configuration.MetaData).ResolveRules(context, rules);

var format = context.Result.Version;

if (!string.IsNullOrWhiteSpace(label))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using SimpleVersion.Rules;
using System;

namespace SimpleVersion.Pipeline.Formatting
{
public class VersionFormatProcess : ICalculatorProcess
{
public void Apply(VersionContext context)
{
var versionString = context.Configuration.Version;
if (versionString.Contains("*"))
versionString = versionString.Replace("*", context.Result.Height.ToString());
var versionString = HeightRule.Instance.Resolve(context, context.Configuration.Version);

if (Version.TryParse(versionString, out var version))
{
Expand Down
Loading

0 comments on commit 070978c

Please sign in to comment.