From 01e944ece66ce7bb23a5b3e671082a5b86c7a36a Mon Sep 17 00:00:00 2001 From: Stefan Kneidl Date: Wed, 19 Feb 2020 13:28:06 +0100 Subject: [PATCH 1/2] add local folder support for gitea --- .../GiteaSettingsReaderTests.cs | 5 +- NuKeeper.Gitea/GiteaSettingsReader.cs | 87 +++++++++++++++---- .../Commands/CollaborationPlatformCommand.cs | 1 + 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/NuKeeper.Gitea.Tests/GiteaSettingsReaderTests.cs b/NuKeeper.Gitea.Tests/GiteaSettingsReaderTests.cs index 34bbe0c8a..92b1db61d 100644 --- a/NuKeeper.Gitea.Tests/GiteaSettingsReaderTests.cs +++ b/NuKeeper.Gitea.Tests/GiteaSettingsReaderTests.cs @@ -3,6 +3,7 @@ using NSubstitute; using NuKeeper.Abstractions.CollaborationPlatform; using NuKeeper.Abstractions.Configuration; +using NuKeeper.Abstractions.Git; using NuKeeper.Gitea; using NUnit.Framework; @@ -13,12 +14,14 @@ public class GiteaSettingsReaderTests { private GiteaSettingsReader _giteaSettingsReader; private IEnvironmentVariablesProvider _environmentVariablesProvider; + private IGitDiscoveryDriver _gitDiscovery; [SetUp] public void Setup() { _environmentVariablesProvider = Substitute.For(); - _giteaSettingsReader = new GiteaSettingsReader(_environmentVariablesProvider); + _gitDiscovery = Substitute.For(); + _giteaSettingsReader = new GiteaSettingsReader(_gitDiscovery, _environmentVariablesProvider); } [Test] diff --git a/NuKeeper.Gitea/GiteaSettingsReader.cs b/NuKeeper.Gitea/GiteaSettingsReader.cs index b07dc978e..7f7c8cbc0 100644 --- a/NuKeeper.Gitea/GiteaSettingsReader.cs +++ b/NuKeeper.Gitea/GiteaSettingsReader.cs @@ -6,6 +6,7 @@ using NuKeeper.Abstractions; using NuKeeper.Abstractions.CollaborationPlatform; using NuKeeper.Abstractions.Configuration; +using NuKeeper.Abstractions.Git; namespace NuKeeper.Gitea { @@ -15,10 +16,12 @@ public class GiteaSettingsReader : ISettingsReader private const string GiteaTokenEnvironmentVariableName = "NuKeeper_gitea_token"; private const string UrlPattern = "https://yourgiteaserver/{owner}/{repo}.git"; private const string ApiBaseAdress = "api/v1/"; + private readonly IGitDiscoveryDriver _gitDriver; - public GiteaSettingsReader(IEnvironmentVariablesProvider environmentVariablesProvider) + public GiteaSettingsReader(IGitDiscoveryDriver gitDiscoveryDriver, IEnvironmentVariablesProvider environmentVariablesProvider) { _environmentVariablesProvider = environmentVariablesProvider; + _gitDriver = gitDiscoveryDriver; } public Platform Platform => Platform.Gitea; @@ -60,7 +63,7 @@ public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings se settings.Token = Concat.FirstValue(envToken, settings.Token); } - public Task RepositorySettings(Uri repositoryUri, string targetBranch = null) + public async Task RepositorySettings(Uri repositoryUri, string targetBranch = null) { if (repositoryUri == null) { @@ -68,6 +71,24 @@ public Task RepositorySettings(Uri repositoryUri, string tar $"The provided uri was is not in the correct format. Provided null and format should be {UrlPattern}"); } + var settings = repositoryUri.IsFile + ? await CreateSettingsFromLocal(repositoryUri, targetBranch) + : await CreateSettingsFromRemote(repositoryUri, targetBranch); + + return settings; + } + + private static Uri GetBaseAddress(Uri repoUri) + { + var newSegments = repoUri.Segments.Take(repoUri.Segments.Length - 2).ToArray(); + var ub = new UriBuilder(repoUri); + ub.Path = string.Concat(newSegments); + + return ub.Uri; + } + + private static Task CreateSettingsFromRemote(Uri repositoryUri, string targetBranch) + { // Assumption - url should look like https://yourgiteaUrl/{username}/{projectname}.git"; var path = repositoryUri.AbsolutePath; var pathParts = path.Split('/') @@ -87,27 +108,61 @@ public Task RepositorySettings(Uri repositoryUri, string tar var baseAddress = GetBaseAddress(repositoryUri); var apiUri = new Uri(baseAddress, ApiBaseAdress); + return InternalCreateRepositorySettings(apiUri, repositoryUri, repoName, repoOwner, targetBranch == null ? null : new RemoteInfo { BranchName = targetBranch }); + } + + private async Task CreateSettingsFromLocal(Uri repositoryUri, string targetBranch) + { + var remoteInfo = new RemoteInfo(); + + var localFolder = repositoryUri; + if (await _gitDriver.IsGitRepo(repositoryUri)) + { + // Check the origin remotes + var origin = (await _gitDriver.GetRemotes(repositoryUri)).FirstOrDefault(); + + if (origin != null) + { + var repo = await _gitDriver.DiscoverRepo(repositoryUri); // Set to the folder, because we found a remote git repository + if (repo != null && (repo.Segments.Last() == @".git/")) + { + var newSegments = repo.Segments.Take(repo.Segments.Length - 1).ToArray(); + newSegments[newSegments.Length - 1] = + newSegments[newSegments.Length - 1].TrimEnd('/'); + var ub = new UriBuilder(repo); + ub.Path = string.Concat(newSegments); + //ub.Query=string.Empty; //maybe? + repo = ub.Uri; + } + + remoteInfo.LocalRepositoryUri = repo; + repositoryUri = origin.Url; + remoteInfo.BranchName = targetBranch ?? await _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri); + remoteInfo.RemoteName = origin.Name; + remoteInfo.WorkingFolder = localFolder; + } + } + else + { + throw new NuKeeperException("No git repository found"); + } + + var remoteSettings = await CreateSettingsFromRemote(repositoryUri, targetBranch); + + + return await InternalCreateRepositorySettings(remoteSettings.ApiUri, remoteSettings.RepositoryUri, remoteSettings.RepositoryName, remoteSettings.RepositoryOwner, remoteInfo); + } + + private static Task InternalCreateRepositorySettings(Uri apiUri, Uri repositoryUri, string repoName, string repoOwner, RemoteInfo remoteInfo = null) + { return Task.FromResult(new RepositorySettings { ApiUri = apiUri, RepositoryUri = repositoryUri, RepositoryName = repoName, RepositoryOwner = repoOwner, - RemoteInfo = targetBranch == null - ? null - : new RemoteInfo { BranchName = targetBranch } + RemoteInfo = remoteInfo }); } - - private static Uri GetBaseAddress(Uri repoUri) - { - var newSegments = repoUri.Segments.Take(repoUri.Segments.Length - 2).ToArray(); - var ub = new UriBuilder(repoUri) - { - Path = string.Concat(newSegments) - }; - - return ub.Uri; - } } } diff --git a/NuKeeper/Commands/CollaborationPlatformCommand.cs b/NuKeeper/Commands/CollaborationPlatformCommand.cs index 286cf0457..2af4e9198 100644 --- a/NuKeeper/Commands/CollaborationPlatformCommand.cs +++ b/NuKeeper/Commands/CollaborationPlatformCommand.cs @@ -61,6 +61,7 @@ internal abstract class CollaborationPlatformCommand : CommandBase _platformsSupportingDeleteBranchAfterMerge.Add(Abstractions.CollaborationPlatform.Platform.AzureDevOps); _platformsSupportingDeleteBranchAfterMerge.Add(Abstractions.CollaborationPlatform.Platform.Bitbucket); _platformsSupportingDeleteBranchAfterMerge.Add(Abstractions.CollaborationPlatform.Platform.GitLab); + _platformsSupportingDeleteBranchAfterMerge.Add(Abstractions.CollaborationPlatform.Platform.Gitea); } protected override async Task PopulateSettings(SettingsContainer settings) From 71929feaed65886fd2517eec4e361dccd41dfee2 Mon Sep 17 00:00:00 2001 From: sharpSteff Date: Wed, 13 May 2020 13:25:23 +0200 Subject: [PATCH 2/2] minor cleanup --- NuKeeper.Gitea/GiteaSettingsReader.cs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/NuKeeper.Gitea/GiteaSettingsReader.cs b/NuKeeper.Gitea/GiteaSettingsReader.cs index 7f7c8cbc0..4cf1ba4ed 100644 --- a/NuKeeper.Gitea/GiteaSettingsReader.cs +++ b/NuKeeper.Gitea/GiteaSettingsReader.cs @@ -78,15 +78,6 @@ public async Task RepositorySettings(Uri repositoryUri, stri return settings; } - private static Uri GetBaseAddress(Uri repoUri) - { - var newSegments = repoUri.Segments.Take(repoUri.Segments.Length - 2).ToArray(); - var ub = new UriBuilder(repoUri); - ub.Path = string.Concat(newSegments); - - return ub.Uri; - } - private static Task CreateSettingsFromRemote(Uri repositoryUri, string targetBranch) { // Assumption - url should look like https://yourgiteaUrl/{username}/{projectname}.git"; @@ -164,5 +155,16 @@ private static Task InternalCreateRepositorySettings(Uri api RemoteInfo = remoteInfo }); } + + private static Uri GetBaseAddress(Uri repoUri) + { + var newSegments = repoUri.Segments.Take(repoUri.Segments.Length - 2).ToArray(); + var ub = new UriBuilder(repoUri) + { + Path = string.Concat(newSegments) + }; + + return ub.Uri; + } } }