Enhance the update-dependencies script:
- Add the new version number to the commit and PR message. - Check if anything has been updated and no-op if there wasn't anything updated. - Add support to CC indviduals on the PR.
This commit is contained in:
parent
f6fa1cf060
commit
e7718b7163
5 changed files with 118 additions and 39 deletions
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Cli.Build.Framework;
|
||||
using Octokit;
|
||||
|
||||
|
@ -14,8 +15,6 @@ namespace Microsoft.DotNet.Scripts
|
|||
/// </summary>
|
||||
public static class PushPRTargets
|
||||
{
|
||||
private const string PullRequestTitle = "Updating dependencies from last known good builds";
|
||||
|
||||
private static readonly Config s_config = Config.Instance;
|
||||
|
||||
[Target(nameof(CommitChanges), nameof(CreatePR))]
|
||||
|
@ -28,14 +27,31 @@ namespace Microsoft.DotNet.Scripts
|
|||
[Target]
|
||||
public static BuildTargetResult CommitChanges(BuildTargetContext c)
|
||||
{
|
||||
Cmd("git", "add", ".")
|
||||
.Execute()
|
||||
.EnsureSuccessful();
|
||||
CommandResult statusResult = Cmd("git", "status", "--porcelain")
|
||||
.CaptureStdOut()
|
||||
.Execute();
|
||||
statusResult.EnsureSuccessful();
|
||||
|
||||
bool hasModifiedFiles = !string.IsNullOrWhiteSpace(statusResult.StdOut);
|
||||
bool hasUpdatedDependencies = c.GetDependencyInfos().Where(d => d.IsUpdated).Any();
|
||||
|
||||
if (hasModifiedFiles != hasUpdatedDependencies)
|
||||
{
|
||||
return c.Failed($"'git status' does not match DependencyInfo information. Git has modified files: {hasModifiedFiles}. DependencyInfo is updated: {hasUpdatedDependencies}.");
|
||||
}
|
||||
|
||||
if (!hasUpdatedDependencies)
|
||||
{
|
||||
c.Warn("Dependencies are currently up to date");
|
||||
return c.Success();
|
||||
}
|
||||
|
||||
string userName = s_config.UserName;
|
||||
string email = s_config.Email;
|
||||
|
||||
Cmd("git", "commit", "-m", PullRequestTitle, "--author", $"{userName} <{email}>")
|
||||
string commitMessage = GetCommitMessage(c);
|
||||
|
||||
Cmd("git", "commit", "-a", "-m", commitMessage, "--author", $"{userName} <{email}>")
|
||||
.EnvironmentVariable("GIT_COMMITTER_NAME", userName)
|
||||
.EnvironmentVariable("GIT_COMMITTER_EMAIL", email)
|
||||
.Execute()
|
||||
|
@ -79,12 +95,19 @@ namespace Microsoft.DotNet.Scripts
|
|||
public static BuildTargetResult CreatePR(BuildTargetContext c)
|
||||
{
|
||||
string remoteBranchName = c.GetRemoteBranchName();
|
||||
string commitMessage = c.GetCommitMessage();
|
||||
|
||||
NewPullRequest prInfo = new NewPullRequest(
|
||||
PullRequestTitle,
|
||||
commitMessage,
|
||||
s_config.GitHubOriginOwner + ":" + remoteBranchName,
|
||||
s_config.GitHubUpstreamBranch);
|
||||
|
||||
string[] prNotifications = s_config.GitHubPullRequestNotifications;
|
||||
if (prNotifications.Length > 0)
|
||||
{
|
||||
prInfo.Body = $"/cc @{string.Join(" @", prNotifications)}";
|
||||
}
|
||||
|
||||
GitHubClient gitHub = new GitHubClient(new ProductHeaderValue("dotnetDependencyUpdater"));
|
||||
|
||||
gitHub.Credentials = new Credentials(s_config.Password);
|
||||
|
@ -104,5 +127,36 @@ namespace Microsoft.DotNet.Scripts
|
|||
{
|
||||
c.BuildContext["RemoteBranchName"] = value;
|
||||
}
|
||||
|
||||
private static string GetCommitMessage(this BuildTargetContext c)
|
||||
{
|
||||
const string commitMessagePropertyName = "CommitMessage";
|
||||
|
||||
string message;
|
||||
object messageObject;
|
||||
if (c.BuildContext.Properties.TryGetValue(commitMessagePropertyName, out messageObject))
|
||||
{
|
||||
message = (string)messageObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
DependencyInfo[] updatedDependencies = c.GetDependencyInfos()
|
||||
.Where(d => d.IsUpdated)
|
||||
.ToArray();
|
||||
|
||||
string updatedDependencyNames = string.Join(", ", updatedDependencies.Select(d => d.Name));
|
||||
string updatedDependencyVersions = string.Join(", ", updatedDependencies.Select(d => d.NewReleaseVersion));
|
||||
|
||||
message = $"Updating {updatedDependencyNames} to {updatedDependencyVersions}";
|
||||
if (updatedDependencies.Count() > 1)
|
||||
{
|
||||
message += " respectively";
|
||||
}
|
||||
|
||||
c.BuildContext[commitMessagePropertyName] = message;
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue