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 0
/
Copy pathindex.js
100 lines (88 loc) · 2.41 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
var debug = require('debug')('gitevents-newJobs');
var yamlFront = require('yaml-front-matter');
var moment = require('moment');
var parser = require('markdown-parse');
var S = require('string');
var GitHubApi = require('github');
var express = require('express');
var job = require('./lib/job');
var jwt = require('express-jwt');
var request = require('request');
var rollbar = require('rollbar');
var router = express.Router();
var config;
var enabled = false;
var jwtCheck;
var github = new GitHubApi({
version: '3.0.0',
debug: true,
protocol: 'https',
timeout: 5000,
headers: {
'user-agent': 'gitevents-jobs'
}
});
var githubInternal = new GitHubApi({
version: '3.0.0',
debug: true,
protocol: 'https',
timeout: 5000,
headers: {
'user-agent': 'gitevents-jobs'
}
});
router.init = function(cfg) {
debug('jobs');
config = cfg;
if (config.rollbar) {
rollbar.init(config.rollbar);
}
if (config.plugins.auth && config.plugins.auth.enabled) {
jwtCheck = jwt({
secret: new Buffer(config.plugins.auth.secret, 'base64'),
audience: config.plugins.auth.audience
});
enabled = config.plugins.auth.enabled;
router.use('/jobs', jwtCheck);
}
router.post('/jobs', function(req, res) {
debug('/post');
request({
'method': 'POST',
'url': req.user.iss + 'tokeninfo',
'headers': {
'Content-Type': 'application/json'
},
'body': {
id_token: req.headers.authorization.split(' ')[1]
},
'json': true
}, function(error, response, user) {
if (!error && response.statusCode == 200) {
github.authenticate({
type: 'oauth',
token: user.identities[0].access_token
});
githubInternal.authenticate({
type: 'oauth',
token: config.github.token
});
job.prepareJob(req.body, req.headers, config, githubInternal)
.then(function(newJob) {
return job.createInternalIssue(newJob, config, githubInternal);
}).then(function(newJob) {
return job.createIssue(newJob, config, githubInternal);
}).then(function() {
res.status(200).send();
}).catch(function(error) {
console.log(error);
if (config.rollbar) {
rollbar.handleError(error);
}
res.status(500).send();
});
}
});
});
};
module.exports = router;