Repository navigation
Expand file tree
/
Copy pathapple-ci-impact.ts
More file actions
88 lines (82 loc) · 3.34 KB
/
Copy pathapple-ci-impact.ts
File metadata and controls
88 lines (82 loc) · 3.34 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
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
import { selectChecks } from './check-affected/model.ts';
type Selection = { run: boolean; reason: string };
function checkOwnership(
changedPaths: readonly string[] | null,
): { early: Selection; plan?: never } | { early?: never; plan: ReturnType<typeof selectChecks> } {
if (changedPaths === null) {
return { early: { run: true, reason: 'the PR change set could not be established' } };
}
if (changedPaths.length === 0) {
return { early: { run: true, reason: 'the PR diff is empty' } };
}
const plan = selectChecks({ changedFiles: changedPaths });
if (plan.failOpen) {
return {
early: {
run: true,
reason: `affected-check ownership is uncertain: ${plan.failOpenReasons[0]?.path}`,
},
};
}
return { plan };
}
export function selectIosXctests(
eventName: string,
changedPaths: readonly string[] | null,
): Selection {
if (eventName !== 'pull_request')
return { run: true, reason: 'main and manual runs exercise XCTest' };
const checked = checkOwnership(changedPaths);
if (checked.early) return checked.early;
const input = checked.plan.reasons.find((entry) => entry.check === 'swift-runner-ios');
return input
? { run: true, reason: `XCTest input changed: ${input.path}` }
: { run: false, reason: 'the affected-check model selected no iOS runner build' };
}
export function selectAppleBridgeProof(changedPaths: readonly string[] | null): Selection {
const checked = checkOwnership(changedPaths);
if (checked.early) return checked.early;
const input = changedPaths.find(
(file) => file.startsWith('apple/') || file.startsWith('packages/platform-apple/src/'),
);
return input
? { run: true, reason: `Apple implementation changed: ${input}` }
: { run: false, reason: 'the PR changed no Apple implementation or gate tooling' };
}
function changedPathsFromGit(baseSha: string): string[] | null {
if (!/^[a-f0-9]{40}$/.test(baseSha)) return null;
const result = spawnSync('git', ['diff', '--name-only', '-z', `${baseSha}...HEAD`], {
encoding: 'utf8',
});
if (result.error || result.status !== 0 || result.stdout === null) return null;
return result.stdout.split('\0').filter(Boolean);
}
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
const target = process.argv[2];
if (target !== 'xctest' && target !== 'bridge') {
process.stderr.write('Expected xctest or bridge target\n');
process.exitCode = 2;
} else {
const eventName = process.env.GITHUB_EVENT_NAME ?? '';
const changedPaths =
eventName === 'pull_request' ? changedPathsFromGit(process.env.BASE_SHA ?? '') : [];
const selection =
target === 'xctest'
? selectIosXctests(eventName, changedPaths)
: selectAppleBridgeProof(changedPaths);
const label = target === 'xctest' ? 'iOS XCTest' : 'Apple bridge proof';
process.stdout.write(`${label}: ${selection.run ? 'run' : 'skip'}; ${selection.reason}\n`);
if (process.env.GITHUB_OUTPUT) {
fs.appendFileSync(process.env.GITHUB_OUTPUT, `run=${selection.run}\n`);
}
if (process.env.GITHUB_STEP_SUMMARY) {
fs.appendFileSync(
process.env.GITHUB_STEP_SUMMARY,
`### ${label} selection\n\n${selection.run ? 'Run' : 'Skip'}: ${selection.reason}.\n`,
);
}
}
}