diff --git a/.github/workflows/milestone-merged-prs.yml b/.github/workflows/milestone-merged-prs.yml new file mode 100644 index 0000000..99bf3af --- /dev/null +++ b/.github/workflows/milestone-merged-prs.yml @@ -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 diff --git a/.github/workflows/reusable-milestone-merged-prs.yml b/.github/workflows/reusable-milestone-merged-prs.yml new file mode 100644 index 0000000..002fc47 --- /dev/null +++ b/.github/workflows/reusable-milestone-merged-prs.yml @@ -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( { + owner, + repo, + issue_number: pr.number, + milestone: milestone.number, + } ); + core.info( `Added #${ pr.number } to milestone ${ milestone.title }.` ); diff --git a/.github/workflows/sync-workflows.yml b/.github/workflows/sync-workflows.yml index 75fac9e..6ec9ae6 100644 --- a/.github/workflows/sync-workflows.yml +++ b/.github/workflows/sync-workflows.yml @@ -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' @@ -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 diff --git a/README.md b/README.md index 2ebbd65..305b30e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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.