expressbuild/index.js
2022-01-17 21:55:20 -05:00

54 lines
1.5 KiB
JavaScript

// 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
const express = require('express')
const crypto = require('crypto')
const bodyParser = require('body-parser')
const { exec } = require('child_process')
require('dotenv').config()
const SECRET = process.env.SECRET_KEY
const app = express()
const port = process.env.PORT
const options = {
inflate: true,
limit: '100kb',
type: 'application/json',
}
app.use(bodyParser.raw(options))
app.post('/', (req, res) => {
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())
console.log(json.repository.clone_url)
// SIGNATURES MATCHES, do your thing here.
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}`);
}
});
res.sendStatus(200);
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})