forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
122 lines (103 loc) · 4.84 KB
/
cleanup-pull-requests.yml
File metadata and controls
122 lines (103 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# This workflow finds and closes any pull requests that are linked to Trac
# tickets that are referenced as fixed in commit messages.
#
# More on using GitHub pull requests for contributing to WordPress: https://make.wordpress.org/core/handbook/contribute/git/github-pull-requests-for-code-review/.
name: Cleanup Pull Requests
on:
push:
branches:
- trunk
- '4.[1-9]'
- '[5-9].[0-9]'
- 'test-close-prs'
# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.sha }}
cancel-in-progress: true
# Disable permissions for all available scopes by default.
# Any needed permissions should be configured at the job level.
permissions: {}
jobs:
# Finds and closes pull requests referencing fixed Trac tickets in commit messages using the
# documented expected format
#
# Commit message format is documented in the Core handbook: https://make.wordpress.org/core/handbook/best-practices/commit-messages/.
#
# Performs the following steps:
# - Parse fixed ticket numbers from the commit message.
# - Parse the SVN revision from the commit message.
# - Searches for pull requests referencing any fixed tickets.
# - Leaves a comment on each PR before closing.
close-prs:
name: Clean up relevant pull requests
runs-on: ubuntu-latest
permissions:
pull-requests: write
# if: ${{ github.repository == 'WordPress/wordpress-develop' }}
steps:
- name: Find fixed ticket numbers
id: trac-tickets
run: |
COMMIT_MESSAGE=$(cat <<'EOF' | sed -n '/^Fixes #/,/\./p'
${{ github.event.head_commit.message }}
EOF
)
echo "fixed_list=$(echo \"$COMMIT_MESSAGE\" | sed -n 's/.*Fixes #\([0-9]\+\).*/\1/p' | tr '\n' ' ')" >> $GITHUB_OUTPUT
- name: Get the SVN revision
id: git-svn-id
run: |
COMMIT_MESSAGE=$(cat <<'EOF' | sed -n '$p'
${{ github.event.head_commit.message }}
EOF
)
echo "svn_revision_number=$(echo \"$COMMIT_MESSAGE\" | sed -n 's/.*git-svn-id: https:\/\/develop.svn.wordpress.org\/[^@]*@\([0-9]*\) .*/\1/p')" >> $GITHUB_OUTPUT
- name: Find pull requests
id: linked-prs
if: ${{ steps.trac-tickets.outputs.fixed_list != '' && steps.git-svn-id.outputs.svn_revision_number != '' }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fixedList = "${{ steps.trac-tickets.outputs.fixed_list }}".split(' ').filter(Boolean);
let prNumbers = [];
for (const ticket of fixedList) {
const query = 'is:pr is:open repo:' + context.repo.owner + '/' + context.repo.repo + ' in:body https://core.trac.wordpress.org/ticket/' + ticket;
const result = await github.rest.search.issuesAndPullRequests({ q: query });
prNumbers = prNumbers.concat(result.data.items.map(pr => pr.number));
}
return prNumbers;
- name: Comment and close pull requests
if: ${{ steps.trac-tickets.outputs.fixed_list != '' && steps.git-svn-id.outputs.svn_revision_number != '' }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const prNumbers = ${{ steps.linked-prs.outputs.result }};
for (const prNumber of prNumbers) {
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
if ( pr.data.state === 'closed' ) {
continue;
}
const commentBody = `A commit was made that fixes the Trac ticket referenced in the description of this pull request.
SVN changeset: [${{ steps.git-svn-id.outputs.svn_revision_number }}](https://core.trac.wordpress.org/changeset/${{ steps.git-svn-id.outputs.svn_revision_number }})
GitHub changeset: ${{ github.sha }}
This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.`;
// Comment on the pull request with details.
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentBody
});
// Close the pull request.
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
state: 'closed'
});
}