Repository navigation
Expand file tree
/
Copy pathtest-webhook.mjs
More file actions
108 lines (93 loc) · 3.3 KB
/
Copy pathtest-webhook.mjs
File metadata and controls
108 lines (93 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
/**
* 本地 Webhook 测试脚本 (零依赖, Node 18+)
* 模拟 GitHub Webhook 事件,测试服务是否正常
*
* 用法:
* node test-webhook.mjs # 默认 http://localhost:8787/api/webhook
* node test-webhook.mjs --url http://localhost:8787/api/webhook --secret test-secret
*/
import { createHmac } from 'node:crypto';
function parseArgs() {
const argv = process.argv.slice(2);
const get = (flag, fallback) => {
const i = argv.indexOf(flag);
return i >= 0 && argv[i + 1] ? argv[i + 1] : fallback;
};
return {
url: get('--url', 'http://localhost:8787/api/webhook'),
secret: get('--secret', 'test-secret'),
};
}
/** 生成 HMAC-SHA256 签名 (与 GitHub 服务端行为一致) */
function generateSignature(payload, secret) {
return 'sha256=' + createHmac('sha256', secret).update(payload, 'utf8').digest('hex');
}
/** 创建测试用的 PR 事件 payload */
function createTestPrPayload() {
return {
action: 'opened',
number: 1,
pull_request: {
number: 1,
title: 'Test PR for Webhook',
body: 'This is a test PR to verify the webhook functionality.',
state: 'open',
user: { login: 'test-user' },
head: { sha: 'abc123def456', ref: 'feature/test-branch' },
base: { ref: 'main' },
},
repository: { name: 'test-repo', owner: { login: 'test-owner' } },
installation: { id: 12345678 },
};
}
async function main() {
const { url, secret } = parseArgs();
console.log('='.repeat(60));
console.log('🧪 AutoGuard Webhook 本地测试工具');
console.log('='.repeat(60));
const payload = createTestPrPayload();
const payloadJson = JSON.stringify(payload);
const signature = generateSignature(payloadJson, secret);
const headers = {
'Content-Type': 'application/json',
'X-GitHub-Event': 'pull_request',
'X-GitHub-Delivery': `test-${Date.now()}`,
'X-Hub-Signature-256': signature,
};
console.log(`\n📤 发送测试请求到: ${url}`);
console.log(`🔗 事件类型: pull_request`);
console.log(`🔑 签名: ${signature.slice(0, 50)}...`);
console.log(`\n📝 Payload 预览:`);
console.log(JSON.stringify(payload, null, 2).slice(0, 500) + '...');
try {
const res = await fetch(url, { method: 'POST', headers, body: payloadJson });
const text = await res.text();
console.log(`\n📥 响应状态码: ${res.status}`);
console.log('📄 响应内容:');
try {
console.log(JSON.stringify(JSON.parse(text), null, 2));
} catch {
console.log(text);
}
if (res.status === 200) {
console.log('\n✅ 测试通过! Webhook 服务运行正常。');
} else if (res.status === 401) {
console.log('\n⚠️ 签名验证失败,请检查 WEBHOOK_SECRET 配置。');
} else if (res.status === 500) {
console.log('\n❌ 服务器内部错误,请检查服务日志。');
} else {
console.log(`\n⚠️ 收到意外的状态码: ${res.status}`);
}
} catch (e) {
if (e.cause?.code === 'ECONNREFUSED') {
console.log(`\n❌ 无法连接到 ${url}`);
console.log('请确保服务已启动: npm run dev');
} else {
console.log(`\n💥 测试过程中发生错误: ${e.message}`);
}
process.exitCode = 1;
}
console.log('\n' + '='.repeat(60));
}
main();