This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
forked from bpedro/gitevents-webhook
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
365 lines (316 loc) · 15.6 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
var debug = require('debug')('gitevents-webhook');
var parser = require('markdown-parse');
var moment = require('moment');
var GitHubApi = require('github');
Object.prototype.findById = function(id) {
for (var i = 0; i < this.length; i++) {
if (this[i].id === id) {
return i;
}
}
return -1;
};
module.exports = function(config) {
return function process(payload) {
return new Promise(
function(resolve, reject) {
debug('action: ' + payload.action);
if (!config.github) {
debug('GitEvents webhook plugin is not activated. Please provide GitHub credentials.');
reject(new Error('no api key'));
}
var github = new GitHubApi({
version: '3.0.0',
debug: config.debug,
protocol: 'https',
timeout: 5000,
headers: {
'user-agent': 'GitEvents'
}
});
github.authenticate({
type: 'oauth',
token: config.github.token
});
if (payload.action === 'opened') {
// do nothing
debug('Do nothing.');
resolve();
} else if (payload.action === 'labeled') {
debug('label: ' + payload.label.name);
github.user.getFrom({
user: payload.sender.login
}, function(error, user) {
if (payload.issue && payload.issue.body) {
var file;
parser(payload.issue.body, function(error, body) {
if (error) {
reject(new Error(error));
} else {
if (payload.label.name === config.labels.proposal) {
// process talk proposal
var proposal = {
id: payload.issue.id,
type: 'proposal',
speaker: {
id: user.id,
name: user.name,
location: user.location,
github: user.login,
gravatar: user.gravatar_id,
avatar: user.avatar_url
},
title: payload.issue.title,
description: body.html
};
if (body.attributes.twitter) {
proposal.speaker.twitter = body.attributes.twitter;
}
if (body.attributes.language) {
proposal.language = body.attributes.language;
}
if (body.attributes.level) {
proposal.level = body.attributes.level;
}
if (body.attributes.month) {
proposal.month = body.attributes.month;
}
if (body.attributes.tags) {
proposal.tags = body.attributes.tags;
}
github.repos.getContent({
user: config.github.user,
repo: config.github.repo,
path: 'proposals.json'
}, function(error, proposals) {
if (error && error.code === 404) {
// create an array for all future proposals and store on GitHub.
debug('proposals.json doesn\'t exist. Creating.');
proposal.updated_at = new Date().toJSON();
proposal.created_at = new Date().toJSON();
proposals = [proposal];
file = new Buffer(JSON.stringify(proposals, null, 2)).toString('base64');
github.repos.createContent({
user: config.github.user,
repo: config.github.repo,
path: 'proposals.json',
content: file,
message: 'Created proposals'
}, function(error) {
if (error) {
reject(new Error(error));
}
resolve(proposal);
});
} else if (error) {
reject(new Error(error));
} else {
// get proposals and update
var updatedProposals;
var message;
try {
updatedProposals = JSON.parse(new Buffer(proposals.content, 'base64').toString('ascii'));
} catch (error) {
reject(new Error(error));
}
var id = updatedProposals.findById(proposal.id);
if (id !== -1) {
debug('Proposal exists. Update.');
// update the proposal; don't change the speaker
proposal.updated_at = new Date().toJSON();
proposal.created_at = updatedProposals[id].created_at;
proposal.speaker = updatedProposals[id].speaker;
updatedProposals[id] = proposal;
message = 'Updated proposal by ' + proposal.speaker.github;
} else {
debug('Push new proposal.');
proposal.created_at = new Date().toJSON();
updatedProposals.push(proposal);
message = 'New proposal by ' + proposal.speaker.github;
}
file = new Buffer(JSON.stringify(updatedProposals, null, 2)).toString('base64');
github.repos.updateFile({
user: config.github.user,
repo: config.github.repo,
path: 'proposals.json',
sha: proposals.sha,
content: file,
message: message
}, function(error) {
if (error) {
debug(error);
reject(new Error(error));
}
debug('All done. Returning.');
resolve(proposal);
});
}
});
} else if (payload.label.name === config.labels.talk) {
// process talk from proposals
github.repos.getContent({
user: config.github.user,
repo: config.github.repo,
path: 'proposals.json'
}, function(error, proposals) {
if (error) {
reject(new Error(error));
} else {
// get proposals and update
var readableProposals;
try {
readableProposals = JSON.parse(new Buffer(proposals.content, 'base64').toString('ascii'));
} catch (error) {
reject(new Error(error));
}
var proposalId = readableProposals.findById(payload.issue.id);
if (proposalId === -1) {
debug('Proposal doesn\'t exist');
reject(new Error('Proposal doesn\'t exist.'));
} else {
debug('Proposal found. Id: ' + proposalId);
var talk = readableProposals[proposalId];
if (!payload.issue.milestone) {
debug('Missing milestone');
reject(new Error('No Milestone (=Event) defined.'));
} else {
var date = moment(payload.issue.milestone.due_on).toArray();
var time = payload.issue.milestone.description.split(';')[0].split(':');
date[3] = parseInt(time[0], 10);
date[4] = parseInt(time[1], 10);
if (!date[4]) {
date[4] = 0;
}
var event = {
id: payload.issue.milestone.id,
type: 'event',
location: {
name: payload.issue.milestone.description.split(';')[1],
address: payload.issue.milestone.description.split(';')[2]
},
date: moment.utc(date).toJSON(),
name: payload.issue.milestone.title
};
github.repos.getContent({
user: config.github.user,
repo: config.github.repo,
path: 'events-' + new Date(payload.issue.created_at).getFullYear() + '.json'
}, function(error, events) {
if (error && error.code === 404) {
// create an array for all future proposals and store on GitHub.
debug('events file doesn\'t exist. Creating a new one.');
event.talks = [];
talk.accepted_at = new Date().toJSON();
talk.type = 'talk';
event.talks.push(talk);
events = [event];
file = new Buffer(JSON.stringify(events, null, 2)).toString('base64');
github.repos.createContent({
user: config.github.user,
repo: config.github.repo,
path: 'events-' + new Date(payload.issue.created_at).getFullYear() + '.json',
content: file,
message: 'Created events'
}, function(error) {
if (error) {
reject(new Error(error));
}
resolve(event);
});
} else {
debug('Found existing events. Parsing.');
var readableEvents;
try {
readableEvents = JSON.parse(new Buffer(events.content, 'base64').toString('ascii'));
} catch (error) {
debug('JSON parse error', error);
reject(new Error(error));
}
var githubEventId = payload.issue.milestone.id;
var foundEventId = readableEvents.findById(githubEventId);
var message;
if (foundEventId !== -1) {
debug('Found event.');
var talkId = readableEvents[foundEventId].talks.findById(payload.issue.id);
if (talkId !== -1) {
debug('Updating existing talk.');
talk = readableEvents[foundEventId].talks[talkId];
talk.updated_at = new Date().toJSON();
talk.created_at = readableEvents[foundEventId].talks[talkId].created_at;
talk.speaker = readableEvents[foundEventId].talks[talkId].speaker;
readableEvents[foundEventId].talks[talkId] = talk;
message = 'Updated talk by ' + talk.speaker.github;
} else {
debug('Adding new talk.');
talk.accepted_at = new Date().toJSON();
talk.type = 'talk';
readableEvents[foundEventId].talks.push(talk);
message = 'Added talk by ' + talk.speaker.github;
}
} else {
debug('No event found. Creating and adding talk.');
event.talks = [];
talk.accepted_at = new Date().toJSON();
talk.type = 'talk';
event.talks.push(talk);
readableEvents.push(event);
message = 'Created new event and added talk by ' + talk.speaker.github;
}
debug('Writing file.');
file = new Buffer(JSON.stringify(readableEvents, null, 2)).toString('base64');
github.repos.updateFile({
user: config.github.user,
repo: config.github.repo,
path: 'events-' + new Date(payload.issue.created_at).getFullYear() + '.json',
sha: events.sha,
content: file,
message: message
}, function(error) {
if (error) {
debug(error);
reject(new Error(error));
}
debug('Removing proposal.');
readableProposals.splice(proposalId, 1);
file = new Buffer(JSON.stringify(readableProposals, null, 2)).toString('base64');
github.repos.updateFile({
user: config.github.user,
repo: config.github.repo,
path: 'proposals.json',
sha: proposals.sha,
content: file,
message: 'Moved proposal to talks.'
}, function(error) {
if (error) {
debug(error);
reject(new Error(error));
}
debug('All done. Returning.');
resolve(event);
});
});
}
});
}
}
}
});
} else if (payload.issue.label === config.labels.job) {
// process jobs
resolve(null);
} else {
// process others
resolve(null);
}
}
});
}
});
} else {
reject(new Error('Unknown error occured.'));
}
}
);
};
};