-
Notifications
You must be signed in to change notification settings - Fork 15
84 lines (75 loc) · 2.8 KB
/
gate.yml
File metadata and controls
84 lines (75 loc) · 2.8 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
name: Gate
on:
issues:
types: [opened]
pull_request_target:
types: [opened]
permissions:
issues: write
pull-requests: write
jobs:
check-membership:
runs-on: ubuntu-latest
steps:
- name: Check org membership and close if external
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const sender = context.payload.sender.login;
const { owner, repo } = context.repo;
// Check if user is an org member
let isMember = false;
try {
const { status } = await github.rest.orgs.checkMembershipForUser({
org: owner,
username: sender,
});
isMember = status === 204 || status === 302;
} catch (e) {
isMember = false;
}
if (isMember) {
console.log(`${sender} is an org member of ${owner}, allowing.`);
return;
}
// Check if user is a repo collaborator
let isCollaborator = false;
try {
const { status } = await github.rest.repos.checkCollaborator({
owner,
repo,
username: sender,
});
isCollaborator = status === 204;
} catch (e) {
isCollaborator = false;
}
if (isCollaborator) {
console.log(`${sender} is a collaborator on ${owner}/${repo}, allowing.`);
return;
}
console.log(`${sender} is NOT a member or collaborator, closing.`);
if (context.payload.issue) {
await github.rest.issues.update({
...context.repo,
issue_number: context.payload.issue.number,
state: 'closed',
});
await github.rest.issues.createComment({
...context.repo,
issue_number: context.payload.issue.number,
body: 'This repository only accepts issues from organization members and collaborators. Your issue has been closed automatically.',
});
} else if (context.payload.pull_request) {
await github.rest.pulls.update({
...context.repo,
pull_number: context.payload.pull_request.number,
state: 'closed',
});
await github.rest.issues.createComment({
...context.repo,
issue_number: context.payload.pull_request.number,
body: 'This repository only accepts pull requests from organization members and collaborators. Your PR has been closed automatically.',
});
}