forked from ShuryginaA/devtools-course-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.57 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
const http = require('http');
const fs = require('fs');
const axios = require('axios');
const { Octokit } = require("@octokit/core");
const { createAppAuth } = require("@octokit/auth-app");
const port = process.env.PORT || 80;
const installationOctokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: 202493,
privateKey: process.env.PRIVATE_KEY,
installationId: 25823314,
},
});
async function checkReadiness(pull_id) {
// Count number of unique approvers
const result = await installationOctokit.request("GET /repos/{owner}/{repo}/pulls/{pull_id}/reviews", {
owner: "UNN-ITMM-Software",
repo: "devtools-course-practice",
pull_id: pull_id,
});
let approvers = new Set();
result.data.forEach(entry => {
if (entry.state == 'APPROVED') {
approvers.add(entry.user.login);
}
});
if (approvers.size < 2)
return;
// Put a readiness label
installationOctokit.request("POST /repos/{owner}/{repo}/issues/{pull_id}/labels", {
owner: "UNN-ITMM-Software",
repo: "devtools-course-practice",
pull_id: pull_id,
labels: ["Ready for merge"],
});
}
http.createServer(function (req, res) {
// Get JSON data for POST request
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
body = JSON.parse(body);
if (body.review.state == "approved") {
checkReadiness(body.pull_request.number);
}
res.end();
});
}).listen(port);