-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubActionsConfigParser.ts
159 lines (143 loc) · 5.44 KB
/
githubActionsConfigParser.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
148
149
150
151
152
153
154
155
156
157
158
159
import { ISpecificCIResolverResponse } from '../../../interfaces/ISpecificCIResolver';
import { injectable } from 'inversify';
import { ILoggerFactory } from '../../../../../utils/logger';
import { ILogger } from '../../../../../utils/logger/interfaces/ILogger';
import { INode, keySorter, NodeSorter, objectIterator } from '../../../../../utils/objectIterator/objectIterator';
import { is, isArrayOfObjects, isString } from 'ts-type-guards';
import { INvmHandler } from '../../../interfaces/INvmHandler';
import { isStringOrNumber } from '../../../../../utils/types/typeGuards';
import { StringOrNumber } from '../../../../../utils/types/types';
const envRegex = /\${{\s*(?:env|matrix|secrets)\.([^\s]+)\s*}}/i;
const nodeEnvReplaceRegex = /\${{\s*(?:env|matrix|secrets)\.([^\s]+)\s*}}/i;
type Matrix = Record<string, string[]>;
const toString = (value: StringOrNumber): string => String(value);
const nodeVersionCommandParser = (matrix: Matrix) => (version: StringOrNumber): string[] => {
version = String(version);
const matrixKey = envRegex.exec(version)?.[1];
if (matrixKey) {
const matrixVersions = matrix[matrixKey.trim()];
return matrixVersions || [];
} else {
return [version];
}
};
const sorter: NodeSorter = (a: INode, b: INode) => {
if (a.parent.isNonRootNode && a.parent.key === `matrix`) {
if (a.key === `include`) {
return 1;
} else if (b.key === `include`) {
return -1;
}
}
return keySorter(a, b);
};
const isStringOrNumberArray = (x: unknown): x is StringOrNumber[] => {
return is(Array)(x) && x.every(isStringOrNumber);
};
export interface IGithubActionsConfigParserOptions {
config: Record<string, unknown>;
}
const isEnvNode = (value: unknown, node: INode): value is StringOrNumber => {
const { parent, isLeaf } = node;
return isLeaf && parent.isNonRootNode && parent.key === `env` && isStringOrNumber(value);
};
const isMatrixNode = (value: unknown, node: INode): value is StringOrNumber[] => {
const { parent } = node;
return (
parent.isNonRootNode &&
parent.key === `matrix` &&
parent.parent.isNonRootNode &&
parent.parent.key === `strategy` &&
isStringOrNumberArray(value)
);
};
const isIncludeMatrixNode = (value: unknown, node: INode): value is object[] => {
const { parent, key } = node;
return (
parent.isNonRootNode &&
parent.key === `matrix` &&
parent.parent.isNonRootNode &&
parent.parent.key === `strategy` &&
key === `include` &&
isArrayOfObjects(value)
);
};
const isNodeVersionNode = (value: unknown, node: INode): value is StringOrNumber => {
const { parent, key, isLeaf } = node;
return isLeaf && key === `node-version` && parent.isNonRootNode && parent.key === `with` && isStringOrNumber(value);
};
const isRunNode = (value: unknown, node: INode): value is string => {
const { key, isLeaf } = node;
return isLeaf && key === `run` && isString(value);
};
@injectable()
export class GithubActionsConfigParser {
private readonly logger: ILogger;
constructor(private readonly nvmHandler: INvmHandler, loggerFactory: ILoggerFactory) {
this.logger = loggerFactory.getLogger(`Github Actions Config Parser`);
}
public async parse({ config }: IGithubActionsConfigParserOptions): Promise<ISpecificCIResolverResponse> {
this.logger.debug(`Parsing Github Actions configuration`);
const rawVersions: StringOrNumber[] = [];
const matrix: Matrix = {};
const nvmCommands: string[] = [];
const it = objectIterator(config, sorter);
let iteration = it.next();
while (!iteration.done) {
const { value: node } = iteration;
const { value, key } = node;
let skip = false;
if (isEnvNode(value, node)) {
if (matrix[key]) {
matrix[key].push(String(value));
} else {
matrix[key] = [String(value)];
}
skip = true;
} else if (isMatrixNode(value, node)) {
if (matrix[key]) {
matrix[key].push(...value.map(toString));
} else {
matrix[key] = value.map(toString);
}
skip = true;
} else if (isIncludeMatrixNode(value, node)) {
for (const element of value) {
for (const [currentKey, currentValue] of Object.entries(element)) {
if (matrix[currentKey]) {
matrix[currentKey].push(String(currentValue));
} else {
matrix[currentKey] = [String(currentValue)];
}
}
}
skip = true;
} else if (isNodeVersionNode(value, node)) {
rawVersions.push(value);
skip = true;
} else if (isRunNode(value, node)) {
const command = value.replace(nodeEnvReplaceRegex, `\${$1}`);
if (this.nvmHandler.isNvmCommand(command)) {
this.logger.debug(`Found nvm command - ${command}`);
nvmCommands.push(command);
}
skip = true;
}
iteration = it.next(skip);
}
const versions = new Set(rawVersions.flatMap(nodeVersionCommandParser(matrix)));
nvmCommands.forEach((command) => {
this.nodeVersionCommandParser(command, matrix, versions);
});
this.logger.debug(`Finished parsing AppVeyor configuration, Found ${versions.size} version(s)`);
return {
nodeVersions: versions,
};
}
private nodeVersionCommandParser(command: string, matrix: Matrix, versions: Set<string>): void {
const foundVersions = this.nvmHandler.getNvmVersionsFromMatrix(command, matrix);
for (const version of foundVersions) {
versions.add(version);
}
}
}