-
Notifications
You must be signed in to change notification settings - Fork 1
feat(bin): don't test on nonfunctional changes in json files #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f36a78d
feat(bin): don't test on nonfunctional changes in json files
metalwarrior665 4b7416e
test: add test for reordering (passes)
metalwarrior665 66bf589
Merge branch 'master' into feat/skip-nonfunctional-changes
metalwarrior665 6aee628
refactor: cleaner logic flow, and use 'function' and 'cosmetic' every…
metalwarrior665 e4b8c9c
fix should build & test tests and smaller touches
metalwarrior665 f3f67ec
Merge branch 'master' into feat/skip-nonfunctional-changes
metalwarrior665 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| 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', includes: 'all-actors' | ActorConfig } | | ||
| // Influences how the Actor works - we need to run tests | ||
| { | ||
| impact: 'functional', includes: 'all-actors' | ActorConfig | ||
| }; | ||
|
|
||
| const classifyFileChange = (lowercaseFilePath: string, actorConfigs: ActorConfig[], commits: Commit[]): FileChange => { | ||
| if (isIgnoredTopLevelFile(lowercaseFilePath)) { | ||
| return { impact: 'ignored' }; | ||
| } | ||
|
|
||
| if (lowercaseFilePath.endsWith('changelog.md')) { | ||
| return { impact: 'cosmetic', 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', includes: actorConfigChanged }; | ||
| } | ||
| if (lowercaseFilePath.endsWith('.json') && isCosmeticOnlyJsonSchemaChange(commits, lowercaseFilePath)) { | ||
| return { impact: 'cosmetic', 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); | ||
|
|
||
| const lowercaseFiles = filepathsChanged.map((file) => file.toLowerCase()); | ||
|
|
||
| for (const lowercaseFilePath of lowercaseFiles) { | ||
| const fileChange = classifyFileChange(lowercaseFilePath, 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 ignoredFilesChanged = lowercaseFiles.filter((file) => classifyFileChange(file, actorConfigs, commits).impact === 'ignored'); | ||
| console.error(`[DIFF]: Ignored files (don't trigger test or build): ${ignoredFilesChanged.join(', ')}`); | ||
|
|
||
| const cosmeticFilesChanged = lowercaseFiles.filter((file) => classifyFileChange(file, actorConfigs, commits).impact === 'cosmetic'); | ||
| console.error(`[DIFF]: Cosmetic files (should only trigger release build): ${cosmeticFilesChanged.join(', ')}`); | ||
|
|
||
| const functionalFilesChanged = lowercaseFiles.filter((file) => classifyFileChange(file, actorConfigs, commits).impact === 'functional'); | ||
| console.error(`[DIFF]: Functional files (trigger test & release build): ${functionalFilesChanged.join(', ')}`); | ||
|
|
||
| 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; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type { Commit } from './types.js'; | ||
| import { spawnCommandInGhWorkspace } from './utils.js'; | ||
|
|
||
| const COSMETIC_JSON_FIELD_NAMES = new Set([ | ||
| 'title', 'description', 'example', 'enumTitles', 'sectionCaption', 'sectionDescription', | ||
| ]); | ||
|
|
||
| const isPlainObject = (val: unknown): val is Record<string, unknown> => | ||
| typeof val === 'object' && val !== null && !Array.isArray(val); | ||
|
|
||
| const isCosmeticObjectChange = (oldVal: unknown, newVal: unknown, currentKey?: string): boolean => { | ||
| // If the key itself is cosmetic, any change under it is fine | ||
| if (currentKey && COSMETIC_JSON_FIELD_NAMES.has(currentKey)) return true; | ||
| if (JSON.stringify(oldVal) === JSON.stringify(newVal)) return true; | ||
| if (isPlainObject(oldVal) && isPlainObject(newVal)) { | ||
| const allKeys = new Set([...Object.keys(oldVal), ...Object.keys(newVal)]); | ||
| return [...allKeys].every((key) => isCosmeticObjectChange(oldVal[key], newVal[key], key)); | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns true if the two JSON strings differ only in cosmetic fields | ||
| * (title, description, example, enumTitles, sectionCaption, sectionDescription). | ||
| */ | ||
| export const isCosmeticOnlyJsonSchemaChange = (commits: Commit[], changedFilepath: string): boolean => { | ||
| // TODO: validate this is the right commit range | ||
| const oldRef = `${commits[0].sha}~`; | ||
| const newRef = commits[commits.length - 1].sha; | ||
| let oldJson: unknown; | ||
| let newJson: unknown; | ||
| try { | ||
| const oldContent = spawnCommandInGhWorkspace(`git show ${oldRef}:${changedFilepath}`); | ||
| const newContent = spawnCommandInGhWorkspace(`git show ${newRef}:${changedFilepath}`); | ||
|
|
||
| oldJson = JSON.parse(oldContent); | ||
| newJson = JSON.parse(newContent); | ||
| } catch { | ||
| console.error(`Failed to get or parse JSON content for ${changedFilepath} at refs ${oldRef} and ${newRef}, maybe it is new file or deleted? Treating it as a non-cosmetic change.`); | ||
| return false; | ||
| } | ||
| return isCosmeticObjectChange(oldJson, newJson); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.