-
Notifications
You must be signed in to change notification settings - Fork 0
131 lines (120 loc) · 4.49 KB
/
approved-issues-only.yml
File metadata and controls
131 lines (120 loc) · 4.49 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: Approved Issues Only
on:
issues:
types: [opened, reopened]
permissions:
issues: write
contents: read
env:
UNAPPROVED_LABEL: unapproved
UNAPPROVED_ISSUE_COMMENT: "This repository is invite-only. Issues from unapproved actors are automatically closed."
APPROVED_ACTORS_FILE: .github/approved-actors.yml
jobs:
enforce-approved-issues:
runs-on: ubuntu-latest
steps:
- name: Enforce invite-only issue participation
uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.payload.issue.number;
const opener = context.payload.issue.user.login;
const label = process.env.UNAPPROVED_LABEL;
const comment = process.env.UNAPPROVED_ISSUE_COMMENT;
const whitelistPath = process.env.APPROVED_ACTORS_FILE;
async function loadWhitelist() {
try {
const { data } = await github.request(
"GET /repos/{owner}/{repo}/contents/{path}",
{ owner, repo, path: whitelistPath }
);
if (!data?.content) return new Set();
const text = Buffer.from(data.content, "base64").toString("utf8");
const users = [];
let inUsers = false;
for (const raw of text.split(/\r?\n/)) {
const line = raw.trim();
if (!line || line.startsWith("#")) continue;
if (line === "users:" || line.startsWith("users:")) {
inUsers = true;
continue;
}
if (inUsers && line.startsWith("- ")) {
users.push(line.slice(2).trim());
continue;
}
if (inUsers && !line.startsWith("- ")) {
break;
}
}
return new Set(users);
} catch (error) {
core.warning(`Unable to read ${whitelistPath}: ${error.message}`);
return new Set();
}
}
const whitelist = await loadWhitelist();
const whitelisted = whitelist.has(opener);
let collaborator = false;
try {
const resp = await github.request(
"GET /repos/{owner}/{repo}/collaborators/{username}",
{ owner, repo, username: opener }
);
collaborator = resp.status === 204;
} catch (error) {
if (error.status !== 404) {
core.warning(`Collaborator check returned ${error.status}: ${error.message}`);
}
collaborator = false;
}
const approved = collaborator || whitelisted;
if (approved) {
core.info(`${opener} is approved (collaborator=${collaborator}, whitelisted=${whitelisted}); no action needed.`);
return;
}
try {
await github.request("GET /repos/{owner}/{repo}/labels/{name}", {
owner,
repo,
name: label,
});
} catch (error) {
if (error.status === 404) {
try {
await github.request("POST /repos/{owner}/{repo}/labels", {
owner,
repo,
name: label,
color: "B60205",
description: "Issue or PR opened by a non-collaborator in invite-only participation mode",
});
} catch (createError) {
if (createError.status !== 422) {
throw createError;
}
}
} else {
throw error;
}
}
await github.request("POST /repos/{owner}/{repo}/issues/{issue_number}/labels", {
owner,
repo,
issue_number,
labels: [label],
});
await github.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", {
owner,
repo,
issue_number,
body: comment,
});
await github.request("PATCH /repos/{owner}/{repo}/issues/{issue_number}", {
owner,
repo,
issue_number,
state: "closed",
});