-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (60 loc) · 2 KB
/
app.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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const express = require('express');
const secrets = require('./secrets.json');
const logger = require('./lib/logger.js');
const signedJson = require('./lib/signed-json.js');
const tasks = require('./lib/tasks.js');
const webhook = require('./lib/webhook.js');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.route('/api/webhook')
.post(signedJson.middleware({
header: 'x-hub-signature',
secret: secrets.github.webhook_secret,
}))
.post((req, res, next) => {
const event = req.get('x-github-event');
const delivery = req.get('x-github-delivery');
const payload = req.body;
logger.info({event, delivery, payload}, 'handling webhook');
webhook(event, payload)
.then(() => res.end(), next);
});
app.route('/api/task')
.post(signedJson.middleware({
header: 'x-self-signature',
secret: secrets.project_secret,
}))
.post((req, res, next) => {
const task = req.body;
logger.info({task}, 'running task');
tasks.run(task.name, task.parameters)
.then(() => res.end(), next);
});
app.get('/checks/:owner/:repo/:sha', (req, res)=>{
res.json(req.params);
});
/* istanbul ignore if */
if (require.main === module) {
const port = process.env.PORT || 8080;
app.listen(port, () => {
logger.info(`Listening on port ${port}`);
});
} else {
// Export app for testing.
module.exports = app;
}