Skip to content

Commit

Permalink
Merge pull request #2398 from DataDog/pierre/add-to-milestone
Browse files Browse the repository at this point in the history
[Automation] Adds automatically merged PRs to next milestone
  • Loading branch information
pierotibou authored Dec 1, 2023
2 parents 9ebc339 + 3891c38 commit 0162598
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
8 changes: 3 additions & 5 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
Any description you feel is relevant and gives more background to this PR, if necessary.
-->

### Readiness checklist
- [ ] (only for Members) Changelog has been added to the release document.
- [ ] Tests added for this feature/bug.
<!-- Fixes #{issue} -->
<!-- Documented in #{doc pr} -->

### Reviewer checklist
- [ ] Test coverage seems ok.
- [ ] Appropriate labels assigned.
- [ ] Milestone is set.
- [ ] Changelog has been added to the release document. For community contributors the reviewer is in charge of this task.
4 changes: 2 additions & 2 deletions .github/labeller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ labels:
allFilesIn: "(appsec\/.*|docker-compose.yml|.*\/.circleci\/.*)"

- name: "profiling"
title: "^*(profiler)"
title: "^.*\\(profiler\\)"

- name: "profiling"
title: "^*(profiling)"
title: "^.*\\(profiling\\)"

- name: "profiling"
allFilesIn: "(profiling\/.*|docker-compose.yml|.*\/.circleci\/.*)"
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/auto_add_pr_to_miletone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Auto add PR to vNext milestone

on:
pull_request:
branches:
- master
- main
types: [closed]

jobs:
add_to_milestone:
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.title, '[Version Bump]') == false
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.101'

- name: "Assign to vNext Milestone"
run: ./tracer/build.sh AssignPullRequestToMilestone
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
PullRequestNumber: "${{ github.event.pull_request.number }}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ tmp/opcache*
docker-compose.override.yml
tests/Sapi/Roadrunner/rr-*
github-actions-helpers/_build.csproj.DotSettings
.nuke
83 changes: 83 additions & 0 deletions github-actions-helpers/Build.Github.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,39 @@ partial class Build

const string GitHubRepositoryOwner = "DataDog";

Target AssignPullRequestToMilestone => _ => _
.Unlisted()
.Requires(() => GitHubRepositoryName)
.Requires(() => GitHubToken)
.Requires(() => PullRequestNumber)
.Executes(async() =>
{
var client = GetGitHubClient();

var currentMilestone = (await client.PullRequest.Get(
owner: GitHubRepositoryOwner,
name: GitHubRepositoryName,
number: PullRequestNumber.Value))
.Milestone;

if (currentMilestone != null && currentMilestone.Number != 0) {
Console.WriteLine($"Pull request {PullRequestNumber} already has a milesotone: {currentMilestone.Title} ({currentMilestone.Number})");
return;
}

var milestone = await GetOrCreateNextMilestone(client);

Console.WriteLine($"Assigning PR {PullRequestNumber} to {milestone.Title} ({milestone.Number})");

await client.Issue.Update(
owner: GitHubRepositoryOwner,
name: GitHubRepositoryName,
number: PullRequestNumber.Value,
new IssueUpdate { Milestone = milestone.Number });

Console.WriteLine($"PR assigned");
});

Target SummaryOfSnapshotChanges => _ => _
.Unlisted()
.Requires(() => GitHubRepositoryName)
Expand Down Expand Up @@ -321,6 +354,56 @@ LabbelerConfiguration GetLabellerConfiguration()
}
});

private async Task<Milestone> GetOrCreateNextMilestone(GitHubClient gitHubClient)
{
var milestoneName = CalculateNextVersion(Version);
var milestone = await GetMilestone(gitHubClient, milestoneName);
if (milestone is not null)
{
Console.WriteLine($"Found {milestoneName} milestone: {milestone.Number}");
return milestone;
}

Console.WriteLine($"{milestoneName} milestone not found, creating");

var milestoneRequest = new NewMilestone(milestoneName);
milestone = await gitHubClient.Issue.Milestone.Create(
owner: GitHubRepositoryOwner,
name: GitHubRepositoryName,
milestoneRequest);
Console.WriteLine($"Created {milestoneName} milestone: {milestone.Number}");
return milestone;
}

private async Task<Milestone> GetMilestone(GitHubClient gitHubClient, string milestoneName)
{
Console.WriteLine("Fetching milestones...");
var allOpenMilestones = await gitHubClient.Issue.Milestone.GetAllForRepository(
owner: GitHubRepositoryOwner,
name: GitHubRepositoryName,
new MilestoneRequest { State = ItemStateFilter.Open });

return allOpenMilestones.FirstOrDefault(x => x.Title == milestoneName);
}

string CalculateNextVersion(string currentVersion)
{
Console.WriteLine("Current version is " + currentVersion);
var parsedVersion = new Version(currentVersion);
var major = parsedVersion.Major;
int minor;
int patch;

// always do minor version bump on 2.x branch
minor = parsedVersion.Minor + 1;
patch = 0;

var nextVersion = $"{major}.{minor}.{patch}";

Console.WriteLine("Next version calculated as " + nextVersion);
return nextVersion;
}

GitHubClient GetGitHubClient() =>
new(new ProductHeaderValue("nuke-ci-client"))
{
Expand Down
3 changes: 3 additions & 0 deletions github-actions-helpers/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ partial class Build : NukeBuild
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;

[Parameter("The current version of the source and build")]
readonly string Version = "0.94.0";

Target Clean => _ => _
.Before(Restore)
.Executes(() =>
Expand Down

0 comments on commit 0162598

Please sign in to comment.