expressbuild/index.js

54 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-01-17 21:50:27 -05:00
// The base for this script was shameless copied from https://discourse.gitea.io/t/how-to-write-a-webhook-in-gitea-in-node-js-javascript/1907
2022-01-17 21:37:07 -05:00
const express = require('express')
const crypto = require('crypto')
const bodyParser = require('body-parser')
2022-01-17 21:50:27 -05:00
const { exec } = require('child_process')
2022-01-17 21:37:07 -05:00
require('dotenv').config()
const SECRET = process.env.SECRET_KEY
const app = express()
2022-01-17 21:50:27 -05:00
const port = process.env.PORT
2022-01-17 21:37:07 -05:00
const options = {
inflate: true,
limit: '100kb',
type: 'application/json',
}
app.use(bodyParser.raw(options))
2022-01-17 21:50:27 -05:00
app.post('/', (req, res) => {
2022-01-17 21:37:07 -05:00
const signature = req.headers['x-gitea-signature']
if (!signature) {
console.error('x-gitea-signature missing')
return res.send()
}
const hmac = crypto.createHmac('sha256', SECRET)
hmac.update(req.body)
const payload_signature = hmac.digest('hex')
if (signature !== payload_signature) {
console.error('signature !== payload_signature')
return res.send()
}
const json = JSON.parse(req.body.toString())
2022-01-17 21:50:27 -05:00
console.log(json.repository.clone_url)
2022-01-17 21:37:07 -05:00
// SIGNATURES MATCHES, do your thing here.
2022-01-17 21:50:27 -05:00
var buildscript = exec('sh ' + process.env.EXECUTE_SCRIPT, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
2022-01-17 21:37:07 -05:00
2022-01-17 21:50:27 -05:00
res.sendStatus(200);
2022-01-17 21:37:07 -05:00
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})