-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff-changes.ts
More file actions
169 lines (147 loc) · 7.34 KB
/
diff-changes.ts
File metadata and controls
169 lines (147 loc) · 7.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
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
import { isCosmeticOnlyJsonSchemaChange } from './diff-json-schema.js';
import type { ActorConfig, Commit } from './types.js';
interface ShouldBuildAndTestOptions {
filepathsChanged: string[];
actorConfigs: ActorConfig[];
isLatest?: boolean;
commits: Commit[];
}
export const maybeParseActorFolder = (
lowercaseFilePath: string,
): { isActorFolder: true; actorName: string } | { isActorFolder: false } => {
const match = lowercaseFilePath.match(/^(?:standalone-)?actors\/([^/]+)\/.+/);
if (match) {
// Some usernames weirdly use underscores, e.g. google_maps_email_extractor_standby-contact-details-scraper so we only need replace the last one
return { isActorFolder: true, actorName: match[1].replace(/_(?=[^_]*$)/, '/') };
}
return { isActorFolder: false };
};
/**
* Also works for folders
*/
const isIgnoredTopLevelFile = (lowercaseFilePath: string) => {
// On top level, we should only have dev-only readme and .actor/ is just for apify push CLI (real Actor configs are in /actors)
const IGNORED_TOP_LEVEL_FILES = [
'.vscode/',
'.gitignore',
'readme.md',
'.husky/',
'.eslintrc',
'eslint.config.mjs',
'.prettierrc',
'.editorconfig',
'.actor/',
];
// Strip out deprecated /code and /shared folders, treat them as top-level code
const sanitizedLowercaseFilePath = lowercaseFilePath.replace(/^code\//, '').replace(/^shared\//, '');
return IGNORED_TOP_LEVEL_FILES.some((ignoredFile) => sanitizedLowercaseFilePath.startsWith(ignoredFile));
};
type FileChange =
| { impact: 'ignored' }
// Only things that influence how the Actor looks - e.g. README and CHANGELOG files, schema titles, descriptions, reordering, etc. We only need to rebuild on release
| { impact: 'cosmetic'; semanticallyVerified: boolean; includes: 'all-actors' | ActorConfig }
// Influences how the Actor works - we need to run tests
| {
impact: 'functional';
includes: 'all-actors' | ActorConfig;
};
const classifyFileChange = (originalFilePath: string, actorConfigs: ActorConfig[], commits: Commit[]): FileChange => {
// Lowercase for case-insensitive matching; keep original for git show (case-sensitive on Linux)
const lowercaseFilePath = originalFilePath.toLowerCase();
if (isIgnoredTopLevelFile(lowercaseFilePath)) {
return { impact: 'ignored' };
}
if (lowercaseFilePath.endsWith('changelog.md')) {
return { impact: 'cosmetic', semanticallyVerified: false, includes: 'all-actors' };
}
const actorFolderInfo = maybeParseActorFolder(lowercaseFilePath);
if (actorFolderInfo.isActorFolder) {
const actorConfigChanged = actorConfigs.find(
({ actorName }) => actorName.toLowerCase() === actorFolderInfo.actorName,
);
// This is some super weird case that happened once in the past but I don't remember the context anymore
if (actorConfigChanged === undefined) {
console.error(
'SHOULD NEVER HAPPEN: changes was found in an actor folder which no longer exists in the current commit, skipping this file',
{
actorName: actorFolderInfo.actorName,
lowercaseFilePath,
},
);
return { impact: 'ignored' };
}
if (lowercaseFilePath.endsWith('readme.md')) {
return { impact: 'cosmetic', semanticallyVerified: false, includes: actorConfigChanged };
}
// originalFilePath must be used here (not lowercaseFilePath) — git show is case-sensitive on Linux
if (lowercaseFilePath.endsWith('.json') && isCosmeticOnlyJsonSchemaChange(commits, originalFilePath)) {
return { impact: 'cosmetic', semanticallyVerified: true, includes: actorConfigChanged };
}
return { impact: 'functional', includes: actorConfigChanged };
}
// For any other files, we assume they can interact with the code
return { impact: 'functional', includes: 'all-actors' };
};
export const getChangedActors = ({
filepathsChanged,
actorConfigs,
isLatest = false,
commits,
}: ShouldBuildAndTestOptions): ActorConfig[] => {
// folder -> ActorConfig
const actorsChangedMap = new Map<string, ActorConfig>();
const actorConfigsWithoutStandalone = actorConfigs.filter(({ isStandalone }) => !isStandalone);
for (const originalFilePath of filepathsChanged) {
const fileChange = classifyFileChange(originalFilePath, actorConfigs, commits);
if (fileChange.impact === 'ignored') {
continue;
}
if (fileChange.impact === 'cosmetic' && !isLatest) {
continue;
}
if (fileChange.includes !== 'all-actors') {
actorsChangedMap.set(fileChange.includes.folder, fileChange.includes);
} else if (fileChange.includes === 'all-actors') {
// Standalone Actors are handled always via specific actors change, not all-actors
for (const actorConfig of actorConfigsWithoutStandalone) {
actorsChangedMap.set(actorConfig.folder, actorConfig);
}
}
}
const actorsChanged = Array.from(actorsChangedMap.values());
// All below here is just for logging
const formatFiles = (files: string[]) => (files.length > 0 ? files.join(', ') : '<no files>');
const ignoredFilesChanged = filepathsChanged.filter(
(file) => classifyFileChange(file, actorConfigs, commits).impact === 'ignored',
);
console.error(`[DIFF]: Ignored files (don't trigger test or build): ${formatFiles(ignoredFilesChanged)}`);
const cosmeticChanges = filepathsChanged
.map((file) => ({ file, change: classifyFileChange(file, actorConfigs, commits) }))
.filter(({ change }) => change.impact === 'cosmetic') as {
file: string;
change: Extract<FileChange, { impact: 'cosmetic' }>;
}[];
const semanticallyVerifiedFiles = cosmeticChanges.filter(({ change }) => change.semanticallyVerified).map(({ file }) => file);
const inherentlyCosmeticFiles = cosmeticChanges.filter(({ change }) => !change.semanticallyVerified).map(({ file }) => file);
console.error(
`[DIFF]: Cosmetic-only JSON schema changes (semantically verified, only trigger release build): ${formatFiles(semanticallyVerifiedFiles)}`,
);
console.error(
`[DIFF]: Inherently cosmetic files (README, CHANGELOG — only trigger release build): ${formatFiles(inherentlyCosmeticFiles)}`,
);
const functionalFilesChanged = filepathsChanged.filter(
(file) => classifyFileChange(file, actorConfigs, commits).impact === 'functional',
);
console.error(`[DIFF]: Functional files (trigger test & release build): ${formatFiles(functionalFilesChanged)}`);
if (actorsChanged.length > 0) {
const miniactors = actorsChanged.filter((config) => !config.isStandalone).map((config) => config.actorName);
const standaloneActors = actorsChanged
.filter((config) => config.isStandalone)
.map((config) => config.actorName);
console.error(`[DIFF]: MiniActors to be built and tested: ${miniactors.join(', ')}`);
console.error(`[DIFF]: Standalone Actors to be built and tested: ${standaloneActors.join(', ')}`);
} else {
console.error(`[DIFF]: No relevant files changed, skipping builds and tests`);
}
return actorsChanged;
};