-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
40 lines (34 loc) · 1.32 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
const { json, send } = require('micro')
const { parse } = require('url')
const logger = require('./lib/log')
const validateReq = require('./lib/validate-req')
const runScript = require('./lib/run-script')
module.exports = async (req, res) => {
const hooks = require('./scripts')
const { pathname } = await parse(req.url, false) // gets url path
if (pathname === '/ping') return send(res, 200, 'pong')
let payload
try {
payload = await json(req) // gets payload
} catch (e) {
logger('err', 'Missing JSON payload')
return send(res, 400, 'Missing JSON payload')
}
logger('debug', `Requesting ${pathname}`)
try {
await validateReq({ pathname, payload, hooks }) // validates token and payload
} catch (e) {
logger('err', e.message)
return send(res, 400, e.message)
}
// everything is on it's right place...
send(res, 204) // sends 'no content' to client
logger('debug', `Payload from docker hub:\n ${JSON.stringify(payload, null, 2)} \nRunning hook on repo: ${payload.repository.repo_name}`)
const hook = hooks(payload.repository.repo_name, payload.push_data.tag)
try {
const result = await runScript(hook, payload) // runs script
logger('debug', `${result}\nFinished running hook "${hook}" for repository "${payload.repository.repo_name}"`)
} catch (e) {
logger('err', e)
}
}