3 Commits

Author SHA1 Message Date
Matthias Langhard
c45ba92f41 feat: adds a 'v' prefix to the new version based on wether the newest last version had one 2021-11-04 17:00:49 +01:00
Matthias Langhard
5d2c1d8f63 feat: bumps version number 2021-11-04 13:39:10 +01:00
Matthias Langhard
aeae68b5c4 feat: allows pipeline to run with 'v' prefix 2021-11-04 13:38:51 +01:00
8 changed files with 95 additions and 60 deletions

View File

@@ -10,7 +10,7 @@ running tests for tag:
publish to nuget:
only:
- /^\d*.\d*.\d*$/ # gets triggered if the commit tag is in the form n.n.n where n is any number
- /^v+\d*.\d*.\d*$/ # gets triggered if the commit tag is in the form n.n.n where n is any number
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: publish
script:

View File

@@ -98,8 +98,8 @@ namespace Cli
.Title("[red]Error evaluating version from newest tag.[/]\nAdd new version tag **AND** push to origin?)")
.PageSize(10)
.AddChoices(
new Selection("yes", new Version(0, 1, 0)),
new Selection("yes", new Version(0, 1, 0, 1)),
new Selection("yes", new Version(0, 1, 0, true)),
new Selection("yes", new Version(0, 1, 0, 1, true)),
new Selection("no", null)
)
);

View File

@@ -9,7 +9,7 @@
<PackageId>Novaloop.UpdateTag</PackageId>
<title>Updates the tag of a repo to the next chosen version according the semver symantic.</title>
<PackageTags>semver;update-tag;tag;git</PackageTags>
<Version>0.3.0</Version>
<Version>0.4.0</Version>
<Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.update-tag</PackageProjectUrl>

View File

@@ -13,37 +13,41 @@ namespace Application.Models
/// </summary>
public class Version
{
public Version(int major, int minor, int patch)
public Version(int major, int minor, int patch, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, int? rc)
public Version(int major, int minor, int patch, int? rc, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc;
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, string rc, string service)
public Version(int major, int minor, int patch, string rc, string service, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc == null ? null : ExtractNumberFromRcString(rc);
Service = service ?? "";
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, int? rc, string service)
public Version(int major, int minor, int patch, int? rc, string service, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc;
Service = service ?? "";
_hasVPrefix = hasVPrefix;
}
private static int? ExtractNumberFromRcString(string rc)
@@ -58,12 +62,16 @@ namespace Application.Models
public int Patch { get; private set; }
public int? Rc { get; private set; }
public string Service { get; private set; }
private readonly bool _hasVPrefix;
public override string ToString()
{
var sb = new StringBuilder();
sb.Append('v');
if (_hasVPrefix)
{
sb.Append('v');
}
sb.Append(Major);
sb.Append('.');
sb.Append(Minor);
@@ -88,7 +96,7 @@ namespace Application.Models
private Version Copy()
{
return new Version(Major, Minor, Patch, Rc, Service);
return new Version(Major, Minor, Patch, Rc, Service, _hasVPrefix);
}
public Version NextMajor()
@@ -191,7 +199,7 @@ namespace Application.Models
throw new ArgumentException("Cannot release RC. Not an RC.");
}
var nextVersion = new Version(Major, Minor, Patch, (string)null, Service);
var nextVersion = new Version(Major, Minor, Patch, (string)null, Service, _hasVPrefix);
return nextVersion;
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Application.Interfaces;
@@ -27,21 +28,23 @@ namespace Infrastructure.Services
{
if (TryParse(tag.FriendlyName, out var semver))
{
yield return new Version(semver.Major, semver.Minor, semver.Patch, semver.Prerelease, semver.Build);
yield return semver;
}
}
}
private static bool TryParse(string version, out SemVersion semverVersion)
private static bool TryParse(string versionStr, out Version version)
{
try
{
semverVersion = SemVersion.Parse(version.TrimStart('v').TrimStart('V'));
var semver = SemVersion.Parse(versionStr.TrimStart('v').TrimStart('V'));
var hasVPrefix = versionStr.StartsWith("v", StringComparison.InvariantCultureIgnoreCase);
version = new Version(semver.Major, semver.Minor, semver.Patch, semver.Prerelease, semver.Build, hasVPrefix);
return true;
}
catch
{
semverVersion = null;
version = null;
return false;
}
}

View File

@@ -16,12 +16,12 @@ namespace UpdateTag.Tests
// Arrange
var mockedVersionList = new List<Version>
{
new Version(0, 1, 5),
new Version(0, 1, 7),
new Version(0, 2, 0),
new Version(0, 2, 0, 0),
new Version(0, 2, 0, 1),
new Version(0, 2, 0, 2)
new Version(0, 1, 5, false),
new Version(0, 1, 7, false),
new Version(0, 2, 0, false),
new Version(0, 2, 0, 0, false),
new Version(0, 2, 0, 1, false),
new Version(0, 2, 0, 2, false)
};
var gitRepoMock = new Mock<IGitRepoReadService>();
gitRepoMock.Setup(m => m.GetAllVersions(It.IsAny<string>()))
@@ -33,7 +33,31 @@ namespace UpdateTag.Tests
var versionInformation = await handler.Handle(query, CancellationToken.None);
// Assert
Assert.Equal("v0.2.0", versionInformation.CurrentVersion.ToString());
Assert.Equal("0.2.0", versionInformation.CurrentVersion.ToString());
}
[Theory]
[InlineData(0, 2, 0, true, "v0.2.0")]
[InlineData(0, 2, 0, false, "0.2.0")]
public async void AddVPrefixToNextVersionIfCurrentVersionHasOne(int major, int minor, int patch, bool hasVPrefix,
string expectedVersionOutput)
{
// Arrange
var mockedVersionList = new List<Version>
{
new Version(major, minor, patch, hasVPrefix)
};
var gitRepoMock = new Mock<IGitRepoReadService>();
gitRepoMock.Setup(m => m.GetAllVersions(It.IsAny<string>()))
.Returns(mockedVersionList);
var handler = new GetVersionInformationFromRepo(gitRepoMock.Object);
var query = new GetVersionInformationFromRepo.Query("");
// Act
var versionInformation = await handler.Handle(query, CancellationToken.None);
// Assert
Assert.Equal(expectedVersionOutput, versionInformation.CurrentVersion.ToString());
}
}
}

View File

@@ -11,15 +11,15 @@ namespace UpdateTag.Tests
{
// Arrange
var version = new Version(1, 0, 1, 1);
var version = new Version(1, 0, 1, 1, false);
// Act
var versionInformation = new VersionInformation(version);
var versions = versionInformation.NextVersions.Select(nv => nv.Version.ToString()).ToList();
// Assert
Assert.Contains("v1.0.1-RC.2", versions); // next rc
Assert.Contains("v1.0.1", versions); // release rc
Assert.Contains("1.0.1-RC.2", versions); // next rc
Assert.Contains("1.0.1", versions); // release rc
Assert.Equal(2, versions.Count);
}
@@ -28,7 +28,7 @@ namespace UpdateTag.Tests
{
// Arrange
var version = new Version(1, 0, 1);
var version = new Version(1, 0, 1, false);
// Act
var versionInformation = new VersionInformation(version);
@@ -36,12 +36,12 @@ namespace UpdateTag.Tests
// Assert
Assert.Contains("v1.0.2-RC.0", versions); // patch RC
Assert.Contains("v1.1.0-RC.0", versions); // minor RC
Assert.Contains("v2.0.0-RC.0", versions); // major RC
Assert.Contains("v1.0.2", versions); // next patch
Assert.Contains("v1.1.0", versions); // next minor
Assert.Contains("v2.0.0", versions); // next major
Assert.Contains("1.0.2-RC.0", versions); // patch RC
Assert.Contains("1.1.0-RC.0", versions); // minor RC
Assert.Contains("2.0.0-RC.0", versions); // major RC
Assert.Contains("1.0.2", versions); // next patch
Assert.Contains("1.1.0", versions); // next minor
Assert.Contains("2.0.0", versions); // next major
Assert.Equal(6, versions.Count);
}
}

View File

@@ -6,82 +6,82 @@ namespace UpdateTag.Tests
public class VersionTests
{
[Theory]
[InlineData(1, 0, 0, "", "", "v2.0.0")]
[InlineData(1, 1, 0, "", "", "v2.0.0")]
[InlineData(1, 1, 1, "", "", "v2.0.0")]
[InlineData(1, 0, 0, "", "", "2.0.0")]
[InlineData(1, 1, 0, "", "", "2.0.0")]
[InlineData(1, 1, 1, "", "", "2.0.0")]
public void NextMajor(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).NextMajor();
var version = new Version(major, minor, patch, rc, service, false).NextMajor();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 0, 0, "", "", "v1.1.0")]
[InlineData(1, 1, 0, "", "", "v1.2.0")]
[InlineData(1, 1, 1, "", "", "v1.2.0")]
[InlineData(1, 0, 0, "", "", "1.1.0")]
[InlineData(1, 1, 0, "", "", "1.2.0")]
[InlineData(1, 1, 1, "", "", "1.2.0")]
public void NextMinor(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).NextMinor();
var version = new Version(major, minor, patch, rc, service, false).NextMinor();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 0, 0, "", "", "v1.0.1")]
[InlineData(1, 1, 0, "", "", "v1.1.1")]
[InlineData(1, 1, 1, "", "", "v1.1.2")]
[InlineData(1, 0, 0, "", "", "1.0.1")]
[InlineData(1, 1, 0, "", "", "1.1.1")]
[InlineData(1, 1, 1, "", "", "1.1.2")]
public void NextPatch(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).NextPatch();
var version = new Version(major, minor, patch, rc, service, false).NextPatch();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, "RC.4", "", "v1.1.1-RC.5")]
[InlineData(1, 1, 1, "RC.4", "ErpNext", "v1.1.1-RC.5+ErpNext")]
[InlineData(1, 1, 1, "RC.4", "", "1.1.1-RC.5")]
[InlineData(1, 1, 1, "RC.4", "ErpNext", "1.1.1-RC.5+ErpNext")]
public void NextRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).NextRc();
var version = new Version(major, minor, patch, rc, service, false).NextRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, null, "", "v1.1.2-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "v1.1.2-RC.0+ErpNext")]
[InlineData(1, 1, 1, null, "", "1.1.2-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "1.1.2-RC.0+ErpNext")]
public void CreatePatchRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).CreatePatchRc();
var version = new Version(major, minor, patch, rc, service, false).CreatePatchRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, null, "", "v1.2.0-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "v1.2.0-RC.0+ErpNext")]
[InlineData(1, 1, 1, null, "", "1.2.0-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "1.2.0-RC.0+ErpNext")]
public void CreateMinorRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).CreateMinorRc();
var version = new Version(major, minor, patch, rc, service, false).CreateMinorRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, null, "", "v2.0.0-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "v2.0.0-RC.0+ErpNext")]
[InlineData(1, 1, 1, null, "", "2.0.0-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "2.0.0-RC.0+ErpNext")]
public void CreateMajroRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).CreateMajorRc();
var version = new Version(major, minor, patch, rc, service, false).CreateMajorRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, "RC.4", "", "v1.1.1")]
[InlineData(1, 1, 1, "RC.4", "ErpNext", "v1.1.1+ErpNext")]
[InlineData(1, 1, 1, "RC.4", "", "1.1.1")]
[InlineData(1, 1, 1, "RC.4", "ErpNext", "1.1.1+ErpNext")]
public void ReleaseRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service).ReleaseRc();
var version = new Version(major, minor, patch, rc, service, false).ReleaseRc();
Assert.Equal(expected, version.ToString());
}
}