-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
223 lines (162 loc) · 6.81 KB
/
index.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
const _ = require('lodash');
const Nv = require('@pkgjs/nv');
const Semver = require('semver');
const internals = {};
internals.parseActionsSetupNode = function * (workflow, file) {
for (const job of Object.values(workflow.jobs)) {
if (!job.steps) {
continue;
}
const nodeSteps = job.steps.filter(({ uses }) => uses && uses.startsWith('actions/setup-node'));
for (const step of nodeSteps) {
const nodeVersion = step.with && step.with['node-version'];
if (!nodeVersion) {
// Docs say: "The node-version input is optional. If not supplied, the node version that is PATH will be used."
// Therefore we cannot reliably detect a specific version, but we do want to let the user know
yield 'not-set';
continue;
}
const matrixMatch = nodeVersion.match(/^\${{\s+matrix.(?<matrixVarName>.*)\s+}}$/);
if (matrixMatch) {
const matrix = job.strategy.matrix[matrixMatch.groups.matrixVarName];
yield * matrix;
continue;
}
const envMatch = nodeVersion.match(/^\${{\s+env.(?<envVarName>.*)\s+}}$/);
if (envMatch) {
const env = {
...workflow.env,
...step.env
};
const envValue = env[envMatch.groups.envVarName];
if (!envValue) {
yield 'not-set';
continue;
}
yield envValue;
continue;
}
yield nodeVersion;
}
}
};
internals.resolveLjharbPreset = function * ({ preset }) {
// @todo: with has more options - resolve to precise versions here and yield the full list
// @todo: return preset as well as resolved version
if (preset === '0.x') {
yield * ['0.8', '0.10', '0.12'];
return;
}
if (preset === 'iojs') {
yield * ['1', '2', '3'];
return;
}
if (!Semver.validRange(preset)) {
yield preset;
return;
}
yield * internals.latestNodeVersions.filter(({ resolved }) => Semver.satisfies(resolved, preset)).map(({ major }) => major);
};
internals.parseLjharbActions = function * (workflow, file) {
for (const job of Object.values(workflow.jobs)) {
if (!job.steps) {
continue;
}
const nodeSteps = job.steps.filter(({ uses }) => {
if (!uses) {
return false;
}
return uses.startsWith('ljharb/actions/node/run') || uses.startsWith('ljharb/actions/node/install');
});
for (const step of nodeSteps) {
const nodeVersion = step.with && step.with['node-version'];
if (!nodeVersion) {
yield 'lts/*'; // @todo: find ref which tells us that this is so
continue;
}
const matrixMatch = nodeVersion.match(/^\${{\s+matrix.(?<matrixVarName>.*)\s+}}$/);
if (matrixMatch) {
let needs = job.strategy.matrix;
if (typeof job.strategy.matrix !== 'string') {
const matrix = job.strategy.matrix[matrixMatch.groups.matrixVarName];
if (!matrix) {
throw new Error(`Unable to find matrix variable '${matrixMatch.groups.matrixVarName}' in the matrix in ${file}`);
}
if (typeof matrix !== 'string') {
// @todo find an example
yield * matrix;
continue;
}
// example: eslint-plugin-react
needs = matrix;
}
const fromJsonMatch = needs.match(/^\${{\s+fromJson\(needs\.(?<needJobName>.*)\.outputs\.(?<needOutputName>.*)\)\s+}}$/);
if (fromJsonMatch) {
const { needJobName, needOutputName } = fromJsonMatch.groups;
const needJob = workflow.jobs[needJobName];
const needOutput = needJob.outputs[needOutputName];
const stepMatch = needOutput.match(/^\${{\s+steps\.(?<needStepName>.*)\.outputs\.(?<needStepOutputName>.*)\s+}}$/);
if (!stepMatch) {
throw new Error(`Unable to parse need output: ${needOutput} in ${file}`);
}
const { needStepName/*, needStepOutputName*/ } = stepMatch.groups;
const needStep = needJob.steps.find(({ id }) => id === needStepName);
if (!needStep || !needStep.uses.startsWith('ljharb/actions/node/matrix')) {
throw new Error(`Unrecognized action in ${needOutput} in ${file}`);
}
yield * internals.resolveLjharbPreset(needStep.with);
continue;
}
throw new Error(`Unable to parse the job matrix: ${job.strategy.matrix} in ${file}`);
}
yield nodeVersion;
}
}
};
exports.detect = async (meta) => {
if (!internals.latestNodeVersions) {
// @todo: unhardcode
const latest = [];
for (const v of ['4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16']) {
const resolved = await Nv(v);
latest.push({ major: v, resolved: resolved[resolved.length - 1].version });
}
internals.latestNodeVersions = latest;
}
const files = await meta.loadFolder('.github/workflows');
const rawSet = new Set();
const byFileSets = {};
if (!files.length) {
// explicitly return no `githubActions` - this is different to finding actions and detecting no Node.js versions
return;
}
for (const file of files) {
if (!file.endsWith('.yaml') && !file.endsWith('.yml')) {
continue;
}
const workflow = await meta.loadFile(`.github/workflows/${file}`, { yaml: true });
byFileSets[file] = byFileSets[file] || new Set();
for (const version of internals.parseActionsSetupNode(workflow, file)) {
rawSet.add(version);
byFileSets[file].add(version);
}
for (const version of internals.parseLjharbActions(workflow, file)) {
rawSet.add(version);
byFileSets[file].add(version);
}
}
const raw = [...rawSet];
const byFile = _.mapValues(byFileSets, (set) => [...set]);
const resolved = {};
for (const version of raw) {
const nv = await Nv(version);
if (!nv.length) {
resolved[version] = false;
}
else {
resolved[version] = nv[nv.length - 1].version;
}
}
return { githubActions: { byFile, raw, resolved } };
};