Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Configurable auto deletion of branch after merge. (#774)
Browse files Browse the repository at this point in the history
* Delete branch after merge.

* Work in progress.

* Use configured value for Gitlab.

* Changed assignment order.

* Fixed build.

* Work in progress

* Removed obsolete test.

* Tightened tests.

* Updated docs.

* Local github repo support (#771) (#12)

* Merge branch 'master' of https://github.com/NuKeeperDotNet/NuKeeper

* github explanation

* remove bitbucket

* update commands

* set favicon

* Add github local support

* fix comment

* update tests

* update references

* Local github repo support (#771) (#13)

* Merge branch 'master' of https://github.com/NuKeeperDotNet/NuKeeper

* github explanation

* remove bitbucket

* update commands

* set favicon

* Add github local support

* fix comment

* update tests

* update references

* Auto deletion boolean default to true.

* Include Bitbucket. Updated docs.

* Corrected merge fault.
  • Loading branch information
MaxMommersteeg authored and MarcBruins committed May 20, 2019
1 parent 981a157 commit 472f715
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class PullRequestRequestTests
[Test]
public void ReplacesRemotesWhenCreatingPullRequestRequestObject()
{
var pr = new PullRequestRequest("head", "title", "origin/master");
var pr2 = new PullRequestRequest("head", "title", "master");
var pr = new PullRequestRequest("head", "title", "origin/master", true);
var pr2 = new PullRequestRequest("head", "title", "master", true);

Assert.That(pr.BaseRef, Is.EqualTo("master"));
Assert.That(pr2.BaseRef, Is.EqualTo("master"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void MissingFileReturnsNoSettings()
Assert.That(data.LogDestination, Is.Null);
Assert.That(data.Platform, Is.Null);
Assert.That(data.BranchNamePrefix, Is.Null);
Assert.That(data.DeleteBranchAfterMerge, Is.Null);
}

[Test]
Expand Down Expand Up @@ -71,6 +72,7 @@ public void EmptyConfigReturnsNoSettings()
Assert.That(data.LogDestination, Is.Null);
Assert.That(data.Platform, Is.Null);
Assert.That(data.BranchNamePrefix, Is.Null);
Assert.That(data.DeleteBranchAfterMerge, Is.Null);
}

private const string FullFileData = @"{
Expand All @@ -93,7 +95,8 @@ public void EmptyConfigReturnsNoSettings()
""OutputDestination"": ""Console"",
""OutputFileName"" : ""out_42.txt"",
""LogDestination"" : ""file"",
""Platform"" : ""Bitbucket""
""Platform"" : ""Bitbucket"",
""DeleteBranchAfterMerge"": ""true""
}";

[Test]
Expand All @@ -115,6 +118,7 @@ public void PopulatedConfigReturnsAllStringSettings()
Assert.That(data.LogFile, Is.EqualTo("somefile.log"));
Assert.That(data.OutputFileName, Is.EqualTo("out_42.txt"));
Assert.That(data.BranchNamePrefix, Is.EqualTo("nukeeper"));
Assert.That(data.DeleteBranchAfterMerge, Is.EqualTo(true));
}

[Test]
Expand Down Expand Up @@ -179,7 +183,8 @@ public void ConfigKeysAreCaseInsensitive()
""MAXrepo"":3,
""vErBoSiTy"": ""Q"",
""CHANGE"": ""PATCH"",
""bRanCHNamEPREfiX"": ""nukeeper""
""bRanCHNamEPREfiX"": ""nukeeper"",
""deLeTEBranCHafTERMerge"": ""true""
}";

var path = MakeTestFile(configData);
Expand All @@ -201,6 +206,7 @@ public void ConfigKeysAreCaseInsensitive()
Assert.That(data.Verbosity, Is.EqualTo(LogLevel.Quiet));
Assert.That(data.Change, Is.EqualTo(VersionChange.Patch));
Assert.That(data.BranchNamePrefix, Is.EqualTo("nukeeper"));
Assert.That(data.DeleteBranchAfterMerge, Is.EqualTo(true));
}

[Test]
Expand All @@ -223,7 +229,6 @@ public void ExtraKeysAreIgnored()
Assert.That(data.Api, Is.EqualTo("http://api.com"));
}


private static string MakeTestFile(string contents)
{
var folder = TemporaryFolder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ namespace NuKeeper.Abstractions.CollaborationModels
{
public class PullRequestRequest
{
public PullRequestRequest(string head, string title, string baseRef)
public PullRequestRequest(string head, string title, string baseRef, bool deleteBranchAfterMerge)
{
Head = head;
Title = title;
DeleteBranchAfterMerge = deleteBranchAfterMerge;

//This can be a remote that has been passed in, this happens when run locally against a targetbranch that is remote
BaseRef = baseRef.Replace("origin/", string.Empty);
Expand All @@ -15,5 +16,6 @@ public PullRequestRequest(string head, string title, string baseRef)
public string Title { get; }
public string BaseRef { get; }
public string Body { get; set; }
public bool DeleteBranchAfterMerge { get; set; }
}
}
2 changes: 2 additions & 0 deletions NuKeeper.Abstractions/Configuration/BranchSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ namespace NuKeeper.Abstractions.Configuration
public class BranchSettings
{
public string BranchNamePrefix { get; set; }

public bool DeleteBranchAfterMerge { get; set; }
}
}
1 change: 1 addition & 0 deletions NuKeeper.Abstractions/Configuration/FileSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class FileSettings
public Platform? Platform { get; set; }

public string BranchNamePrefix { get; set; }
public bool? DeleteBranchAfterMerge { get; set; }

public static FileSettings Empty()
{
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.AzureDevOps/AzureDevopsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task OpenPullRequest(ForkData target, PullRequestRequest request, I
targetRefName = $"refs/heads/{request.BaseRef}",
completionOptions = new GitPullRequestCompletionOptions
{
deleteSourceBranch = true
deleteSourceBranch = request.DeleteBranchAfterMerge
}
};

Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.BitBucket/BitbucketPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task OpenPullRequest(ForkData target, PullRequestRequest request, I
}
},
description = request.Body,
close_source_branch = true
close_source_branch = request.DeleteBranchAfterMerge
};

await _client.CreatePullRequest(req, target.Owner, repo.name);
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.Gitlab/GitlabPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task OpenPullRequest(ForkData target, PullRequestRequest request, I
Description = request.Body,
TargetBranch = request.BaseRef,
Id = $"{projectName}/{repositoryName}",
RemoveSourceBranch = true
RemoveSourceBranch = request.DeleteBranchAfterMerge
};

await _client.OpenMergeRequest(projectName, repositoryName, mergeRequest);
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.Tests/Commands/GlobalCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public async Task EmptyFileResultsInDefaultSettings()
Assert.That(settings.UserSettings.OutputFormat, Is.EqualTo(OutputFormat.Text));

Assert.That(settings.BranchSettings.BranchNamePrefix, Is.Null);
Assert.That(settings.BranchSettings.DeleteBranchAfterMerge, Is.EqualTo(true));

Assert.That(settings.SourceControlServerSettings.Scope, Is.EqualTo(ServerScope.Global));
Assert.That(settings.SourceControlServerSettings.IncludeRepos, Is.Null);
Assert.That(settings.SourceControlServerSettings.ExcludeRepos, Is.Null);
}


[Test]
public async Task WillReadApiFromFile()
{
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper.Tests/Commands/OrganisationCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public async Task EmptyFileResultsInDefaultSettings()

Assert.That(settings.UserSettings.MaxRepositoriesChanged, Is.EqualTo(10));

Assert.That(settings.BranchSettings.DeleteBranchAfterMerge, Is.EqualTo(true));

Assert.That(settings.SourceControlServerSettings.IncludeRepos, Is.Null);
Assert.That(settings.SourceControlServerSettings.ExcludeRepos, Is.Null);
}
Expand Down
3 changes: 1 addition & 2 deletions NuKeeper.Tests/Commands/RepositoryCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NSubstitute;
using NuKeeper.Abstractions.CollaborationModels;
Expand All @@ -13,7 +12,6 @@
using NuKeeper.Collaboration;
using NuKeeper.Commands;
using NuKeeper.Engine;
using NuKeeper.Git;
using NuKeeper.GitHub;
using NuKeeper.Inspection.Files;
using NuKeeper.Inspection.Logging;
Expand Down Expand Up @@ -223,6 +221,7 @@ public async Task EmptyFileResultsInDefaultSettings()

Assert.That(settings.BranchSettings, Is.Not.Null);
Assert.That(settings.BranchSettings.BranchNamePrefix, Is.Null);
Assert.That(settings.BranchSettings.DeleteBranchAfterMerge, Is.EqualTo(true));

Assert.That(settings.SourceControlServerSettings.IncludeRepos, Is.Null);
Assert.That(settings.SourceControlServerSettings.ExcludeRepos, Is.Null);
Expand Down
33 changes: 33 additions & 0 deletions NuKeeper/Commands/CollaborationPlatformCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ internal abstract class CollaborationPlatformCommand : CommandBase
Description = "Sets the collaboration platform type. By default this is inferred from the Url.")]
public Platform? Platform { get; set; }

[Option(CommandOptionType.NoValue, ShortName = "", LongName = "deletebranchaftermerge",
Description = "Deletes branch created by NuKeeper after merge. Defaults to false.")]
public bool? DeleteBranchAfterMerge { get; set; }

protected CollaborationPlatformCommand(ICollaborationEngine engine, IConfigureLogger logger,
IFileSettingsCache fileSettingsCache, ICollaborationFactory collaborationFactory) :
base(logger, fileSettingsCache)
Expand Down Expand Up @@ -108,6 +112,12 @@ protected override ValidationResult PopulateSettings(SettingsContainer settings)
settings.SourceControlServerSettings.Labels =
Concat.FirstPopulatedList(Label, fileSettings.Label, defaultLabels);

var deleteBranchAfterMergeValid = PopulateDeleteBranchAfterMerge(settings);
if (!deleteBranchAfterMergeValid.IsSuccess)
{
return deleteBranchAfterMergeValid;
}

return ValidationResult.Success;
}

Expand All @@ -116,5 +126,28 @@ protected override async Task<int> Run(SettingsContainer settings)
await _engine.Run(settings);
return 0;
}

private ValidationResult PopulateDeleteBranchAfterMerge(
SettingsContainer settings)
{
var fileSettings = FileSettingsCache.GetSettings();

if (!Platform.HasValue)
{
settings.BranchSettings.DeleteBranchAfterMerge = true;
return ValidationResult.Success;
}

if (Platform != Abstractions.CollaborationPlatform.Platform.AzureDevOps
&& Platform != Abstractions.CollaborationPlatform.Platform.GitLab
&& Platform != Abstractions.CollaborationPlatform.Platform.Bitbucket)
{
return ValidationResult.Failure(
$"Deletion of source branch after merge is currently only available for Azure DevOps, Gitlab and Bitbucket.");
}

settings.BranchSettings.DeleteBranchAfterMerge = Concat.FirstValue(DeleteBranchAfterMerge, fileSettings.DeleteBranchAfterMerge, true);
return ValidationResult.Success;
}
}
}
2 changes: 1 addition & 1 deletion NuKeeper/Engine/Packages/PackageUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private async Task<int> MakeUpdatePullRequests(
qualifiedBranch = repository.Push.Owner + ":" + branchWithChanges;
}

var pullRequestRequest = new PullRequestRequest(qualifiedBranch, title, repository.DefaultBranch) { Body = body };
var pullRequestRequest = new PullRequestRequest(qualifiedBranch, title, repository.DefaultBranch, settings.BranchSettings.DeleteBranchAfterMerge) { Body = body };

await _collaborationFactory.CollaborationPlatform.OpenPullRequest(repository.Pull, pullRequestRequest, settings.SourceControlServerSettings.Labels);

Expand Down
4 changes: 3 additions & 1 deletion site/content/basics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ title: "Configuration"
| excluderepos | | `org`, `global` | _null_ |
| | | | |
| branchnameprefix | | `repo`, `org`, `global`, `update`| _null_ |
| deletebranchaftermerge | | _all_ | true |

* *age* The minimum package age. In order to not consume packages immediately after they are released, exclude updates that do not meet a minimum age. The default is 7 days. This age is the duration between the published date of the selected package update and now.
A value can be expressed in command options as an integer and a unit suffix,
Expand Down Expand Up @@ -68,9 +69,10 @@ Examples: `0` = zero, `12h` = 12 hours, `3d` = 3 days, `2w` = two weeks.
* *maxpackageupdates* The maximum number of package updates to apply. In `repo`,`org` and `global` commands, this limits the number of updates per repository. If the `--consolidate` flag is used, these wll be consolidated into one Pull Request. If not, then there will be one Pull Request per update applied. In the `update` command, The default value is 1. When changed, multiple updates can be applied in a single `update` run, up to this number.
* *maxrepo* The maximum number of repositories to change. Used in Organisation and Global mode.
* *consolidate* Consolidate updates into a single pull request, instead of the default of 1 pull request per package update applied.
* *platform* One of `GitHub`, `AzureDevOps`, `Bitbucket`, `BitbucketLocal`. Determines which kind of source control api will be used. This is typicaly infered from the api url structure, but since this does not always work, it can be specified here if neccessary.
* *platform* One of `GitHub`, `AzureDevOps`, `Bitbucket`, `BitbucketLocal`, `Gitlab`, `Gitea`. Determines which kind of source control api will be used. This is typicaly infered from the api url structure, but since this does not always work, it can be specified here if neccessary.

* *includerepos* A regex to filter repositories by name. Only consider repositories where the name matches this regex pattern. Used in Organisation and Global mode.
* *excluderepos* A regex to filter repositories by name. Do not consider repositories where the name matches this regex pattern. Used in Organisation and Global mode.

* *branchnameprefix* A prefix that gets added to branch name that NuKeeper creates. Allows you to put those branches in hierarchy (E.G. 'nukeeper/').
* *deletebranchaftermerge* Specifies whether a branch should be automatically deleted or not once the branch has been merged. Currently only works with `Platform` equal to `AzureDevOps`, `Gitlab` or `Bitbucket`.

0 comments on commit 472f715

Please sign in to comment.