-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.js
80 lines (72 loc) · 2.1 KB
/
util.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
// Bot Error Handler
function botErrorHandler(err, bot, message) {
console.log("\n" + err);
var errText = ":rotating_light: " + err;
bot.reply(message, {
"attachments": [{
"fallback": err,
"color": "#FF0000",
"title": Error,
"text": errText
}]
});
}
function slackMsgHandler(bot, message, header) {
var header = ":rotating_light: over riding header";
bot.reply(message, {
"attachments": [{
"fallback": header,
"color": "#36a64f",
"title": header,
"text": header
}]
});
}
function githubApiCall(bot, message, team, repo) {
var githubToken = "token " + BotConfig.admin_config.github.github_token_encrypted;
var http = require("https");
var path = /repos/ + team + "/" + repo + "/pulls";
var options = {
"method": "GET",
"hostname": "api.github.com",
"port": null,
"path": path,
"headers": {
"accept": "application/vnd.github.v3+json",
"Authorization": githubToken,
"User-Agent": "slackbot"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
constructSlackResponse(bot, message, body);
});
});
req.end();
}
function constructSlackResponse(bot, message, jsonRaw) {
var jsonObj = JSON.parse(jsonRaw);
var objLength = jsonObj.length;
var response = "";
var header = ":rotating_light: Github PR Response: ";
for(var i=0; i<objLength; i++) {
response += "\n PR# " + jsonObj[i].number + " - " + jsonObj[i].title + "\n " + jsonObj[i].html_url;
}
console.log(response);
slackMsgHandler(bot, message, header, response);
}
function slackMsgHandler(bot, message, header, response) {
bot.reply(message, {
"attachments": [{
"fallback": header,
"color": "#36a64f",
"title": header,
"text": response
}]
});
}