Skip to content

Commit 615c4ae

Browse files
authored
Close invalid repo-sync PRs as part of the repo-sync workflow (#18650)
* Delete flawed 'close-external-repo-sync-prs.yml' workflow since it won't run for spammy actors * Add the core logic to close invalid repo sync PRs into a new job within the 'repo-sync.yml' workflow
1 parent 1881ccc commit 615c4ae

File tree

2 files changed

+74
-68
lines changed

2 files changed

+74
-68
lines changed

.github/workflows/close-external-repo-sync-prs.yml

Lines changed: 0 additions & 65 deletions
This file was deleted.

.github/workflows/repo-sync.yml

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# The docs.github.com project has two repositories: github/docs (public) and github/docs-internal (private)
22
#
3-
# This GitHub Actions workflow keeps the main branch of those two repos in sync.
3+
# This GitHub Actions workflow keeps the `main` branch of those two repos in sync.
44
#
55
# For more details, see https://github.com/repo-sync/repo-sync#how-it-works
66

77
name: Repo Sync
88

9-
# **What it does**: Syncs docs and docs-internal.
10-
# **Why we have it**: To keep the open-source repository up-to-date, while still having an internal repository for sensitive work.
9+
# **What it does**:
10+
# - close-invalid-repo-sync: Close repo sync pull requests not created by Octomerger or a Hubber.
11+
# - repo-sync: Syncs docs and docs-internal.
12+
# **Why we have it**:
13+
# - close-invalid-repo-sync: Another form of spam prevention for the open-source repository.
14+
# - repo-sync: To keep the open-source repository up-to-date, while still having an internal
15+
# repository for sensitive work.
1116
# **Who does it impact**: Open-source.
1217

1318
on:
@@ -16,7 +21,73 @@ on:
1621
- cron: '*/15 * * * *' # every 15 minutes
1722

1823
jobs:
24+
close-invalid-repo-sync:
25+
name: Close invalid Repo Sync PRs
26+
if: github.repository == 'github/docs'
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Find pull request
30+
uses: juliangruber/find-pull-request-action@2fc55e82a6d5d36fe1e7f1848f7e64fd02d99de9
31+
id: find-pull-request
32+
with:
33+
github-token: ${{ secrets.GITHUB_TOKEN }}
34+
branch: repo-sync
35+
base: main
36+
37+
- name: Close pull request if unwanted
38+
if: ${{ steps.find-pull-request.outputs.number }}
39+
uses: actions/github-script@626af12fe9a53dc2972b48385e7fe7dec79145c9
40+
with:
41+
github-token: ${{ secrets.DOCUBOT_FR_PROJECT_BOARD_WORKFLOWS_REPO_ORG_READ_SCOPES }}
42+
script: |
43+
const { owner, repo } = context.repo
44+
45+
const pr = await github.pulls.get({
46+
owner,
47+
repo,
48+
pull_number: parseInt(${{ steps.find-pull-request.outputs.number }})
49+
})
50+
51+
const prCreator = pr.user.login
52+
53+
// If the PR creator is the expected account, stop now
54+
if (prCreator === 'Octomerger') {
55+
return
56+
}
57+
58+
try {
59+
await github.teams.getMembershipForUserInOrg({
60+
org: 'github',
61+
team_slug: 'employees',
62+
username: prCreator
63+
})
64+
65+
// If the PR creator is a GitHub employee, stop now
66+
return
67+
} catch (err) {
68+
// An error will be thrown if the user is not a GitHub employee.
69+
// That said, we still want to proceed anyway!
70+
}
71+
72+
// Close the PR and add the invalid label
73+
await github.issues.update({
74+
owner,
75+
repo,
76+
issue_number: pr.number,
77+
labels: ['invalid'],
78+
state: 'closed'
79+
})
80+
81+
// Comment on the PR
82+
await github.issues.createComment({
83+
owner,
84+
repo,
85+
issue_number: pr.number,
86+
body: "Please leave this `repo-sync` branch to the robots!\n\nI'm going to close this pull request now, but feel free to open a new issue or ask any questions in [discussions](https://github.com/github/docs/discussions)!"
87+
})
88+
1989
repo-sync:
90+
needs: close-invalid-repo-sync
2091
if: github.repository == 'github/docs-internal' || github.repository == 'github/docs'
2192
name: Repo Sync
2293
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)