forked from github/copilot-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-github-webhook-preview-apply.js
More file actions
367 lines (338 loc) · 15.1 KB
/
Copy pathtest-github-webhook-preview-apply.js
File metadata and controls
367 lines (338 loc) · 15.1 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/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-webhook-'));
process.env.LIKU_HOME_OVERRIDE = path.join(tempRoot, '.liku');
process.env.LIKU_HOME_OLD_OVERRIDE = path.join(tempRoot, '.liku-cli-old');
const {
createGitHubCommandExecutor,
} = require(path.join(__dirname, '..', 'src', 'main', 'github', 'command-executor.js'));
const {
readGitHubWriteApplyResultArtifact,
readGitHubWriteApprovalArtifact,
readGitHubWriteEventLog,
readGitHubWritePreviewArtifact,
} = require(path.join(__dirname, '..', 'src', 'main', 'github', 'write-artifacts.js'));
let pass = 0;
async function test(name, fn) {
await fn();
pass += 1;
console.log(`PASS ${name}`);
}
function createHeaders(values = {}) {
return {
get(name) {
return values[String(name || '').toLowerCase()] || values[name] || null;
},
};
}
function createJsonResponse(status, payload, headers = {}, url = 'https://api.github.com/repos/example/project/hooks') {
return {
ok: status >= 200 && status < 300,
status,
url,
headers: createHeaders(headers),
async text() {
return JSON.stringify(payload);
},
};
}
function createExecutor(env = {}) {
return createGitHubCommandExecutor({
env: {
GH_TOKEN: 'github_pat_test_webhook_preview_apply_1234567890',
...env,
},
cwd: path.join(__dirname, '..'),
});
}
function buildWebhookResponse(overrides = {}) {
return {
id: 9001,
type: 'Repository',
name: 'web',
active: true,
events: ['push', 'pull_request'],
config: {
url: 'https://assistant.example.com/github/webhook',
content_type: 'json',
insecure_ssl: '0',
},
deliveries_url: 'https://api.github.com/repos/example/project/hooks/9001/deliveries',
ping_url: 'https://api.github.com/repos/example/project/hooks/9001/pings',
test_url: 'https://api.github.com/repos/example/project/hooks/9001/tests',
url: 'https://api.github.com/repos/example/project/hooks/9001',
created_at: '2026-05-28T00:00:00.000Z',
updated_at: '2026-05-28T00:00:00.000Z',
last_response: {
code: null,
status: 'unused',
message: null,
},
...overrides,
};
}
(async () => {
try {
await test('webhook create draft persists reviewed artifacts with a secret ref but without the secret value', async () => {
const executor = createExecutor({ LIKU_WEBHOOK_SECRET: 'super-secret-webhook-value' });
const report = await executor.execute({
source: 'cli',
area: 'webhook',
action: 'create',
positionals: ['webhook', 'create', 'draft'],
options: {
slug: 'example/project',
events: 'push,pull_request,workflow_run',
'target-url': 'https://assistant.example.com/github/webhook',
'secret-ref': 'repo:LIKU_WEBHOOK_SECRET',
'content-type': 'json',
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(report.success, true);
assert.strictEqual(report.schemaVersion, 'github.webhook-create-draft.v1');
assert.strictEqual(report.capability.key, 'webhook.create.draft');
assert.strictEqual(report.policy.allowed, true);
assert.strictEqual(report.policy.state, 'preview-allowed');
assert.strictEqual(report.policy.riskLevel, 'medium');
assert.strictEqual(report.draft.targetUrl, 'https://assistant.example.com/github/webhook');
assert.deepStrictEqual(report.draft.events, ['push', 'pull_request', 'workflow_run']);
assert.strictEqual(report.draft.contentType, 'json');
assert.strictEqual(report.draft.secretRef, 'repo:LIKU_WEBHOOK_SECRET');
assert.strictEqual(report.draft.secretEnvName, 'LIKU_WEBHOOK_SECRET');
assert.strictEqual(report.draft.active, true);
assert.ok(report.previewId);
assert.ok(fs.existsSync(report.previewArtifact.filePath));
assert.ok(fs.existsSync(report.approvalArtifact.filePath));
const previewArtifact = readGitHubWritePreviewArtifact({ previewId: report.previewId });
const approvalArtifact = readGitHubWriteApprovalArtifact({ previewId: report.previewId });
const eventLog = readGitHubWriteEventLog({ previewId: report.previewId });
const previewText = JSON.stringify(previewArtifact);
assert.strictEqual(previewArtifact.target.secretRef, 'repo:LIKU_WEBHOOK_SECRET');
assert.strictEqual(previewArtifact.target.secretEnvName, 'LIKU_WEBHOOK_SECRET');
assert.ok(!previewText.includes('super-secret-webhook-value'));
assert.strictEqual(approvalArtifact.status, 'requested');
assert.ok(eventLog.events.some((entry) => entry.eventName === 'preview.created'));
assert.ok(eventLog.events.some((entry) => entry.eventName === 'approval.requested'));
});
await test('webhook create apply resolves the secret ref from local env and creates the webhook once', async () => {
const executor = createExecutor({ LIKU_WEBHOOK_SECRET: 'super-secret-webhook-value' });
let fetchCalls = 0;
const preview = await executor.execute({
source: 'cli',
area: 'webhook',
action: 'create',
positionals: ['webhook', 'create', 'draft'],
options: {
slug: 'example/project',
events: 'push,pull_request',
'target-url': 'https://assistant.example.com/github/webhook',
'secret-ref': 'repo:LIKU_WEBHOOK_SECRET',
'content-type': 'json',
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
const apply = await executor.execute({
source: 'cli',
area: 'apply',
action: preview.previewId,
positionals: ['apply', preview.previewId],
options: {
approve: true,
approvalFile: preview.approvalArtifact.filePath,
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
fetchImpl: async (url, init = {}) => {
fetchCalls += 1;
const parsed = new URL(url);
assert.strictEqual(String(init.method || 'GET').toUpperCase(), 'POST');
assert.strictEqual(parsed.pathname, '/repos/example/project/hooks');
const body = JSON.parse(String(init.body || '{}'));
assert.strictEqual(body.name, 'web');
assert.strictEqual(body.active, true);
assert.deepStrictEqual(body.events, ['push', 'pull_request']);
assert.strictEqual(body.config.url, 'https://assistant.example.com/github/webhook');
assert.strictEqual(body.config.content_type, 'json');
assert.strictEqual(body.config.secret, 'super-secret-webhook-value');
return createJsonResponse(201, buildWebhookResponse({ events: body.events }), {}, url);
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(apply.success, true);
assert.strictEqual(apply.schemaVersion, 'github.write-apply.v1');
assert.strictEqual(apply.approval.status, 'applied');
assert.strictEqual(apply.result.type, 'webhook-create');
assert.strictEqual(apply.result.webhookId, 9001);
assert.strictEqual(apply.result.webhook.id, 9001);
assert.strictEqual(apply.result.webhook.config.url, 'https://assistant.example.com/github/webhook');
assert.strictEqual(fetchCalls, 1);
const resultArtifact = readGitHubWriteApplyResultArtifact({ previewId: preview.previewId });
const eventLog = readGitHubWriteEventLog({ previewId: preview.previewId });
assert.strictEqual(resultArtifact.success, true);
assert.strictEqual(resultArtifact.result.type, 'webhook-create');
assert.ok(eventLog.events.some((entry) => entry.eventName === 'apply.succeeded'));
});
await test('webhook update and ping apply flows use the direct webhook API', async () => {
const executor = createExecutor();
const updatePreview = await executor.execute({
source: 'cli',
area: 'webhook',
action: 'update',
positionals: ['webhook', 'update', 'draft', '9001'],
options: {
slug: 'example/project',
events: 'workflow_run,pull_request_review',
'target-url': 'https://assistant.example.com/github/webhook',
active: 'false',
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(updatePreview.success, true);
assert.strictEqual(updatePreview.schemaVersion, 'github.webhook-update-draft.v1');
assert.strictEqual(updatePreview.draft.webhookId, 9001);
assert.ok(updatePreview.draft.updates.includes('events'));
assert.ok(updatePreview.draft.updates.includes('target-url'));
assert.ok(updatePreview.draft.updates.includes('active'));
const updateApply = await executor.execute({
source: 'cli',
area: 'apply',
action: updatePreview.previewId,
positionals: ['apply', updatePreview.previewId],
options: {
approve: true,
approvalFile: updatePreview.approvalArtifact.filePath,
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
fetchImpl: async (url, init = {}) => {
const parsed = new URL(url);
assert.strictEqual(String(init.method || 'GET').toUpperCase(), 'PATCH');
assert.strictEqual(parsed.pathname, '/repos/example/project/hooks/9001');
const body = JSON.parse(String(init.body || '{}'));
assert.deepStrictEqual(body.events, ['workflow_run', 'pull_request_review']);
assert.strictEqual(body.active, false);
assert.strictEqual(body.config.url, 'https://assistant.example.com/github/webhook');
assert.ok(!Object.prototype.hasOwnProperty.call(body.config, 'secret'));
return createJsonResponse(200, buildWebhookResponse({
active: false,
events: body.events,
}), {}, url);
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(updateApply.success, true);
assert.strictEqual(updateApply.result.type, 'webhook-update');
assert.strictEqual(updateApply.result.webhookId, 9001);
assert.strictEqual(updateApply.result.webhook.active, false);
assert.deepStrictEqual(updateApply.result.webhook.events, ['workflow_run', 'pull_request_review']);
const pingPreview = await executor.execute({
source: 'cli',
area: 'webhook',
action: 'ping',
positionals: ['webhook', 'ping', 'draft', '9001'],
options: {
slug: 'example/project',
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(pingPreview.success, true);
assert.strictEqual(pingPreview.schemaVersion, 'github.webhook-ping-draft.v1');
assert.strictEqual(pingPreview.draft.webhookId, 9001);
const pingApply = await executor.execute({
source: 'cli',
area: 'apply',
action: pingPreview.previewId,
positionals: ['apply', pingPreview.previewId],
options: {
approve: true,
approvalFile: pingPreview.approvalArtifact.filePath,
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
fetchImpl: async (url, init = {}) => {
const parsed = new URL(url);
assert.strictEqual(String(init.method || 'GET').toUpperCase(), 'POST');
assert.strictEqual(parsed.pathname, '/repos/example/project/hooks/9001/pings');
assert.deepStrictEqual(JSON.parse(String(init.body || '{}')), {});
return createJsonResponse(204, undefined, {}, url);
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(pingApply.success, true);
assert.strictEqual(pingApply.result.type, 'webhook-ping');
assert.strictEqual(pingApply.result.webhookId, 9001);
assert.strictEqual(pingApply.result.accepted, true);
});
await test('webhook create apply fails before network when the local secret env is missing', async () => {
const executor = createExecutor();
let fetchCalls = 0;
const preview = await executor.execute({
source: 'cli',
area: 'webhook',
action: 'create',
positionals: ['webhook', 'create', 'draft'],
options: {
slug: 'example/project',
events: 'push',
'target-url': 'https://assistant.example.com/github/webhook',
'secret-ref': 'repo:LIKU_WEBHOOK_SECRET',
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
const apply = await executor.execute({
source: 'cli',
area: 'apply',
action: preview.previewId,
positionals: ['apply', preview.previewId],
options: {
approve: true,
approvalFile: preview.approvalArtifact.filePath,
featureFlags: { enableGitHub: true, enableGitHubWrites: true },
fetchImpl: async () => {
fetchCalls += 1;
throw new Error('fetch should not be called when the secret ref cannot be resolved');
},
},
featureFlagEnabled: true,
writeFeatureFlagEnabled: true,
executionPreferences: { approvalMode: 'prompt', dryRunDefault: false },
});
assert.strictEqual(apply.success, false);
assert.strictEqual(apply.error, 'GITHUB_API_FAILURE');
assert.ok(String(apply.message || '').includes('LIKU_WEBHOOK_SECRET'));
assert.strictEqual(fetchCalls, 0);
const resultArtifact = readGitHubWriteApplyResultArtifact({ previewId: preview.previewId });
assert.strictEqual(resultArtifact.success, false);
assert.strictEqual(resultArtifact.error.code, 'GITHUB_API_FAILURE');
});
console.log(`PASS github webhook preview/apply (${pass} assertions)`);
} finally {
fs.rmSync(tempRoot, { recursive: true, force: true });
}
})().catch((error) => {
console.error('FAIL github webhook preview/apply');
console.error(error.stack || error.message);
process.exit(1);
});