Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for Gitea #264

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Source/Gitea"]
path = Source/Gitea
url = [email protected]:belidzs/Gitea.git
12 changes: 12 additions & 0 deletions Source/GitReleaseManager.Cli/Options/BaseVcsSubOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace GitReleaseManager.Cli.Options

public abstract class BaseVcsOptions : BaseSubOptions
{
public enum VcsProvider
{
Github,
Gitea,
}

internal const string OBSOLETE_MESSAGE = "Authentication using username and password has been deprecated, and will be removed in a future release. Please use --token instead!";

[Obsolete(OBSOLETE_MESSAGE)]
Expand All @@ -31,5 +37,11 @@ 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; }

[Option("providerUrl", HelpText = "Custom URL of the chosen VCS provider")]
public string ProviderUrl { get; set; }
}
}
16 changes: 12 additions & 4 deletions Source/GitReleaseManager.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private static async Task<int> CloseMilestoneAsync(CloseSubOptions subOptions)
Log.Information("Closing milestone {Milestone}", subOptions.Milestone);
_vcsProvider = GetVcsProvider(subOptions);

await _vcsProvider.CloseMilestone(subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone).ConfigureAwait(false);
await _vcsProvider.CloseMilestoneAsync(subOptions.RepositoryOwner, subOptions.RepositoryName, subOptions.Milestone).ConfigureAwait(false);

return 0;
}
Expand Down Expand Up @@ -258,16 +258,24 @@ private static async Task<int> CreateLabelsAsync(LabelSubOptions subOptions)
Log.Information("Creating standard labels");
_vcsProvider = GetVcsProvider(subOptions);

await _vcsProvider.CreateLabels(subOptions.RepositoryOwner, subOptions.RepositoryName).ConfigureAwait(false);
await _vcsProvider.CreateLabelsAsync(subOptions.RepositoryOwner, subOptions.RepositoryName).ConfigureAwait(false);
return 0;
}

private static IVcsProvider GetVcsProvider(BaseVcsOptions subOptions)
{
var configuration = ConfigurationProvider.Provide(subOptions.TargetDirectory ?? Environment.CurrentDirectory, _fileSystem);

Log.Information("Using {Provider} as VCS Provider", "GitHub");
return new GitHubProvider(_mapper, configuration, subOptions.UserName, subOptions.Password, subOptions.Token);
Log.Information("Using {Provider} as VCS Provider", subOptions.Provider);
if (subOptions.Provider == BaseVcsOptions.VcsProvider.Gitea)
{
return new GiteaProvider(configuration, subOptions.Token, subOptions.ProviderUrl);
}
else
{
// default to Github
return new GitHubProvider(_mapper, configuration, subOptions.UserName, subOptions.Password, subOptions.Token);
}
}

private static void LogOptions(BaseSubOptions options)
Expand Down
4 changes: 2 additions & 2 deletions Source/GitReleaseManager.Tests/FakeGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Task<string> ExportReleases(string owner, string repository, string tagNa
throw new System.NotImplementedException();
}

public Task CloseMilestone(string owner, string repository, string milestoneTitle)
public Task CloseMilestoneAsync(string owner, string repository, string milestoneTitle)
{
throw new System.NotImplementedException();
}
Expand All @@ -113,7 +113,7 @@ public Task PublishRelease(string owner, string repository, string tagName)
throw new System.NotImplementedException();
}

public Task CreateLabels(string owner, string repository)
public Task CreateLabelsAsync(string owner, string repository)
{
throw new System.NotImplementedException();
}
Expand Down
8 changes: 7 additions & 1 deletion Source/GitReleaseManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Chocolatey", "Chocolatey",
..\nuspec\chocolatey\VERIFICATION.TXT = ..\nuspec\chocolatey\VERIFICATION.TXT
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitReleaseManager.Tool", "GitReleaseManager.Tool\GitReleaseManager.Tool.csproj", "{E60129C7-706A-4B24-9D8D-6D5F23543280}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GitReleaseManager.Tool", "GitReleaseManager.Tool\GitReleaseManager.Tool.csproj", "{E60129C7-706A-4B24-9D8D-6D5F23543280}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gitea", "Gitea\src\Gitea\Gitea.csproj", "{9E3EE633-423A-4705-971D-30C221A88E1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{E60129C7-706A-4B24-9D8D-6D5F23543280}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E60129C7-706A-4B24-9D8D-6D5F23543280}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E60129C7-706A-4B24-9D8D-6D5F23543280}.Release|Any CPU.Build.0 = Release|Any CPU
{9E3EE633-423A-4705-971D-30C221A88E1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E3EE633-423A-4705-971D-30C221A88E1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E3EE633-423A-4705-971D-30C221A88E1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E3EE633-423A-4705-971D-30C221A88E1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
89 changes: 89 additions & 0 deletions Source/GitReleaseManager/BaseVcsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace GitReleaseManager.Core
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GitReleaseManager.Core.Configuration;
using GitReleaseManager.Core.Model;
using Serilog;

public abstract class BaseVcsProvider : IVcsProvider
{
public BaseVcsProvider(Config configuration, ILogger logger)
{
Configuration = configuration;
Logger = logger;
}

protected Config Configuration { get; }

protected ILogger Logger { get; }

public abstract Task AddAssets(string owner, string repository, string tagName, IList<string> assets);

public abstract Task CloseMilestoneAsync(string owner, string repository, string milestoneTitle);

public abstract Task<Release> CreateReleaseFromInputFile(string owner, string repository, string name, string inputFilePath, string targetCommitish, IList<string> assets, bool prerelease);

public abstract Task<Release> CreateReleaseFromMilestone(string owner, string repository, string milestone, string releaseName, string targetCommitish, IList<string> assets, bool prerelease);

public abstract Task DiscardRelease(string owner, string repository, string name);

public abstract Task<string> ExportReleases(string owner, string repository, string tagName);

public abstract string GetCommitsLink(string user, string repository, Milestone milestone, Milestone previousMilestone);

public abstract Task<List<Issue>> GetIssuesAsync(Milestone targetMilestone);

public abstract Task<int> GetNumberOfCommitsBetween(Milestone previousMilestone, Milestone currentMilestone, string user, string repository);

public abstract Task<ReadOnlyCollection<Milestone>> GetReadOnlyMilestonesAsync(string user, string repository);

public abstract Task<List<Release>> GetReleasesAsync(string user, string repository);

public abstract Task<Release> GetSpecificRelease(string tagName, string user, string repository);

public abstract Task OpenMilestone(string owner, string repository, string milestoneTitle);

public abstract Task PublishRelease(string owner, string repository, string tagName);

public virtual async Task CreateLabelsAsync(string owner, string repository)
{
if (Configuration.Labels.Any())
{
Logger.Verbose("Removing existing labels");
await DeleteExistingLabelsAsync(owner, repository).ConfigureAwait(false);
Logger.Verbose("Creating new standard labels");
var newLabelTasks = new List<Task>();
foreach (var label in Configuration.Labels)
{
newLabelTasks.Add(CreateLabelAsync(owner, repository, label));
}

await Task.WhenAll(newLabelTasks).ConfigureAwait(false);
}
else
{
Logger.Warning("No labels defined");
}
}

protected abstract Task DeleteExistingLabelsAsync(string owner, string repository);

protected abstract Task CreateLabelAsync(string owner, string repository, LabelConfig label);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of a public default implementation of an interface method which calls for two abstract protected methods.

It's completely possible that it is somehow anti-pattern, however it reduces redundant code quite a bit


protected abstract Task<List<string>> GetCommentsForIssueAsync(string owner, string repository, long index);


protected virtual async Task<bool> DoesAnyCommentIncludeStringAsync(string owner, string repository, long index, string comment)
{
Logger.Verbose("Finding issue comment created by GitReleaseManager for issue #{IssueNumber}", index);
var issueComments = await GetCommentsForIssueAsync(owner, repository, index).ConfigureAwait(false);
return issueComments.Any(c => c.Contains(comment));
}

}
}
4 changes: 2 additions & 2 deletions Source/GitReleaseManager/GitHubProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public Task<string> ExportReleases(string owner, string repository, string tagNa
return releaseNotesExporter.ExportReleaseNotes(tagName);
}

public async Task CloseMilestone(string owner, string repository, string milestoneTitle)
public async Task CloseMilestoneAsync(string owner, string repository, string milestoneTitle)
{
_logger.Verbose("Finding open milestone with title '{Title}' on '{Owner}/{Repository}'", milestoneTitle, owner, repository);
var milestoneClient = _gitHubClient.Issue.Milestone;
Expand Down Expand Up @@ -341,7 +341,7 @@ public async Task PublishRelease(string owner, string repository, string tagName
await _gitHubClient.Repository.Release.Edit(owner, repository, release.Id, releaseUpdate).ConfigureAwait(false);
}

public async Task CreateLabels(string owner, string repository)
public async Task CreateLabelsAsync(string owner, string repository)
{
if (_configuration.Labels.Any())
{
Expand Down
3 changes: 3 additions & 0 deletions Source/GitReleaseManager/GitReleaseManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
<PackageReference Include="YamlDotNet" Version="8.1.2" />
<PackageReference Include="AutoMapper" Version="10.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Gitea\src\Gitea\Gitea.csproj" />
</ItemGroup>
</Project>
Loading