-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathcheck_runs_helpers.cjs
More file actions
83 lines (75 loc) · 2.3 KB
/
check_runs_helpers.cjs
File metadata and controls
83 lines (75 loc) · 2.3 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
// @ts-check
/**
* Returns true for check runs that represent deployment environment gates rather
* than CI checks.
* @param {any} run
* @returns {boolean}
*/
function isDeploymentCheck(run) {
return run?.app?.slug === "github-deployments";
}
/**
* Select latest check run per name and apply standard filtering.
* @param {any[]} checkRuns
* @param {{
* includeList?: string[]|null,
* excludeList?: string[]|null,
* excludedCheckRunIds?: Set<number>,
* }} [options]
* @returns {{relevant: any[], deploymentCheckCount: number, currentRunFilterCount: number}}
*/
function selectLatestRelevantChecks(checkRuns, options = {}) {
const includeList = options.includeList || null;
const excludeList = options.excludeList || null;
const excludedCheckRunIds = options.excludedCheckRunIds || new Set();
/** @type {Map<string, any>} */
const latestByName = new Map();
let deploymentCheckCount = 0;
let currentRunFilterCount = 0;
for (const run of checkRuns) {
if (isDeploymentCheck(run)) {
deploymentCheckCount++;
continue;
}
if (excludedCheckRunIds.has(run.id)) {
currentRunFilterCount++;
continue;
}
const existing = latestByName.get(run.name);
if (!existing || new Date(run.started_at ?? 0) > new Date(existing.started_at ?? 0)) {
latestByName.set(run.name, run);
}
}
const relevant = [];
for (const [name, run] of latestByName) {
if (includeList && includeList.length > 0 && !includeList.includes(name)) {
continue;
}
if (excludeList && excludeList.length > 0 && excludeList.includes(name)) {
continue;
}
relevant.push(run);
}
return { relevant, deploymentCheckCount, currentRunFilterCount };
}
/**
* Computes failing checks with shared semantics.
* @param {any[]} checkRuns
* @param {{allowPending?: boolean}} [options]
* @returns {any[]}
*/
function getFailingChecks(checkRuns, options = {}) {
const allowPending = options.allowPending === true;
const failedConclusions = new Set(["failure", "cancelled", "timed_out"]);
return checkRuns.filter(run => {
if (run.status === "completed") {
return run.conclusion != null && failedConclusions.has(run.conclusion);
}
return !allowPending;
});
}
module.exports = {
isDeploymentCheck,
selectLatestRelevantChecks,
getFailingChecks,
};