Skip to content

Commit 481d4e3

Browse files
authored
Merge pull request #46423 from github/repo-sync
Repo sync
2 parents 89dc77d + 7ef56ed commit 481d4e3

5 files changed

Lines changed: 252 additions & 179 deletions

File tree

‎.github/workflows/check-for-spammy-prs.yml‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Check for Spammy PRs
22

33
# Flags low-value public PRs and closes PRs that target branches other than main.
4+
# Maintainers add the skip-spammy-check label to exempt a PR.
45

56
on:
67
pull_request_target:
@@ -16,7 +17,8 @@ jobs:
1617
name: Label spammy PRs and reject non-main targets
1718
if: >
1819
github.repository == 'github/docs' && github.event_name == 'pull_request_target' &&
19-
github.event.pull_request.user.login != 'docs-bot'
20+
github.event.pull_request.user.login != 'docs-bot' &&
21+
!contains(github.event.pull_request.labels.*.name, 'skip-spammy-check')
2022
runs-on: ubuntu-latest
2123
steps:
2224
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3
@@ -102,6 +104,18 @@ jobs:
102104
onlyRenames ||
103105
onlyIndexMd
104106
) {
107+
// The payload can predate a label added right after the PR opened, so read the labels again.
108+
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
109+
owner,
110+
repo,
111+
issue_number: pull_number,
112+
per_page: 100,
113+
})
114+
if (currentLabels.some(label => label.name.toLowerCase() === 'skip-spammy-check')) {
115+
core.info('Skipping because the pull request has the skip-spammy-check label.')
116+
return
117+
}
118+
105119
await github.rest.issues.addLabels({
106120
owner,
107121
repo,

‎.github/workflows/close-on-invalid-label.yaml‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ name: Close issue/PR on adding invalid label
22

33
# Closes public issues and PRs when the invalid label is added.
44
# Re-closes invalid PRs that someone without write access reopens.
5+
# Reopens PRs closed as invalid when maintainers add the skip-spammy-check label.
56

67
on:
78
issues:
89
types: [labeled]
9-
# pull_request_target lets the workflow close forked PRs when they're labeled invalid or reopened.
10+
# pull_request_target lets the workflow close or reopen forked PRs when they're labeled or reopened.
1011
pull_request_target:
1112
types: [labeled, reopened]
1213

@@ -71,3 +72,73 @@ jobs:
7172
exit 0
7273
fi
7374
gh pr close "$PR_URL" --comment 'This pull request is labeled `invalid`, so only maintainers can reopen it. If you think this is incorrect, please mention this in a corresponding issue.'
75+
76+
# Maintainers add skip-spammy-check to recover a PR that automated checks closed as invalid.
77+
reopen-on-adding-skip-label:
78+
if: github.repository == 'github/docs' && github.event_name == 'pull_request_target' && github.event.label.name == 'skip-spammy-check'
79+
runs-on: ubuntu-latest
80+
permissions:
81+
contents: read
82+
steps:
83+
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
84+
with:
85+
# A PAT makes the reopen trigger other workflows, such as triage, like a manual reopen.
86+
github-token: ${{ secrets.DOCS_BOT_PAT_BASE }}
87+
script: |
88+
const { owner, repo } = context.repo
89+
const pull_number = context.payload.pull_request.number
90+
91+
// The event payload can be stale by the time this job runs, so read the current state.
92+
const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number })
93+
const labels = pr.labels.map(label => label.name.toLowerCase())
94+
95+
if (pr.merged || pr.state !== 'closed') {
96+
core.info('The pull request is not closed, so there is nothing to reopen.')
97+
return
98+
}
99+
100+
if (!labels.includes('skip-spammy-check')) {
101+
core.info('The pull request no longer has the skip-spammy-check label.')
102+
return
103+
}
104+
105+
// Automated checks add the invalid label before closing, so PRs without it were closed for another reason.
106+
if (!labels.includes('invalid')) {
107+
core.info('The pull request was not closed as invalid, so it stays closed.')
108+
return
109+
}
110+
111+
try {
112+
await github.rest.pulls.update({ owner, repo, pull_number, state: 'open' })
113+
} catch (error) {
114+
if (error.status !== 422) throw error
115+
116+
// GitHub refuses to reopen a PR after its head or base branch was deleted or force-pushed.
117+
core.warning(`Could not reopen the pull request: ${error.message}`)
118+
await github.rest.issues.createComment({
119+
owner,
120+
repo,
121+
issue_number: pull_number,
122+
body: 'I could not reopen this pull request automatically, most likely because its head or base branch was deleted or force-pushed after it was closed. Restore the branch, then remove and re-add the `skip-spammy-check` label to retry automatic reopening and removal of the `invalid` label.',
123+
})
124+
return
125+
}
126+
127+
try {
128+
await github.rest.issues.removeLabel({
129+
owner,
130+
repo,
131+
issue_number: pull_number,
132+
name: 'invalid',
133+
})
134+
} catch (error) {
135+
// A maintainer or an earlier run may have removed the label already.
136+
if (error.status !== 404) throw error
137+
}
138+
139+
await github.rest.issues.createComment({
140+
owner,
141+
repo,
142+
issue_number: pull_number,
143+
body: 'The `skip-spammy-check` label was added, so I removed the `invalid` label and reopened this pull request. The spammy pull request check skips it while the label is applied.',
144+
})

‎.github/workflows/close-prs-not-targeting-main.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Reject PRs targeting non-default branch (scheduled)
22

3-
# Backup check for check-for-spammy-prs.yml: closes PRs that don't target main
3+
# Backup check for check-for-spammy-prs.yml: closes PRs that don't target main, unless they have the skip-spammy-check label
44

55
on:
66
schedule:
@@ -39,6 +39,9 @@ jobs:
3939
if (pr.user.login === 'docs-bot') continue
4040
4141
const labels = pr.labels.map(l => l.name)
42+
// Maintainers add this label to allow a PR that targets another branch.
43+
if (labels.some(label => label.toLowerCase() === 'skip-spammy-check')) continue
44+
4245
if (!labels.includes('invalid')) {
4346
await github.rest.issues.addLabels({
4447
owner,

0 commit comments

Comments
 (0)