-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarn.js
120 lines (101 loc) · 3.33 KB
/
warn.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
/*
* Copyright 2018 DeepCode AG
*
* Author: Lorenzo Donati
*/
'use strict';
const fs = require('fs');
const express = require('express')
const bodyParser = require('body-parser');
const axios_ = require('axios');
const jwt = require('atlassian-jwt');
const assert = require('assert');
const debug = require('debug')(`app:${require('path').basename(__filename)}`);
let descriptor = fs.readFileSync('descriptor.json', 'utf8');
const Env = require('./env/env');
const LOCAL_PORT = Env('LOCAL_PORT');
const HOST_URL = Env('HOST_URL');
const DC_CLIENT_URL = Env('DC_CLIENT_URL');
const DC_SERVER_URL = Env('DC_SERVER_URL');
const BIT_BUCKET_APP_KEY = Env('BIT_BUCKET_APP_KEY');
const BIT_BUCKET_API_URL = Env('BIT_BUCKET_API_URL');
const BIT_BUCKET_URL = Env('BIT_BUCKET_URL');
// Set defaults for API calls.
const server = axios_.create({
baseURL: DC_SERVER_URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
const app = express()
app.use(bodyParser.json())
let mSharedSecret = 'u3t4wcWYsmmdLyWJQ7R9xzpmMoKSCd7UpvYH5yXBjg0';
let mReq;
app.get('/', (req, res) => {
debug(`Descriptor requested`);
try{
var d = JSON.parse(descriptor);
d.key = BIT_BUCKET_APP_KEY ? BIT_BUCKET_APP_KEY : d.key;
d.baseUrl = HOST_URL ? HOST_URL : d.baseUrl;
res.json(d);
} catch (error) {
debug(error);
res.json(JSON.parse(descriptor));
}
});
app.get('/redirect', async (req, res) => {
debug(`Redirecting get request to ${DC_CLIENT_URL}/app/bb/review`);
res.redirect(`${DC_CLIENT_URL}/app/bb/review`);
});
app.post('/redirect', async (req, res) => {
debug(`Redirecting post request to ${DC_CLIENT_URL}/app/bb/review`);
res.redirect(`${DC_CLIENT_URL}/app/bb/review`);
});
app.post('/installed', async (req, res) => {
const {key, clientKey, sharedSecret} = req.body;
debug(`App installed for client ${clientKey}`);
req.headers['X-Event-Key'] = 'app:installed';
try{
req.body.event = 'app:installed'
await server.post(`webhook/bb`, req.body);
res.sendStatus(204);
} catch(error) {
console.error(error);
res.sendStatus(503);
}
});
app.post('/uninstalled', async (req, res) => {
const {key, clientKey, sharedSecret} = req.body;
debug(`App uninstalled for client ${clientKey}`);
try{
req.body.event = 'app:uninstalled'
await server.post(`webhook/bb`, req.body);
res.sendStatus(204);
} catch(error) {
console.error(error);
res.sendStatus(503);
}
});
app.post('/webhook', async (req, res) => {
res.sendStatus(204);
mReq = req.body;
debug(`Webhook received`);
try{
if(req.headers.authorization && req.headers.authorization.startsWith('JWT ') && req.body.event && req.body.event === req.headers['x-event-key'] && req.body.data.repository.name && req.body.data.repository.owner.uuid) {
debug(`Webhook received for event ${req.body.event} on repo ${req.body.data.repository.name} owned by ${req.body.data.repository.owner.uuid}`);
req.body.jwt = req.headers.authorization.split(' ')[1];
await server.post(`webhook/bb`, req.body);
} else {
console.error("Received a bad webhook format");
}
} catch(error) {
console.error(error);
}
});
try{
debug(`App reachable on url ${HOST_URL}`);
app.listen(LOCAL_PORT, () => console.log(`App listening on port ${LOCAL_PORT}!`));
}catch(error){
console.error(error);
}