Skip to content

Commit

Permalink
feat: Add short sha to VersionContext Result (#82)
Browse files Browse the repository at this point in the history
* #76 Add short sha to VersionContext Result.
  • Loading branch information
asci13 authored and Kieranties committed Jun 15, 2019
1 parent 3a2af2f commit 962e9d7
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/articles/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",
"Sha7": "ebc8f22",
"CanonicalBranchName": "refs/heads/preview/test",
"BranchName": "preview/test",
"Formats": {
Expand All @@ -38,6 +39,7 @@ Properties
| Height | int | The calculated height |
| HeightPadded | int | The calculated height padded to four digits |
| Sha | string | The sha of the current commit at the time of invocation |
| Sha7 | string | The sha of the current commit at the time of invocation, shortened to 7 characters |
| BranchName | string | The checked out branch at the time of invocation |
| CanonicalBranchName | string | The full canonical name of the checked out branch at the time of invocation |

Expand Down
5 changes: 5 additions & 0 deletions src/SimpleVersion.Abstractions/Model/VersionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class VersionResult
/// </summary>
public string Sha { get; set; }

/// <summary>
/// Gets or sets the shortened (7 char) sha of the current commit.
/// </summary>
public string Sha7 { get; set; }

/// <summary>
/// Gets or sets the friendly branch name of the current branch.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/SimpleVersion.Core/Pipeline/VersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public VersionContext(Git.IRepository repository)

private VersionResult SetIntialResult()
{
var sha = Repository.Head.Tip?.Sha;
return new VersionResult
{
BranchName = Repository.Head.FriendlyName,
CanonicalBranchName = Repository.Head.CanonicalName,
Sha = Repository.Head.Tip?.Sha
Sha = sha,
Sha7 = (string.IsNullOrEmpty(sha) || sha.Length < 7) ? null : sha.Substring(0, 7)
};
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/SimpleVersion.Core/Rules/ShortShaTokenRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class ShortShaTokenRule : ITokenRule<string>
/// <inheritdoc/>
public string Resolve(IVersionContext context, string value)
{
var shortSha = context.Result.Sha.Substring(0, 7);
return Regex.Replace(value, Regex.Escape(Token), $"c{shortSha}", RegexOptions.IgnoreCase);
return Regex.Replace(value, Regex.Escape(Token), $"c{context.Result.Sha7}", RegexOptions.IgnoreCase);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public void Apply_LabelParts_NonRelease_Is_Formatted(
};
context.Result.Version = context.Configuration.Version;

var shaSub = context.Result.Sha.Substring(0, 7);
var fullExpected = $"{expectedPart}-c{shaSub}";
var fullExpected = $"{expectedPart}-c{context.Result.Sha7}";

// Act
_sut.Apply(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void Apply_LabelParts_NonRelease_Is_Formatted(
context.Result.Version = context.Configuration.Version;

var divider = parts.Length > 0 ? '.' : '-';
var shaSub = context.Result.Sha.Substring(0, 7);
var fullExpected = $"{expectedPart}{divider}c{shaSub}";
var fullExpected = $"{expectedPart}{divider}c{context.Result.Sha7}";

// Act
_sut.Apply(context);
Expand Down Expand Up @@ -112,13 +111,12 @@ public void Apply_MetadataParts_NonRelease_Is_Formatted(
Result = Utils.GetVersionResult(height, false)
};
context.Result.Version = context.Configuration.Version;
var shaSub = context.Result.Sha.Substring(0, 7);

string expected;
if (parts.Length > 0)
expected = $"{version}-c{shaSub}+{string.Join(".", parts)}";
expected = $"{version}-c{context.Result.Sha7}+{string.Join(".", parts)}";
else
expected = $"{version}-c{shaSub}";
expected = $"{version}-c{context.Result.Sha7}";

// Act
_sut.Apply(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void Ctor_WithEmptyRepo_SetsProperties()
sut.Result.BranchName.Should().Be(fixture.Repository.Head.FriendlyName);
sut.Result.CanonicalBranchName.Should().Be(fixture.Repository.Head.CanonicalName);
sut.Result.Sha.Should().BeNull();
sut.Result.Sha7.Should().BeNull();
}
}

Expand All @@ -56,6 +57,7 @@ public void Ctor_WithCommittedRepo_SetsProperties()
sut.Result.BranchName.Should().Be(fixture.Repository.Head.FriendlyName);
sut.Result.CanonicalBranchName.Should().Be(fixture.Repository.Head.CanonicalName);
sut.Result.Sha.Should().Be(fixture.Repository.Head.Tip.Sha);
sut.Result.Sha7.Should().Be(fixture.Repository.Head.Tip.Sha.Substring(0, 7));
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/SimpleVersion.Core.Tests/Rules/ShortShaTokenRuleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ public void Resolve_ReplacesToken_IfNeeded(string input, string expected)
// Act
var result = _sut.Resolve(context, input);

// Assert
result.Should().Be(expected);
}
}

[Theory]
[InlineData("this-{shortsha}", "this-c4ca82d2")]
public void Resolve_ReplacesToken2_IfNeeded(string input, string expected)
{
// Arrange
using (var fixture = new EmptyRepositoryFixture())
{
var context = new VersionContext(fixture.Repository)
{
Result = Utils.GetVersionResult(10)
};

// Act
var result = _sut.Resolve(context, input);

// Assert
result.Should().Be(expected);
}
Expand Down
1 change: 1 addition & 0 deletions test/SimpleVersion.Core.Tests/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static VersionResult GetVersionResult(int height, bool release = true)
BranchName = branchName,
CanonicalBranchName = "refs/heads/" + branchName,
Sha = "4ca82d2c58f48007bf16d69ebf036fc4ebfdd059",
Sha7 = "4ca82d2",
Height = height
};
}
Expand Down

0 comments on commit 962e9d7

Please sign in to comment.