This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgitevents.js
181 lines (150 loc) · 4.43 KB
/
gitevents.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
'use strict';
const config = require('./config');
const talks = require('./lib/talks');
const events = require('./lib/events');
const bodyParser = require('body-parser');
const express = require('express');
const cors = require('cors');
const crypto = require('crypto');
const log = require('./lib/log');
// current plugins
let jobs = require('gitevents-jobs');
let meetup = require('gitevents-meetup');
let rollbar = {};
let opbeat = {};
if (config.rollbar) {
let rollbar = require('rollbar');
rollbar.init(config.rollbar);
}
jobs.init(config);
meetup.init(config);
let app = express();
app.set('port', process.env.PORT || 3000);
app.use(cors());
app.use(bodyParser.urlencoded({
extended: false
}));
// app.use(bodyParser.json());
app.use(bodyParser.raw({
type: 'application/json'
}));
if (!config || !config.github) {
process.exit(-1);
}
let issueHandler = (event) => {
log.debug('New event: ' + event.event);
if (event.payload) {
let payload = event.payload;
if (payload.action === 'labeled') {
log.debug('label: ' + payload.label.name);
let labels = [];
if (payload.labels) {
labels = payload.labels.map(function (label) {
return label.name;
});
} else {
labels = payload.label.name;
}
payload.labelMap = labels;
if (labels.indexOf(config.labels.proposal) > -1) {
log.debug('New talk proposal. Nothing to do yet');
}
// Chain for planning events
if (labels.indexOf(config.labels.event) > -1) {
log.debug('New event planning.');
events(payload)
.then(function (event) {
// !! add event-related plugins here, for example tito !!
meetup.create(event)
.then(function (meetupId) {
log.debug(meetupId);
});
})
.catch(function (error) {
log.error(error);
if (config.rollbar) {
rollbar.handleError(error);
}
if (config.opbeat) {
opbeat.captureError(error);
}
});
}
// Chain for talks
//TODO: 'talk proposal' contains 'talk', so this should be refactored. Going with 'proposal' for now.
if (labels.indexOf(config.labels.talk) > -1) {
log.debug('New talk: ' + payload.issue.title);
events(payload)
.then(function (event) {
talks(payload, event)
.then(function (talk) {
log.debug('talk processed.');
log.debug(talk);
});
})
.catch(function (error) {
log.error(error);
if (config.rollbar) {
rollbar.handleError(error);
}
if (config.opbeat) {
opbeat.captureError(error);
}
});
}
}
} else {
log.debug('no action recognised.', event);
}
};
app.post('/github/delivery', function (req, res) {
log.debug('delivery');
let signature = req.headers['x-hub-signature'];
let event = req.headers['x-github-event'];
let id = req.headers['x-github-delivery'];
let rawPayload = req.body.toString('utf8');
let hmac = crypto.createHmac('sha1', config.github.secret);
hmac.update(rawPayload);
let calculatedSignature = 'sha1=' + hmac.digest('hex');
if (!signature) {
log.debug('No X-Hub-Signature found on request');
res.status(400)
.send('No X-Hub-Signature found on request');
} else if (!event) {
log.debug('No X-Github-Event found on request');
res.status(400)
.send('No X-Github-Event found on request');
} else if (!id) {
log.debug('No X-Github-Delivery found on request');
res.status(400)
.send('No X-Github-Delivery found on request');
} else if (signature !== calculatedSignature) {
log.debug('X-Hub-Signature does not match blob signature');
res.status(400)
.send('X-Hub-Signature does not match blob signature');
} else {
log.debug('processing payload');
let payload = {};
try {
payload = JSON.parse(rawPayload);
} catch (e) {
log.error(e);
}
issueHandler({
event: event,
id: id,
payload: payload,
protocol: req.protocol,
host: req.headers.host,
url: req.url
});
res.send({
'ok': true
});
}
});
app.use(jobs);
let server = app.listen(app.get('port'), function () {
log.info('gitevents server listening on port ' + server.address()
.port);
});