-
Notifications
You must be signed in to change notification settings - Fork 626
Expand file tree
/
Copy pathdetect-impacted-packages.mjs
More file actions
294 lines (251 loc) · 7.6 KB
/
detect-impacted-packages.mjs
File metadata and controls
294 lines (251 loc) · 7.6 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env node
// Determines which workspace packages should be tested by diffing
// with the default branch and expanding the dependency graph so
// that downstream dependents are also exercised.
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, '..', '..', '..');
const packagesRoot = path.join(repoRoot, 'packages');
const defaultBranch = process.env.DEFAULT_BRANCH || 'main';
const forceAll = process.env.FORCE_ALL === 'true';
const baseShaEnv = (process.env.BASE_SHA || '').trim();
const GLOBAL_IMPACT_PATHS = new Set([
'package.json',
// Keep it, although it's not under version control - be explicit.
'pnpm-lock.yaml',
'pnpm-workspace.yaml'
]);
const GLOBAL_IMPACT_PREFIXES = [];
const IGNORED_PACKAGE_DIRS = new Set([
'vue-material-design-icons'
]);
/**
* @typedef {Object} MatrixEntry
* @property {string} package - Workspace package name (scope included).
* @property {string} directory - Workspace-relative directory for the package.
*/
/**
* @typedef {Object} ImpactResult
* @property {{ include: MatrixEntry[] }} matrix - JSON payload consumed by GitHub matrix jobs.
* @property {boolean} hasPackages - True when at least one package needs to run.
*/
async function main() {
const packages = await loadPackages();
const packageNames = [...packages.keys()];
const packagesWithTests = packageNames.filter((name) => packages.get(name)?.hasTestScript);
const dirLookup = buildDirLookup(packages);
const baseSha = baseShaEnv || resolveBaseSha();
const hasForceCI = checkForceCI(baseSha);
const shouldForceAll = forceAll || !baseSha || hasForceCI;
const changedFiles = shouldForceAll
? []
: getChangedFiles(baseSha);
const { changedPackages, hasGlobalImpact } = determineChangedPackages(
changedFiles,
dirLookup
);
const impactedPackages = shouldForceAll || hasGlobalImpact
? packagesWithTests
: expandImpactedPackages(changedPackages, packages);
const uniqueImpacted = [...new Set(impactedPackages)].filter((name) => {
const pkg = packages.get(name);
return pkg && pkg.hasTestScript;
});
uniqueImpacted.sort();
const matrix = {
include: uniqueImpacted.map((name) => ({
package: name,
directory: packages.get(name).relativeDir,
requiresMongo: packages.get(name).requiresMongo !== false,
requiresRedis: packages.get(name).requiresRedis === true,
mongodbOnly: packages.get(name).mongodbOnly === true
}))
};
/** @type {ImpactResult} */
const result = {
matrix,
hasPackages: matrix.include.length > 0
};
process.stdout.write(JSON.stringify(result));
}
async function loadPackages() {
const map = new Map();
let entries;
try {
entries = await fs.readdir(packagesRoot, { withFileTypes: true });
} catch (error) {
throw new Error(`Unable to read packages directory: ${error.message}`);
}
await Promise.all(entries
.filter((entry) => entry.isDirectory())
.map(async (entry) => {
if (IGNORED_PACKAGE_DIRS.has(entry.name)) {
return;
}
const packageDir = path.join(packagesRoot, entry.name);
const manifestPath = path.join(packageDir, 'package.json');
let manifest;
try {
const raw = await fs.readFile(manifestPath, 'utf8');
manifest = JSON.parse(raw);
} catch (error) {
console.warn(`Skipping ${entry.name}: ${error.message}`);
return;
}
if (!manifest.name) {
console.warn(`Skipping ${entry.name}: missing package name`);
return;
}
const scripts = manifest.scripts || {};
const hasTestScript = Boolean(scripts.test);
const dependencies = new Set([
...Object.keys(manifest.dependencies || {}),
...Object.keys(manifest.devDependencies || {}),
...Object.keys(manifest.peerDependencies || {}),
...Object.keys(manifest.optionalDependencies || {})
]);
const testConfig = manifest.apostropheTestConfig || {};
const requiresMongo = testConfig.requiresMongo !== false;
const requiresRedis = testConfig.requiresRedis === true;
const mongodbOnly = testConfig.mongodbOnly === true;
map.set(manifest.name, {
name: manifest.name,
relativeDir: path.posix.join('packages', entry.name),
dependencies,
hasTestScript,
requiresMongo,
requiresRedis,
mongodbOnly
});
}));
return map;
}
function buildDirLookup(packages) {
const dirMap = new Map();
for (const pkg of packages.values()) {
dirMap.set(pkg.relativeDir, pkg.name);
}
return dirMap;
}
function resolveBaseSha() {
try {
const mergeBase = execSync(`git merge-base HEAD origin/${defaultBranch}`, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore']
}).trim();
return mergeBase || '';
} catch (error) {
return '';
}
}
function checkForceCI(baseSha) {
if (!baseSha) {
return false;
}
try {
const output = execSync(`git log ${baseSha}..HEAD --pretty=format:%s`, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore']
}).trim();
if (!output) {
return false;
}
const commitMessages = output.split('\n');
return commitMessages.some((msg) => /\[force ci\]/i.test(msg));
} catch (error) {
return false;
}
}
function getChangedFiles(baseSha) {
if (!baseSha) {
return [];
}
try {
const output = execSync(`git diff --name-only ${baseSha} HEAD`, {
encoding: 'utf8'
}).trim();
if (!output) {
return [];
}
return output.split('\n').map((line) => line.trim()).filter(Boolean);
} catch (error) {
return [];
}
}
function determineChangedPackages(changedFiles, dirLookup) {
const changedPackages = new Set();
let hasGlobalImpact = false;
for (const file of changedFiles) {
if (file.startsWith('packages/')) {
const [, packageDir] = file.split('/');
if (!packageDir) {
continue;
}
const dirKey = `packages/${packageDir}`;
const pkgName = dirLookup.get(dirKey);
if (pkgName) {
changedPackages.add(pkgName);
}
continue;
}
if (GLOBAL_IMPACT_PATHS.has(file)) {
hasGlobalImpact = true;
break;
}
if (GLOBAL_IMPACT_PREFIXES.some((prefix) => file.startsWith(prefix))) {
hasGlobalImpact = true;
break;
}
}
return { changedPackages, hasGlobalImpact };
}
function expandImpactedPackages(changedPackages, packages) {
if (!changedPackages.size) {
return [];
}
const dependentsMap = buildDependentsMap(packages);
const queue = [...changedPackages];
const impacted = new Set();
while (queue.length) {
const current = queue.shift();
if (!current || impacted.has(current)) {
continue;
}
impacted.add(current);
const dependents = dependentsMap.get(current);
if (dependents) {
dependents.forEach((dependent) => {
if (!impacted.has(dependent)) {
queue.push(dependent);
}
});
}
}
return [...impacted];
}
function buildDependentsMap(packages) {
const map = new Map();
const packageNames = new Set(packages.keys());
for (const pkg of packages.values()) {
for (const depName of pkg.dependencies) {
if (!packageNames.has(depName)) {
continue;
}
if (!map.has(depName)) {
map.set(depName, new Set());
}
map.get(depName).add(pkg.name);
}
}
return map;
}
try {
await main();
} catch (error) {
console.error(error);
process.exitCode = 1;
}