|
| 1 | +## |
| 2 | +# A reusable workflow that finds and closes any pull requests that are linked to Trac |
| 3 | +# tickets that are referenced as fixed in commit messages. |
| 4 | +# |
| 5 | +# More info about using GitHub pull requests for contributing to WordPress can be found in the handbook: https://make.wordpress.org/core/handbook/contribute/git/github-pull-requests-for-code-review/. |
| 6 | +## |
| 7 | +name: Run pull request cleanup |
| 8 | + |
| 9 | +on: |
| 10 | + workflow_call: |
| 11 | + |
| 12 | +jobs: |
| 13 | + # Finds and closes pull requests referencing fixed Trac tickets in commit messages using the |
| 14 | + # documented expected format |
| 15 | + # |
| 16 | + # Commit message format is documented in the Core handbook: https://make.wordpress.org/core/handbook/best-practices/commit-messages/. |
| 17 | + # |
| 18 | + # Performs the following steps: |
| 19 | + # - Parse fixed ticket numbers from the commit message. |
| 20 | + # - Parse the SVN revision from the commit message. |
| 21 | + # - Searches for pull requests referencing any fixed tickets. |
| 22 | + # - Leaves a comment on each PR before closing. |
| 23 | + close-prs: |
| 24 | + name: Find and close PRs |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + pull-requests: write |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Find fixed ticket numbers |
| 31 | + id: trac-tickets |
| 32 | + run: | |
| 33 | + COMMIT_MESSAGE=$(cat <<'EOF' | sed -n '/^Fixes #/,/\./p' |
| 34 | + ${{ github.event.head_commit.message }} |
| 35 | + EOF |
| 36 | + ) |
| 37 | + echo "fixed_list=$(echo \"$COMMIT_MESSAGE\" | sed -n 's/.*Fixes #\([0-9]\+\).*/\1/p' | tr '\n' ' ')" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - name: Get the SVN revision |
| 40 | + id: git-svn-id |
| 41 | + run: | |
| 42 | + COMMIT_MESSAGE=$(cat <<'EOF' | sed -n '$p' |
| 43 | + ${{ github.event.head_commit.message }} |
| 44 | + EOF |
| 45 | + ) |
| 46 | + echo "svn_revision_number=$(echo \"$COMMIT_MESSAGE\" | sed -n 's/.*git-svn-id: https:\/\/develop.svn.wordpress.org\/[^@]*@\([0-9]*\) .*/\1/p')" >> $GITHUB_OUTPUT |
| 47 | +
|
| 48 | + - name: Find pull requests |
| 49 | + id: linked-prs |
| 50 | + if: ${{ steps.trac-tickets.outputs.fixed_list != '' && steps.git-svn-id.outputs.svn_revision_number != '' }} |
| 51 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 52 | + with: |
| 53 | + script: | |
| 54 | + const fixedList = "${{ steps.trac-tickets.outputs.fixed_list }}".split(' ').filter(Boolean); |
| 55 | +
|
| 56 | + let prNumbers = []; |
| 57 | +
|
| 58 | + for (const ticket of fixedList) { |
| 59 | + const query = 'is:pr is:open repo:' + context.repo.owner + '/' + context.repo.repo + ' in:body https://core.trac.wordpress.org/ticket/' + ticket; |
| 60 | + const result = await github.rest.search.issuesAndPullRequests({ q: query }); |
| 61 | +
|
| 62 | + prNumbers = prNumbers.concat(result.data.items.map(pr => pr.number)); |
| 63 | + } |
| 64 | +
|
| 65 | + return prNumbers; |
| 66 | +
|
| 67 | + - name: Comment and close pull requests |
| 68 | + if: ${{ steps.trac-tickets.outputs.fixed_list != '' && steps.git-svn-id.outputs.svn_revision_number != '' }} |
| 69 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 70 | + with: |
| 71 | + script: | |
| 72 | + const prNumbers = ${{ steps.linked-prs.outputs.result }}; |
| 73 | +
|
| 74 | + const commentBody = `A commit was made that fixes the Trac ticket referenced in the description of this pull request. |
| 75 | +
|
| 76 | + SVN changeset: [${{ steps.git-svn-id.outputs.svn_revision_number }}](https://core.trac.wordpress.org/changeset/${{ steps.git-svn-id.outputs.svn_revision_number }}) |
| 77 | + GitHub commit: https://github.com/WordPress/wordpress-develop/commit/${{ github.sha }} |
| 78 | +
|
| 79 | + This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.`; |
| 80 | +
|
| 81 | + // Update all matched pull requests. |
| 82 | + for (const prNumber of prNumbers) { |
| 83 | + // Comment on the pull request with details. |
| 84 | + await github.rest.issues.createComment({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + issue_number: prNumber, |
| 88 | + body: commentBody |
| 89 | + }); |
| 90 | +
|
| 91 | + // Close the pull request. |
| 92 | + await github.rest.pulls.update({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + pull_number: prNumber, |
| 96 | + state: 'closed' |
| 97 | + }); |
| 98 | + } |
0 commit comments