Repository navigation
Add workflow to milestone merged pull requests #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( { | ||
| owner, | ||
| repo, | ||
| issue_number: pr.number, | ||
| milestone: milestone.number, | ||
| } ); | ||
| core.info( `Added #${ pr.number } to milestone ${ milestone.title }.` ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.