-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathauto-close-restricted-paths.yml
More file actions
107 lines (91 loc) · 4.03 KB
/
Copy pathauto-close-restricted-paths.yml
File metadata and controls
107 lines (91 loc) · 4.03 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
name: 'Auto-close restricted path edits'
# This workflow automatically closes pull requests from external contributors
# that attempt to modify files within restricted directories (like .github or libs/gltfio).
#
# Pull Requests carrying the "internal" label/tag are exempt from this
# auto-close restriction (serving as an escape hatch).
#
# We use 'pull_request_target' because it runs in the context of the base
# repository, granting it the necessary permissions to close PRs from forks.
on:
pull_request_target:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
auto-close-restricted-paths:
runs-on: ubuntu-latest
steps:
- name: Auto-close PR if it touches restricted paths
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
env:
MSG_GITHUB: >
Thank you for your contribution. To maintain the security and stability of our CI/CD pipelines,
modifications to the `.github` directory are restricted to project maintainers.
As a result, this Pull Request has been automatically closed. If you have suggestions for
improvements or bug fixes related to our GitHub configuration, please open an issue to discuss
them with the team first. We appreciate your understanding.
MSG_GLTFIO: >
Thank you for your contribution. To ensure consistency and stability of the glTF loader library,
modifications to the `libs/gltfio` directory are restricted to project maintainers.
As a result, this Pull Request has been automatically closed. If you have suggestions for
improvements or bug fixes, please open an issue to discuss them with the team first. We appreciate
your understanding.
with:
script: |
const { owner, repo } = context.repo;
const pull_number = context.payload.pull_request.number;
const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username: context.payload.pull_request.user.login,
});
const isMaintainer = ['admin', 'maintain', 'write'].includes(permission.permission);
if (isMaintainer) {
console.log(`User ${context.payload.pull_request.user.login} is a maintainer. Skipping check.`);
return;
}
const labels = context.payload.pull_request.labels || [];
const hasInternalLabel = labels.some(l => l.name === 'internal');
if (hasInternalLabel) {
console.log(`Pull Request #${pull_number} has the "internal" label. Skipping restricted paths check.`);
return;
}
const files = await github.paginate(github.rest.pulls.listFiles, {
owner,
repo,
pull_number,
});
const restrictions = [
{
path: '.github/',
message: process.env.MSG_GITHUB.trim()
},
{
path: 'libs/gltfio/',
message: process.env.MSG_GLTFIO.trim()
}
];
let matchedRestriction = null;
for (const restriction of restrictions) {
if (files.some(f => f.filename.startsWith(restriction.path))) {
matchedRestriction = restriction;
break;
}
}
if (matchedRestriction) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: matchedRestriction.message,
});
await github.rest.pulls.update({
owner,
repo,
pull_number,
state: 'closed',
});
console.log(`Closed PR #${pull_number} because it touched the restricted path: ${matchedRestriction.path}`);
}