This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprPayload.js
281 lines (248 loc) · 9.25 KB
/
prPayload.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
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
271
272
273
274
275
276
277
278
279
280
281
// Description:
// Serves a daily PR report of PR's that have not been updated within 24-72 + hours
//
// Dependencies:
// cronjob
// request
// lodash
//
// Configuration:
// GITHUB_TOKEN
// HUBOT_SLACK_TOKEN
// Commands:
// show my prs - Sends only your pr's
//
// Author:
// YetiAllMighty
module.exports = function(robot) {
var orgName = "";
var ghRepos, ghUsers;
var request = require("request");
var cronJob = require('cron').CronJob;
var _ = require("lodash");
// 0 0 9 * * 1-5 is for running the task every weekday (mon-1 => fri-5) at 9am
new cronJob('0 0 9 * * 1-5', sendUserPrs, null, true, 'America/Chicago');
startUp();
//used for testing daily task => before using this you must set repos and
//add at least 1 user to github user list by commanding candy i.e. candy set repos
robot.respond(/show my prs/i, function(res) {
sendUserPrs(orgName, res.message.user.name);
});
robot.respond(/update/i, function(res) {
startUp();
res.send("User list and Repo set updated succesfully.");
});
//used to verify persistence has been set
robot.respond(/show repos/i, function(res) {
var ghRepos = robot.brain.get("ghReposBrain");
var payload = "Current Repo List: \n";
payload += ghRepos.join("\n");
res.send(payload);
});
//this will add whatever user you specify ***NOTE This will asign the GH User to the current slack user
//only do this as the user pair you would want to add
robot.respond(/add (.*) to github users/i, function(res) {
var ghUsers = robot.brain.get("ghUsersBrain") || {};
var payload = "Adding: " + res.match[1] + "\n";
ghUsers[res.message.user.name] = res.match[1];
robot.brain.set("ghUsersBrain", ghUsers);
Object.keys(robot.brain.get("ghUsersBrain"))
.forEach(function(user) {
payload += "@" + user + ": " + ghUsers[user] + "\n";
});
res.send(payload);
});
//obvious purpose
robot.respond(/show github users/i, function(res) {
if (Object.keys(robot.brain.get("ghUsersBrain")).length === 0) {
res.send("Currently no users on the list captain!\nTry adding some?");
} else {
var ghUsers = robot.brain.get("ghUsersBrain");
var payload = "Current user list: \n";
Object.keys(robot.brain.get("ghUsersBrain"))
.forEach(function(user) {
payload += "@" + user + ": " + ghUsers[user] + "\n";
});
res.send(payload);
}
});
//this is not constrained by the current user.
//use the SLACK username to remove/undefine the user from the
//user list
robot.respond(/remove (.*) from github users/i, function(res) {
var ghUsers = robot.brain.get("ghUsersBrain");
if (!(res.match[1] in ghUsers)) {
res.send("No one by that name on the list!");
} else {
var payload = "They're gone! \nCurrent User List: \n";
delete ghUsers[res.match[1]];
robot.brain.set("ghUsersBrain", ghUsers);
Object.keys(robot.brain.get("ghUsersBrain"))
.forEach(function(user) {
payload += "@" + user + ": " + ghUsers[user];
});
res.send(payload);
}
});
function getPrs(org, repos, usr, callback) {
var options = {
url: "https://api.github.com/repos/" + org + "/" + repos + "/pulls",
headers: {
'User-Agent': usr,
'Authorization': 'token ' + process.env.GITHUB_TOKEN
}
};
request.get(options, function(err, response, body) {
callback(err, JSON.parse(body));
});
}
function getRepos(org, callback) {
var options = {
url: "https://api.github.com/orgs/" + org + "/repos",
headers: {
'User-Agent': org,
'Authorization': 'token ' + process.env.GITHUB_TOKEN
}
};
request.get(options, function(err, response, body) {
callback(err, JSON.parse(body));
});
}
function getUsers(callback) {
var options = {
url: "https://slack.com/api/users.list",
qs: {
"token": process.env.HUBOT_SLACK_TOKEN
}
};
request.get(options, function(err, response, body) {
callback(err, JSON.parse(body));
});
}
//this is used to send a CUSTOM payload vs the standard/default
//robot functions. Allows user and attachments to be easily specified for whatever platform is currently being used. i.e. slack, flowdock etc
function sendPayload(payload) {
robot.adapter.customMessage(payload);
}
//currently send an object for EACH Pr. Makes multiple payloads. Current comments would help with forcing it
//to only send one whole payload. Need a way to have a way to sendPayload() when we are at the end of the response list
function sendUserPrs(org, userName) {
//redefined in the daily task to force update data. instead of using global.
org = orgName;
startUp(org);
var ghUsers = robot.brain.get("ghUsersBrain");
var ghRepos = robot.brain.get("ghReposBrain");
var invertUsers = _.invert(ghUsers);
if (!userName) {
Object.keys(ghUsers).forEach(function(user) {
buildPRs(org, user);
});
} else {
buildPRs(org, userName);
}
}
function howManyHoursAgo(updated) {
var now = new Date();
updated = Date.parse(updated);
var difference = now - updated;
var hours = difference / 1000 / 60 / 60;
return hours;
}
function updateText(invertUsers, pr_html_url, pr_number, pr_title, pr_user_login) {
return "<" + pr_html_url +
" | #" + pr_number + "> " +
pr_title + " (<@" + invertUsers[pr_user_login] + ">)\n";
}
function loginsFromUsers(users) {
return users.map(function(user) {
return user.login;
});
}
function prCreatedByUser(pr, user) {
return pr.user.login === user;
}
function getAgeLocation(hoursDifference) {
if (hoursDifference <= 24) {
return 2;
} else if (hoursDifference <= 72) {
return 1;
} else {
return 0;
}
}
function startUp(org) {
org = orgName;
ghRepos = robot.brain.get("ghReposBrain") || [];
ghUsers = robot.brain.get("ghUsersBrain") || {};
getRepos(org, function(err, response) {
if (err) {
console.log(err);
throw err;
}
ghRepos = response.map(function(repo) {
return repo.name;
});
robot.brain.set("ghReposBrain", ghRepos);
});
getUsers(function(err, response) {
if (err) {
console.log(err);
throw err;
}
response.members.filter(function(user) {
return user.profile.skype
})
.forEach(function(user) {
ghUsers[user.name] = user.profile.skype;
});
robot.brain.set("ghUsersBrain", ghUsers);
});
}
function buildPRs(org, user) {
var invertUsers = _.invert(ghUsers);
var userPayload = {
channel: user,
text: "Incoming! PR's rollin out!",
attachments: [{
color: 'danger',
title: '72hrs+',
text: ""
}, {
color: 'warning',
title: '24-72hrs',
text: ""
}, {
color: 'good',
title: '< 24hrs',
text: ""
}, {
title: 'Unassigned',
text: ""
}]
};
var repoCount = 0;
ghRepos.forEach(function(repo) {
getPrs(org, repo, user, function(err, prList) {
if (err) {
console.log(err);
throw err;
}
repoCount++;
prList.forEach(function(pr) {
if ((pr.assignees.length === 0 && pr.assignee === null) && !prCreatedByUser(pr, ghUsers[user])) {
if (!(pr.user.login in invertUsers)) {
userPayload.attachments[3].text += updateText(invertUsers, pr.html_url, pr.number, pr.title, pr.user.login);
}
} else if (prCreatedByUser(pr, ghUsers[user]) || (loginsFromUsers(pr.assignees).indexOf(ghUsers[user]) !== -1 || pr.assignee.login === ghUsers[user])) {
var hoursDifference = howManyHoursAgo(pr.updated_at);
var ageLocation = getAgeLocation(hoursDifference);
userPayload.attachments[ageLocation].text += updateText(invertUsers, pr.html_url, pr.number, pr.title, pr.user.login);
}
});
if (repoCount === ghRepos.length) {
sendPayload(userPayload);
}
});
});
}
};