115 lines
3.7 KiB
JavaScript
115 lines
3.7 KiB
JavaScript
const fs = require("fs");
|
|
const express = require("express");
|
|
const app = express();
|
|
const { exec } = require("child_process");
|
|
const nodeMailer = require("nodemailer");
|
|
|
|
// Read configuration file
|
|
const contentFile = fs.readFileSync("./service_run/giteaService.conf", "utf8");
|
|
let checkStatus = "ready";
|
|
|
|
// Get port from configuration file
|
|
const PORT = contentFile
|
|
.split("\n")
|
|
.find(line => line.includes("PORT_SERVICE"))
|
|
?.split("=")[1]
|
|
.trim() || 5000;
|
|
|
|
// Get email configuration from config file
|
|
const getConfigValue = (key) => contentFile
|
|
.split("\n")
|
|
.find(line => line.includes(key))
|
|
?.split("=")[1]
|
|
.trim();
|
|
|
|
// Configure email transporter
|
|
const createTransporter = () => nodeMailer.createTransport({
|
|
pool: true,
|
|
host: getConfigValue("EMAIL_HOST") || "smtp.gmail.com",
|
|
port: parseInt(getConfigValue("EMAIL_PORT") || "587"),
|
|
secure: getConfigValue("EMAIL_SECURE") === "True",
|
|
auth: {
|
|
user: getConfigValue("EMAIL_USER") || "git@notification.com",
|
|
pass: getConfigValue("EMAIL_PASSWORD") || "123456789",
|
|
},
|
|
});
|
|
|
|
app.use(express.json());
|
|
|
|
app.post("/git/gitea-webhook", async (req, res) => {
|
|
let title = "";
|
|
const checkSendMail = getConfigValue("SEND_EMAIL");
|
|
const emailAddress = getConfigValue("RECEIVE_EMAIL_ADDRESS");
|
|
const emailFrom = getConfigValue("EMAIL_FROM") || "git@notification.com";
|
|
const giteaEvent = req.headers["x-gitea-event"];
|
|
|
|
// Return response immediately
|
|
res.status(200).send({ mess: "The event has been received!", data: req.body });
|
|
console.log("STATUS:", checkStatus);
|
|
|
|
// Check if there is a running process
|
|
if (checkStatus === "busy") {
|
|
if (checkSendMail === "True") {
|
|
const transporter = createTransporter();
|
|
const options = {
|
|
from: emailFrom,
|
|
to: emailAddress,
|
|
subject: "Git notifications (Fail)",
|
|
html: "<h3>There is an active build process. Please check and push again in a few minutes.</h3>",
|
|
};
|
|
return transporter.sendMail(options);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Process new event
|
|
console.log("New event: " + giteaEvent);
|
|
checkStatus = "busy";
|
|
|
|
exec("./service_run/giteaHook.sh", (error, stdout, stderr) => {
|
|
let content = stdout || stderr;
|
|
|
|
if (error) {
|
|
console.log("Pull failed. Please check");
|
|
console.log(`Error executing command: ${error}`);
|
|
title = `New Git event: ${giteaEvent}`;
|
|
} else {
|
|
console.log(stdout);
|
|
const hasError = [stderr, stdout].some(output =>
|
|
output && (output.includes("Error") || output.includes("failed"))
|
|
);
|
|
|
|
title = `New Git event: ${giteaEvent}${hasError ? "" : " (success)"}`;
|
|
|
|
// Reset status after 10 seconds
|
|
setTimeout(() => {
|
|
checkStatus = "ready";
|
|
}, 10000);
|
|
}
|
|
|
|
// Send notification email if configured
|
|
if (checkSendMail === "True") {
|
|
const transporter = createTransporter();
|
|
const options = {
|
|
from: emailFrom,
|
|
to: emailAddress,
|
|
subject: title,
|
|
html: `
|
|
<h1>*** ${giteaEvent} event ***</h1>
|
|
<h4>Committer: ${req.body.commits[0]?.committer.name || "N/A"}</h4>
|
|
<h4>Message: ${req.body.commits[0]?.message || "N/A"}</h4>
|
|
<h4>Branch: ${req.body.ref || "N/A"}</h4>
|
|
${req.body.commits[0]?.url ? `<a href='${req.body.commits[0].url}'>Link: ${req.body.commits[0].url}</a>` : ""}
|
|
<h4>Process output:</h4>
|
|
<textarea style='wordWrap:break-word; display: block; width:100%; height:70vh;border:solid 2px orange'>${content.replace(/error|fail|warning/gi, match => `<span style="background-color:yellow;color:red;font-weight:bold">${match}</span>`)}</textarea>
|
|
`,
|
|
};
|
|
return transporter.sendMail(options);
|
|
}
|
|
});
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log("Service is running on port", PORT);
|
|
});
|