From 88699194d33f1417454288288f8ca426094067a8 Mon Sep 17 00:00:00 2001 From: Sven Huebner Date: Thu, 21 Oct 2021 05:55:00 +0200 Subject: [PATCH] add setting for commit message prefix --commitmessageprefix --- .../CollaborationPlatform/ICommitWorder.cs | 2 +- .../Configuration/CommitSettings.cs | 7 +++++++ NuKeeper.Abstractions/Configuration/FileSettings.cs | 2 ++ .../Configuration/SettingsContainer.cs | 2 ++ NuKeeper.AzureDevOps/AzureDevOpsCommitWorder.cs | 4 ++-- NuKeeper.BitBucket/BitbucketCommitWorder.cs | 4 ++-- .../Engine/ExistingCommitFilterTest.cs | 6 +++--- NuKeeper.Tests/Engine/DefaultCommitWorderTests.cs | 12 ++++++------ .../Engine/Packages/PackageUpdaterTests.cs | 6 ++++++ NuKeeper.Tests/Engine/RepositoryUpdaterTests.cs | 6 +++++- NuKeeper/Commands/CommandBase.cs | 9 +++++++++ NuKeeper/Engine/DefaultCommitWorder.cs | 4 ++-- NuKeeper/Engine/Packages/ExistingCommitFilter.cs | 4 ++-- NuKeeper/Engine/Packages/IExistingCommitFilter.cs | 3 ++- NuKeeper/Engine/Packages/PackageUpdater.cs | 6 +++--- .../AzureDevOpsCommitWorderTests.cs | 12 ++++++------ 16 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 NuKeeper.Abstractions/Configuration/CommitSettings.cs diff --git a/NuKeeper.Abstractions/CollaborationPlatform/ICommitWorder.cs b/NuKeeper.Abstractions/CollaborationPlatform/ICommitWorder.cs index ac4c9ec85..437261878 100644 --- a/NuKeeper.Abstractions/CollaborationPlatform/ICommitWorder.cs +++ b/NuKeeper.Abstractions/CollaborationPlatform/ICommitWorder.cs @@ -7,7 +7,7 @@ public interface ICommitWorder { string MakePullRequestTitle(IReadOnlyCollection updates); - string MakeCommitMessage(PackageUpdateSet updates); + string MakeCommitMessage(PackageUpdateSet updates, string commitMessagePrefix); string MakeCommitDetails(IReadOnlyCollection updates); } diff --git a/NuKeeper.Abstractions/Configuration/CommitSettings.cs b/NuKeeper.Abstractions/Configuration/CommitSettings.cs new file mode 100644 index 000000000..1eb2e89d6 --- /dev/null +++ b/NuKeeper.Abstractions/Configuration/CommitSettings.cs @@ -0,0 +1,7 @@ +namespace NuKeeper.Abstractions.Configuration +{ + public class CommitSettings + { + public string CommitMessagePrefix { get; set; } + } +} diff --git a/NuKeeper.Abstractions/Configuration/FileSettings.cs b/NuKeeper.Abstractions/Configuration/FileSettings.cs index d750f3ae9..9eccf54ae 100644 --- a/NuKeeper.Abstractions/Configuration/FileSettings.cs +++ b/NuKeeper.Abstractions/Configuration/FileSettings.cs @@ -42,6 +42,8 @@ public class FileSettings public string BranchNameTemplate { get; set; } public bool? DeleteBranchAfterMerge { get; set; } + public string CommitMessagePrefix { get; set; } + public string GitCliPath { get; set; } public int? MaxOpenPullRequests { get; set; } diff --git a/NuKeeper.Abstractions/Configuration/SettingsContainer.cs b/NuKeeper.Abstractions/Configuration/SettingsContainer.cs index ebd444e92..3a7cadb56 100644 --- a/NuKeeper.Abstractions/Configuration/SettingsContainer.cs +++ b/NuKeeper.Abstractions/Configuration/SettingsContainer.cs @@ -12,6 +12,8 @@ public class SettingsContainer public BranchSettings BranchSettings { get; set; } + public CommitSettings CommitSettings { get; set; } + public IFolder WorkingFolder { get; set; } } } diff --git a/NuKeeper.AzureDevOps/AzureDevOpsCommitWorder.cs b/NuKeeper.AzureDevOps/AzureDevOpsCommitWorder.cs index 502b08ad5..03ff419b0 100644 --- a/NuKeeper.AzureDevOps/AzureDevOpsCommitWorder.cs +++ b/NuKeeper.AzureDevOps/AzureDevOpsCommitWorder.cs @@ -38,14 +38,14 @@ private static string PackageTitle(PackageUpdateSet updates) return $"{CommitEmoji} Automatic update of {updates.SelectedId} to {updates.SelectedVersion}"; } - public string MakeCommitMessage(PackageUpdateSet updates) + public string MakeCommitMessage(PackageUpdateSet updates, string commitMessagePrefix) { if (updates == null) { throw new ArgumentNullException(nameof(updates)); } - return $"{PackageTitle(updates)}"; + return $"{commitMessagePrefix}{PackageTitle(updates)}"; } public string MakeCommitDetails(IReadOnlyCollection updates) diff --git a/NuKeeper.BitBucket/BitbucketCommitWorder.cs b/NuKeeper.BitBucket/BitbucketCommitWorder.cs index 3463f7dd9..8466e845b 100644 --- a/NuKeeper.BitBucket/BitbucketCommitWorder.cs +++ b/NuKeeper.BitBucket/BitbucketCommitWorder.cs @@ -34,14 +34,14 @@ private static string PackageTitle(PackageUpdateSet updates) return $"Automatic update of {updates.SelectedId} to {updates.SelectedVersion}"; } - public string MakeCommitMessage(PackageUpdateSet updates) + public string MakeCommitMessage(PackageUpdateSet updates, string commitMessagePrefix) { if (updates == null) { throw new ArgumentNullException(nameof(updates)); } - return $":{CommitEmoji}: {PackageTitle(updates)}"; + return $"{commitMessagePrefix}:{CommitEmoji}: {PackageTitle(updates)}"; } public string MakeCommitDetails(IReadOnlyCollection updates) diff --git a/NuKeeper.Integration.Tests/Engine/ExistingCommitFilterTest.cs b/NuKeeper.Integration.Tests/Engine/ExistingCommitFilterTest.cs index 7cc5e2e5a..508cf7487 100644 --- a/NuKeeper.Integration.Tests/Engine/ExistingCommitFilterTest.cs +++ b/NuKeeper.Integration.Tests/Engine/ExistingCommitFilterTest.cs @@ -38,7 +38,7 @@ public async Task DoFilter() var subject = MakeExistingCommitFilter(); - var result = await subject.Filter(git, updates.AsReadOnly(), "base", "head"); + var result = await subject.Filter(git, updates.AsReadOnly(), "base", "head", string.Empty); Assert.AreEqual(1, result.Count); Assert.AreEqual("First.Nuget", result.FirstOrDefault()?.SelectedId); @@ -64,7 +64,7 @@ public async Task DoNotFilter() var subject = MakeExistingCommitFilter(); - var result = await subject.Filter(git, updates.AsReadOnly(), "base", "head"); + var result = await subject.Filter(git, updates.AsReadOnly(), "base", "head", string.Empty); Assert.AreEqual(2, result.Count); } @@ -77,7 +77,7 @@ private IExistingCommitFilter MakeExistingCommitFilter() collaborationFactory.CollaborationPlatform.Returns(gitClient); var commitWorder = Substitute.For(); - commitWorder.MakeCommitMessage(Arg.Any()).Returns(p => $"Automatic update of {((PackageUpdateSet)p[0]).SelectedId} to {((PackageUpdateSet)p[0]).SelectedVersion}"); + commitWorder.MakeCommitMessage(Arg.Any(), Arg.Any()).Returns(p => $"Automatic update of {((PackageUpdateSet)p[0]).SelectedId} to {((PackageUpdateSet)p[0]).SelectedVersion}"); collaborationFactory.CommitWorder.Returns(commitWorder); return new ExistingCommitFilter(collaborationFactory, NukeeperLogger); diff --git a/NuKeeper.Tests/Engine/DefaultCommitWorderTests.cs b/NuKeeper.Tests/Engine/DefaultCommitWorderTests.cs index 52e53da48..06f02dc20 100644 --- a/NuKeeper.Tests/Engine/DefaultCommitWorderTests.cs +++ b/NuKeeper.Tests/Engine/DefaultCommitWorderTests.cs @@ -39,11 +39,11 @@ public void MakeCommitMessage_OneUpdateIsCorrect() { var updates = PackageUpdates.For(MakePackageForV110()); - var report = _sut.MakeCommitMessage(updates); + var report = _sut.MakeCommitMessage(updates, "someprefix: "); Assert.That(report, Is.Not.Null); Assert.That(report, Is.Not.Empty); - Assert.That(report, Is.EqualTo(":package: Automatic update of foo.bar to 1.2.3")); + Assert.That(report, Is.EqualTo("someprefix: :package: Automatic update of foo.bar to 1.2.3")); } [Test] @@ -51,11 +51,11 @@ public void MakeCommitMessage_TwoUpdatesIsCorrect() { var updates = PackageUpdates.For(MakePackageForV110(), MakePackageForV100()); - var report = _sut.MakeCommitMessage(updates); + var report = _sut.MakeCommitMessage(updates, "someprefix: "); Assert.That(report, Is.Not.Null); Assert.That(report, Is.Not.Empty); - Assert.That(report, Is.EqualTo(":package: Automatic update of foo.bar to 1.2.3")); + Assert.That(report, Is.EqualTo("someprefix: :package: Automatic update of foo.bar to 1.2.3")); } [Test] @@ -63,11 +63,11 @@ public void MakeCommitMessage_TwoUpdatesSameVersionIsCorrect() { var updates = PackageUpdates.For(MakePackageForV110(), MakePackageForV110InProject3()); - var report = _sut.MakeCommitMessage(updates); + var report = _sut.MakeCommitMessage(updates, "someprefix: "); Assert.That(report, Is.Not.Null); Assert.That(report, Is.Not.Empty); - Assert.That(report, Is.EqualTo(":package: Automatic update of foo.bar to 1.2.3")); + Assert.That(report, Is.EqualTo("someprefix: :package: Automatic update of foo.bar to 1.2.3")); } diff --git a/NuKeeper.Tests/Engine/Packages/PackageUpdaterTests.cs b/NuKeeper.Tests/Engine/Packages/PackageUpdaterTests.cs index d56f621ef..f0d4fe650 100644 --- a/NuKeeper.Tests/Engine/Packages/PackageUpdaterTests.cs +++ b/NuKeeper.Tests/Engine/Packages/PackageUpdaterTests.cs @@ -39,6 +39,7 @@ public void Initialize() Arg.Any(), Arg.Any>(), Arg.Any(), + Arg.Any(), Arg.Any() ) .Returns(ci => ((IReadOnlyCollection)ci[1])); @@ -115,6 +116,7 @@ public async Task MakeUpdatePullRequest_UpdateDoesNotCreatePrDueToExistingCommit Arg.Any(), Arg.Any>(), Arg.Any(), + Arg.Any(), Arg.Any() ) .Returns(new List(), new List { packageSetTwo }); @@ -245,6 +247,10 @@ private static SettingsContainer MakeSettings( { MaxPackageUpdates = 3, MinimumAge = new TimeSpan(7, 0, 0, 0), + }, + CommitSettings = new CommitSettings + { + CommitMessagePrefix = string.Empty } }; } diff --git a/NuKeeper.Tests/Engine/RepositoryUpdaterTests.cs b/NuKeeper.Tests/Engine/RepositoryUpdaterTests.cs index 746615626..d3bf86e79 100644 --- a/NuKeeper.Tests/Engine/RepositoryUpdaterTests.cs +++ b/NuKeeper.Tests/Engine/RepositoryUpdaterTests.cs @@ -152,7 +152,7 @@ int expectedPrs var filteredUpdates = updates.Skip(existingCommitsPerBranch).ToList().AsReadOnly(); - existingCommitFilder.Filter(Arg.Any(), Arg.Any>(), Arg.Any(), Arg.Any()).Returns(filteredUpdates); + existingCommitFilder.Filter(Arg.Any(), Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(filteredUpdates); var settings = MakeSettings(consolidateUpdates); @@ -358,6 +358,10 @@ private static SettingsContainer MakeSettings( { MaxPackageUpdates = 3, MinimumAge = new TimeSpan(7, 0, 0, 0), + }, + CommitSettings = new CommitSettings + { + CommitMessagePrefix = string.Empty } }; } diff --git a/NuKeeper/Commands/CommandBase.cs b/NuKeeper/Commands/CommandBase.cs index cc79836df..321efbf19 100644 --- a/NuKeeper/Commands/CommandBase.cs +++ b/NuKeeper/Commands/CommandBase.cs @@ -81,6 +81,10 @@ internal abstract class CommandBase Description = "Template used for creating the branch name.")] public string BranchNameTemplate { get; set; } + [Option(CommandOptionType.SingleValue, ShortName = "", LongName = "commitmessageprefix", + Description = "Prefix used for the package update commit messages, e. g. an issue number")] + public string CommitMessagePrefix { get; set; } + [Option(CommandOptionType.SingleValue, ShortName = "git", LongName = "gitclipath", Description = "Path to git to use instead of lib2gitsharp implementation")] public string GitCliPath { get; set; } @@ -131,6 +135,7 @@ private SettingsContainer MakeSettings() var allowedChange = Concat.FirstValue(AllowedChange, fileSettings.Change, VersionChange.Major); var usePrerelease = Concat.FirstValue(UsePrerelease, fileSettings.UsePrerelease, Abstractions.Configuration.UsePrerelease.FromPrerelease); var branchNameTemplate = Concat.FirstValue(BranchNameTemplate, fileSettings.BranchNameTemplate); + var commitMessagePrefix = Concat.FirstValue(CommitMessagePrefix, fileSettings.CommitMessagePrefix); var gitpath = Concat.FirstValue(GitCliPath, fileSettings.GitCliPath); var settings = new SettingsContainer @@ -147,6 +152,10 @@ private SettingsContainer MakeSettings() BranchSettings = new BranchSettings { BranchNameTemplate = branchNameTemplate + }, + CommitSettings = new CommitSettings + { + CommitMessagePrefix = commitMessagePrefix } }; diff --git a/NuKeeper/Engine/DefaultCommitWorder.cs b/NuKeeper/Engine/DefaultCommitWorder.cs index c67d251b2..d7e652fea 100644 --- a/NuKeeper/Engine/DefaultCommitWorder.cs +++ b/NuKeeper/Engine/DefaultCommitWorder.cs @@ -34,14 +34,14 @@ private static string PackageTitle(PackageUpdateSet updates) return $"Automatic update of {updates.SelectedId} to {updates.SelectedVersion}"; } - public string MakeCommitMessage(PackageUpdateSet updates) + public string MakeCommitMessage(PackageUpdateSet updates, string commitMessagePrefix) { if (updates == null) { throw new ArgumentNullException(nameof(updates)); } - return $":{CommitEmoji}: {PackageTitle(updates)}"; + return $"{commitMessagePrefix}:{CommitEmoji}: {PackageTitle(updates)}"; } public string MakeCommitDetails(IReadOnlyCollection updates) diff --git a/NuKeeper/Engine/Packages/ExistingCommitFilter.cs b/NuKeeper/Engine/Packages/ExistingCommitFilter.cs index 633ece708..73cb1260e 100644 --- a/NuKeeper/Engine/Packages/ExistingCommitFilter.cs +++ b/NuKeeper/Engine/Packages/ExistingCommitFilter.cs @@ -20,7 +20,7 @@ public ExistingCommitFilter(ICollaborationFactory collaborationFactory, INuKeepe _logger = logger; } - public async Task> Filter(IGitDriver git, IReadOnlyCollection updates, string baseBranch, string headBranch) + public async Task> Filter(IGitDriver git, IReadOnlyCollection updates, string baseBranch, string headBranch, string commitMessagePrefix) { if (git == null) { @@ -41,7 +41,7 @@ public async Task> Filter(IGitDriver git, foreach (var update in updates) { - var updateCommitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(update); + var updateCommitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(update, commitMessagePrefix); var compactUpdateCommitMessage = new string(updateCommitMessage.Where(c => !char.IsWhiteSpace(c)).ToArray()); if (!compactCommitMessages.Contains(compactUpdateCommitMessage)) diff --git a/NuKeeper/Engine/Packages/IExistingCommitFilter.cs b/NuKeeper/Engine/Packages/IExistingCommitFilter.cs index be07dd23a..90ead3411 100644 --- a/NuKeeper/Engine/Packages/IExistingCommitFilter.cs +++ b/NuKeeper/Engine/Packages/IExistingCommitFilter.cs @@ -11,6 +11,7 @@ Task> Filter( IGitDriver git, IReadOnlyCollection updates, string baseBranch, - string headBranch); + string headBranch, + string commitMessagePrefix); } } diff --git a/NuKeeper/Engine/Packages/PackageUpdater.cs b/NuKeeper/Engine/Packages/PackageUpdater.cs index 2f18eb556..7ba619216 100644 --- a/NuKeeper/Engine/Packages/PackageUpdater.cs +++ b/NuKeeper/Engine/Packages/PackageUpdater.cs @@ -112,18 +112,18 @@ SettingsContainer settings await git.CheckoutRemoteToLocal(branchWithChanges); } - var filteredUpdates = await _existingCommitFilter.Filter(git, updates, repository.DefaultBranch, branchWithChanges); + var filteredUpdates = await _existingCommitFilter.Filter(git, updates, repository.DefaultBranch, branchWithChanges, settings.CommitSettings.CommitMessagePrefix); foreach (var filtered in updates.Where(u => !filteredUpdates.Contains(u))) { - var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(filtered); + var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(filtered, settings.CommitSettings.CommitMessagePrefix); _logger.Normal($"Commit '{commitMessage}' already in branch '{branchWithChanges}'"); } var haveUpdates = filteredUpdates.Any(); foreach (var updateSet in filteredUpdates) { - var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(updateSet); + var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(updateSet, settings.CommitSettings.CommitMessagePrefix); await _updateRunner.Update(updateSet, sources); diff --git a/Nukeeper.AzureDevOps.Tests/AzureDevOpsCommitWorderTests.cs b/Nukeeper.AzureDevOps.Tests/AzureDevOpsCommitWorderTests.cs index 171d73f7f..192c190fa 100644 --- a/Nukeeper.AzureDevOps.Tests/AzureDevOpsCommitWorderTests.cs +++ b/Nukeeper.AzureDevOps.Tests/AzureDevOpsCommitWorderTests.cs @@ -45,11 +45,11 @@ public void MakeCommitMessage_OneUpdateIsCorrect() { var updates = PackageUpdates.For(MakePackageForV110()); - var report = _sut.MakeCommitMessage(updates); + var report = _sut.MakeCommitMessage(updates, "someprefix: "); Assert.That(report, Is.Not.Null); Assert.That(report, Is.Not.Empty); - Assert.That(report, Is.EqualTo($"{CommitEmoji} Automatic update of foo.bar to 1.2.3")); + Assert.That(report, Is.EqualTo($"someprefix: {CommitEmoji} Automatic update of foo.bar to 1.2.3")); } [Test] @@ -57,11 +57,11 @@ public void MakeCommitMessage_TwoUpdatesIsCorrect() { var updates = PackageUpdates.For(MakePackageForV110(), MakePackageForV100()); - var report = _sut.MakeCommitMessage(updates); + var report = _sut.MakeCommitMessage(updates, "someprefix: "); Assert.That(report, Is.Not.Null); Assert.That(report, Is.Not.Empty); - Assert.That(report, Is.EqualTo($"{CommitEmoji} Automatic update of foo.bar to 1.2.3")); + Assert.That(report, Is.EqualTo($"someprefix: {CommitEmoji} Automatic update of foo.bar to 1.2.3")); } [Test] @@ -69,11 +69,11 @@ public void MakeCommitMessage_TwoUpdatesSameVersionIsCorrect() { var updates = PackageUpdates.For(MakePackageForV110(), MakePackageForV110InProject3()); - var report = _sut.MakeCommitMessage(updates); + var report = _sut.MakeCommitMessage(updates, "someprefix: "); Assert.That(report, Is.Not.Null); Assert.That(report, Is.Not.Empty); - Assert.That(report, Is.EqualTo($"{CommitEmoji} Automatic update of foo.bar to 1.2.3")); + Assert.That(report, Is.EqualTo($"someprefix: {CommitEmoji} Automatic update of foo.bar to 1.2.3")); } [Test]