forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue.ts
More file actions
244 lines (212 loc) · 7.18 KB
/
Copy pathissue.ts
File metadata and controls
244 lines (212 loc) · 7.18 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { LogReturn } from "ubiquibot-logger";
import { Context } from "../types/context";
import { HandlerReturnValuesNoVoid } from "../types/handlers";
import { GitHubComment } from "../types/payload";
export async function clearAllPriceLabelsOnIssue(context: Context) {
const payload = context.payload;
if (!payload.issue) return;
const labels = payload.issue.labels;
const issuePrices = labels.filter((label) => label.name.toString().startsWith("Price: "));
if (!issuePrices.length) return;
try {
await context.event.octokit.issues.removeLabel({
...context.event.issue(),
name: issuePrices[0].name,
});
} catch (e: unknown) {
context.logger.fatal("Clearing all price labels failed!", e);
}
}
export async function addLabelToIssue(context: Context, labelName: string) {
const payload = context.payload;
if (!payload.issue) {
throw context.logger.fatal("Issue object is null");
}
try {
await context.octokit.issues.addLabels({
...context.event.issue(),
labels: [labelName],
});
} catch (e: unknown) {
context.logger.fatal("Adding a label to issue failed!", e);
}
}
export async function addCommentToIssue(
context: Context,
message: HandlerReturnValuesNoVoid,
issueNumber: number,
owner?: string,
repo?: string
) {
let comment = message as string;
if (message instanceof LogReturn) {
comment = message.logMessage.diff;
console.trace(
"one of the places that metadata is being serialized as an html comment. this one is unexpected and serves as a fallback"
);
const metadataSerialized = JSON.stringify(message.metadata);
const metadataSerializedAsComment = `<!-- ${metadataSerialized} -->`;
comment = comment.concat(metadataSerializedAsComment);
}
const payload = context.payload;
try {
await context.octokit.issues.createComment({
owner: owner ?? payload.repository.owner.login,
repo: repo ?? payload.repository.name,
issue_number: issueNumber,
body: comment,
});
} catch (e: unknown) {
context.logger.fatal("Adding a comment failed!", e);
}
}
// async function upsertLastCommentToIssue(context: Context, issueNumber: number, commentBody: string) {
// try {
// const comments = await getAllIssueComments(context, issueNumber);
// if (comments.length > 0 && comments[comments.length - 1].body !== commentBody)
// await addCommentToIssue(context, commentBody, issueNumber);
// } catch (e: unknown) {
// context.logger.fatal("Upserting last comment failed!", e);
// }
// }
// async function getIssueDescription(
// context: Context,
// issueNumber: number,
// format: "raw" | "html" | "text" = "raw"
// ): Promise<string> {
// const payload = context.payload;
// try {
// const response = await context.octokit.rest.issues.get({
// owner: payload.repository.owner.login,
// repo: payload.repository.name,
// issue_number: issueNumber,
// mediaType: {
// format,
// },
// });
// let result = response.data.body;
// if (format === "html") {
// result = response.data.body_html;
// } else if (format === "text") {
// result = response.data.body_text;
// }
// return result as string;
// } catch (e: unknown) {
// throw context.logger.fatal("Fetching issue description failed!", e);
// }
// }
export async function getAllIssueComments(
context: Context,
issueNumber: number,
format: "raw" | "html" | "text" | "full" = "raw"
): Promise<GitHubComment[]> {
const payload = context.payload;
try {
const comments = (await context.octokit.paginate(context.octokit.rest.issues.listComments, {
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: issueNumber,
per_page: 100,
mediaType: {
format,
},
})) as GitHubComment[];
return comments;
} catch (e: unknown) {
context.logger.fatal("Fetching all issue comments failed!", e);
return [];
}
}
export async function isUserAdminOrBillingManager(
context: Context,
username: string
): Promise<"admin" | "billing_manager" | false> {
const payload = context.payload;
const isAdmin = await checkIfIsAdmin();
if (isAdmin) return "admin";
const isBillingManager = await checkIfIsBillingManager();
if (isBillingManager) return "billing_manager";
return false;
async function checkIfIsAdmin() {
const response = await context.octokit.rest.repos.getCollaboratorPermissionLevel({
owner: payload.repository.owner.login,
repo: payload.repository.name,
username,
});
if (response.data.permission === "admin") {
return true;
} else {
return false;
}
}
async function checkIfIsBillingManager() {
if (!payload.organization) throw context.logger.fatal(`No organization found in payload!`);
try {
const { data: membership } = await context.octokit.rest.orgs.getMembershipForUser({
org: payload.organization.login,
username: payload.repository.owner.login,
});
console.trace(membership);
return membership.role === "billing_manager";
} catch (e) {
context.logger.error(
`Could not get the Billing Manager status for ${payload.repository.owner.login} within ${payload.organization.login}`,
e
);
return false;
}
}
}
export async function addAssignees(context: Context, issue: number, assignees: string[]) {
const payload = context.payload;
try {
await context.octokit.rest.issues.addAssignees({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: issue,
assignees,
});
} catch (e: unknown) {
context.logger.fatal("Adding assignees failed!", e);
}
}
export async function getAllPullRequests(context: Context, state: "open" | "closed" | "all" = "open") {
const payload = context.payload;
try {
const pulls = await context.octokit.paginate(context.octokit.rest.pulls.list, {
owner: payload.repository.owner.login,
repo: payload.repository.name,
state,
per_page: 100,
});
return pulls;
} catch (err: unknown) {
context.logger.fatal("Fetching all pull requests failed!", err);
return [];
}
}
// async function getReviewRequests(context: Context, pullNumber: number, owner: string, repo: string) {
// try {
// const response = await context.octokit.pulls.listRequestedReviewers({
// owner: owner,
// repo: repo,
// pull_number: pullNumber,
// });
// return response.data;
// } catch (err: unknown) {
// context.logger.fatal("Could not get requested reviewers", err);
// return null;
// }
// }
// Get issues assigned to a username
// async function getOpenedPullRequestsForAnIssue(context: Context, issueNumber: number, userName: string) {
// const pulls = await getOpenedPullRequests(context, userName);
// return pulls.filter((pull) => {
// if (!pull.body) return false;
// const issues = pull.body.match(/#(\d+)/gi);
// if (!issues) return false;
// const linkedIssueNumbers = Array.from(new Set(issues.map((issue) => issue.replace("#", ""))));
// if (linkedIssueNumbers.indexOf(`${issueNumber}`) !== -1) return true;
// return false;
// });
// }