forked from github/copilot-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-github-event-runtime.js
More file actions
176 lines (159 loc) · 6.59 KB
/
Copy pathtest-github-event-runtime.js
File metadata and controls
176 lines (159 loc) · 6.59 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'liku-github-event-runtime-'));
process.env.LIKU_HOME_OVERRIDE = path.join(tempRoot, '.liku');
process.env.LIKU_HOME_OLD_OVERRIDE = path.join(tempRoot, '.liku-cli-old');
process.env.LIKU_ENABLE_GITHUB = '1';
const { inspectGitHubEvent } = require(path.join(__dirname, '..', 'src', 'main', 'github', 'event-inspect.js'));
const { listGitHubEvents } = require(path.join(__dirname, '..', 'src', 'main', 'github', 'event-list.js'));
const { ingestGitHubWebhookEvent } = require(path.join(__dirname, '..', 'src', 'main', 'github', 'webhook-event-runtime.js'));
let pass = 0;
async function test(name, fn) {
await fn();
pass += 1;
console.log(`PASS ${name}`);
}
function createGitHubEnv() {
return {
LIKU_ENABLE_GITHUB: '1',
LIKU_HOME_OVERRIDE: process.env.LIKU_HOME_OVERRIDE,
LIKU_HOME_OLD_OVERRIDE: process.env.LIKU_HOME_OLD_OVERRIDE,
};
}
(async () => {
try {
await test('event runtime ingestion persists a sanitized artifact and journal entry', async () => {
const report = await ingestGitHubWebhookEvent({
slug: 'owner/repo',
eventName: 'pull_request',
deliveryId: 'delivery-artifact-1',
webhookId: 9001,
headers: {
'x-github-event': 'pull_request',
authorization: 'Bearer ghp_secret_token_12345678901234567890',
'x-hub-signature-256': 'sha256=abcdef123456',
},
payload: {
action: 'opened',
repository: { full_name: 'owner/repo' },
sender: { login: 'octocat' },
installation: { id: 7001 },
number: 123,
pull_request: {
title: 'Add event journal foundation',
body: 'Authorization: Bearer ghp_secret_token_12345678901234567890',
state: 'open',
merged: false,
},
},
});
assert.strictEqual(report.schemaVersion, 'github.event-runtime.v1');
assert.strictEqual(report.success, true);
assert.strictEqual(report.summary.eventName, 'pull_request');
assert.strictEqual(report.summary.slug, 'owner/repo');
assert.ok(report.artifact.filePath);
assert.ok(fs.existsSync(report.artifact.filePath));
const artifact = JSON.parse(fs.readFileSync(report.artifact.filePath, 'utf8'));
assert.strictEqual(artifact.delivery.eventName, 'pull_request');
assert.strictEqual(artifact.target.slug, 'owner/repo');
assert.strictEqual(artifact.headers.authorization, '[redacted]');
assert.strictEqual(artifact.headers['x-hub-signature-256'], '[redacted]');
assert.ok(String(artifact.payload.pull_request.body).startsWith('[redacted issue body;'));
assert.ok(!String(artifact.payload.pull_request.body).includes('ghp_secret_token_12345678901234567890'));
assert.ok((artifact.review?.redactionCount || 0) >= 2);
});
await test('event list filters by repo and event name from the local journal', async () => {
await ingestGitHubWebhookEvent({
slug: 'owner/repo',
eventName: 'push',
deliveryId: 'delivery-list-1',
headers: { 'x-github-event': 'push' },
payload: {
repository: { full_name: 'owner/repo' },
sender: { login: 'octocat' },
ref: 'refs/heads/main',
after: 'abcdef1234567890',
commits: [{ id: '1' }, { id: '2' }],
head_commit: { message: 'Ship it' },
},
});
await ingestGitHubWebhookEvent({
slug: 'other/repo',
eventName: 'workflow_run',
deliveryId: 'delivery-list-2',
headers: { 'x-github-event': 'workflow_run' },
payload: {
action: 'completed',
repository: { full_name: 'other/repo' },
workflow_run: { id: 44, name: 'CI', status: 'completed', conclusion: 'success', event: 'push' },
},
});
const report = await listGitHubEvents({
cwd: tempRoot,
env: createGitHubEnv(),
featureFlagEnabled: true,
slug: 'owner/repo',
limit: 10,
event: 'push',
});
assert.strictEqual(report.schemaVersion, 'github.event-list.v1');
assert.strictEqual(report.localOnly, true);
assert.strictEqual(report.target.slug, 'owner/repo');
assert.strictEqual(report.totalCount, 1);
assert.strictEqual(report.events.length, 1);
assert.strictEqual(report.events[0].eventName, 'push');
assert.strictEqual(report.events[0].slug, 'owner/repo');
assert.strictEqual(report.events[0].payloadPreview.commitCount, 2);
assert.ok(report.eventLog.filePath.endsWith('github-events.jsonl'));
});
await test('event inspect returns the sanitized payload and review metadata', async () => {
const ingested = await ingestGitHubWebhookEvent({
slug: 'owner/repo',
eventName: 'issues',
deliveryId: 'delivery-inspect-1',
headers: {
'x-github-event': 'issues',
'x-hub-signature-256': 'sha256=abcdef123456',
},
payload: {
action: 'opened',
repository: { full_name: 'owner/repo' },
sender: { login: 'octocat' },
issue: {
number: 77,
title: 'Investigate event journal',
body: 'Authorization: Bearer ghp_secret_token_12345678901234567890',
state: 'open',
},
},
});
const report = await inspectGitHubEvent({
cwd: tempRoot,
env: createGitHubEnv(),
featureFlagEnabled: true,
id: ingested.eventId,
slug: 'owner/repo',
});
assert.strictEqual(report.schemaVersion, 'github.event-inspect.v1');
assert.strictEqual(report.event.eventName, 'issues');
assert.strictEqual(report.event.slug, 'owner/repo');
assert.strictEqual(report.event.senderLogin, 'octocat');
assert.ok(Array.isArray(report.event.payloadKeys));
assert.strictEqual(report.event.headers['x-hub-signature-256'], '[redacted]');
assert.ok(String(report.event.payload.issue.body).startsWith('[redacted issue body;'));
assert.ok(!String(report.event.payload.issue.body).includes('ghp_secret_token_12345678901234567890'));
assert.strictEqual(report.artifact.eventId, ingested.eventId);
assert.ok(report.artifact.filePath);
assert.ok(fs.existsSync(report.artifact.filePath));
});
console.log(`PASS github event runtime (${pass} assertions)`);
} finally {
fs.rmSync(tempRoot, { recursive: true, force: true });
}
})().catch((error) => {
console.error(error);
process.exit(1);
});