forked from homeyer/gocodebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jarvis.js
305 lines (257 loc) · 8.29 KB
/
jarvis.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
'use strict';
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var request = require('request');
class Jarvis {
constructor(github){
this.github = github;
this.team = null;
}
createTeam(convo, callback) {
var github = this.github;
var me = this;
convo.say('OK! Let\'s get started. But first, make sure everyone on your team already has a GitHub.com user account (or stop now and come back when they do).')
convo.ask('Ready? What’s your team’s name? No spaces or hyphens, please.', [{
pattern: /^[a-z0-9]+$/i,
callback: function(response, convo) {
github.repos.createForOrg({
name: response.text,
org: process.env.GITHUB_ORGANIZATION,
private: true
}, function(err, data){
if(err){
convo.say(`Sorry, I can't do that: ${JSON.parse(err).message}`);
return convo.next();
}
me.teamName = response.text
github.orgs.createTeam({
org: process.env.GITHUB_ORGANIZATION,
name: me.teamName
}, function(err, data){
if(err){
convo.say(`Sorry, I can't do that: ${JSON.parse(err.message).errors[0].message}`);
return convo.next();
}
me.team = data.data;
github.orgs.addTeamRepo({
id: me.team.id,
org: process.env.GITHUB_ORGANIZATION,
repo: me.teamName,
permission: 'admin'
}, function(err, data){
if(err){
convo.say(`Sorry, I can't do that: ${JSON.parse(err.message).errors[0].message}`);
return convo.next();
}
convo.say(`Great! I've created ${me.teamName} in ${process.env.GITHUB_ORGANIZATION}.`);
callback();
convo.next();
});
});
});
}
},
{
default: true,
callback: function(response, convo) {
convo.say("I'm sorry, please only use letters and numbers.");
me.createTeam(convo, callback);
convo.next();
}
}])
}
addTeamMembers(convo, callback) {
var github = this.github;
var me = this;
var printableTeamMembers;
convo.ask('Who would you like to add to your team? (this should be a comma-separated list of all your team member\'s GitHub usernames, yourself included, and please do not include the @-sign)', function(response, convo){
var teamMembers = response.text.split(/[\s,]+/);
if(teamMembers.length == 1){
printableTeamMembers = teamMembers[0];
} else {
printableTeamMembers = teamMembers.slice(0, teamMembers.length-1).join(', ') + ' and ' + teamMembers[teamMembers.length-1];
}
var userExistencePromises = teamMembers.map(function(member){
return new Promise(function(resolve, reject){
request.get({
url: 'https://api.github.com/users/' + member,
qs: {
access_token: process.env.GITHUB_API_TOKEN
},
headers: {
'User-Agent': 'gocodebot'
}
}, function(err, response, body){
if(err || response.statusCode != 200){
console.log(err || body);
return reject(`Uh oh, we couldn't find ${member}. Let's try that again.`);
}
resolve();
})
});
});
Promise.all(userExistencePromises)
.then(function(){
var sequence = Promise.resolve();
teamMembers.forEach(function(member){
sequence = sequence.then(function(){
return new Promise(function(resolve, reject){
console.log('team: ', me.team);
github.orgs.addTeamMembership({
id: me.team.id,
username: member,
role: 'maintainer'
}, function(err, data){
if(err){
return reject(`Uh oh, we couldn't add ${member}.`);
}
resolve();
});
});
});
});
sequence.then(function(){
convo.say(`Great, I’ve invited ${printableTeamMembers} to join the team.`);
callback();
})
})
.catch(function(reason){
convo.say(reason);
me.addTeamMembers(convo, callback);
convo.next();
})
});
}
createContent(convo, callback) {
var me = this;
convo.say("I'm starting your team off with some tips in your README. Hang on a sec.");
me.createReadme()
.then(function(){
return me.createLabels();
})
.then(function(){
// we get errors if we don't wait a moment after creating labels
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve();
}, 1000);
});
})
.then(function(){
return me.createIssues();
})
.then(function(){
callback();
})
.catch(function(reason){
convo.say(reason);
})
}
createReadme() {
var me = this;
var github = this.github;
return new Promise(function(resolve, reject){
fs.readFile(path.join(__dirname, 'content', 'README-template.md'), function(err, data){
if(err){
return callback(err);
}
var readme = data.toString();
readme = readme.replace(/:owner/g, process.env.GITHUB_ORGANIZATION);
readme = readme.replace(/:repo/g, me.team.name);
var base64 = new Buffer(readme).toString('base64');
github.repos.createFile({
owner: process.env.GITHUB_ORGANIZATION,
repo: me.team.name,
path: 'README.md',
message: 'GoCodeBot was here',
content: base64
}, function(err, data){
if(err){
return reject(err);
}
resolve();
});
});
});
}
createLabels() {
var me = this;
var github = this.github;
var labels = [
{name: 'blocked', color: 'e11d21'},
{name: 'validated', color: '5319e7'},
{name: 'invalidated', color: 'fbca04'},
{name: 'design needed', color: 'eb6420'},
{name: 'dev needed', color: 'bfd4f2'},
{name: 'sprint 1', color: '91aab3'},
{name: 'sprint 2', color: '235668'},
{name: 'sprint 3', color: 'f7b5b2'},
{name: 'sprint 4', color: '91aab3'},
{name: 'sprint 5', color: 'f7b5b2'}
];
var sequence = Promise.resolve();
labels.forEach(function(label){
sequence = sequence.then(function(){
new Promise(function(resolve, reject){
github.issues.createLabel({
owner: process.env.GITHUB_ORGANIZATION,
repo: me.team.name,
name: label.name,
color: label.color
}, function(err, data){
if(err){
return reject(err);
}
resolve(data.data);
})
});
})
});
return sequence;
}
createIssues() {
var me = this;
var github = this.github;
return new Promise(function(resolve, reject){
fs.readFile(path.join(__dirname, 'content', 'cards.json'), 'utf8', function(err, data){
if(err){
return reject(err);
}
var cardsMetadata = JSON.parse(data);
var cards = cardsMetadata.map(function(metadata){
return {
title: metadata.title,
labels: metadata.labels,
description: fs.readFileSync(path.join(__dirname, 'content', 'cards', metadata.file), 'utf8')
}
});
var sequence = Promise.resolve();
cards.forEach(function(card){
sequence = sequence.then(function(){
return new Promise(function(resolve, reject){
github.issues.create({
owner: process.env.GITHUB_ORGANIZATION,
repo: me.team.name,
title: card.title,
labels: card.labels,
body: card.description
}, function(err, data){
if(err){
return reject(err);
}
resolve(data.data);
});
});
});
});
sequence.then(function(){
resolve();
}).catch(function(err){
reject(err);
})
});
});
}
}
module.exports = Jarvis;