-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing from one of SEEK's internal repos
- Loading branch information
Showing
5 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
--- | ||
'skuba': minor | ||
--- | ||
|
||
Replace `.buildkite/` files with duplicated YAML merge keys, for example: | ||
|
||
```yaml | ||
# Before | ||
- <<: *deploy | ||
<<: *docker | ||
label: stuff | ||
|
||
# After | ||
- <<: [*deploy, *docker] | ||
label: stuff | ||
``` | ||
This should have no functional change, and is to support standardised YAML parsing across different tools, including the latest ESLint upgrades. | ||
This migration will not be capture all cases of this (e.g. if there are keys between the merge keys). If you have other cases, update them following the example above. |
This file contains 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
80 changes: 80 additions & 0 deletions
80
src/cli/lint/internalLints/upgrade/patches/8.2.1/collapseDuplicateMergeKeys.ts
This file contains 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,80 @@ | ||
import { inspect } from 'util'; | ||
|
||
import fg from 'fast-glob'; | ||
import { readFile, writeFile } from 'fs-extra'; | ||
|
||
import type { PatchFunction, PatchReturnType } from '../..'; | ||
import { log } from '../../../../../../utils/logging'; | ||
|
||
const collapseDuplicateMergeKeys: PatchFunction = async ({ | ||
mode, | ||
}): Promise<PatchReturnType> => { | ||
const buildkiteFiles = await fg(['.buildkite/**/*'], { onlyFiles: true }); | ||
|
||
if (buildkiteFiles.length === 0) { | ||
return { result: 'skip', reason: 'no Buildkite files found' }; | ||
} | ||
|
||
const input = await Promise.all( | ||
buildkiteFiles.map((name) => readFile(name, 'utf-8')), | ||
); | ||
|
||
const replaced = await Promise.all( | ||
input.map(collapseDuplicateMergeKeysInFile), | ||
); | ||
|
||
if (replaced.every((r, i) => r === input[i])) { | ||
return { result: 'skip' }; | ||
} | ||
|
||
if (mode === 'lint') { | ||
return { result: 'apply' }; | ||
} | ||
|
||
await Promise.all( | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
buildkiteFiles.map((name, i) => writeFile(name, replaced[i]!)), | ||
); | ||
|
||
return { result: 'apply' }; | ||
}; | ||
|
||
const collapseDuplicateMergeKeysInFile = (input: string) => | ||
replaceAllUntilStable( | ||
input, | ||
/^([ \-]*)<<: \[?(\*[^\n\]]+)\]?$\n^( *)<<: \[?(\*[^\n\]]+)\]?$/gm, | ||
(match, a, b, c, d) => { | ||
if (a.length === c.length) { | ||
return `${a}<<: [${b}, ${d}]`; | ||
} | ||
return match; | ||
}, | ||
); | ||
|
||
const replaceAllUntilStable = ( | ||
input: string, | ||
searchValue: RegExp, | ||
replacer: (substring: string, ...args: string[]) => string, | ||
): string => { | ||
let output = input; | ||
let previousOutput; | ||
|
||
do { | ||
previousOutput = output; | ||
output = output.replace(searchValue, replacer); | ||
} while (output !== previousOutput); | ||
|
||
return output; | ||
}; | ||
|
||
export const tryCollapseDuplicateMergeKeys: PatchFunction = async (config) => { | ||
try { | ||
return await collapseDuplicateMergeKeys(config); | ||
} catch (err) { | ||
log.warn('Failed to collapse duplicate merge keys.'); | ||
log.subtle(inspect(err)); | ||
return { result: 'skip', reason: 'due to an error' }; | ||
} | ||
}; | ||
|
||
// TODO: write some tests |
This file contains 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 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