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

Commit

Permalink
Platform specific commit wording. (#665)
Browse files Browse the repository at this point in the history
* Work in progress on platform specific CommitWording.

* Work in progress on Commitwordings.

* Updated tests.

* Work in progress on CommitWording.

* Use ICommitWorder on CollaborationFactory.

* Remove old file.

* Cloned PackageUpdateSet.

* Strip cloned classes of excessive functionality.

* Made start on AzureDevOpsCommitWorders.cs.

* Strip end of max. size was exceeded.

* Minor changes.

* Stripped poco's of unnecessary functionality.

* Revert "Stripped poco's of unnecessary functionality."

This reverts commit d0d46f8.

* Moved classes to Abstractions project. Fixed tests.

* Added unittest.
  • Loading branch information
MaxMommersteeg authored and MarcBruins committed Apr 18, 2019
1 parent 6a6e63c commit bc357aa
Show file tree
Hide file tree
Showing 115 changed files with 814 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface ICollaborationFactory
ValidationResult Initialise(Uri apiUri, string token,
ForkMode? forkModeFromSettings, Platform? platformFromSettings);

ICommitWorder CommitWorder { get; }
CollaborationPlatformSettings Settings { get; }
IForkFinder ForkFinder { get; }
IRepositoryDiscovery RepositoryDiscovery { get; }
Expand Down
14 changes: 14 additions & 0 deletions NuKeeper.Abstractions/CollaborationPlatform/ICommitWorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NuKeeper.Abstractions.RepositoryInspection;
using System.Collections.Generic;

namespace NuKeeper.Abstractions.CollaborationPlatform
{
public interface ICommitWorder
{
string MakePullRequestTitle(IReadOnlyCollection<PackageUpdateSet> updates);

string MakeCommitMessage(PackageUpdateSet updates);

string MakeCommitDetails(IReadOnlyCollection<PackageUpdateSet> updates);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using NuKeeper.Abstractions.Configuration;

namespace NuKeeper.Inspection.NuGetApi
namespace NuKeeper.Abstractions.NuGetApi
{
public class PackageLookupResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using NuGet.Packaging.Core;
using NuKeeper.Abstractions.Formats;

namespace NuKeeper.Inspection.NuGetApi
namespace NuKeeper.Abstractions.NuGetApi
{
public class PackageSearchMedatadata
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using NuGet.Versioning;
using NuKeeper.Abstractions.NuGet;

namespace NuKeeper.Inspection.RepositoryInspection
namespace NuKeeper.Abstractions.RepositoryInspection
{
public class PackageInProject
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace NuKeeper.Inspection.RepositoryInspection
namespace NuKeeper.Abstractions.RepositoryInspection
{
public class PackagePath
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace NuKeeper.Inspection.RepositoryInspection
namespace NuKeeper.Abstractions.RepositoryInspection
{
public enum PackageReferenceType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using NuGet.Versioning;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Formats;
using NuKeeper.Inspection.NuGetApi;
using NuKeeper.Abstractions.NuGetApi;

namespace NuKeeper.Inspection.RepositoryInspection
namespace NuKeeper.Abstractions.RepositoryInspection
{
public class PackageUpdateSet
{
Expand Down
244 changes: 244 additions & 0 deletions NuKeeper.AzureDevOps/AzureDevOpsCommitWorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using NuKeeper.Abstractions.CollaborationPlatform;
using NuKeeper.Abstractions.Formats;
using NuKeeper.Abstractions.RepositoryInspection;

namespace NuKeeper.AzureDevOps
{
public class AzureDevOpsCommitWorder : ICommitWorder
{
// Azure DevOps allows a maximum of 4000 characters to be used in a pull request description:
// https://visualstudio.uservoice.com/forums/330519-azure-devops-formerly-visual-studio-team-services/suggestions/20217283-raise-the-character-limit-for-pull-request-descrip
private const int MaxCharacterCount = 4000;

public string MakePullRequestTitle(IReadOnlyCollection<PackageUpdateSet> updates)
{
if (updates.Count == 1)
{
return PackageTitle(updates.First());
}

return $"Automatic update of {updates.Count} packages";
}

private static string PackageTitle(PackageUpdateSet updates)
{
return $"Automatic update of {updates.SelectedId} to {updates.SelectedVersion}";
}

public string MakeCommitMessage(PackageUpdateSet updates)
{
return $"{PackageTitle(updates)}";
}

public string MakeCommitDetails(IReadOnlyCollection<PackageUpdateSet> updates)
{
var builder = new StringBuilder();

if (updates.Count > 1)
{
MultiPackagePrefix(updates, builder);
}

foreach (var update in updates)
{
builder.AppendLine(MakeCommitVersionDetails(update));
}

if (updates.Count > 1)
{
MultiPackageFooter(builder);
}

AddCommitFooter(builder);

if (builder.Length > MaxCharacterCount)
{
// Strip end of commit details since Azure DevOps can't handle a bigger pull request description.
return $"{builder.ToString(0, MaxCharacterCount - 3)}...";
}

return builder.ToString();
}

private static void MultiPackagePrefix(IReadOnlyCollection<PackageUpdateSet> updates, StringBuilder builder)
{
var packageNames = updates
.Select(p => CodeQuote(p.SelectedId))
.JoinWithCommas();

var projects = updates.SelectMany(
u => u.CurrentPackages)
.Select(p => p.Path.FullName)
.Distinct()
.ToList();

var projectOptS = (projects.Count > 1) ? "s" : string.Empty;

builder.AppendLine($"{updates.Count} packages were updated in {projects.Count} project{projectOptS}:");
builder.AppendLine(packageNames);
builder.AppendLine("<details>");
builder.AppendLine("<summary>Details of updated packages</summary>");
builder.AppendLine("");
}

private static void MultiPackageFooter(StringBuilder builder)
{
builder.AppendLine("</details>");
builder.AppendLine("");
}

private static string MakeCommitVersionDetails(PackageUpdateSet updates)
{
var versionsInUse = updates.CurrentPackages
.Select(u => u.Version)
.Distinct()
.ToList();

var oldVersions = versionsInUse
.Select(v => CodeQuote(v.ToString()))
.ToList();

var minOldVersion = versionsInUse.Min();

var newVersion = CodeQuote(updates.SelectedVersion.ToString());
var packageId = CodeQuote(updates.SelectedId);

var changeLevel = ChangeLevel(minOldVersion, updates.SelectedVersion);

var builder = new StringBuilder();

if (oldVersions.Count == 1)
{
builder.AppendLine($"NuKeeper has generated a {changeLevel} update of {packageId} to {newVersion} from {oldVersions.JoinWithCommas()}");
}
else
{
builder.AppendLine($"NuKeeper has generated a {changeLevel} update of {packageId} to {newVersion}");
builder.AppendLine($"{oldVersions.Count} versions of {packageId} were found in use: {oldVersions.JoinWithCommas()}");
}

if (updates.Selected.Published.HasValue)
{
var packageWithVersion = CodeQuote(updates.SelectedId + " " + updates.SelectedVersion);
var pubDateString = CodeQuote(DateFormat.AsUtcIso8601(updates.Selected.Published));
var pubDate = updates.Selected.Published.Value.UtcDateTime;
var ago = TimeSpanFormat.Ago(pubDate, DateTime.UtcNow);

builder.AppendLine($"{packageWithVersion} was published at {pubDateString}, {ago}");
}

var highestVersion = updates.Packages.Major?.Identity.Version;
if (highestVersion != null && (highestVersion > updates.SelectedVersion))
{
LogHighestVersion(updates, highestVersion, builder);
}

builder.AppendLine();

if (updates.CurrentPackages.Count == 1)
{
builder.AppendLine("1 project update:");
}
else
{
builder.AppendLine($"{updates.CurrentPackages.Count} project updates:");
}

foreach (var current in updates.CurrentPackages)
{
var line = $"Updated {CodeQuote(current.Path.RelativePath)} to {packageId} {CodeQuote(updates.SelectedVersion.ToString())} from {CodeQuote(current.Version.ToString())}";
builder.AppendLine(line);
}

if (SourceIsPublicNuget(updates.Selected.Source.SourceUri))
{
builder.AppendLine();
builder.AppendLine(NugetPackageLink(updates.Selected.Identity));
}

return builder.ToString();
}

private static void AddCommitFooter(StringBuilder builder)
{
builder.AppendLine();
builder.AppendLine("This is an automated update. Merge only if it passes tests");
builder.AppendLine("**NuKeeper**: https://github.com/NuKeeperDotNet/NuKeeper");
}

private static string ChangeLevel(NuGetVersion oldVersion, NuGetVersion newVersion)
{
if (newVersion.Major > oldVersion.Major)
{
return "major";
}

if (newVersion.Minor > oldVersion.Minor)
{
return "minor";
}

if (newVersion.Patch > oldVersion.Patch)
{
return "patch";
}

if (!newVersion.IsPrerelease && oldVersion.IsPrerelease)
{
return "out of beta";
}

return string.Empty;
}

private static void LogHighestVersion(PackageUpdateSet updates, NuGetVersion highestVersion, StringBuilder builder)
{
var allowedChange = CodeQuote(updates.AllowedChange.ToString());
var highest = CodeQuote(updates.SelectedId + " " + highestVersion);

var highestPublishedAt = HighestPublishedAt(updates.Packages.Major.Published);

builder.AppendLine(
$"There is also a higher version, {highest}{highestPublishedAt}, " +
$"but this was not applied as only {allowedChange} version changes are allowed.");
}

private static string HighestPublishedAt(DateTimeOffset? highestPublishedAt)
{
if (!highestPublishedAt.HasValue)
{
return string.Empty;
}

var highestPubDate = highestPublishedAt.Value;
var formattedPubDate = CodeQuote(DateFormat.AsUtcIso8601(highestPubDate));
var highestAgo = TimeSpanFormat.Ago(highestPubDate.UtcDateTime, DateTime.UtcNow);

return $" published at {formattedPubDate}, {highestAgo}";
}

private static string CodeQuote(string value)
{
return "`" + value + "`";
}

private static bool SourceIsPublicNuget(Uri sourceUrl)
{
return
sourceUrl != null &&
sourceUrl.ToString().StartsWith("https://api.nuget.org/", StringComparison.OrdinalIgnoreCase);
}

private static string NugetPackageLink(PackageIdentity package)
{
var url = $"https://www.nuget.org/packages/{package.Id}/{package.Version}";
return $"[{package.Id} {package.Version} on NuGet.org]({url})";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NuGet.Versioning;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.NuGetApi;
using NuKeeper.Inspection.NuGetApi;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Logging;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.NuGetApi;
using NuKeeper.Inspection.NuGetApi;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NuGet.Versioning;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Logging;
using NuKeeper.Abstractions.NuGetApi;
using NuKeeper.Inspection.NuGetApi;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using NuGet.Packaging.Core;
using NuGet.Versioning;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Inspection.NuGetApi;
using NuKeeper.Abstractions.NuGetApi;

namespace NuKeeper.Inspection.Tests.NuGetApi
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NuGet.Versioning;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.NuGetApi;
using NuKeeper.Inspection.NuGetApi;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.RepositoryInspection;
using NuKeeper.Inspection.Report.Formats;
using NuKeeper.Inspection.RepositoryInspection;
using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using NuKeeper.Abstractions.RepositoryInspection;
using NuKeeper.Inspection.Report;
using NuKeeper.Inspection.Report.Formats;
using NuKeeper.Inspection.RepositoryInspection;
Expand Down
6 changes: 3 additions & 3 deletions NuKeeper.Inspection.Tests/Report/PackageUpdates.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NuGet.Configuration;
using NuKeeper.Inspection.NuGetApi;
using NuKeeper.Inspection.RepositoryInspection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NuGet.Configuration;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.RepositoryInspection;
using NuKeeper.Abstractions.NuGetApi;

namespace NuKeeper.Inspection.Tests.Report
{
Expand Down
Loading

0 comments on commit bc357aa

Please sign in to comment.