-
Notifications
You must be signed in to change notification settings - Fork 534
/
iac-test-result.ts
95 lines (85 loc) · 2.6 KB
/
iac-test-result.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
import { BasicResultData, SEVERITY, TestDepGraphMeta } from './legacy';
export interface AnnotatedIacIssue {
id: string;
title: string;
description: string;
severity: SEVERITY;
isIgnored: boolean;
cloudConfigPath: string[];
type: string;
subType: string;
path: string[];
// Legacy fields from Registry, unused.
name?: string;
from?: string[];
lineNumber?: number;
iacDescription: {
issue: string;
impact: string;
resolve: string;
};
}
type FILTERED_OUT_FIELDS = 'cloudConfigPath' | 'name' | 'from';
export interface IacTestResponse extends BasicResultData {
path: string;
targetFile: string;
projectName: string;
displayTargetFile: string; // used for display only
foundProjectCount: number;
meta: TestDepGraphMeta;
result: {
cloudConfigResults: AnnotatedIacIssue[];
projectType: string;
};
}
const IAC_ISSUES_KEY = 'infrastructureAsCodeIssues';
export function mapIacTestResult(
iacTest: IacTestResponse,
): MappedIacTestResponse | IacTestError {
if (iacTest instanceof Error) {
return {
ok: false,
error: iacTest.message,
path: (iacTest as any).path,
};
}
const infrastructureAsCodeIssues =
iacTest?.result?.cloudConfigResults.map(mapIacIssue) || [];
const {
result: { projectType },
...filteredIacTest
} = iacTest;
return {
...filteredIacTest,
projectType,
ok: infrastructureAsCodeIssues.length === 0,
[IAC_ISSUES_KEY]: infrastructureAsCodeIssues,
};
}
/**
* The following types represent manipulations to the data structure returned from Registry's `test-iac`.
* These manipulations are being done prior to outputing as JSON, for renaming fields only.
* The types above, IacTestResult & AnnotatedIacIssue, represent how the response from Registry actually is.
* These were introduced in order to prevent cascading complex changes caused by changing Registry's `test-iac` response.
*/
export interface IacTestError {
ok: boolean;
error: string;
path: string;
}
export interface MappedIacTestResponse extends Omit<IacTestResponse, 'result'> {
[IAC_ISSUES_KEY]: MappedAnnotatedIacIssue[];
projectType: string;
}
export interface MappedAnnotatedIacIssue
extends Omit<AnnotatedIacIssue, FILTERED_OUT_FIELDS> {
path: string[];
}
export function mapIacIssue(
iacIssue: AnnotatedIacIssue,
): MappedAnnotatedIacIssue {
// filters out & renames properties we're getting from registry and don't need for the JSON output.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { cloudConfigPath: path, name, from, ...mappedIacIssue } = iacIssue;
return { ...mappedIacIssue, path };
}