-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
270 lines (216 loc) · 6.98 KB
/
main.ts
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import dayjs from "https://deno.land/x/deno_dayjs/mod.ts";
import { cron } from "https://deno.land/x/deno_cron/cron.ts";
import { serve } from "https://deno.land/std/http/server.ts";
import { parse } from "https://deno.land/std/flags/mod.ts";
import "https://deno.land/x/dotenv/load.ts";
type PullRequest = {
title: string;
author: string;
url: string;
target: string;
};
type BitbucketConfig = {
baseUrl: string;
endpoint: string;
username: string;
password: string;
patterns: string[];
authors: string[];
};
type BitbucketResponse = {
pagelen: number;
values: PullRequestsResult[];
page: number;
size: number;
};
type PullRequestsResult = {
title: string;
author: { display_name: string };
links: { html: { href: string } };
destination: { branch: { name: string } };
};
type FlockConfig = {
baseUrl: string;
channel: string;
};
type Period = {
time: number;
interval: string;
};
const FETCH_DELAY = +(Deno.env.get("APP_FETCH_DELAY") ?? 5);
const FETCH_INTERVAL = Deno.env.get("APP_FETCH_INTERVAL") ?? "minutes";
const BULK_DELAY = +(Deno.env.get("APP_BULK_DELAY") ?? 1);
const BULK_INTERVAL = Deno.env.get("APP_BULK_INTERVAL") ?? "day";
const getOpenPullRequests = async (
config: BitbucketConfig,
period: Period,
): Promise<PullRequest[]> => {
const { time, interval } = period;
const datetime = dayjs().subtract(time, interval).toISOString();
// setup url query
const branchNames = config.patterns.map((pattern) =>
`source.branch.name ~ "${pattern}"`
).join(" OR ");
const authorNames = config.authors;
const query =
`?q=state="OPEN" AND (${branchNames}) AND created_on >= ${datetime}&sort=-updated_on&pagelen=50`;
const url = encodeURI(`${config.baseUrl}${config.endpoint}${query}`);
const credential = btoa(`${config.username}:${config.password}`);
const res = await fetch(url, {
method: "GET",
headers: {
"Authorization": `Basic ${credential}`,
"Content-Type": "application/json",
"Accept": "application/json",
},
});
const json: BitbucketResponse = await res.json();
if (!json || !json?.values) {
return [];
}
// filter & map open pull requests
const pullRequests = json.values
.filter(({ author }) => authorNames.includes(author.display_name))
.map(({ title, author, links, destination }) => {
return {
title: title,
author: author.display_name,
url: links.html.href,
target: destination.branch.name,
};
});
return pullRequests;
};
const sendToFlock = async (config: FlockConfig, message: string) => {
const res = await fetch(`${config.baseUrl}/${config.channel}`, {
method: "POST",
body: JSON.stringify({ flockml: message }),
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
});
const json = await res.json();
return json;
};
const pickMessage = async (
title: string,
url: string,
author: string,
isTargetRelease = false,
): Promise<string> => {
if (isTargetRelease) {
const message =
`<flockml>minta tolong review ya<br/><a href="${url}">${title}</a> by ${author}</flockml>`;
return message;
}
const greetersFile = await Deno.readTextFile("./greeters.txt");
const greeters = greetersFile.split("\n");
const idx = Math.floor(Math.random() * greeters.length);
const greeter = greeters[idx];
const message =
`<flockml>${greeter}<br/><a href="${url}">${title}</a> by ${author}</flockml>`;
return message;
};
const pickBulkMessage = (greeter: string, pullRequests: PullRequest[]) => {
const links = pullRequests.map(({ title, author, url }) =>
`<a href="${url}">${title}</a> by ${author}`
).join("<br/>");
const message = `<flockml>${greeter}<br/>${links}</flockml>`;
return message;
};
const jakartaTime = () => {
return dayjs().add(7, "hours").format();
};
const main = async (config = { bulk: false }) => {
// grab list of PR that need to be review
const bitbucketConfig = {
baseUrl: Deno.env.get("BITBUCKET_BASE_URL")!,
endpoint: "/repositories/" + Deno.env.get("BITBUCKET_REPOSITORY") +
"/pullrequests",
username: Deno.env.get("BITBUCKET_USERNAME")!,
password: Deno.env.get("BITBUCKET_PASSWORD")!,
patterns: Deno.env.get("PR_PATTERNS")!.split(","),
authors: Deno.env.get("PR_AUTHORS")!.split(","),
};
const period = { time: FETCH_DELAY, interval: FETCH_INTERVAL };
if (config.bulk) {
period.time = BULK_DELAY;
period.interval = BULK_INTERVAL;
}
const pullRequests = await getOpenPullRequests(bitbucketConfig, period);
console.log(
`[${jakartaTime()}] Found ${pullRequests.length} PR(s)`,
);
if (!pullRequests?.length) return;
// build flock message & send to flock channel
const flockConfig: FlockConfig = {
baseUrl: Deno.env.get("FLOCK_BASE_URL")!,
channel: Deno.env.get("FLOCK_CHANNEL")!,
};
const flockConfigRelease = {
...flockConfig,
channel: Deno.env.get("FLOCK_REVIEW_CHANNEL")!,
};
if (config.bulk) {
const message = pickBulkMessage(
`masih ada <b>${pullRequests.length}</b> PR yang OPEN, dibantu review ya`,
pullRequests,
);
sendToFlock(flockConfig, message);
const releases = pullRequests.filter(({ target }) =>
target.search("master|release") !== -1
);
if (!releases?.length) return;
const messageRelease = pickBulkMessage(
`tolong bantu review PR utk release ya`,
releases,
);
sendToFlock(flockConfigRelease, messageRelease);
return;
}
return await Promise.allSettled(
pullRequests.map(async ({ title, author, url, target }) => {
if (target.search("master|release") !== -1) {
const message = await pickMessage(title, url, author, true);
await sendToFlock(flockConfigRelease, message);
}
const message = await pickMessage(title, url, author);
await sendToFlock(flockConfig, message);
}),
);
};
// schedule script to run every x minute
await cron(`1 */${FETCH_DELAY} * * * *`, async () => {
console.log(`[${jakartaTime()}] Starting PR Reminder`);
try {
await main();
} catch (e) {
console.error(`[${jakartaTime()}] Error Happen: `, e);
}
console.log(`[${jakartaTime()}] Finished PR Reminder`);
});
// schedule script to run every morning & afternoon
await cron("1 * * * * *", async () => {
const time = dayjs().add(7, "hours").format("HH:mm");
const day = dayjs().add(7, "hours").format("ddd");
const scheduleTimes = Deno.env.get("APP_BULK_SCHEDULE")!.split(",");
if (!scheduleTimes.includes(time) || ["Sun", "Sat"].includes(day)) return;
console.log(
`[${jakartaTime()}] Starting Bulk PR Reminder`,
);
try {
await main({ bulk: true });
} catch (e) {
console.error(`[${jakartaTime()}] Error Happen: `, e);
}
console.log(
`[${jakartaTime()}] Finished Bulk PR Reminder`,
);
});
// health check
const port = +(parse(Deno.args).port ?? Deno.env.get("APP_PORT") ?? 8000);
const handler = (_request: Request): Response => {
return new Response("OK", { status: 200 });
};
await serve(handler, { port });