This commit is contained in:
2026-05-14 21:11:32 +01:00
parent 254ae207b1
commit 01d3e79493
3 changed files with 102 additions and 13 deletions
+3
View File
@@ -107,3 +107,6 @@ listener restarts and GitHub push deploy start/success/failure events, set:
```sh ```sh
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/... DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/...
``` ```
Deploy completion and failure notifications include a changed-file summary and a
truncated patch preview for the pushed diff.
-1
View File
@@ -1,7 +1,6 @@
import React, { useEffect, useRef } from "react"; import React, { useEffect, useRef } from "react";
import { TRUNK_TOP_CSS } from "./Tree"; import { TRUNK_TOP_CSS } from "./Tree";
// aaa
interface Leaf { interface Leaf {
x: number; x: number;
y: number; y: number;
+99 -12
View File
@@ -74,6 +74,17 @@ function truncate(value, maxLength = 900) {
return `${text.slice(0, maxLength - 3)}...` return `${text.slice(0, maxLength - 3)}...`
} }
function codeBlock(value, language = '', maxLength = 1000) {
const fence = language ? `\`\`\`${language}\n` : '```\n'
const suffix = '\n```'
const body = truncate(value || 'No diff returned', maxLength - fence.length - suffix.length)
return `${fence}${body}${suffix}`
}
function validGitSha(value) {
return /^[0-9a-f]{7,40}$/i.test(String(value || ''))
}
function verifySignature(rawBody, signature) { function verifySignature(rawBody, signature) {
if (!SECRET) return true if (!SECRET) return true
if (!signature || !signature.startsWith('sha256=')) return false if (!signature || !signature.startsWith('sha256=')) return false
@@ -119,6 +130,35 @@ function run(command, args, options = {}) {
}) })
} }
function runCapture(command, args, options = {}) {
return new Promise((resolve, reject) => {
const label = [command, ...args].join(' ')
const child = spawn(commandFor(command), args, {
cwd: __dirname,
env: { ...process.env, ...options.env },
shell: process.platform === 'win32',
stdio: ['ignore', 'pipe', 'pipe'],
})
let stdout = ''
let stderr = ''
child.stdout.on('data', (chunk) => {
stdout += chunk
})
child.stderr.on('data', (chunk) => {
stderr += chunk
})
child.on('error', reject)
child.on('exit', (code) => {
if (code === 0) {
resolve(stdout.trim())
} else {
reject(new Error(`${label} exited with ${code}: ${truncate(stderr || stdout, 500)}`))
}
})
})
}
async function ensureBuildDependencies() { async function ensureBuildDependencies() {
await run('npm', ['install', '--production=false', '--include=dev', '--include=optional'], { await run('npm', ['install', '--production=false', '--include=dev', '--include=optional'], {
env: { env: {
@@ -230,6 +270,44 @@ function deployFields(push) {
return fields return fields
} }
async function deployDiff(push) {
const before = push?.before
const after = push?.after
if (!validGitSha(before) || !validGitSha(after) || /^0+$/.test(before)) {
return {
summary: 'Diff unavailable for this push range',
patch: push?.compare ? `Compare: ${push.compare}` : '',
}
}
const range = `${before}..${after}`
const [nameStatus, shortStat, patch] = await Promise.all([
runCapture('git', ['diff', '--name-status', '--find-renames', range]),
runCapture('git', ['diff', '--shortstat', range]),
runCapture('git', ['diff', '--find-renames', '--unified=1', range]),
])
return {
summary: [shortStat, nameStatus].filter(Boolean).join('\n'),
patch,
}
}
function diffFields(diff) {
if (!diff) return []
const fields = []
if (diff.summary) {
fields.push({ name: 'Diff summary', value: codeBlock(diff.summary, 'diff'), inline: false })
}
if (diff.patch) {
fields.push({ name: 'Patch preview', value: codeBlock(diff.patch, 'diff'), inline: false })
}
return fields
}
async function notifyDeployStarted(push) { async function notifyDeployStarted(push) {
await sendDiscordEmbed({ await sendDiscordEmbed({
title: 'GitHub push deploy started', title: 'GitHub push deploy started',
@@ -239,21 +317,22 @@ async function notifyDeployStarted(push) {
}) })
} }
async function notifyDeployCompleted(push) { async function notifyDeployCompleted(push, diff) {
await sendDiscordEmbed({ await sendDiscordEmbed({
title: 'GitHub push deploy completed', title: 'GitHub push deploy completed',
color: 0x00f2ff, color: 0x00f2ff,
fields: deployFields(push), fields: [...deployFields(push), ...diffFields(diff)],
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
}) })
} }
async function notifyDeployFailed(push, error) { async function notifyDeployFailed(push, error, diff) {
await sendDiscordEmbed({ await sendDiscordEmbed({
title: 'GitHub push deploy failed', title: 'GitHub push deploy failed',
color: 0xe82517, color: 0xe82517,
fields: [ fields: [
...deployFields(push), ...deployFields(push),
...diffFields(diff),
{ name: 'Error', value: truncate(error?.message || error), inline: false }, { name: 'Error', value: truncate(error?.message || error), inline: false },
], ],
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
@@ -261,16 +340,24 @@ async function notifyDeployFailed(push, error) {
} }
async function deploy(push) { async function deploy(push) {
await notifyDeployStarted(push) let diff = null
await run('git', ['pull', '--ff-only'])
await ensureBuildDependencies()
await run('npm', ['run', 'build'])
for (const target of RESTART_TARGETS) { try {
await run('pm2', ['reload', target, '--update-env']) await notifyDeployStarted(push)
await run('git', ['pull', '--ff-only'])
diff = await deployDiff(push)
await ensureBuildDependencies()
await run('npm', ['run', 'build'])
for (const target of RESTART_TARGETS) {
await run('pm2', ['reload', target, '--update-env'])
}
await notifyDeployCompleted(push, diff)
} catch (error) {
error.deployDiff = diff
throw error
} }
await notifyDeployCompleted(push)
} }
http http
@@ -313,7 +400,7 @@ http
.then(() => console.log('GitHub push deploy completed')) .then(() => console.log('GitHub push deploy completed'))
.catch((error) => { .catch((error) => {
console.error('GitHub push deploy failed:', error) console.error('GitHub push deploy failed:', error)
notifyDeployFailed(push, error) notifyDeployFailed(push, error, error.deployDiff)
}) })
.finally(() => { .finally(() => {
deploying = false deploying = false