This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
import { Express, Request, Response } from 'express';
|
||||
import { EmbedBuilder, TextChannel } from 'discord.js';
|
||||
import { CHANNELS } from '../config';
|
||||
import { client, db } from '../index';
|
||||
import { Express, Request, Response } from "express";
|
||||
import { EmbedBuilder, TextChannel } from "discord.js";
|
||||
import { CHANNELS } from "../config";
|
||||
import { client, db } from "../index";
|
||||
|
||||
const configured_channel = CHANNELS.UPDATES;
|
||||
|
||||
export default async function gitCommitPOST(app: Express) {
|
||||
app.post('/git-commit', async (req: Request, res: Response) => {
|
||||
app.post("/git-commit", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const event = req.headers['x-github-event'] as string;
|
||||
const event = req.headers["x-github-event"] as string;
|
||||
|
||||
// Acknowledge ping events
|
||||
if (event === 'ping') {
|
||||
console.log('[WEB-gitCommit] Received GitHub ping event');
|
||||
return res.status(200).json({ success: true, message: 'pong' });
|
||||
if (event === "ping") {
|
||||
console.log("[WEB-gitCommit] Received GitHub ping event");
|
||||
return res.status(200).json({ success: true, message: "pong" });
|
||||
}
|
||||
|
||||
if (event !== 'push') {
|
||||
return res.status(200).json({ success: true, message: `Event '${event}' ignored` });
|
||||
if (event !== "push") {
|
||||
return res
|
||||
.status(200)
|
||||
.json({
|
||||
success: true,
|
||||
message: `Event '${event}' ignored`,
|
||||
});
|
||||
}
|
||||
|
||||
const body = req.body;
|
||||
@@ -25,20 +30,20 @@ export default async function gitCommitPOST(app: Express) {
|
||||
const pusher = body.pusher;
|
||||
const commits: any[] = body.commits ?? [];
|
||||
const headCommit = body.head_commit;
|
||||
const ref: string = body.ref ?? '';
|
||||
const branch = ref.replace('refs/heads/', '');
|
||||
const compareUrl: string = body.compare ?? '';
|
||||
const ref: string = body.ref ?? "";
|
||||
const branch = ref.replace("refs/heads/", "");
|
||||
const compareUrl: string = body.compare ?? "";
|
||||
const forced: boolean = body.forced ?? false;
|
||||
|
||||
if (!repo || !headCommit) {
|
||||
return res.status(400).json({ error: 'Invalid push payload' });
|
||||
return res.status(400).json({ error: "Invalid push payload" });
|
||||
}
|
||||
|
||||
// Build commit list (max X)
|
||||
const SHOW_MAX = 5;
|
||||
const commitLines = commits.slice(0, SHOW_MAX).map((c: any) => {
|
||||
const shortId = c.id.substring(0, 7);
|
||||
const msg = c.message.split('\n')[0].substring(0, 64);
|
||||
const msg = c.message.split("\n")[0].substring(0, 64);
|
||||
return `[\`${shortId}\`](${c.url}) ${msg}...`;
|
||||
});
|
||||
|
||||
@@ -48,7 +53,9 @@ export default async function gitCommitPOST(app: Express) {
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(forced ? 0xff4444 : 0x2ea44f)
|
||||
.setTitle(`${forced ? '⚠️ Force Push' : '📦 New Push'} to \`${branch}\``)
|
||||
.setTitle(
|
||||
`${forced ? "⚠️ Force Push" : "📦 New Push"} to \`${branch}\``,
|
||||
)
|
||||
.setURL(compareUrl)
|
||||
.setAuthor({
|
||||
name: pusher.name,
|
||||
@@ -56,28 +63,45 @@ export default async function gitCommitPOST(app: Express) {
|
||||
url: `https://github.com/${pusher.name}`,
|
||||
})
|
||||
.addFields(
|
||||
{ name: '🌿 Branch', value: `\`${branch}\``, inline: true },
|
||||
{ name: `📝 Commits (${commits.length})`, value: commitLines.join('\n') || '_No commits_' },
|
||||
{ name: "🌿 Branch", value: `\`${branch}\``, inline: true },
|
||||
{
|
||||
name: `📝 Commits (${commits.length})`,
|
||||
value: commitLines.join("\n") || "_No commits_",
|
||||
},
|
||||
)
|
||||
.setFooter({ text: `Delivery: ${req.headers['x-github-delivery'] ?? 'unknown'}` })
|
||||
.setTimestamp(headCommit.timestamp ? new Date(headCommit.timestamp) : new Date());
|
||||
.setFooter({
|
||||
text: `Delivery: ${req.headers["x-github-delivery"] ?? "unknown"}`,
|
||||
})
|
||||
.setTimestamp(
|
||||
headCommit.timestamp
|
||||
? new Date(headCommit.timestamp)
|
||||
: new Date(),
|
||||
);
|
||||
|
||||
const channel = await client.channels.fetch(configured_channel) as TextChannel | null;
|
||||
const channel = (await client.channels.fetch(
|
||||
configured_channel,
|
||||
)) as TextChannel | null;
|
||||
if (!channel || !channel.isTextBased()) {
|
||||
console.error('[WEB-gitCommit] Configured channel not found or not text-based');
|
||||
return res.status(500).json({ error: 'Discord channel unavailable' });
|
||||
console.error(
|
||||
"[WEB-gitCommit] Configured channel not found or not text-based",
|
||||
);
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: "Discord channel unavailable" });
|
||||
}
|
||||
|
||||
const message = await channel.send({ embeds: [embed] });
|
||||
console.log(`[WEB-gitCommit] Push event sent to configured channel (${commits.length} commits on ${branch})`);
|
||||
console.log(
|
||||
`[WEB-gitCommit] Push event sent to configured channel (${commits.length} commits on ${branch})`,
|
||||
);
|
||||
|
||||
// Reactions for engagement
|
||||
await message.react('👍');
|
||||
await message.react('🔥');
|
||||
await message.react('🤯');
|
||||
await message.react("👍");
|
||||
await message.react("🔥");
|
||||
await message.react("🤯");
|
||||
|
||||
// Add to DB
|
||||
await db.collection('git_commits').insertOne({
|
||||
await db.collection("git_commits").insertOne({
|
||||
repository: repo.full_name,
|
||||
pusher: pusher.name,
|
||||
branch,
|
||||
@@ -89,8 +113,10 @@ export default async function gitCommitPOST(app: Express) {
|
||||
|
||||
return res.status(200).json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('[WEB-gitCommit] Error handling git commit:', error);
|
||||
return res.status(500).json({ success: false, error: 'Internal Server Error' });
|
||||
console.error("[WEB-gitCommit] Error handling git commit:", error);
|
||||
return res
|
||||
.status(500)
|
||||
.json({ success: false, error: "Internal Server Error" });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user