/
allowlist.ts
50 lines (46 loc) · 1.57 KB
/
allowlist.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
import type { GitHubAdvisoryId } from "audit-types";
import { isGitHubAdvisoryId } from "./common";
import type { AuditCiPreprocessedConfig } from "./config";
class Allowlist {
modules: string[];
advisories: GitHubAdvisoryId[];
paths: string[];
/**
* @param input the allowlisted module names, advisories, and module paths
*/
constructor(input?: string[]) {
this.modules = [];
this.advisories = [];
this.paths = [];
if (!input) {
return;
}
for (const allowlist of input) {
if (typeof allowlist === "number") {
throw new TypeError(
"Unsupported number as allowlist. Perform codemod to update config to use GitHub advisory as identifiers: https://github.com/quinnturner/audit-ci-codemod with `npx @quinnturner/audit-ci-codemod`. See also: https://github.com/IBM/audit-ci/pull/217"
);
}
if (allowlist.includes(">") || allowlist.includes("|")) {
this.paths.push(allowlist);
} else if (isGitHubAdvisoryId(allowlist)) {
this.advisories.push(allowlist);
} else {
this.modules.push(allowlist);
}
}
}
static mapConfigToAllowlist(
config: Pick<AuditCiPreprocessedConfig, "allowlist">
) {
const { allowlist } = config;
// It's possible someone duplicated the inputs.
// The solution is to merge into one array, change to set, and back to array.
// This will remove duplicates.
const set = new Set(allowlist || []);
const input = [...set];
const allowlistObject = new Allowlist(input);
return allowlistObject;
}
}
export default Allowlist;