forked from godwokenrises/godwoken-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
481 lines (435 loc) · 18.3 KB
/
integration-test-chat-bot-v1.yml
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
name: Integration Test Chat bot
# Triggered use /itest comment from pr
on:
workflow_dispatch:
inputs:
dispatch:
type: string
description: "Dispatch contains pr context that want to trigger integration test"
required: true
jobs:
component-info:
runs-on: ubuntu-latest
outputs:
components_info: ${{ steps.filter_prebuilds_components.outputs.result }}
comment_id: ${{ steps.post_workflow_run_comment.outputs.result }}
extra_github_env: ${{ steps.escape_multiple_lines_test_inputs.outputs.result }}
steps:
- uses: actions/github-script@v6
id: get_components
with:
script: |
const dispatch = JSON.parse(`${{ github.event.inputs.dispatch }}`);
core.info(`comment body ${dispatch.comment_body}`);
const getPrebuilds = async (org, page, perPage) => {
return await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
package_type: "container",
package_name: "godwoken-prebuilds",
org,
page,
per_page: perPage,
});
};
const getPrebuildsByTag = async (org, tag) => {
const pkgs = await getPrebuilds(org, 1, 100);
for (const pkg of pkgs.data) {
if (pkg.metadata.container.tags.find(pTag => pTag === tag || pTag.match(tag))) {
return pkg;
}
}
};
// Parse commentBody
const prebuilds = {
org: "godwokenrises",
repo: "godwoken-docker-prebuilds",
pattern: /prebuilds: (.*)/,
packageType: "container",
packageName: "godwoken-prebuilds",
image: "ghcr.io/godwokenrises/godwoken-prebuilds",
tags: ["^[v]?1"],
sha: undefined,
htmlUrl: undefined,
manifest: {
godwoken: {
pattern: /"ref.component.godwoken-sha1": "(.{40})"/,
sha: undefined,
},
scripts: {
pattern: /"ref.component.godwoken-scripts-sha1": "(.{40})"/,
sha: undefined,
},
polyjuice: {
pattern: /"ref.component.godwoken-polyjuice-sha1": "(.{40})"/,
sha: undefined,
},
},
};
const components = {
godwoken: {
owner: "godwokenrises",
repo: "godwoken",
branch: "develop",
pattern: /godwoken: (.*)/,
commit: undefined,
htmlUrl: undefined,
branchOrCommit: undefined,
usePrebuilds: undefined,
},
scripts: {
owner: "godwokenrises",
repo: "godwoken-scripts",
branch: "master",
pattern: /scripts: (.*)/,
commit: undefined,
htmlUrl: undefined,
branchOrCommit: undefined,
usePrebuilds: undefined,
},
polyjuice: {
owner: "godwokenrises",
repo: "godwoken-polyjuice",
branch: "main",
pattern: /polyjuice: (.*)/,
commit: undefined,
htmlUrl: undefined,
branchOrCommit: undefined,
usePrebuilds: undefined,
},
web3: {
owner: "godwokenrises",
repo: "godwoken-web3",
branch: "main",
pattern: /web3: (.*)/,
commit: undefined,
htmlUrl: undefined,
branchOrCommit: undefined,
},
kicker: {
owner: "godwokenrises",
repo: "godwoken-kicker",
branch: "master",
pattern: /kicker: (.*)/,
commit: undefined,
htmlUrl: undefined,
branchOrCommit: undefined,
},
tests: {
owner: "godwokenrises",
repo: "godwoken-tests",
branch: "develop",
pattern: /tests: (.*)/,
commit: undefined,
htmlUrl: undefined,
branchOrCommit: undefined,
},
};
// Fetch pr commit
const pr = (
await github.rest.pulls.get({
owner: dispatch.repo.owner,
repo: dispatch.repo.repo,
pull_number: dispatch.issue.number,
})
).data.head;
core.info(`${JSON.stringify(pr.ref)}`);
core.info(`${JSON.stringify(pr.repo.name)}`);
// Fetch prebuilds
const matchPrebuilds = async () => {
const match = prebuilds.pattern.exec(`${dispatch.comment_body}`);
if (match) {
return await getPrebuildsByTag(prebuilds.org, match[1]);
} else {
return await getPrebuildsByTag(prebuilds.org, prebuilds.tags[0]);
}
};
const pkg = await matchPrebuilds();
prebuilds.tags = pkg.metadata.container.tags;
prebuilds.htmlUrl = pkg.html_url;
prebuilds.sha = pkg.name;
core.info(`pkg: ${JSON.stringify(pkg)}`);
// Fetch prebuilds components commit;
const packagePage = JSON.stringify(
await github.request(`GET ${prebuilds.htmlUrl}`)
);
const manifestLabelsStart = packagePage.indexOf(""labels":");
const manifestLabelsEnd = packagePage.lastIndexOf("</pre>");
const manifestLabelsContent = packagePage.substring(manifestLabelsStart, manifestLabelsEnd);
for (const name in prebuilds.manifest) {
const label = prebuilds.manifest[name];
const match = label.pattern.exec(manifestLabelsContent);
if (match) {
label.sha = match[1];
} else {
console.log(`${manifestLabelsContent}`);
throw `${name}-sha1 not found`;
}
}
core.info(`${JSON.stringify(prebuilds)}`);
for (const name in components) {
const comp = components[name];
// Ref from pr is priority
if (pr.repo.name === comp.repo) {
comp.branchOrCommit = pr.sha;
continue;
}
const match = comp.pattern.exec(`${dispatch.comment_body}`);
if (match) {
comp.branchOrCommit = match[1];
} else if (prebuilds.manifest[name]) {
comp.branchOrCommit = prebuilds.manifest[name].sha;
comp.usePrebuilds = true;
} else {
if (name === "web3") {
continue;
}
comp.branchOrCommit = comp.branch;
}
}
core.info(JSON.stringify(components));
// Fetch branch/commit sha and html url
for (const name in components) {
const comp = components[name];
core.info(`fetch branch or commit ${name}`);
if (comp.branchOrCommit === undefined) {
core.info(`skip fetch component info ${name}`);
continue;
}
try {
// Try branch
const resp = await github.rest.repos.getBranch({
owner: comp.owner,
repo: comp.repo,
branch: comp.branchOrCommit,
});
comp.commit = resp.data.commit.sha;
comp.htmlUrl = resp.data.commit.html_url;
} catch {
core.info(`${comp.branchOrCommit} branch not found`);
}
try {
if (comp.commit === undefined) {
// Try commit
const resp = await github.rest.git.getCommit({
owner: comp.owner,
repo: comp.repo,
commit_sha: comp.branchOrCommit,
});
comp.commit = resp.data.sha;
comp.htmlUrl = resp.data.html_url;
}
} catch {
core.info(`${comp.branchOrCommit} full commit not found`);
}
try {
if (comp.commit === undefined) {
// Try serach commits
const resp = await github.rest.repos.listCommits({
owner: comp.owner,
repo: comp.repo,
sha: comp.branchOrCommit,
per_page: 1,
page: 1,
});
if (resp.data[0].sha.startsWith(comp.branchOrCommit)) {
comp.commit = resp.data[0].sha;
comp.htmlUrl = resp.data[0].html_url;
} else {
throw `${comp.branchOrCommit} short sha commit not found`;
}
}
} catch {
core.error(`${comp.branchOrCommit} short commit not found`);
}
if (comp.commit === undefined) {
throw `${comp.branchOrCommit} not found`;
}
}
core.info(JSON.stringify(components));
return {prebuilds, components};
- name: Generate gw-bot token
id: generate_gw_bot_token
uses: wow-actions/use-app-token@v1
with:
app_id: ${{ secrets.GW_BOT_APP_ID }}
private_key: ${{ secrets.GW_BOT_PRIVATE_KEY }}
- name: Post a comment about component information to PR
id: post_workflow_run_comment
uses: actions/github-script@v6
with:
github-token: ${{ steps.generate_gw_bot_token.outputs.token }}
script: |
const components_info = JSON.parse(`${{ steps.get_components.outputs.result }}`);
const prebuilds = components_info.prebuilds;
const components = components_info.components;
const dispatch = JSON.parse(`${{ github.event.inputs.dispatch }}`);
const workflowRun = (await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: `${{ github.run_id }}`,
})).data;
core.info(`action html url: ${workflowRun.html_url}`)
// Post integration test info
let integrationTestInfo = `
\### Running integration test
Workflow Run Id: [${workflowRun.id}](${workflowRun.html_url})
\### Components:
- prebuilds: [${prebuilds.tags}](${prebuilds.htmlUrl})`;
for (const name in components) {
const comp = components[name];
if (comp.commit === undefined) {
continue;
}
let shortSha = comp.commit.substr(0, 7);
if (comp.usePrebuilds) {
shortSha = `prebuilds ${shortSha}`;
}
integrationTestInfo = `${integrationTestInfo}\n- ${name}: [${shortSha}](${comp.htmlUrl})`;
}
// Add manual trigger example
integrationTestInfo = `${integrationTestInfo}
\### Manually running integration test
Post a comment contains
\`\`\`
/itest
[prebuilds: tag]
[godwoken: branch/ref]
[scripts: branch/ref]
[polyjuice: branch/ref]
[web3: branch/ref]
[kicker: branch/ref]
[tests: branch/ref]
\`\`\`
Note: \`\`\`[]\`\`\` means optional, for example
\`\`\`
/itest
prebuilds: dev-202203280240
godwoken: develop
scripts: 81676d9d53ffdf5bbaa60483928d07da16eb4a88
polyjuice: e37553b9
\`\`\``;
core.info(`${integrationTestInfo}`);
const comment = await github.rest.issues.createComment({
issue_number: dispatch.issue.number,
owner: dispatch.repo.owner,
repo: dispatch.repo.repo,
body: integrationTestInfo,
});
core.info(`comment ${JSON.stringify(comment.data, null, 2)}`);
return comment.data.id;
- name: Filter components use prebuilds
id: filter_prebuilds_components
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const components_info = JSON.parse(`${{ steps.get_components.outputs.result }}`);
const prebuilds = components_info.prebuilds;
const components = components_info.components;
core.info(`${ JSON.stringify(prebuilds, null, 2) }`)
core.info(`${ JSON.stringify(components, null, 2) }`)
for (const name in components) {
const comp = components[name];
if (comp.commit === undefined) {
continue;
}
if (comp.usePrebuilds) {
comp.commit = undefined;
}
}
core.info(JSON.stringify(components));
return {prebuilds, components};
- name: Generate integration test inputs
id: generate_integration_test_inputs
uses: actions/github-script@v6
with:
script: |
const components_info = JSON.parse(`${{ steps.filter_prebuilds_components.outputs.result }}`);
const prebuilds = components_info.prebuilds;
const components = components_info.components;
const componentGitUrl = (comp) => {
return `https://github.com/${comp.owner}/${comp.repo}`
};
let extraGithubENV = `\nGODWOKEN_PREBUILD_IMAGE_NAME=${prebuilds.image}:${prebuilds.tags[0]}`;
if (components.kicker.commit) {
extraGithubENV = `${extraGithubENV}\nGODWOKEN_KICKER_REF=${components.kicker.commit}`
}
if (components.tests.commit) {
extraGithubENV = `${extraGithubENV}\nGODWOKEN_TESTS_REF=${components.tests.commit}`
}
if (components.godwoken.commit) {
const gitUrl = componentGitUrl(components.godwoken);
extraGithubENV = `${extraGithubENV}\nMANUAL_BUILD_GODWOKEN=true`
extraGithubENV = `${extraGithubENV}\nGODWOKEN_GIT_URL=${gitUrl}`
extraGithubENV = `${extraGithubENV}\nGODWOKEN_GIT_CHECKOUT=${components.godwoken.commit}`
}
if (components.scripts.commit) {
const gitUrl = componentGitUrl(components.scripts);
extraGithubENV = `${extraGithubENV}\nMANUAL_BUILD_SCRIPTS=true`
extraGithubENV = `${extraGithubENV}\nSCRIPTS_GIT_URL=${gitUrl}`
extraGithubENV = `${extraGithubENV}\nSCRIPTS_GIT_CHECKOUT=${components.scripts.commit}`
extraGithubENV = `${extraGithubENV}\nOMNI_LOCK_GIT_URL=https://github.com/nervosnetwork/ckb-production-scripts`
extraGithubENV = `${extraGithubENV}\nOMNI_LOCK_GIT_CHECKOUT=rc_lock`
}
if (components.polyjuice.commit) {
const gitUrl = componentGitUrl(components.polyjuice);
extraGithubENV = `${extraGithubENV}\nMANUAL_BUILD_POLYJUICE=true`
extraGithubENV = `${extraGithubENV}\nPOLYJUICE_GIT_URL=${gitUrl}`
extraGithubENV = `${extraGithubENV}\nPOLYJUICE_GIT_CHECKOUT=${components.polyjuice.commit}`
}
if (components.web3.commit) {
const gitUrl = componentGitUrl(components.web3);
extraGithubENV = `${extraGithubENV}\nMANUAL_BUILD_WEB3=true`
extraGithubENV = `${extraGithubENV}\nMANUAL_BUILD_WEB3_INDEXER=true`
extraGithubENV = `${extraGithubENV}\nWEB3_GIT_URL=${gitUrl}`
extraGithubENV = `${extraGithubENV}\nWEB3_GIT_CHECKOUT=${components.web3.commit}`
}
core.info(`${extraGithubENV}`)
return extraGithubENV;
# Ref: https://github.community/t/set-output-truncates-multiline-strings/16852/9
- name: Escape multiple lines test inputs
id: escape_multiple_lines_test_inputs
run: |
inputs=${{ steps.generate_integration_test_inputs.outputs.result }}
inputs="${inputs//'%'/'%25'}"
inputs="${inputs//'\n'/'%0A'}"
inputs="${inputs//'\r'/'%0D'}"
echo "::set-output name=result::$inputs"
run-integration-test:
needs: component-info
uses: godwokenrises/godwoken-tests/.github/workflows/reusable-integration-test-v1.yml@develop
with:
extra_github_env: |
${{ needs.component-info.outputs.extra_github_env }}
run-result-feedback:
needs: [component-info, run-integration-test]
runs-on: ubuntu-latest
if: always()
steps:
# Generate new token in case the tests run over an hour
- name: Generate gw-bot token
id: generate_gw_bot_token
uses: wow-actions/use-app-token@v1
with:
app_id: ${{ secrets.GW_BOT_APP_ID }}
private_key: ${{ secrets.GW_BOT_PRIVATE_KEY }}
- name: Update comment about integration test result
uses: actions/github-script@v6
with:
github-token: ${{ steps.generate_gw_bot_token.outputs.token }}
script: |
const dispatch = JSON.parse(`${{ github.event.inputs.dispatch }}`);
const comment_id = `${{ needs.component-info.outputs.comment_id }}`;
core.info(`comment id ${comment_id}`);
const comment = await github.rest.issues.getComment({
owner: dispatch.repo.owner,
repo: dispatch.repo.repo,
comment_id,
});
const appendConclusion = `${comment.data.body}\n### Run Result\n**${{ needs.run-integration-test.result }}**`;
const resp = await github.rest.issues.updateComment({
owner: dispatch.repo.owner,
repo: dispatch.repo.repo,
comment_id,
body: appendConclusion,
});
core.info(`${JSON.stringify(resp, null, 2)}`);