feat: auto fetching for future commits
+ Introduced a new auto fetch script that will automatically fetch and pull any commits pushed to GitHub. + Ensures token matches from the webhook. + Configurable via .env where necessary.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
require("dotenv").config();
|
||||
const { exec } = require('child_process');
|
||||
const crypto = require('crypto');
|
||||
const express = require('express');
|
||||
const cookieParser = require('cookie-parser');
|
||||
const cors = require('cors');
|
||||
|
||||
// Express Setup & Config
|
||||
const app = express();
|
||||
app.use(cookieParser());
|
||||
app.use(cors());
|
||||
app.use(express.json({limit: '50mb'}));
|
||||
app.listen(5000);
|
||||
|
||||
// POST listener from GitHub
|
||||
console.log("Started fetch listener successfully...")
|
||||
app.post('/fetch-gh', (req, res) => {
|
||||
console.log("Received webhook POST request...")
|
||||
|
||||
var hmac, calculatedSignature, payload = req.body;
|
||||
hmac = crypto.createHmac('sha1', process.env.GH_WEBHOOK_SECRET);
|
||||
hmac.update(JSON.stringify(payload));
|
||||
calculatedSignature = 'sha1=' + hmac.digest('hex');
|
||||
|
||||
// Ensure that the secret is valid
|
||||
if (req.headers['x-hub-signature'] === calculatedSignature) {
|
||||
console.log("Authenticated webhook, secret matches, fetching and pulling git...")
|
||||
exec(`cd ${process.env.WORK_DIR} && git fetch && git pull`)
|
||||
}
|
||||
|
||||
else {
|
||||
console.log("Authentication failed... secret not matching")
|
||||
}
|
||||
|
||||
res.sendStatus(201)
|
||||
})
|
||||
Reference in New Issue
Block a user