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: "

There is an active build process. Please check and push again in a few minutes.

", }; 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: `

*** ${giteaEvent} event ***

Committer: ${req.body.commits[0]?.committer.name || "N/A"}

Message: ${req.body.commits[0]?.message || "N/A"}

Branch: ${req.body.ref || "N/A"}

${req.body.commits[0]?.url ? `Link: ${req.body.commits[0].url}` : ""}

Process output:

`, }; return transporter.sendMail(options); } }); }); app.listen(PORT, () => { console.log("Service is running on port", PORT); });