Skip to content

Commit 5e74d34

Browse files
committed
Build/Test Tools: Auto-close linked PRs on GitHub.
There are currently ~2,000 open pull requests on GitHub for `wordpress-develop`. Many of these were for testing changes that have already been merged. To help prevent orphaned pull requests, this new workflow will search for any pull requests referencing the `Fixed` Trac tickets as noted in the commit message and close them out. For now, this only happens for `push` events. Props peterwilsoncc, swissspidy, johnbillion, davidbaumwald. Fixes #62817. git-svn-id: https://develop.svn.wordpress.org/trunk@59661 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6677425 commit 5e74d34

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Cleanup Pull Requests
2+
3+
on:
4+
push:
5+
branches:
6+
- trunk
7+
- '4.[1-9]'
8+
- '[5-9].[0-9]'
9+
10+
# Cancels all previous workflow runs for pull requests that have not completed.
11+
concurrency:
12+
# The concurrency group contains the workflow name and the branch name for pull requests
13+
# or the commit hash for any other events.
14+
group: ${{ github.workflow }}-${{ github.sha }}
15+
cancel-in-progress: true
16+
17+
# Disable permissions for all available scopes by default.
18+
# Any needed permissions should be configured at the job level.
19+
permissions: {}
20+
21+
jobs:
22+
# Runs pull request cleanup.
23+
close-prs:
24+
name: Clean up pull requests
25+
permissions:
26+
pull-requests: write
27+
if: ${{ github.repository == 'WordPress/wordpress-develop' }}
28+
uses: ./.github/workflows/reusable-cleanup-pull-requests.yml
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)