-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
89 lines (83 loc) · 3.7 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const PROD = true;
const GITHUB_TOKEN = require("./keys.js");
if (PROD) {
var REPO_OWNER = "Chaosthebot";
var REPO_NAME = "chaos2";
} else {
var REPO_OWNER = "Smittyvb";
var REPO_NAME = "chaos-2";
}
const THRESHOLD = 4;
const { exec } = require('child_process');
const UPVOTES = ["+1", "upvote", "i like it", "i really like it", "👍"];
const DOWNVOTES = ["-1", "downvote", "i dislike it", "i really dislike it", "i hate it", "i really hate it", "👎"];
var GitHub = require('github-api');
// basic auth
var gh = new GitHub({
username: 'Smittyvb',
token: GITHUB_TOKEN
});
var rateLimit = 500; //Wait between requests, to stay under rate limits. Before we get data, assume that it's high.
gh.getRateLimit().getRateLimit().then(function (newRateLimit) {
rateLimit = ((newRateLimit.data.resources.core.reset - (Date.now() / 1000)) * 1000) / newRateLimit.data.resources.core.remaining;
rateLimit = 1 / rateLimit;
console.log("Got rate limit: " + (rateLimit * 1).toFixed(19) + " reqs/s.");
});
var repo = gh.getRepo(REPO_OWNER, REPO_NAME);
var issues = gh.getIssues(REPO_OWNER, REPO_NAME);
function lookAtPrs() {
repo.listPullRequests({
state: "open",
base: "master" //ignore non-master PRs
}).then(function (prs) {
prs = prs.data;
prs.forEach(function (prOverview) {
issues.listIssueComments(prOverview.number).then(function (comments) {
var score = 0;
var voters = []; //anyone in this list will have their votes ignored
comments = comments.data;
comments.forEach(function (comment) {
if (voters.indexOf(comment.user.id) > -1) {
if (PROD) {
return;
}
}
var commentBody = comment.body.toLowerCase();
for (var upIndex = 0; upIndex < UPVOTES.length; upIndex++) {
if ((commentBody.indexOf(UPVOTES[upIndex]) > -1)) {
score++;
voters.push(comment.user.id);
return;
}
}
for (var downIndex = 0; downIndex < DOWNVOTES.length; downIndex++) {
if ((commentBody.indexOf(DOWNVOTES[downIndex]) > -1)) {
score--;
voters.push(comment.user.id);
return;
}
}
});
console.log(score);
if (score >= THRESHOLD) {
repo.mergePullRequest(prOverview.number, {
commit_message: "Merging #" + prOverview.number + ", with a score of " + score + "."
}).then(function (res) {
if (res.status === 200) {
console.log("Suceessfully merged pull request #" + prOverview.number + "!");
exec("git pull", function () {
require('child_process').execSync('node index');
console.log("An update caused the new server code to exit: #" + prOverview.number);
process.exit(1);
});
} else {
console.log("Attempted, but failed, to merge #" + prOverview.number + ", with a status of " + res.status);
}
});
}
});
});
});
}
lookAtPrs();
setInterval(lookAtPrs, 30000)