first commit
This commit is contained in:
96
src/web/gitCommit.ts
Normal file
96
src/web/gitCommit.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
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) => {
|
||||
try {
|
||||
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 !== 'push') {
|
||||
return res.status(200).json({ success: true, message: `Event '${event}' ignored` });
|
||||
}
|
||||
|
||||
const body = req.body;
|
||||
const repo = body.repository;
|
||||
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 forced: boolean = body.forced ?? false;
|
||||
|
||||
if (!repo || !headCommit) {
|
||||
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);
|
||||
return `[\`${shortId}\`](${c.url}) ${msg}...`;
|
||||
});
|
||||
|
||||
if (commits.length > SHOW_MAX) {
|
||||
commitLines.push(`...and ${commits.length - SHOW_MAX} more`);
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(forced ? 0xff4444 : 0x2ea44f)
|
||||
.setTitle(`${forced ? '⚠️ Force Push' : '📦 New Push'} to \`${branch}\``)
|
||||
.setURL(compareUrl)
|
||||
.setAuthor({
|
||||
name: pusher.name,
|
||||
iconURL: `https://github.com/${pusher.name}.png`,
|
||||
url: `https://github.com/${pusher.name}`,
|
||||
})
|
||||
.addFields(
|
||||
{ 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());
|
||||
|
||||
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' });
|
||||
}
|
||||
|
||||
const message = await channel.send({ embeds: [embed] });
|
||||
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('🤯');
|
||||
|
||||
// Add to DB
|
||||
await db.collection('git_commits').insertOne({
|
||||
repository: repo.full_name,
|
||||
pusher: pusher.name,
|
||||
branch,
|
||||
commitCount: commits.length,
|
||||
compareUrl,
|
||||
forced,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
|
||||
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' });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user