Skip to content

Commit

Permalink
(#146) Add GitLab VCS Provider
Browse files Browse the repository at this point in the history
This commit introduces a new GitLabProvider class, and implements the
necessary methods to allow GRM to create release notes on GitLab. This
is made possible by using the NGitLab library. A new option has been
added to the base command, which allows settings of the --provider at
the command line.  The default value for this option is GitHub, so
everything continues to work as it is expected to.
  • Loading branch information
gep13 committed Sep 8, 2023
1 parent b9de09b commit e5de814
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 6 deletions.
27 changes: 23 additions & 4 deletions src/GitReleaseManager.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
using GitReleaseManager.Core.Commands;
using GitReleaseManager.Core.Configuration;
using GitReleaseManager.Core.Helpers;
using GitReleaseManager.Core.Model;
using GitReleaseManager.Core.Options;
using GitReleaseManager.Core.Provider;
using GitReleaseManager.Core.ReleaseNotes;
using GitReleaseManager.Core.Templates;
using Microsoft.Extensions.DependencyInjection;
using NGitLab;
using Octokit;
using Serilog;

Expand Down Expand Up @@ -96,7 +98,6 @@ private static void RegisterServices(BaseSubOptions options)
.AddSingleton<IFileSystem>(fileSystem)
.AddSingleton<IReleaseNotesExporter, ReleaseNotesExporter>()
.AddSingleton<IReleaseNotesBuilder, ReleaseNotesBuilder>()
.AddSingleton<IVcsProvider, GitHubProvider>()
.AddSingleton<IVcsService, VcsService>();

if (options is BaseVcsOptions vcsOptions)
Expand All @@ -106,9 +107,7 @@ private static void RegisterServices(BaseSubOptions options)
throw new Exception("The token option is not defined");
}

var gitHubClient = new GitHubClient(new ProductHeaderValue("GitReleaseManager")) { Credentials = new Credentials(vcsOptions.Token) };
serviceCollection = serviceCollection
.AddSingleton<IGitHubClient>(gitHubClient);
RegisterVcsProvider(vcsOptions, serviceCollection);
}

serviceCollection = serviceCollection
Expand Down Expand Up @@ -197,5 +196,25 @@ private static Task<int> ExecuteCommand<TOptions>(TOptions options)

private static void LogOptions(BaseSubOptions options)
=> Log.Debug("{@Options}", options);

private static void RegisterVcsProvider(BaseVcsOptions vcsOptions, IServiceCollection serviceCollection)
{
Log.Information("Using {Provider} as VCS Provider", vcsOptions.Provider);
if (vcsOptions.Provider == VcsProvider.GitLab)
{
var gitlabClient = new GitLabClient("https://gitlab.com", vcsOptions.Token);
serviceCollection
.AddSingleton<IVcsProvider, GitLabProvider>()
.AddSingleton<IGitLabClient>(gitlabClient);
}
else
{
// default to Github
var gitHubClient = new GitHubClient(new ProductHeaderValue("GitReleaseManager")) { Credentials = new Credentials(vcsOptions.Token) };
serviceCollection
.AddSingleton<IGitHubClient>(gitHubClient)
.AddSingleton<IVcsProvider, GitHubProvider>();
}
}
}
}
26 changes: 24 additions & 2 deletions src/GitReleaseManager.Core/Extensions/MilestoneExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Octokit;
using Serilog;

namespace GitReleaseManager.Core.Extensions
Expand All @@ -8,7 +7,30 @@ public static class MilestoneExtensions
{
public static readonly ILogger _logger = Log.ForContext(typeof(MilestoneExtensions));

public static Version Version(this Milestone ver)
public static Version Version(this Octokit.Milestone ver)
{
if (ver is null)
{
throw new ArgumentNullException(nameof(ver));
}

var nameWithoutPrerelease = ver.Title.Split('-')[0];
if (nameWithoutPrerelease.StartsWith("v", StringComparison.OrdinalIgnoreCase))
{
_logger.Debug("Removing version prefix from {Name}", ver.Title);
nameWithoutPrerelease = nameWithoutPrerelease.Remove(0, 1);
}

if (!System.Version.TryParse(nameWithoutPrerelease, out Version parsedVersion))
{
_logger.Warning("No valid version was found on {Title}", ver.Title);
return new Version(0, 0);
}

return parsedVersion;
}

public static Version Version(this NGitLab.Models.Milestone ver)
{
if (ver is null)
{
Expand Down
1 change: 1 addition & 0 deletions src/GitReleaseManager.Core/GitReleaseManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="NGitLab" Version="6.39.0" />
<PackageReference Include="Octokit" Version="7.1.0" />
<PackageReference Include="Scriban" Version="5.7.0" />
<PackageReference Include="seriloganalyzer" Version="0.15.0" />
Expand Down
48 changes: 48 additions & 0 deletions src/GitReleaseManager.Core/MappingProfiles/GitLabProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace GitReleaseManager.Core.MappingProfiles
{
using System;
using AutoMapper;
using GitReleaseManager.Core.Extensions;

public class GitLabProfile : Profile
{
public GitLabProfile()
{
CreateMap<NGitLab.Models.Milestone, Model.Milestone>()
.ForMember(dest => dest.PublicNumber, act => act.MapFrom(src => src.Iid))
.ForMember(dest => dest.InternalNumber, act => act.MapFrom(src => src.Id))
.AfterMap((src, dest) => dest.Version = src.Version());
CreateMap<NGitLab.Models.ReleaseInfo, Model.Release>()
.ForMember(dest => dest.Draft, act => act.MapFrom(src => src.ReleasedAt > DateTime.UtcNow))
.ForMember(dest => dest.Body, act => act.MapFrom(src => src.Description))
.ForMember(dest => dest.Assets, act => act.MapFrom(src => src.Assets.Links))
.ReverseMap();
CreateMap<NGitLab.Models.ReleaseLink, Model.ReleaseAsset>().ReverseMap();
CreateMap<NGitLab.Models.Issue, Model.Issue>()
.ForMember(dest => dest.InternalNumber, act => act.MapFrom(src => src.Id))
.ForMember(dest => dest.PublicNumber, act => act.MapFrom(src => src.IssueId))
.ForMember(dest => dest.HtmlUrl, act => act.MapFrom(src => src.WebUrl))
.ForMember(dest => dest.IsPullRequest, act => act.MapFrom(src => false))
.ReverseMap();
CreateMap<NGitLab.Models.MergeRequest, Model.Issue>()
.ForMember(dest => dest.InternalNumber, act => act.MapFrom(src => src.Id))
.ForMember(dest => dest.PublicNumber, act => act.MapFrom(src => src.Iid))
.ForMember(dest => dest.HtmlUrl, act => act.MapFrom(src => src.WebUrl))
.ForMember(dest => dest.IsPullRequest, act => act.MapFrom(src => true))
.ReverseMap();
CreateMap<string, Model.Label>().ForMember(dest => dest.Name, act => act.MapFrom(src => src));
CreateMap<Model.Release, NGitLab.Models.ReleaseCreate>()
.ForMember(dest => dest.Description, act => act.MapFrom(src => src.Body))
.ForMember(dest => dest.Ref, act => act.MapFrom(src => src.TargetCommitish))
.ForMember(dest => dest.Milestones, act => act.MapFrom(src => new string[] { src.TagName }))
.ForMember(dest => dest.ReleasedAt, act => act.MapFrom(src => src.Draft ? DateTime.UtcNow.AddYears(1) : DateTime.UtcNow))
.ForMember(dest => dest.Assets, act => act.Ignore())
.ReverseMap();
CreateMap<NGitLab.Models.ProjectIssueNote, Model.IssueComment>()
.ForMember(dest => dest.Id, act => act.MapFrom(src => src.NoteId))
.ReverseMap();
CreateMap<NGitLab.Models.MergeRequestComment, Model.IssueComment>()
.ReverseMap();
}
}
}
8 changes: 8 additions & 0 deletions src/GitReleaseManager.Core/Model/VcsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GitReleaseManager.Core.Model
{
public enum VcsProvider
{
GitHub = 0,
GitLab = 1,
}
}
4 changes: 4 additions & 0 deletions src/GitReleaseManager.Core/Options/BaseVcsSubOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommandLine;
using Destructurama.Attributed;
using GitReleaseManager.Core.Model;

namespace GitReleaseManager.Core.Options
{
Expand All @@ -14,5 +15,8 @@ public abstract class BaseVcsOptions : BaseSubOptions

[Option('r', "repository", HelpText = "The name of the repository.", Required = true)]
public string RepositoryName { get; set; }

[Option("provider", HelpText = "Version Control System provider", Default = VcsProvider.GitHub)]
public VcsProvider Provider { get; set; }
}
}
Loading

0 comments on commit e5de814

Please sign in to comment.