Files
Gitea-Badge-Creator/script.js
T
npease 01c1c1e054
Update Server / Primary Server Repo Update (push) Successful in 5m9s
Update Server / Physical Server Update Trigger (push) Successful in 4s
Add /heartbeat endpoint
2023-10-13 21:32:48 +00:00

56 lines
1.7 KiB
JavaScript

const express = require("express");
const { exec } = require("child_process");
const gradientBadge = require("gradient-badge");
const fs = require("fs");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.get('/heartbeat', (req, res) => {
res.sendStatus(200)
})
app.post("/repo_change", (request, response) => {
var { body } = request;
if (body.action == "deleted") {
exec(`rm /var/www/html/badges/cloc/${body.repository.full_name}.svg`);
} else {
exec(`git clone ${body.repository.clone_url}`, function (e, o, i) {
exec(
`cd ${body.repository.name} && scc --format json --remap-all "":"Misc."`,
function (err, out, stderr) {
const svgString = gradientBadge({
subject: "Lines of Code", // <text>
status: `${JSON.parse(out)[0].Lines}`, // <text>
gradient: ["0f8fff"], // array of colors (Hexadecimal or name)
});
fs.writeFile("output.svg", svgString, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
exec(`rm -rf ${body.repository.name}`);
exec(
`rm /var/www/html/badges/cloc/${body.repository.full_name}.svg`,
function (a, e, i) {
exec(
`mv output.svg /var/www/html/badges/cloc/${body.repository.full_name}.svg`,
function (err, o, oerr) {
console.log("Done");
}
);
}
);
}
);
});
}
response.end();
});
app.listen(port, () => {
console.log(`Express api/webhook app listening at http://localhost:${port}`);
});