-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
185 lines (161 loc) · 7.24 KB
/
index.ts
File metadata and controls
185 lines (161 loc) · 7.24 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Probot, ProbotOctokit } from "probot";
import { OctokitResponse, GetResponseTypeFromEndpointMethod } from "@octokit/types";
type Octokit = InstanceType<typeof ProbotOctokit>;
type PullsListFilesResponseData = GetResponseTypeFromEndpointMethod<Octokit["pulls"]["listFiles"]>["data"]
const NEW_PACK = "pack added";
const REMOVE_PACK = "pack removed";
const PACK_CHANGE = "pack change";
const NON_AUTHOR_PACK_CHANGE = "non-author pack change";
const READY_TO_MERGE = "ready to merge";
const URL_ISSUE = "Invalid URL";
const TEAM = {
org: "runelite",
team_slug: "plugin-approvers",
};
// If you run the app outside of an org it can't get to the org's teams
let globalTeamGithub : Octokit | undefined;
if (process.env.TEAM_TOKEN) {
globalTeamGithub = new ProbotOctokit({auth: {token: process.env.TEAM_TOKEN}});
}
export = (app: Probot) => {
app.on(['pull_request.opened', 'pull_request.synchronize', 'pull_request.reopened'], async context => {
const github = context.octokit;
let { data: labelList } = await github.issues.listLabelsOnIssue(context.issue());
let labels = new Set(labelList.map(l => l.name));
const setHasLabel = async (condition: boolean, label: string) => {
if (condition && !labels.has(label)) {
await github.issues.addLabels(context.issue({ labels: [label] }));
} else if (!condition && labels.has(label)) {
await github.issues.removeLabel(context.issue({ name: label }));
}
};
await setHasLabel(false, READY_TO_MERGE);
let pluginFiles: PullsListFilesResponseData = [];
let dependencyFiles: PullsListFilesResponseData = [];
let otherFiles: PullsListFilesResponseData = [];
(await github.pulls.listFiles(context.pullRequest()))
.data
.forEach(f => {
if (f.filename.startsWith("packs/")) {
pluginFiles.push(f);
} else {
otherFiles.push(f);
}
});
await setHasLabel(pluginFiles.some(f => f.status == "added"), NEW_PACK);
await setHasLabel(pluginFiles.some(f => f.status == "modified"), PACK_CHANGE);
await setHasLabel(pluginFiles.some(f => f.status == "removed"), REMOVE_PACK);
let diffLines: string[] = [];
const prAuthor = (await github.issues.get(context.issue())).data.user!.login.toLowerCase();
let nonAuthorChange = false;
let urlvalid = true;
await Promise.all(pluginFiles.map(async file => {
let pluginName = file.filename.replace("packs/", "");
if (file.status == "removed") {
diffLines.push(`Removed resource Pack \`${pluginName}\`. `);
}
let readKV = (res: OctokitResponse<string>) => res.data.split("\n")
.map(i => /([^=]+)=(.*)/.exec(i))
.filter(i => i)
.reduce((acc: { [key: string]: string }, val) => {
acc[val![1]] = val![2];
return acc;
}, {});
let newPlugin = readKV(await github.request(file.raw_url));
let extractURL = (cloneURL: string) => {
let urlmatch = /https:\/\/github\.com\/([^/]+)\/([^.]+)/.exec(cloneURL);
if (!urlmatch) {
urlvalid = false;
throw `Pack repository must be a github https clone url, not \`${cloneURL}\``;
}
let [, user, repo] = urlmatch;
return { user, repo };
};
let { user, repo } = extractURL(newPlugin.repository);
let changedPluginAuthors: Set<string> = new Set();
let sanitizeAuthor = (author: string) => author.trim().toLowerCase();
let addPluginAuthors = (authors?: string) => {
if (!authors) {
return;
}
authors.split(',')
.forEach(author => changedPluginAuthors.add(sanitizeAuthor(author)));
};
changedPluginAuthors.add(sanitizeAuthor(user));
addPluginAuthors(newPlugin.authors);
if (file.status == "modified") {
let oldPlugin = readKV(await github.request(`https://github.com/${context.repo().owner}/${context.repo().repo}/raw/main/packs/${pluginName}`));
let oldPluginURL = extractURL(oldPlugin.repository);
changedPluginAuthors.add(sanitizeAuthor(oldPluginURL.user));
addPluginAuthors(oldPlugin.authors);
diffLines.push(`\`${pluginName}\`: [${oldPlugin.commit}...${newPlugin.commit}](https://github.com/${oldPluginURL.user}/${oldPluginURL.repo}/compare/${oldPlugin.commit}...${user}:${newPlugin.commit})`);
} else if (file.status == "added") {
diffLines.push(`New pack \`${pluginName}\`: https://github.com/${user}/${repo}/tree/${newPlugin.commit}`);
} else if (file.status == "renamed") {
let oldPluginName = ((file as any).previous_filename as string).replace("packs/", "");
let oldPlugin = readKV(await github.request(`https://github.com/${context.repo().owner}/${context.repo().repo}/raw/main/packs/${oldPluginName}`));
let oldPluginURL = extractURL(oldPlugin.repository);
diffLines.push(`\`${oldPluginName}\` renamed to \`${pluginName}\`; this will cause all current installs to become uninstalled.
[${oldPlugin.commit}...${newPlugin.commit}](https://github.com/${oldPluginURL.user}/${oldPluginURL.repo}/compare/${oldPlugin.commit}...${user}:${newPlugin.commit})`);
} else {
diffLines.push(`What is a \`${file.status}\`?`);
}
nonAuthorChange ||= !changedPluginAuthors.has(prAuthor);
}));
let difftext = diffLines.join("\n\n");
if (nonAuthorChange) {
difftext = "**Includes changes by non-author**\n\n" + difftext;
await setHasLabel(true, NON_AUTHOR_PACK_CHANGE);
} else {
await setHasLabel(false, NON_AUTHOR_PACK_CHANGE);
}
if (!urlvalid) {
difftext = "**Plugin repository must be a github https clone url**\n\n" + difftext;
await setHasLabel(true, URL_ISSUE);
} else {
await setHasLabel(false, URL_ISSUE);
}
if (dependencyFiles.length > 0 || otherFiles.length > 0) {
difftext = "**Includes non-pack changes**\n\n" + difftext;
}
let marker = "<!-- rlphc -->";
let body = marker + "\n" + difftext;
let sticky = (await github.issues.listComments(context.issue()))
.data.find(c => c.body?.startsWith(marker));
if (sticky) {
await github.issues.updateComment(context.issue({ comment_id: sticky.id, body }));
} else if (difftext) {
await github.issues.createComment(context.issue({ body }));
}
});
app.on(["pull_request_review.submitted"], async context => {
const github = context.octokit;
const teamGithub = globalTeamGithub || github;
let { data: labelList } = await github.issues.listLabelsOnIssue(context.issue());
let labels = new Set(labelList.map(l => l.name));
if (!labels.has(PACK_CHANGE)) return;
let { data: reviews } = await github.pulls.listReviews(context.pullRequest());
if (reviews.length <= 0) return;
let { data: memberList } = await teamGithub.teams.listMembersInOrg(TEAM);
let members = new Set(memberList.map(m => m.login));
let reviewStates: { [key: string]: boolean } = {};
reviews.filter(r => r.user && members.has(r.user.login))
.forEach(r => {
let approved = r.state == "APPROVED" || r.body == "lgtm";
if (approved || (!approved && r.state != "COMMENTED")) {
reviewStates[r.user!.login] = approved;
}
});
let unapproved = Object.keys(reviewStates).filter(k => !reviewStates[k]);
if (unapproved.length > 0) {
console.log(`Unapproved for #${context.issue().issue_number}: ${unapproved}`);
if (labels.has(READY_TO_MERGE)) {
await github.issues.removeLabel(context.issue({ name: READY_TO_MERGE }));
}
} else if (Object.keys(reviewStates).length != 0) {
if (!labels.has(READY_TO_MERGE)) {
await github.issues.addLabels(context.issue({ labels: [READY_TO_MERGE] }));
}
}
});
}