1 Commits

Author SHA1 Message Date
Matthias Langhard
17c8787a3e Configure SAST in .gitlab-ci.yml, creating this file if it does not already exist 2021-11-03 14:55:59 +00:00
14 changed files with 51 additions and 163 deletions

View File

@@ -1,20 +1,26 @@
# You can override the included template(s) by including variable overrides
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/#customizing-settings
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
# Note that environment variables can be set in several places
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
stages:
- test
- publish
- test
- publish
running tests for tag:
only:
- tags
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: test
script:
- dotnet test tests/update-tag.tests
- dotnet test tests/update-tag.tests
publish to nuget:
only:
- /^v+\d*.\d*.\d*$/ # gets triggered if the commit tag is in the form n.n.n where n is any number
- "/^\\d*.\\d*.\\d*$/"
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: publish
script:
- dotnet pack src/Cli -o ./packaged
- dotnet nuget push ./packaged/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
- dotnet pack src/Cli -o ./packaged
- dotnet nuget push ./packaged/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
sast:
stage: test
include:
- template: Security/SAST.gitlab-ci.yml

View File

@@ -25,18 +25,11 @@ namespace Cli
var repoBasePath = await GetRepoBasePath(workingDir);
var chosenService = await ChooseService(repoBasePath);
var selection = await SelectVersion(repoBasePath, chosenService);
await AnsiConsole.Status()
.StartAsync("pushing to remote...", async _ =>
{
await AddVersionTagToRepo(repoBasePath, selection.Version.ToString());
await PushTagsToRemote(repoBasePath);
await PushCommitsToRemote(repoBasePath);
}
);
await AddVersionTagToRepo(repoBasePath, selection.Version.ToString());
await PushTagsToRemote(repoBasePath);
await PushCommitsToRemote(repoBasePath);
}
private async Task<string> GetRepoBasePath(string workingDir)
{
var repoBasePath = "";
@@ -105,20 +98,11 @@ 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, true)),
new Selection("yes", new Version(0, 1, 0, 1, true)),
new Selection("yes", new Version(0, 1, 0)),
new Selection("yes", new Version(0, 1, 0, 1)),
new Selection("no", null)
)
);
var serviceName = AnsiConsole.Prompt(
new TextPrompt<string>("[grey][[Optional]][/] Enter [green]service name[/]:")
.AllowEmpty()
);
if (!string.IsNullOrWhiteSpace(serviceName))
{
selection.Version.SetService(serviceName.Trim());
}
}
if (selection.Version == null)

View File

@@ -6,11 +6,10 @@
<RootNamespace>Cli</RootNamespace>
<PackAsTool>true</PackAsTool>
<ToolCommandName>update-tag</ToolCommandName>
<AssemblyTitle>update-tag</AssemblyTitle>
<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.6.1</Version>
<Version>0.1.7</Version>
<Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.update-tag</PackageProjectUrl>
@@ -19,7 +18,6 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0"/>
<PackageReference Include="Spectre.Console" Version="0.42.0"/>

View File

@@ -1,10 +0,0 @@
using CommandLine;
namespace Cli.Models
{
public class CliParams
{
[Option('r', "repository-path", Required = false, HelpText = "Run update-tag on a git repository other than the current directory.")]
public string RepositoryPath { get; set; }
}
}

View File

@@ -1,8 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Application;
using Cli.Models;
using CommandLine;
using Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -22,14 +21,9 @@ namespace Cli
.ServiceProvider
.GetRequiredService<AppRunner>();
await Parser.Default
.ParseArguments<CliParams>(args)
.WithParsedAsync(
async options => { await appRunner.Run(options.RepositoryPath ?? Environment.CurrentDirectory); }
);
await appRunner.Run(args.FirstOrDefault() ?? Environment.CurrentDirectory);
}
private static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()

View File

@@ -13,41 +13,37 @@ namespace Application.Models
/// </summary>
public class Version
{
public Version(int major, int minor, int patch, bool hasVPrefix)
public Version(int major, int minor, int patch)
{
Major = major;
Minor = minor;
Patch = patch;
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, int? rc, bool hasVPrefix)
public Version(int major, int minor, int patch, int? rc)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc;
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, string rc, string service, bool hasVPrefix)
public Version(int major, int minor, int patch, string rc, string service)
{
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, bool hasVPrefix)
public Version(int major, int minor, int patch, int? rc, string service)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc;
Service = service ?? "";
_hasVPrefix = hasVPrefix;
}
private static int? ExtractNumberFromRcString(string rc)
@@ -61,17 +57,12 @@ namespace Application.Models
public int Minor { get; private set; }
public int Patch { get; private set; }
public int? Rc { get; private set; }
public string Service { get; private set; }
private readonly bool _hasVPrefix;
public string Service { get; }
public override string ToString()
{
var sb = new StringBuilder();
if (_hasVPrefix)
{
sb.Append('v');
}
sb.Append(Major);
sb.Append('.');
sb.Append(Minor);
@@ -96,7 +87,7 @@ namespace Application.Models
private Version Copy()
{
return new Version(Major, Minor, Patch, Rc, Service, _hasVPrefix);
return new Version(Major, Minor, Patch, Rc, Service);
}
public Version NextMajor()
@@ -199,7 +190,7 @@ namespace Application.Models
throw new ArgumentException("Cannot release RC. Not an RC.");
}
var nextVersion = new Version(Major, Minor, Patch, (string)null, Service, _hasVPrefix);
var nextVersion = new Version(Major, Minor, Patch, (string)null, Service);
return nextVersion;
}
@@ -207,10 +198,5 @@ namespace Application.Models
{
return Rc != null;
}
public void SetService(string service)
{
Service = service;
}
}
}

View File

@@ -16,7 +16,7 @@ namespace Application.Models
{
NextVersions.Add(new NextVersion("patch-rc", currentVersion.CreatePatchRc()));
NextVersions.Add(new NextVersion("minor-rc", currentVersion.CreateMinorRc()));
NextVersions.Add(new NextVersion("major-rc", currentVersion.CreateMajorRc()));
NextVersions.Add(new NextVersion("minor-rc", currentVersion.CreateMajorRc()));
NextVersions.Add(new NextVersion("patch ", currentVersion.NextPatch()));
NextVersions.Add(new NextVersion("minor ", currentVersion.NextMinor()));
NextVersions.Add(new NextVersion("major ", currentVersion.NextMajor()));

View File

@@ -1,14 +1,12 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Application.Interfaces;
using Application.Models;
using MediatR;
namespace Application.Queries
{
public class GetVersionInformationFromRepo : IRequestHandler<GetVersionInformationFromRepo.Query, VersionInformation>
public class GetVersionInformationFromRepo : RequestHandler<GetVersionInformationFromRepo.Query, VersionInformation>
{
public class Query : IRequest<VersionInformation>
{
@@ -29,7 +27,7 @@ namespace Application.Queries
_gitRepoReadService = gitRepoReadService;
}
public async Task<VersionInformation> Handle(Query request, CancellationToken cancellationToken)
protected override VersionInformation Handle(Query request)
{
var versions = _gitRepoReadService
.GetAllVersions(request.RepositoryPath);
@@ -44,11 +42,10 @@ namespace Application.Queries
.OrderByDescending(v => v.Major)
.ThenByDescending(v => v.Minor)
.ThenByDescending(v => v.Patch)
.ThenByDescending(v => v.Rc == null)
.ThenByDescending(v => v.Rc)
.FirstOrDefault();
return await Task.FromResult(currentVersion == null ? null : new VersionInformation(currentVersion));
return currentVersion == null ? null : new VersionInformation(currentVersion);
}
}
}

View File

@@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.3.3" />
<PackageReference Include="LibGit2Sharp" Version="0.27.0-preview-0158" />
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="semver" Version="2.0.6" />
</ItemGroup>

View File

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

View File

@@ -1,63 +0,0 @@
using System.Collections.Generic;
using System.Threading;
using Application.Interfaces;
using Application.Models;
using Application.Queries;
using Moq;
using Xunit;
namespace UpdateTag.Tests
{
public class GetVersionInformationFromRepoTests
{
[Fact]
public async void DoesReadCurrentVersionCorrectly()
{
// Arrange
var mockedVersionList = new List<Version>
{
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>()))
.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("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,7 +11,7 @@ namespace UpdateTag.Tests
{
// Arrange
var version = new Version(1, 0, 1, 1, false);
var version = new Version(1, 0, 1, 1);
// Act
var versionInformation = new VersionInformation(version);
@@ -28,7 +28,7 @@ namespace UpdateTag.Tests
{
// Arrange
var version = new Version(1, 0, 1, false);
var version = new Version(1, 0, 1);
// Act
var versionInformation = new VersionInformation(version);

View File

@@ -12,7 +12,7 @@ namespace UpdateTag.Tests
public void NextMajor(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).NextMajor();
var version = new Version(major, minor, patch, rc, service).NextMajor();
Assert.Equal(expected, version.ToString());
}
@@ -24,7 +24,7 @@ namespace UpdateTag.Tests
public void NextMinor(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).NextMinor();
var version = new Version(major, minor, patch, rc, service).NextMinor();
Assert.Equal(expected, version.ToString());
}
@@ -35,7 +35,7 @@ namespace UpdateTag.Tests
public void NextPatch(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).NextPatch();
var version = new Version(major, minor, patch, rc, service).NextPatch();
Assert.Equal(expected, version.ToString());
}
@@ -45,7 +45,7 @@ namespace UpdateTag.Tests
public void NextRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).NextRc();
var version = new Version(major, minor, patch, rc, service).NextRc();
Assert.Equal(expected, version.ToString());
}
@@ -54,7 +54,7 @@ namespace UpdateTag.Tests
[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, false).CreatePatchRc();
var version = new Version(major, minor, patch, rc, service).CreatePatchRc();
Assert.Equal(expected, version.ToString());
}
@@ -63,7 +63,7 @@ namespace UpdateTag.Tests
[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, false).CreateMinorRc();
var version = new Version(major, minor, patch, rc, service).CreateMinorRc();
Assert.Equal(expected, version.ToString());
}
@@ -72,7 +72,7 @@ namespace UpdateTag.Tests
[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, false).CreateMajorRc();
var version = new Version(major, minor, patch, rc, service).CreateMajorRc();
Assert.Equal(expected, version.ToString());
}
@@ -81,7 +81,7 @@ namespace UpdateTag.Tests
[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, false).ReleaseRc();
var version = new Version(major, minor, patch, rc, service).ReleaseRc();
Assert.Equal(expected, version.ToString());
}
}

View File

@@ -9,7 +9,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>