Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/milestone-merged-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Milestone Merged Pull Requests

'on':
pull_request_target:
types: [closed]
branches:
- main
- master

permissions:
issues: write
pull-requests: write

jobs:
milestone:
uses: wp-cli/.github/.github/workflows/reusable-milestone-merged-prs.yml@main
107 changes: 107 additions & 0 deletions .github/workflows/reusable-milestone-merged-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
name: Milestone Merged Pull Requests

'on':
workflow_call:

permissions:
issues: write
pull-requests: write

jobs:
milestone:
name: Add merged pull request to milestone
runs-on: ubuntu-latest
if: ${{ github.repository_owner == 'wp-cli' && github.event.pull_request.merged == true && github.event.pull_request.milestone == null }}
steps:
- name: Assign next open milestone
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const { owner, repo } = context.repo;
const pr = context.payload.pull_request;

// The event payload is a snapshot from the merge, so a milestone
// set by hand since then would otherwise be overwritten.
const { data: current } = await github.rest.issues.get( { owner, repo, issue_number: pr.number } );
if ( current.milestone ) {
core.info( `#${ pr.number } already has milestone ${ current.milestone.title }, skipping.` );
return;
}

// Accepts titles like `2.12.0`, `v2.12.0` and `2.12`.
const parse = ( title ) => {
const match = /^(v?)(\d+)\.(\d+)(?:\.(\d+))?$/.exec( title.trim() );
if ( ! match ) {
return null;
}
return {
prefix: match[1],
parts: [ Number( match[2] ), Number( match[3] ), Number( match[4] ?? 0 ) ],
};
};

const compare = ( a, b ) => {
for ( let i = 0; i < 3; i++ ) {
if ( a.version.parts[ i ] !== b.version.parts[ i ] ) {
return a.version.parts[ i ] - b.version.parts[ i ];
}
}
return 0;
};

const versioned = ( milestones ) => milestones
.map( ( milestone ) => ( { milestone, version: parse( milestone.title ) } ) )
.filter( ( entry ) => entry.version !== null )
.sort( compare );

const fetchMilestones = ( state ) => github.paginate( github.rest.issues.listMilestones, {
owner,
repo,
state,
per_page: 100,
} );

let milestone;

// Pick and derive from a single snapshot, so that concurrent runs
// that both find no open milestone compute the same next title.
const all = versioned( await fetchMilestones( 'all' ) );
const open = all.filter( ( entry ) => entry.milestone.state === 'open' );
if ( open.length > 0 ) {
milestone = open[0].milestone;
core.info( `Using lowest open milestone ${ milestone.title }.` );
} else {
if ( all.length === 0 ) {
core.notice( 'No open milestone and no versioned milestone to derive one from, skipping.' );
return;
}

const latest = all[ all.length - 1 ].version;
const [ major, minor, patch ] = latest.parts;
const title = `${ latest.prefix }${ major }.${ minor }.${ patch + 1 }`;

try {
( { data: milestone } = await github.rest.issues.createMilestone( { owner, repo, title } ) );
core.info( `Created milestone ${ title }.` );
} catch ( error ) {
// A concurrent run for another merged pull request may have
// created it in the meantime.
if ( error.status !== 422 ) {
throw error;
}
milestone = ( await fetchMilestones( 'all' ) ).find( ( m ) => m.title === title );
if ( ! milestone ) {
throw error;
}
core.info( `Milestone ${ title } already exists.` );
}
}

await github.rest.issues.update( {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
owner,
repo,
issue_number: pr.number,
milestone: milestone.number,
} );
core.info( `Added #${ pr.number } to milestone ${ milestone.title }.` );
2 changes: 2 additions & 0 deletions .github/workflows/sync-workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ on:
- '.github/workflows/copilot-setup-steps.yml'
- '.github/workflows/issue-triage.yml'
- '.github/workflows/manage-labels.yml'
- '.github/workflows/milestone-merged-prs.yml'
- '.github/workflows/regenerate-readme.yml'
- '.github/workflows/welcome-new-contributors.yml'
- '.github/workflows/sync-workflows.yml'
Expand Down Expand Up @@ -62,6 +63,7 @@ jobs:
^\.github/workflows/issue-triage\.yml
^\.github/workflows/check-branch-alias\.yml
^\.github/workflows/manage-labels\.yml
^\.github/workflows/milestone-merged-prs\.yml
^AGENTS\.md
TARGET_REPOS: |
wp-cli/ai-command
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This repository contains reusable GitHub Actions workflows that are automaticall
- **Code Quality Checks** (`code-quality.yml`) - Runs linting, PHPCS, PHPStan, and other code quality tools
- **Regenerate README** (`regenerate-readme.yml`) - Automatically regenerates README.md files from source
- **Check Branch Alias** (`check-branch-alias.yml`) - Monitors and updates Composer branch-alias configuration
- **Milestone Merged Pull Requests** (`milestone-merged-prs.yml`) - Adds merged pull requests to the next milestone

#### Branch Alias Checker

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

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`.

#### Milestone Merged Pull Requests

When a pull request is merged into `main` or `master` and has no milestone yet, this workflow adds it to the next release milestone:

1. If there are open milestones with version titles (e.g. `2.12.1`), it uses the lowest one
2. Otherwise it creates a new milestone one patch version above the highest existing one (e.g. `2.12.0` → `2.12.1`)
3. If the repository has no versioned milestones at all, it does nothing

## Installing

There's nothing to install, this package cannot be used directly.
Expand Down
Loading