-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (56 loc) · 1.87 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
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'));
var apiHost = process.env.API_HOST;
var apiToken = process.env.API_TOKEN;
var requestToken = process.env.REQUEST_TOKEN;
var projectId = parseInt(process.env.PROJECT_ID, 10);
app.get('/', function(req, response) {
response.end('OK');
});
app.post('/bug', function(req, response) {
var body = req.body;
console.log('~~BODY', body);
if (requestToken && body.token !== requestToken) {
response.end('Token required');
return;
}
var bugName = body.text;
if (!bugName) {
response.end('Bug name expected');
}
console.log('~~NAME', bugName);
var requestOptions = {
url: apiHost + '/api/v1/bugs?token=' + apiToken,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
json: {
name: bugName,
project: {
id: projectId
}
}
};
console.log('~~~REQUEST PAYLOAD', JSON.stringify(requestOptions));
request(requestOptions, function(error, apiResponse) {
if (error) {
console.log('~~RESPONSE ERROR', error);
response.end('Error occurred: ' + error);
return;
}
console.log('~~RESPONSE BODY', apiResponse.body);
var id = apiResponse.body.Id;
var url = apiHost + '/entity/' + id;
response.end('Created bug <' + url + '> ' + apiResponse.body.Name);
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});