|
| 1 | +import { isCosmeticOnlyJsonSchemaChange } from './diff-json-schema.js'; |
| 2 | +import type { ActorConfig, Commit } from './types.js'; |
| 3 | + |
| 4 | +interface ShouldBuildAndTestOptions { |
| 5 | + filepathsChanged: string[]; |
| 6 | + actorConfigs: ActorConfig[]; |
| 7 | + isLatest?: boolean; |
| 8 | + commits: Commit[]; |
| 9 | +} |
| 10 | + |
| 11 | +export const maybeParseActorFolder = (lowercaseFilePath: string): { isActorFolder: true, actorName: string } | { isActorFolder: false } => { |
| 12 | + const match = lowercaseFilePath.match(/^(?:standalone-)?actors\/([^/]+)\/.+/); |
| 13 | + if (match) { |
| 14 | + // Some usernames weirdly use underscores, e.g. google_maps_email_extractor_standby-contact-details-scraper so we only need replace the last one |
| 15 | + return { isActorFolder: true, actorName: match[1].replace(/_(?=[^_]*$)/, '/') }; |
| 16 | + } |
| 17 | + return { isActorFolder: false }; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Also works for folders |
| 22 | + */ |
| 23 | +const isIgnoredTopLevelFile = (lowercaseFilePath: string) => { |
| 24 | + // On top level, we should only have dev-only readme and .actor/ is just for apify push CLI (real Actor configs are in /actors) |
| 25 | + const IGNORED_TOP_LEVEL_FILES = ['.vscode/', '.gitignore', 'readme.md', '.husky/', '.eslintrc', 'eslint.config.mjs', '.prettierrc', '.editorconfig', '.actor/']; |
| 26 | + // Strip out deprecated /code and /shared folders, treat them as top-level code |
| 27 | + const sanitizedLowercaseFilePath = lowercaseFilePath.replace(/^code\//, '').replace(/^shared\//, ''); |
| 28 | + |
| 29 | + return IGNORED_TOP_LEVEL_FILES.some((ignoredFile) => sanitizedLowercaseFilePath.startsWith(ignoredFile)); |
| 30 | +}; |
| 31 | + |
| 32 | +type FileChange = |
| 33 | + { impact: 'ignored' } | |
| 34 | + // 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 |
| 35 | + { impact: 'cosmetic', includes: 'all-actors' | ActorConfig } | |
| 36 | + // Influences how the Actor works - we need to run tests |
| 37 | + { |
| 38 | + impact: 'functional', includes: 'all-actors' | ActorConfig |
| 39 | + }; |
| 40 | + |
| 41 | +const classifyFileChange = (lowercaseFilePath: string, actorConfigs: ActorConfig[], commits: Commit[]): FileChange => { |
| 42 | + if (isIgnoredTopLevelFile(lowercaseFilePath)) { |
| 43 | + return { impact: 'ignored' }; |
| 44 | + } |
| 45 | + |
| 46 | + if (lowercaseFilePath.endsWith('changelog.md')) { |
| 47 | + return { impact: 'cosmetic', includes: 'all-actors' }; |
| 48 | + } |
| 49 | + |
| 50 | + const actorFolderInfo = maybeParseActorFolder(lowercaseFilePath); |
| 51 | + if (actorFolderInfo.isActorFolder) { |
| 52 | + const actorConfigChanged = actorConfigs.find(({ actorName }) => actorName.toLowerCase() === actorFolderInfo.actorName); |
| 53 | + // This is some super weird case that happened once in the past but I don't remember the context anymore |
| 54 | + if (actorConfigChanged === undefined) { |
| 55 | + console.error('SHOULD NEVER HAPPEN: changes was found in an actor folder which no longer exists in the current commit, skipping this file', { |
| 56 | + actorName: actorFolderInfo.actorName, |
| 57 | + lowercaseFilePath, |
| 58 | + }); |
| 59 | + return { impact: 'ignored' }; |
| 60 | + } |
| 61 | + if (lowercaseFilePath.endsWith('readme.md')) { |
| 62 | + return { impact: 'cosmetic', includes: actorConfigChanged }; |
| 63 | + } |
| 64 | + if (lowercaseFilePath.endsWith('.json') && isCosmeticOnlyJsonSchemaChange(commits, lowercaseFilePath)) { |
| 65 | + return { impact: 'cosmetic', includes: actorConfigChanged }; |
| 66 | + } |
| 67 | + |
| 68 | + return { impact: 'functional', includes: actorConfigChanged }; |
| 69 | + } |
| 70 | + |
| 71 | + // For any other files, we assume they can interact with the code |
| 72 | + return { impact: 'functional', includes: 'all-actors' }; |
| 73 | +} |
| 74 | + |
| 75 | +export const getChangedActors = ( |
| 76 | + { filepathsChanged, actorConfigs, isLatest = false, commits }: ShouldBuildAndTestOptions, |
| 77 | +): ActorConfig[] => { |
| 78 | + // folder -> ActorConfig |
| 79 | + const actorsChangedMap = new Map<string, ActorConfig>(); |
| 80 | + |
| 81 | + const actorConfigsWithoutStandalone = actorConfigs.filter(({ isStandalone }) => !isStandalone); |
| 82 | + |
| 83 | + const lowercaseFiles = filepathsChanged.map((file) => file.toLowerCase()); |
| 84 | + |
| 85 | + for (const lowercaseFilePath of lowercaseFiles) { |
| 86 | + const fileChange = classifyFileChange(lowercaseFilePath, actorConfigs, commits); |
| 87 | + if (fileChange.impact === 'ignored') { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if (fileChange.impact === 'cosmetic' && !isLatest) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + if (fileChange.includes !== 'all-actors') { |
| 96 | + actorsChangedMap.set(fileChange.includes.folder, fileChange.includes); |
| 97 | + } else if (fileChange.includes === 'all-actors') { |
| 98 | + // Standalone Actors are handled always via specific actors change, not all-actors |
| 99 | + for (const actorConfig of actorConfigsWithoutStandalone) { |
| 100 | + actorsChangedMap.set(actorConfig.folder, actorConfig); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const actorsChanged = Array.from(actorsChangedMap.values()); |
| 106 | + |
| 107 | + // All below here is just for logging |
| 108 | + const ignoredFilesChanged = lowercaseFiles.filter((file) => classifyFileChange(file, actorConfigs, commits).impact === 'ignored'); |
| 109 | + console.error(`[DIFF]: Ignored files (don't trigger test or build): ${ignoredFilesChanged.join(', ')}`); |
| 110 | + |
| 111 | + const cosmeticFilesChanged = lowercaseFiles.filter((file) => classifyFileChange(file, actorConfigs, commits).impact === 'cosmetic'); |
| 112 | + console.error(`[DIFF]: Cosmetic files (should only trigger release build): ${cosmeticFilesChanged.join(', ')}`); |
| 113 | + |
| 114 | + const functionalFilesChanged = lowercaseFiles.filter((file) => classifyFileChange(file, actorConfigs, commits).impact === 'functional'); |
| 115 | + console.error(`[DIFF]: Functional files (trigger test & release build): ${functionalFilesChanged.join(', ')}`); |
| 116 | + |
| 117 | + if (actorsChanged.length > 0) { |
| 118 | + const miniactors = actorsChanged.filter((config) => !config.isStandalone).map((config) => config.actorName); |
| 119 | + const standaloneActors = actorsChanged.filter((config) => config.isStandalone).map((config) => config.actorName); |
| 120 | + console.error(`[DIFF]: MiniActors to be built and tested: ${miniactors.join(', ')}`); |
| 121 | + console.error(`[DIFF]: Standalone Actors to be built and tested: ${standaloneActors.join(', ')}`); |
| 122 | + } else { |
| 123 | + console.error(`[DIFF]: No relevant files changed, skipping builds and tests`); |
| 124 | + } |
| 125 | + |
| 126 | + return actorsChanged; |
| 127 | +}; |
0 commit comments