forked from elastic/elastic-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_exit.ts
126 lines (116 loc) · 3.29 KB
/
pre_exit.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { yarnInstall } from './../utils/exec';
import { bkEnv, buildkiteGQLQuery, codeCheckIsCompleted, getJobMetadata, updateCheckStatus } from '../utils';
const skipChecks = new Set(['playwright']);
void (async function () {
const { checkId, jobId, jobUrl } = bkEnv;
if (checkId && jobId && !skipChecks.has(checkId)) {
await yarnInstall();
const jobStatus = await getJobStatus(jobId);
if (jobStatus) {
if (jobStatus.state === 'CANCELING') {
const user = getCancelledBy(jobStatus.events ?? []);
await updateCheckStatus(
{
status: 'completed',
conclusion: 'cancelled',
details_url: jobUrl,
},
checkId,
user && `Cancelled by ${user}`,
);
} else {
const isFailedJob = (await getJobMetadata('failed')) === 'true';
if (isFailedJob || !(await codeCheckIsCompleted())) {
await updateCheckStatus(
{
status: 'completed',
conclusion: isFailedJob ? 'failure' : 'success',
details_url: jobUrl,
},
checkId,
);
}
}
}
}
})();
export interface Response {
data: {
job: {
__typename: string;
exitStatus: string;
label: string;
state: string;
passed: boolean;
canceledAt: string;
events: {
edges?:
| {
node: {
type: string;
timestamp: string;
actor: {
type: string;
node: {
__typename: string;
name?: string | null;
bot?: boolean | null;
};
};
};
}[]
| null;
};
};
};
}
function getCancelledBy(jobEvents: Response['data']['job']['events']) {
const lastEvent = jobEvents.edges?.[0].node ?? null;
if (lastEvent?.type !== 'CANCELATION') return null;
return lastEvent.actor.type !== 'USER' ? null : lastEvent.actor.node.name ?? null;
}
async function getJobStatus(id: string) {
try {
const { data } = await buildkiteGQLQuery<Response>(`query getJobStatus {
job(uuid: "${id}") {
__typename
... on JobTypeCommand {
exitStatus
label
state
passed
canceledAt
events(last: 1) {
edges {
node {
type
timestamp
actor {
type
node {
__typename
... on User {
name
bot
}
}
}
}
}
}
}
}
}`);
return data.job.__typename === 'JobTypeCommand' ? data.job : null;
} catch (error) {
console.error(`Failed to get buildkite job status for jobId=${id}`);
throw error;
}
}