forked from augbog/slack-github-issue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·128 lines (93 loc) · 3.22 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
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
#!/usr/bin/env node
var Client = require('slack-client'),
request = require('request'),
maps = require('./repo-maps.js');
module.exports = Client;
// Instantiate maps().
var repoMaps = new maps();
var slack = new Client(repoMaps.bot_token, true, true);
slack.on('open', function () {
var channels = Object.keys(slack.channels)
.map(function (k) { return slack.channels[k]; })
.filter(function (c) { return c.is_member; })
.map(function (c) { return c.name; });
var groups = Object.keys(slack.groups)
.map(function (k) { return slack.groups[k]; })
.filter(function (g) { return g.is_open && !g.is_archived; })
.map(function (g) { return g.name; });
console.log('Welcome to Slack. You are ' + slack.self.name + ' of ' + slack.team.name);
if (channels.length > 0) {
console.log('You are in: ' + channels.join(', '));
}
else {
console.log('You are not in any channels.');
}
if (groups.length > 0) {
console.log('As well as: ' + groups.join(', '));
}
});
// when someone posts to the channel
slack.on('message', function(message) {
var channel = slack.getChannelGroupOrDMByID(message.channel);
var user = slack.getUserByID(message.user);
var repo = repoMaps.getByChannel( channel.name );
if (message.type === 'message' && message.hasOwnProperty('text') ) {
// if we find a #:
if ( message.text.indexOf('#') > -1 ) {
var issueNums = message.text.match( /[a-z]+\d+/g );
if ( null === issueNums ) {
return;
}
issueNums.forEach( function( element ) {
var abbr = element.match( /[a-z]+/ );
// console.log( abbr );
if ( null === abbr ) {
return;
}
repo = repoMaps.getByAbbr( abbr[0], channel.name );
// Rewrite the issueNum minus the abbreviation.
issueNum = '#' + element.match(/\d+$/);
// console.log(repo);
// console.log(issueNum);
// console.log( element.match( /\d+$/ ) );
// console.log( /^#\d+$/.test( issueNum ) );
if (/^#\d+$/.test(issueNum) ) {
var number = issueNum.match( /\d+$/ );
var issueDescription,
token = repoMaps.auth_token,
options = {
url: 'https://api.github.com/repos/' + repo + '/issues/' + issueNum.substr(1),
method: 'GET',
headers: {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'token ' + token
}
};
// console.log( options );
//Github API requires User Agent
request(options, function (error, response, body) {
var json = JSON.parse(body);
if (!error && response.statusCode == 200) {
issueDescription = "*" + repo + "*\n";
issueDescription += "[#" + json.number + "] " + json.title + "\n " + json.html_url;
channel.send(issueDescription)
} else {
console.log( error );
}
// console.log( response.headers );
});
}
} );
// If we get '@github list':
} else if ( message.text.match( /^\<@U6BH7TC3C\>\slist$/ ) ) {
var mapObject = repoMaps.getMapObject( channel.name );
if ( null !== mapObject ) {
channel.send( "*Repo Abbreviation Map:*\n" + "```\n" + JSON.stringify( mapObject, null, 4 ) + "\n```" );
} else {
console.log( "Nothing to print." );
}
}
}
});
slack.login();