-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest-builds.ts
147 lines (128 loc) · 3.68 KB
/
latest-builds.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { setBaseURL } from './utils/baseURL.ts';
import { fetchText } from './utils/fetchText.ts';
import color from './utils/color.ts';
import { formatDuration } from './utils/formatDuration.ts';
const job = Deno.args[0] || 'pull-ci-openshift-console-master-e2e-gcp-console';
console.log('Job:', color.blue(job));
setBaseURL(
'https://prow.ci.openshift.org/job-history/gs/origin-ci-test/pr-logs/directory/',
);
type BuildResult = 'PENDING' | 'SUCCESS' | 'FAILURE' | 'ABORTED';
interface Build {
ID: string;
Started: string;
Duration: number;
Result: BuildResult;
Refs: {
org: string;
repo: string;
repo_link: string;
base_ref: string;
base_sha: string;
base_link: string;
pulls: {
number: number;
author: string;
sha: string;
title: string;
link: string;
commit_link: string;
author_link: string;
}[];
};
}
interface PaginatedBuilds {
builds: Build[];
olderBuildId: string | undefined;
newerBuildId: string | undefined;
}
const extractPaginatedBuilds = async (
job: string,
buildId?: string,
): Promise<PaginatedBuilds> => {
const fullHTML: string = await fetchText(
buildId ? `${job}?buildId=${buildId}` : job,
);
const allBuildsStart = ' var allBuilds = ';
const allBuildsEnd = ';\n';
const allBuildsIndex = fullHTML.indexOf(allBuildsStart);
if (allBuildsIndex === -1) {
console.log('Full prow HTML:\n' + fullHTML);
throw new Error('Could not find allBuilds in prow HTML!');
}
const allBuilds = fullHTML.substring(
allBuildsIndex + allBuildsStart.length,
fullHTML.indexOf(allBuildsEnd, allBuildsIndex),
);
const builds = JSON.parse(allBuilds);
const olderBuildId = fullHTML.match(/buildId=([0-9]*)"><- Older Runs/)
?.[1];
const newerBuildId = fullHTML.match(/buildId=([0-9]*)">Newer Runs ->/)
?.[1];
return {
builds,
olderBuildId,
newerBuildId,
};
};
let buildId = '';
const builds: Build[] = [];
for (let i = 0; i < 10; i++) {
console.log(`Fetching page ${i + 1} with buildId`, color.blue(buildId));
const paginatedBuilds: PaginatedBuilds = await extractPaginatedBuilds(
job,
buildId,
);
builds.push(...paginatedBuilds.builds);
if (!paginatedBuilds.olderBuildId) {
break;
}
buildId = paginatedBuilds.olderBuildId;
}
const formatResult = (result: BuildResult) => {
switch (result) {
case 'PENDING':
return color.yellow(result);
case 'SUCCESS':
return color.green(result);
case 'FAILURE':
return color.red(result);
case 'ABORTED':
return color.yellow(result);
default:
return result;
}
};
builds.forEach((build) => {
console.log(
build.ID,
new Date(build.Started),
formatDuration(build.Duration / 1000000000),
formatResult(build.Result),
build.Refs.pulls.map((pull) => `#${pull.number}`).join(' '),
);
});
const success = builds.filter((build) => build.Result === 'SUCCESS').length;
const failure = builds.filter((build) => build.Result === 'FAILURE').length;
const pending = builds.filter((build) => build.Result === 'PENDING').length;
const aborted = builds.filter((build) => build.Result === 'ABORTED').length;
const successRate = Math.round(success / (success + failure) * 100) / 100;
const failureRate = Math.round(failure / (success + failure) * 100) / 100;
console.log('');
console.log(
color.green(
`Success rate: ${successRate * 100} % (${success} / ${success + failure})`,
),
);
console.log(
color.red(
`Failure rate: ${failureRate * 100} % (${failure} / ${success + failure})`,
),
);
if (aborted > 0) {
console.log(color.yellow(`Aborted: ${aborted}`));
}
if (pending > 0) {
console.log(color.yellow(`Pending: ${pending}`));
}
console.log('');