Skip to content

Commit ba28768

Browse files
swissspidyclaude
andauthored
Add workflow to milestone merged pull requests (#323)
* Add workflow to milestone merged pull requests Adds merged pull requests without a milestone to the lowest open versioned milestone, creating the next patch milestone if none is open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5n2TBcDvRzhQfqhhaB * Address review feedback on milestone workflow Re-check the pull request's milestone before assigning one, since the event payload may be stale. Drop the concurrency group: it could cancel pending runs, and concurrent milestone creation is already handled by reusing the milestone on a 422. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5n2TBcDvRzhQfqhhaB * Derive milestone from a single snapshot Two concurrent runs could both find no open milestone, and the later one would then see the milestone the earlier one just created and derive the patch version after it. Selecting and deriving from one listing makes both compute the same title, so the 422 handling reuses it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5n2TBcDvRzhQfqhhaB --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1a8fe7d commit ba28768

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Milestone Merged Pull Requests
3+
4+
'on':
5+
pull_request_target:
6+
types: [closed]
7+
branches:
8+
- main
9+
- master
10+
11+
permissions:
12+
issues: write
13+
pull-requests: write
14+
15+
jobs:
16+
milestone:
17+
uses: wp-cli/.github/.github/workflows/reusable-milestone-merged-prs.yml@main
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
name: Milestone Merged Pull Requests
3+
4+
'on':
5+
workflow_call:
6+
7+
permissions:
8+
issues: write
9+
pull-requests: write
10+
11+
jobs:
12+
milestone:
13+
name: Add merged pull request to milestone
14+
runs-on: ubuntu-latest
15+
if: ${{ github.repository_owner == 'wp-cli' && github.event.pull_request.merged == true && github.event.pull_request.milestone == null }}
16+
steps:
17+
- name: Assign next open milestone
18+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
19+
with:
20+
script: |
21+
const { owner, repo } = context.repo;
22+
const pr = context.payload.pull_request;
23+
24+
// The event payload is a snapshot from the merge, so a milestone
25+
// set by hand since then would otherwise be overwritten.
26+
const { data: current } = await github.rest.issues.get( { owner, repo, issue_number: pr.number } );
27+
if ( current.milestone ) {
28+
core.info( `#${ pr.number } already has milestone ${ current.milestone.title }, skipping.` );
29+
return;
30+
}
31+
32+
// Accepts titles like `2.12.0`, `v2.12.0` and `2.12`.
33+
const parse = ( title ) => {
34+
const match = /^(v?)(\d+)\.(\d+)(?:\.(\d+))?$/.exec( title.trim() );
35+
if ( ! match ) {
36+
return null;
37+
}
38+
return {
39+
prefix: match[1],
40+
parts: [ Number( match[2] ), Number( match[3] ), Number( match[4] ?? 0 ) ],
41+
};
42+
};
43+
44+
const compare = ( a, b ) => {
45+
for ( let i = 0; i < 3; i++ ) {
46+
if ( a.version.parts[ i ] !== b.version.parts[ i ] ) {
47+
return a.version.parts[ i ] - b.version.parts[ i ];
48+
}
49+
}
50+
return 0;
51+
};
52+
53+
const versioned = ( milestones ) => milestones
54+
.map( ( milestone ) => ( { milestone, version: parse( milestone.title ) } ) )
55+
.filter( ( entry ) => entry.version !== null )
56+
.sort( compare );
57+
58+
const fetchMilestones = ( state ) => github.paginate( github.rest.issues.listMilestones, {
59+
owner,
60+
repo,
61+
state,
62+
per_page: 100,
63+
} );
64+
65+
let milestone;
66+
67+
// Pick and derive from a single snapshot, so that concurrent runs
68+
// that both find no open milestone compute the same next title.
69+
const all = versioned( await fetchMilestones( 'all' ) );
70+
const open = all.filter( ( entry ) => entry.milestone.state === 'open' );
71+
if ( open.length > 0 ) {
72+
milestone = open[0].milestone;
73+
core.info( `Using lowest open milestone ${ milestone.title }.` );
74+
} else {
75+
if ( all.length === 0 ) {
76+
core.notice( 'No open milestone and no versioned milestone to derive one from, skipping.' );
77+
return;
78+
}
79+
80+
const latest = all[ all.length - 1 ].version;
81+
const [ major, minor, patch ] = latest.parts;
82+
const title = `${ latest.prefix }${ major }.${ minor }.${ patch + 1 }`;
83+
84+
try {
85+
( { data: milestone } = await github.rest.issues.createMilestone( { owner, repo, title } ) );
86+
core.info( `Created milestone ${ title }.` );
87+
} catch ( error ) {
88+
// A concurrent run for another merged pull request may have
89+
// created it in the meantime.
90+
if ( error.status !== 422 ) {
91+
throw error;
92+
}
93+
milestone = ( await fetchMilestones( 'all' ) ).find( ( m ) => m.title === title );
94+
if ( ! milestone ) {
95+
throw error;
96+
}
97+
core.info( `Milestone ${ title } already exists.` );
98+
}
99+
}
100+
101+
await github.rest.issues.update( {
102+
owner,
103+
repo,
104+
issue_number: pr.number,
105+
milestone: milestone.number,
106+
} );
107+
core.info( `Added #${ pr.number } to milestone ${ milestone.title }.` );

‎.github/workflows/sync-workflows.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616
- '.github/workflows/copilot-setup-steps.yml'
1717
- '.github/workflows/issue-triage.yml'
1818
- '.github/workflows/manage-labels.yml'
19+
- '.github/workflows/milestone-merged-prs.yml'
1920
- '.github/workflows/regenerate-readme.yml'
2021
- '.github/workflows/welcome-new-contributors.yml'
2122
- '.github/workflows/sync-workflows.yml'
@@ -62,6 +63,7 @@ jobs:
6263
^\.github/workflows/issue-triage\.yml
6364
^\.github/workflows/check-branch-alias\.yml
6465
^\.github/workflows/manage-labels\.yml
66+
^\.github/workflows/milestone-merged-prs\.yml
6567
^AGENTS\.md
6668
TARGET_REPOS: |
6769
wp-cli/ai-command

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This repository contains reusable GitHub Actions workflows that are automaticall
1818
- **Code Quality Checks** (`code-quality.yml`) - Runs linting, PHPCS, PHPStan, and other code quality tools
1919
- **Regenerate README** (`regenerate-readme.yml`) - Automatically regenerates README.md files from source
2020
- **Check Branch Alias** (`check-branch-alias.yml`) - Monitors and updates Composer branch-alias configuration
21+
- **Milestone Merged Pull Requests** (`milestone-merged-prs.yml`) - Adds merged pull requests to the next milestone
2122

2223
#### Branch Alias Checker
2324

@@ -31,6 +32,14 @@ The branch alias checker workflow automatically ensures that the Composer `branc
3132

3233
For example, if a repository releases `v2.2.6` or `v2.3.0`, the branch-alias stays at `2.x-dev`. Only when releasing `v3.0.0` should it change to `3.x-dev`.
3334

35+
#### Milestone Merged Pull Requests
36+
37+
When a pull request is merged into `main` or `master` and has no milestone yet, this workflow adds it to the next release milestone:
38+
39+
1. If there are open milestones with version titles (e.g. `2.12.1`), it uses the lowest one
40+
2. Otherwise it creates a new milestone one patch version above the highest existing one (e.g. `2.12.0` → `2.12.1`)
41+
3. If the repository has no versioned milestones at all, it does nothing
42+
3443
## Installing
3544

3645
There's nothing to install, this package cannot be used directly.

0 commit comments

Comments
 (0)