Files
Elite-Gaming-FiveM/autofetch-script/fetch-script.js
T
Jacob 509d7d60e0 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.
2024-11-22 21:29:54 +00:00

36 lines
1.1 KiB
JavaScript

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)
})