-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (57 loc) · 2.12 KB
/
index.js
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
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
// Your code here
app.log.info('Yay, the app was loaded!');
async function get_reviews(context) {
const {data: reviews} = await context.octokit.pulls.listReviews({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
});
return reviews.filter((review) => review.state === 'APPROVED').length;
}
async function labelExists(context, labelName) {
const {data: labels} =
await context.octokit.issues.listLabelsOnIssue(context.issue());
return labels.some((label) => label.name === labelName);
}
app.on('pull_request.opened', async (context) => {
return context.octokit.issues.addLabels(
context.issue({labels: ['PR: Needs first approval']}));
})
app.on('pull_request_review.submitted', async (context) => {
// does not work. Why?
try {
const count = await get_reviews(context);
if (count == 1) {
if (!await labelExists(context, 'PR: Needs second approval')) {
context.octokit.issues.addLabels(
context.issue({labels: ['PR: Needs second approval']}));
}
if (await labelExists(context, 'PR: Needs first approval')) {
context.octokit.issues.removeLabel(
context.issue({name: 'PR: Needs first approval'}));
}
} else if (count == 2) {
if (!await labelExists(context, 'PR: Awaiting merge')) {
context.octokit.issues.addLabels(
context.issue({labels: ['PR: Awaiting merge']}));
}
if (await labelExists(context, 'PR: Needs second approval')) {
context.octokit.issues.removeLabel(
context.issue({name: 'PR: Needs second approval'}));
}
}
} catch (error) {
// handle error
console.log(error);
}
});
// For more information on building apps:
// https://probot.github.io/docs/
// To get your app running against GitHub, see:
// https://probot.github.io/docs/development/
};