Repository navigation
71 lines (67 loc) · 3.28 KB
/
Copy pathpr-target-check.yml
File metadata and controls
71 lines (67 loc) · 3.28 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
name: PR target check
on:
pull_request_target:
types: [opened, reopened, edited, synchronize]
branches: [master]
permissions:
contents: read
pull-requests: write
jobs:
redirect:
runs-on: ubuntu-latest
steps:
- name: Check for an authorized WordPress.org PR
id: target
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
with:
result-encoding: string
script: |
const pull = context.payload.pull_request;
if (pull.head.repo?.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
return 'retarget';
}
if (/^release\/[0-9]+\.[0-9]+\.[0-9]+$/.test(pull.head.ref)) {
return 'allow';
}
if (!/^wordpress-org\/[a-z0-9][a-z0-9._-]*$/.test(pull.head.ref)) {
return 'retarget';
}
let permission = '';
try {
const response = await github.rest.repos.getCollaboratorPermissionLevel({
...context.repo,
username: pull.user.login,
});
permission = response.data.permission;
} catch (error) {
if (error.status !== 404) throw error;
}
if (!['admin', 'maintain', 'write'].includes(permission)) {
return 'retarget';
}
const files = await github.paginate(github.rest.pulls.listFiles, {
...context.repo,
pull_number: pull.number,
per_page: 100,
});
const allowedPath = (filename) =>
filename === 'readme.txt' || filename.startsWith('.wordpress-org/');
const removesReadme = files.some((file) =>
(file.filename === 'readme.txt' && file.status === 'removed') ||
(file.status === 'renamed' && file.previous_filename === 'readme.txt')
);
const allowed = files.length > 0 && !removesReadme && files.every((file) =>
allowedPath(file.filename) &&
(file.status !== 'renamed' || allowedPath(file.previous_filename || ''))
);
return allowed ? 'allow' : 'retarget';
- name: Retarget to develop
if: steps.target.outputs.result == 'retarget'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
gh pr edit "${PR_NUMBER}" --repo "${REPO}" --base develop
gh pr comment "${PR_NUMBER}" --repo "${REPO}" \
--body "Heads up — this PR was retargeted to \`develop\`. Master only accepts full releases from \`release/X.Y.Z\` or readme/assets-only updates from \`wordpress-org/<topic>\`."