-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
969 lines (851 loc) · 26.9 KB
/
index.js
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
import assert from 'assert';
import path from 'path';
import WebhooksApi from '@octokit/webhooks';
import createLogger from 'silk-log';
import express from 'express';
import github from 'octonode';
import {promisify} from 'es6-promisify';
import createBuildKiteClient from 'buildnode';
import moment from 'moment';
import axios from 'axios';
const log = createLogger('index');
const STATUS_CONTEXT = 'ci-gate';
const CI_LABEL = 'CI';
const NOCI_LABEL = 'noCI';
const envconst = {
/*
The buildkite token requires the following scopes:
* read_builds
* write_builds
* read_build_logs
* read_organizations
* read_pipelines
*/
BUILDKITE_TOKEN: null,
/*
The buildkite organization slug name to use
*/
BUILDKITE_ORG_SLUG: null,
/*
List of buildkite pipelines that may have their logs exposed to the public
*/
BUILDKITE_PIPELINE_PUBLIC_LOG_WHITELIST: '', // comma separated, no spaces
/*
Exposes all logs of whitelisted pipelines if set to true
*/
BUILDKITE_EXPOSE_ALL_JOB_LOGS: false,
/*
Github OAuth token with access to the relevant github projects with the
required scopes:
* repo:status
* repo_deployment
* public_repo
*/
GITHUB_TOKEN: null,
/*
Default http port (used for local dev only normally)
*/
PORT: 5000,
/*
Public URL to this server
*/
PUBLIC_URL_ROOT: 'http://localhost:5000',
/*
Secret string added in the Github webhook configuration
*/
GITHUB_WEBHOOK_SECRET: null,
/*
List of users without write access to the repo that should also be
automatically granted CI access
*/
CI_USER_WHITELIST: '', // comma separated, no spaces
/*
If true, allow all CI from all users
*/
CI_FOR_EVERYBODY: false,
/*
Labels that are specific to instance of CI gate.
These labels are passed on to buildkite.
*/
CUSTOM_PR_LABEL_BUILDS: '', // comma separated, no spaces
/*
Merge method to use for Github PRs: merge, squash or rebase
*/
GITHUB_MERGE_METHOD: 'rebase',
/*
Enables automerge feature if true
*/
AUTOMERGE: false,
/*
Specify the string label (eg, 'automerge')
*/
AUTOMERGE_LABEL: 'automerge',
};
for (const v in envconst) {
envconst[v] = process.env[v] || envconst[v];
if (envconst[v] === null) {
throw new Error(`${v} environment variable not defined`);
}
}
const githubClient = github.client(envconst.GITHUB_TOKEN);
const needsCiLabelDescription = `A project member must add the '${CI_LABEL}' label for tests to start`;
let buildkiteClient = null;
let buildkiteOrg = null;
async function getBuildkitePipeline(pipeline) {
let nullBuildkiteOrg = buildkiteOrg === null;
console.log(`nullBuildkiteOrg: ${nullBuildkiteOrg}`);
if (nullBuildkiteOrg) {
console.log(`Connecting to ${envconst.BUILDKITE_ORG_SLUG} Buildkite organization`);
buildkiteClient = createBuildKiteClient({
accessToken: envconst.BUILDKITE_TOKEN
});
buildkiteClient.getOrganizationAsync = promisify(buildkiteClient.getOrganization);
buildkiteOrg = await buildkiteClient.getOrganizationAsync(envconst.BUILDKITE_ORG_SLUG);
buildkiteOrg.getPipelineAsync = promisify(buildkiteOrg.getPipeline);
}
try {
return buildkiteOrg.getPipelineAsync(pipeline);
} catch (err) {
console.log(`Failed to get buildkite pipeline: ${err}`);
if (!nullBuildkiteOrg) {
buildkiteClient = buildkiteOrg = null;
return getBuildkitePipeline(pipeline);
}
}
return pipeline;
}
async function triggerPipeline(pipelineName, repoName, baseBranch, prNumber, commit, label, title, user) {
if (pipelineName != 'solana') {
log.info('only support solana pipeline');
return;
}
const repo = githubClient.repo(repoName);
const branch = `pull/${prNumber}/head`;
const message = `PR#${prNumber} - ${commit.substring(0, 8)} - ${title} - ${user}`;
log.info(`Triggering pull request: ${repoName}:${branch} at ${commit}`);
const pr = repo.pr(prNumber);
const prFiles = await pr.filesAsync();
const prFilenames = prFiles[0].map(f => f.filename);
const affected_files = prFilenames.join(':');
log.info(`files affected by this PR: ${affected_files}`);
let data = {
'branch': branch,
'commit': commit,
'message': message,
meta_data: {
'affected_files': affected_files,
'pr_number': prNumber,
},
env: {
'GITHUB_USER': user,
},
pull_request_base_branch: baseBranch
};
axios.post(
`https://api.buildkite.com/v2/organizations/${envconst.BUILDKITE_ORG_SLUG}/pipelines/solana/builds`,
data,
{
headers: {
'Authorization': `Bearer ${envconst.BUILDKITE_TOKEN}`,
},
},
)
.then(function (response) {
log.info(response);
})
.catch(function (error) {
log.info(error);
});
let description = `${pipelineName} pipeline not present`;
await repo.statusAsync(
commit,
{
state: 'success',
context: STATUS_CONTEXT,
description,
}
);
await prRemoveLabel(repoName, prNumber, label);
}
async function triggerLabelsOnPipeline(repoName, baseBranch, prNumber, commit, title, user) {
const customLabels = envconst.CUSTOM_PR_LABEL_BUILDS.split(',');
for (let index = 0; index < customLabels.length; ++index) {
let label = customLabels[index];
if (await prHasLabel(repoName, prNumber, label)) {
await triggerPipeline(path.basename(repoName) + '-' + label, repoName,
baseBranch, prNumber, commit, label, title, user);
}
}
}
async function hasNoCILabel(repoName, prNumber, commit) {
const repo = githubClient.repo(repoName);
if (await prHasLabel(repoName, prNumber, NOCI_LABEL)) {
await repo.statusAsync(commit, {
'state': 'failure',
'context': STATUS_CONTEXT,
'description': `Remove ${NOCI_LABEL} label to continue`,
});
return true;
}
return false;
}
/*
async function prSetLabel(repoName, prNumber, labelName) {
const issue = githubClient.issue(repoName, prNumber);
await issue.addLabelsAsync([labelName]);
}
*/
async function prHasLabel(repoName, prNumber, labelName) {
const issue = githubClient.issue(repoName, prNumber);
const [labels] = await issue.labelsAsync();
const labelNames = labels.map((label) => label.name.toLowerCase());
return labelNames.includes(labelName.toLowerCase());
}
async function userInCiWhitelist(repoName, prNumber, user) {
if (envconst.CI_FOR_EVERYBODY) {
log.info(`CI_FOR_EVERYBODY is enabled`);
return true;
}
if (envconst.CI_USER_WHITELIST.split(',').includes(user)) {
log.info(`${user} is in CI_USER_WHITELIST`);
return true;
}
const repo = githubClient.repo(repoName);
try {
if (await repo.collaboratorsAsync(user)) {
log.info(`${user} is a collaborator`);
return true;
}
} catch (err) {
log.info(`${user} is not a collaborator:`, err);
}
try {
// If this PR has previously been accepted by somebody adding the CI_LABEL,
// then accept future updates to the PR. This avoids the need to
// continually re-add the `CI_LABEL` at the expense of trusting the PR author
// more.
const statusesResponse = await repo.statusesAsync(`pull/${prNumber}/head`);
const statuses = statusesResponse[0];
const ciGateSuccess = statuses.some(s => {
return (s.context === STATUS_CONTEXT) && (s.state === 'success');
});
if (ciGateSuccess) {
log.info(`${STATUS_CONTEXT} passed for PR ${prNumber}`);
return true;
}
} catch (err) {
log.warn(`${STATUS_CONTEXT} status check failed for PR ${prNumber}:`, err);
}
return false;
}
async function handleCommitsPushedToPullRequest(repoName, prNumber) {
const repo = githubClient.repo(repoName);
const issue = repo.issue(prNumber);
if (!await prHasLabel(repoName, prNumber, envconst.AUTOMERGE_LABEL)) {
log.debug(`handleCommitsPushedToPullRequest: ${envconst.AUTOMERGE_LABEL} label is not set`);
return;
}
if (await prRemoveLabel(repoName, prNumber, envconst.AUTOMERGE_LABEL)) {
const body = ':scream: New commits were pushed while the automerge label was present.';
log.info(body);
await issue.createCommentAsync({body});
}
}
async function autoMergePullRequest(repoName, prNumber) {
if (!envconst.AUTOMERGE) {
return;
}
const repo = githubClient.repo(repoName);
const pr = repo.pr(prNumber);
const issue = repo.issue(prNumber);
const info = await pr.infoAsync();
assert(typeof info === 'object');
const {state, mergeable, head} = info[0];
if (state !== 'open') {
return;
}
if (!await prHasLabel(repoName, prNumber, envconst.AUTOMERGE_LABEL)) {
log.debug(`autoMergePullRequest: ${envconst.AUTOMERGE_LABEL} label is not set`);
return;
}
if (mergeable === null) {
// https://developer.github.com/v3/pulls/#response-1
log.debug(`mergeable state is not yet known.`);
return;
}
if (mergeable === false) {
if (await prRemoveLabel(repoName, prNumber, envconst.AUTOMERGE_LABEL)) {
const body = ':broken_heart: Unable to automerge due to merge conflict';
log.info(body);
await issue.createCommentAsync({body});
}
return;
}
// Check the CI status of the head SHA
log.debug(`fetching CI status for head SHA ${head.sha}`);
const status = (await repo.combinedStatusAsync(head.sha))[0];
log.info(`CI status: ${status.state} with ${status.statuses.length} statuses`);
log.debug('All statuses:', status.statuses);
switch (status.state) {
case 'success':
{
if (status.statuses.length < 2) {
log.warn(`Refusing to automerge with no evidence of success`);
} else {
log.info(`CI status is success, trying to merge...`);
const mergeResult = await pr.mergeAsync({
sha: head.sha,
commit_message: 'automerge',
merge_method: envconst.GITHUB_MERGE_METHOD,
});
log.info(`successfully merged`, mergeResult);
}
break;
}
case 'failure':
{
if (await prRemoveLabel(repoName, prNumber, envconst.AUTOMERGE_LABEL)) {
const body = ':broken_heart: Unable to automerge due to CI failure';
log.info(body);
await issue.createCommentAsync({body});
}
break;
}
default:
break;
}
}
async function autoMergePullRequests(repoName) {
log.info(`autoMergePullRequests for ${repoName}...`);
const openPrs = await new Promise((resolve, reject) => {
const repo = githubClient.repo(repoName);
repo.prs((err, pulls) => {
if (err) {
reject(err);
return;
}
resolve(
pulls.filter((pull) => {
return pull.state === 'open';
})
);
});
});
for (let openPr of openPrs) {
log.info(`Processing #${openPr.number} ${openPr.title}`);
await autoMergePullRequest(repoName, openPr.number);
}
}
function pipelineInPublicLogWhitelist(pipeline) {
const wl = envconst.BUILDKITE_PIPELINE_PUBLIC_LOG_WHITELIST;
return wl.split(',').includes(pipeline);
}
async function prRemoveLabel(repoName, prNumber, labelName) {
log.info(`Removing label ${labelName} from ${repoName}#${prNumber}`);
const issue = githubClient.issue(repoName, prNumber);
try {
await issue.removeLabelAsync(labelName);
} catch (err) {
log.warn(
`Error removing label ${labelName} from ${repoName}#${prNumber}`,
err.message
);
return false;
}
return true;
}
function isBuildkitePublicLogUrl(url) {
if (typeof(url) !== 'string') {
return false;
}
const orgUrlPrefix = `https://buildkite.com/${envconst.BUILDKITE_ORG_SLUG}/`;
if (!url.startsWith(orgUrlPrefix)) {
return false;
}
const buildInfo = url.slice(orgUrlPrefix.length);
const reMatch = buildInfo.match(/^([a-z-]+)\/builds\/([1-9[0-9]+|latest\/[a-z]+)$/);
if (!reMatch) {
return false;
}
assert(reMatch.index === 0);
assert(reMatch.length === 3);
const pipeline = reMatch[1];
const buildNumber = reMatch[2].startsWith('latest/') ? reMatch[2].substr(7) : Number(reMatch[2]);
return {pipeline, buildNumber};
}
function isBuildkitePublicArtifactUrl(url) {
if (typeof(url) !== 'string') {
return false;
}
const orgUrlPrefix = `https://api.buildkite.com/v2/organizations/${envconst.BUILDKITE_ORG_SLUG}/pipelines/`;
if (!url.startsWith(orgUrlPrefix)) {
return false;
}
const buildInfo = url.slice(orgUrlPrefix.length);
const reMatch = buildInfo.match(
/^([a-z-]+)\/builds\/([1-9[0-9]+)\/jobs\/([-a-z0-9]+)\/artifacts\/([-a-z0-9]+)\/download$/
);
if (!reMatch) {
return false;
}
assert(reMatch.index === 0);
assert(reMatch.length === 5);
const pipeline = reMatch[1];
const buildNumber = Number(reMatch[2]);
const jobId = reMatch[3];
const artifactId = reMatch[4];
return {pipeline, buildNumber, jobId, artifactId};
}
let autoMergePullRequestsBusy = false;
let autoMergePullRequestsPending = false;
async function onGithubStatusUpdate(payload) {
log.info('onGithubStatusUpdate', payload);
// Rewrite buildkite URLs to make the buildkite logs read-accessible to everybody
// (temporary hack until buildkite supports public logs)
const {
context,
description,
name,
sha,
state,
target_url,
} = payload;
const buildInfo = isBuildkitePublicLogUrl(target_url);
if (!buildInfo) {
log.info(`Ignoring non-buildkite URL: ${target_url}`);
} else {
// Overwrite the buildkite status url with the public log equivalent if this
// pipeline is in the whitelist
if (pipelineInPublicLogWhitelist(buildInfo.pipeline)) {
const new_target_url = envconst.PUBLIC_URL_ROOT + '/buildkite_public_log?' + target_url;
log.info('updating to', new_target_url);
const repo = githubClient.repo(name);
await repo.statusAsync(sha, {
state,
context,
description,
target_url: new_target_url,
});
}
}
if (!envconst.AUTOMERGE) {
return;
}
autoMergePullRequestsPending = true;
if (autoMergePullRequestsBusy) {
log.info('autoMergePullRequests busy');
return;
}
autoMergePullRequestsBusy = true;
while (autoMergePullRequestsPending) {
autoMergePullRequestsPending = false;
try {
// Check if any PRs in this repo should be merged, as unfortunately the status
// API provides no link from commit status to the corresponding pull request
await autoMergePullRequests(name);
} catch (err) {
log.error('autoMergePullRequests failed with:', err);
}
}
autoMergePullRequestsBusy = false;
}
async function onGithubPullRequestReview(payload) {
const {action, review, pull_request} = payload;
const prNumber = pull_request.number;
const repoName = pull_request.head.repo.full_name;
log.info(`onGithubPullRequestReview ${action} on ${repoName}#{prNumber}`, review);
await autoMergePullRequest(repoName, prNumber);
}
async function onGithubPullRequest(payload) {
const prNumber = payload.number;
const repoName = payload.repository.full_name;
const user = payload.sender.login;
const {pull_request} = payload;
const title = pull_request.title;
const headSha = pull_request.head.sha;
const baseBranch = pull_request.base.ref;
const merged = pull_request.merged;
const repo = githubClient.repo(repoName);
log.info(payload.action, headSha, prNumber, repoName, baseBranch);
switch (payload.action) {
case 'synchronize':
handleCommitsPushedToPullRequest(repoName, prNumber);
//fall through
case 'opened':
case 'reopened':
{
await prRemoveLabel(repoName, prNumber, CI_LABEL);
if (await userInCiWhitelist(repoName, prNumber, user)) {
if (!await hasNoCILabel(repoName, prNumber, headSha)) {
await triggerPipeline(path.basename(repoName), repoName, baseBranch, prNumber, headSha, CI_LABEL, title, user);
}
await triggerLabelsOnPipeline(repoName, baseBranch, prNumber, headSha, title, user);
} else {
await repo.statusAsync(headSha, {
'state': 'pending',
'context': STATUS_CONTEXT,
'description': needsCiLabelDescription,
});
}
break;
}
case 'labeled':
if (!merged) {
if (await prHasLabel(repoName, prNumber, CI_LABEL)) {
if (!await hasNoCILabel(repoName, prNumber, headSha)) {
await triggerPipeline(path.basename(repoName), repoName, baseBranch, prNumber, headSha, CI_LABEL, title, user);
}
}
await autoMergePullRequest(repoName, prNumber);
}
await triggerLabelsOnPipeline(repoName, prNumber, headSha, title, user);
break;
default:
log.info('Ignored pull request action:', payload.action);
}
}
async function onGithubPush(payload) {
log.info(payload);
await Promise.resolve(); // pacify eslint
}
async function onGithubPing(payload) {
log.info('Github ping:', payload.zen);
await Promise.resolve(); // pacify eslint
}
async function onGithub({id, name, payload}) {
try {
log.debug('Github webhook:', name, id);
log.verbose(payload);
const hooks = {
'ping': onGithubPing,
'pull_request': onGithubPullRequest,
'push': onGithubPush,
'status': onGithubStatusUpdate,
'pull_request_review': onGithubPullRequestReview,
};
if (hooks[name]) {
await hooks[name](payload);
} else {
log.warn('Unhandled Github webhook:', name);
}
} catch (err) {
log.error(err);
}
}
function buildkiteActiveState(state) {
switch (state) {
case 'canceling':
case 'canceled':
case 'failed':
case 'passed':
case 'timed_out':
case 'waiting_failed':
return false;
default:
return true;
}
}
function buildkiteStateStyle(state) {
const colorByState = {
accepted: 'gray',
assigned: 'gray',
blocked: 'gray',
canceled: 'red',
canceling: 'red',
failed: 'red',
passed: 'green',
running: 'orange',
scheduled: 'gray',
skipped: 'magenta',
timed_out: 'red',
waiting: 'gray',
waiting_failed: 'red',
};
assert(
typeof colorByState[state] === 'string',
`Missing state in colorByState: ${state}`
);
return `font-weight: bold; color: ${colorByState[state]};`;
}
function buildkiteHumanTimeInfo(buildData) {
assert(typeof buildData.state === 'string');
let description = '';
switch (buildData.state) {
case 'scheduled':
case 'waiting':
case 'assigned':
case 'accepted':
{
assert(typeof buildData.scheduled_at === 'string');
const scheduledTime = moment.utc(buildData.scheduled_at);
scheduledTime.local();
description = 'waiting since ' + scheduledTime.format('HH:mm:ss on dddd');
break;
}
case 'blocked':
{
description = 'blocked';
break;
}
case 'timed_out':
{
description = 'timed out';
break;
}
case 'waiting_failed':
case 'canceling':
case 'canceled':
{
description = 'aborted';
break;
}
case 'running':
{
assert(typeof buildData.scheduled_at === 'string');
assert(typeof buildData.started_at === 'string');
const startedTime = moment.utc(buildData.started_at);
startedTime.local();
description = 'running since ' + startedTime.format('HH:mm:ss on dddd');
break;
}
case 'failed':
case 'passed':
{
assert(typeof buildData.scheduled_at === 'string');
assert(typeof buildData.started_at === 'string');
assert(typeof buildData.finished_at === 'string');
const scheduledTime = moment.utc(buildData.scheduled_at);
const startedTime = moment.utc(buildData.started_at);
const finishedTime = moment.utc(buildData.finished_at);
scheduledTime.local();
startedTime.local();
finishedTime.local();
const runDuration = moment.duration(finishedTime.diff(startedTime));
const waitDuration = moment.duration(startedTime.diff(scheduledTime));
description = 'ran for ' + runDuration.humanize();
if (waitDuration.minutes() > 0) {
description += ', queued for ' + waitDuration.humanize();
}
break;
}
default:
throw new Error(`Unknown state: ${buildData.state}`);
}
return description;
}
async function onBuildKitePublicLogRequest(req, res) {
res.set('Content-Type', 'text/html');
const queryIndex = req.originalUrl.indexOf('?');
const url = (queryIndex >= 0) ? req.originalUrl.slice(queryIndex + 1) : '';
const buildInfo = isBuildkitePublicLogUrl(url);
if (!buildInfo) {
log.warn(`Invalid public log url:`, url);
res.status(400).send('');
return;
}
if (!pipelineInPublicLogWhitelist(buildInfo.pipeline)) {
log.warn(`Pipeline is not in whitelist:`, buildInfo.pipeline);
res.status(400).send('');
return;
}
const pipeline = await getBuildkitePipeline(buildInfo.pipeline);
pipeline.listBuildsAsync = promisify(pipeline.listBuilds);
// TODO: Add pagination support for older builds
const builds = await pipeline.listBuildsAsync();
const build = builds.find(
(build) => {
if (typeof buildInfo.buildNumber === 'string') {
return build.branch === buildInfo.buildNumber;
} else {
return build.number === buildInfo.buildNumber;
}
}
);
if (!build) {
let msg = `Build ${buildInfo.buildNumber} not found, try <a href="${url}">here</a> instead.`;
// TODO: Add pagination support for older builds
msg += '<p>TODO: Add pagination support for older builds';
log.warn(msg);
res.status(400).send(msg);
return;
}
const {provider} = build.data.pipeline;
let branchHtml = build.branch;
if (provider.id === 'github') {
const {repository} = provider.settings;
const prMatch = build.branch.match(/^pull\/([1-9[0-9]+)\/head$/);
if (prMatch) {
const prNumber = prMatch[1];
branchHtml = `
<a
href="https://github.com/${repository}/pull/${prNumber}"
>#${prNumber}</a>
`;
} else {
branchHtml = `
<a
href="https://github.com/${repository}/tree/${build.branch}"
>${branchHtml}</a>
`;
}
}
const jobs = build.jobs.filter((job) => job.name);
let spinnerHtml = '';
if (buildkiteActiveState(build.state)) {
spinnerHtml = `<img style='vertical-align:middle;' src='spinner.gif'>`;
}
let header = `
<!DOCTYPE html>
<html>
<head>
<title>${build.message}</title>
<link rel="stylesheet" type="text/css" href="/terminal.css" />
</head>
<body>
<h2>${spinnerHtml} ${build.message}</h2>
<b>State:</b>
<span style="${buildkiteStateStyle(build.state)}">
${build.state}
</span>
- <i>${buildkiteHumanTimeInfo(build.data)}</i>
<br/>
<b>Branch:</b> ${branchHtml}</br>
<b>Buildkite Log:</b> <a href="${build.data.web_url}"/>link</a></br>
`;
let body = '';
const footer = '</body></html>';
if (jobs.length > 0) {
const brief = jobs.length === 1;
if (!brief) {
header += `
<b>Steps:</b>
<ol>
`;
body += '</ol>';
}
let jobSpinnerRendered = false;
for (let job of jobs) {
const jobName = job.name.replace(/\[public\]/gi, '').trim();
const jobNameUri = encodeURI(jobName);
job.getLogHtmlAsync = promisify(job.getLogHtml);
const jobHumanTime = buildkiteHumanTimeInfo(job.data);
let jobLog = '<br><i>Build log not available</i><br>';
let artifacts;
if (envconst.BUILDKITE_EXPOSE_ALL_JOB_LOGS || job.name.includes('[public]')) {
const html = await job.getLogHtmlAsync();
if (html) {
jobLog = `<div class="term-container">${html}</div>`;
}
job.listArtifactsAsync = promisify(job.listArtifacts);
const jobArtifacts = await job.listArtifactsAsync();
if (jobArtifacts.length > 0) {
artifacts = jobArtifacts.map(a => {
const url = envconst.PUBLIC_URL_ROOT + '/buildkite_public_artifact?' +
`https://api.buildkite.com/v2/organizations/${envconst.BUILDKITE_ORG_SLUG}/pipelines/${buildInfo.pipeline}/builds/${build.number}/jobs/${a.jobId}/artifacts/${a.id}/download`;
return `<li><a href="${url}" target="_blank">${a.path}</a> (${a.size} bytes)</li>`;
}).join('');
artifacts = `<ul>${artifacts}</ul>`;
}
}
if (!jobSpinnerRendered && buildkiteActiveState(job.data.state)) {
jobSpinnerRendered = true;
jobLog += `
<img style='vertical-align:middle;' src='spinner.gif'>
<div style='color:orange; vertical-align:middle; display:inline;'>
<i>Job active, refresh page manually for updates...</i>
</div>
`;
}
if (!brief) {
header += `
<li>
<span style="${buildkiteStateStyle(job.data.state)}">
${job.data.state}
</span>
- <a href="#${jobNameUri}">${jobName}</a>
- <i>${jobHumanTime}</i>
`;
if (artifacts) {
header += `
<br>
${artifacts}
`;
}
header += `
</li>
`;
body += `
<hr><h3><a name="${jobNameUri}">${jobName}</a></h3>
<b>State:</b>
<span style="${buildkiteStateStyle(job.data.state)}">
${job.data.state}
</span>
- <i>${jobHumanTime}</i>
<br/>
<b>Buildkite Log:</b> <a href="${job.data.web_url}"/>link</a></br>
`;
}
if (artifacts) {
body += `
<b>Artifacts:</b>
${artifacts}
`;
}
body += `
<b>Command:</b> <code>${job.command}</code></br>
${jobLog}
`;
}
}
log.info('Emitting log for', url);
res.send(header + body + footer);
}
async function onBuildKitePublicArtifactRequest(req, res) {
const queryIndex = req.originalUrl.indexOf('?');
const url = (queryIndex >= 0) ? req.originalUrl.slice(queryIndex + 1) : '';
const buildInfo = isBuildkitePublicArtifactUrl(url);
if (!buildInfo) {
log.warn(`Invalid public artifact url:`, url);
res.status(400).send('');
return;
}
if (!pipelineInPublicLogWhitelist(buildInfo.pipeline)) {
log.warn(`Pipeline is not in whitelist:`, buildInfo.pipeline);
res.status(400).send('');
return;
}
const pipeline = await getBuildkitePipeline(buildInfo.pipeline);
pipeline.getBuildAsync = promisify(pipeline.getBuild);
const build = await pipeline.getBuildAsync(buildInfo.buildNumber);
build.getArtifactAsync = promisify(build.getArtifact);
const job = build.jobs.find(j => j.id === buildInfo.jobId);
job.listArtifactsAsync = promisify(job.listArtifacts);
const jobArtifacts = await job.listArtifactsAsync();
const artifact = jobArtifacts.find(a => a.id === buildInfo.artifactId);
artifact.getDownloadUrlAsync = promisify(artifact.getDownloadUrl);
const artifactUrl = await artifact.getDownloadUrlAsync();
log.info('Emitting artifact for', url);
res.writeHead(302, {
'Location': artifactUrl
});
res.end();
}
function main() {
try {
const webhooks = new WebhooksApi({
secret: envconst.GITHUB_WEBHOOK_SECRET,
path: '/github',
});
webhooks.on('*', onGithub);
const app = express();
app.use(webhooks.middleware);
app.use(express.static(path.join(__dirname, 'public_html')));
app.get('/buildkite_public_log', onBuildKitePublicLogRequest);
app.get('/buildkite_public_artifact', onBuildKitePublicArtifactRequest);
app.listen(envconst.PORT, () => log.info(`Listening on ${envconst.PORT}`));
} catch (err) {
log.error(err);
process.exit(1);
}
}
main();