Skip to content

Commit

Permalink
feat: Automatically assign Pull Requests to GitHub Milestones
Browse files Browse the repository at this point in the history
When building a Pull Request in CI, automatically assign the Pull Request being built to a GitHub milestone for the current version.

The name of the Milestone is determined from the version being built using the format 'v<MAJOR>.<MINOR>'.

Pull-Request: #26
  • Loading branch information
ap0llo authored May 1, 2022
1 parent 2d04232 commit b01e8aa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/SharedBuild/TaskNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public static class TaskNames
public const string ValidateCodeFormatting = "ValidateCodeFormatting";
public const string Validate = "Validate";
public const string Generate = "Generate";
public const string SetGitHubMilestone = "SetGitHubMilestone";
}
}
1 change: 1 addition & 0 deletions src/SharedBuild/Tasks/CITask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Grynwald.SharedBuild.Tasks
[IsDependentOn(typeof(PackTask))]
[IsDependentOn(typeof(GenerateChangeLogTask))]
[IsDependentOn(typeof(PushTask))]
[IsDependentOn(typeof(SetGitHubMilestoneTask))]
[IsDependentOn(typeof(CreateGitHubReleaseTask))]
public class CITask : FrostingTask
{ }
Expand Down
46 changes: 46 additions & 0 deletions src/SharedBuild/Tasks/SetGitHubMilestoneTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Threading.Tasks;
using Cake.Core.Diagnostics;
using Cake.Frosting;
using Cake.GitHub;
using Cake.GitVersioning;

namespace Grynwald.SharedBuild.Tasks
{
[TaskName(TaskNames.SetGitHubMilestone)]
public class SetGitHubMilestoneTask : AsyncFrostingTask<IBuildContext>
{
public override bool ShouldRun(IBuildContext context)
{
return context.IsRunningInCI && context.GitHub.PullRequest.IsPullRequest;
}


public override async Task RunAsync(IBuildContext context)
{
var versionInfo = context.GitVersioningGetVersion(context.RootDirectory.FullPath);
var milestoneTitle = $"v{versionInfo.Version.Major}.{versionInfo.Version.Minor}";

context.Log.Information($"Assinging Pull Request {context.GitHub.PullRequest.Number} to milestone {milestoneTitle}");

if (context.GitHub.TryGetAccessToken() is not string accessToken)
{
throw new Exception("No GitHub access token specified. Cannot set Milestone for Pull Request");
}

await context.GitHubSetMilestoneAsync(
userName: null,
accessToken,
context.GitHub.RepositoryOwner,
context.GitHub.RepositoryName,
context.GitHub.PullRequest.Number,
milestoneTitle,
new GitHubSetMilestoneSettings()
{
Overwrite = true,
CreateMilestone = true
});
}

}
}

0 comments on commit b01e8aa

Please sign in to comment.