-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove docker-compose version autofix (#1638)
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 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,5 @@ | ||
--- | ||
'skuba': minor | ||
--- | ||
|
||
lint: Removes obsolete version field from docker-compose.yml files |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Patches } from '../..'; | ||
|
||
import { tryPatchDockerComposeFiles } from './patchDockerCompose'; | ||
|
||
export const patches: Patches = [ | ||
{ | ||
apply: tryPatchDockerComposeFiles, | ||
description: 'Remove version field from docker-compose files', | ||
}, | ||
]; |
85 changes: 85 additions & 0 deletions
85
src/cli/lint/internalLints/upgrade/patches/8.2.1/patchDockerCompose.test.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,85 @@ | ||
import fg from 'fast-glob'; | ||
import { readFile, writeFile } from 'fs-extra'; | ||
|
||
import type { PatchConfig } from '../..'; | ||
|
||
import { tryPatchDockerComposeFiles } from './patchDockerCompose'; | ||
jest.mock('fast-glob'); | ||
jest.mock('fs-extra'); | ||
|
||
describe('patchDockerComposeFile', () => { | ||
afterEach(() => jest.resetAllMocks()); | ||
|
||
const mockDockerComposeFile = 'docker-compose.yml'; | ||
const mockDockerComposeContents = | ||
'services:\n' + | ||
'app:\n' + | ||
"image: ${BUILDKITE_PLUGIN_DOCKER_IMAGE:-''}\n" + | ||
'init: true\n' + | ||
'volumes:'; | ||
const mockPatchableDockerComposeContents = `version: '3.8'\n${mockDockerComposeContents}`; | ||
|
||
it('should skip if no Dockerfile is found', async () => { | ||
jest.mocked(fg).mockResolvedValueOnce([]); | ||
await expect( | ||
tryPatchDockerComposeFiles({ mode: 'format' } as PatchConfig), | ||
).resolves.toEqual({ | ||
result: 'skip', | ||
reason: 'no docker-compose files found', | ||
}); | ||
}); | ||
it('should patch docker-compose files with version field', async () => { | ||
jest.mocked(fg).mockResolvedValueOnce([mockDockerComposeFile]); | ||
jest | ||
.mocked(readFile) | ||
.mockResolvedValueOnce(mockPatchableDockerComposeContents as never); | ||
await expect( | ||
tryPatchDockerComposeFiles({ mode: 'format' } as PatchConfig), | ||
).resolves.toEqual({ | ||
result: 'apply', | ||
}); | ||
expect(writeFile).toHaveBeenCalledWith( | ||
'docker-compose.yml', | ||
'services:\n' + | ||
'app:\n' + | ||
"image: ${BUILDKITE_PLUGIN_DOCKER_IMAGE:-''}\n" + | ||
'init: true\n' + | ||
'volumes:', | ||
); | ||
}); | ||
it('should skip if no docker-compose files contain a version field', async () => { | ||
jest.mocked(fg).mockResolvedValueOnce([mockDockerComposeFile]); | ||
jest | ||
.mocked(readFile) | ||
.mockResolvedValueOnce(mockDockerComposeContents as never); | ||
await expect( | ||
tryPatchDockerComposeFiles({ mode: 'format' } as PatchConfig), | ||
).resolves.toEqual({ | ||
result: 'skip', | ||
reason: 'no docker-compose files to patch', | ||
}); | ||
}); | ||
it('should not remove intended version in docker compose file', async () => { | ||
jest.mocked(fg).mockResolvedValueOnce([mockDockerComposeFile]); | ||
jest | ||
.mocked(readFile) | ||
.mockResolvedValueOnce( | ||
`${mockPatchableDockerComposeContents}\n version: 7\nversion: 0.2` as never, | ||
); | ||
await expect( | ||
tryPatchDockerComposeFiles({ mode: 'format' } as PatchConfig), | ||
).resolves.toEqual({ | ||
result: 'apply', | ||
}); | ||
expect(writeFile).toHaveBeenCalledWith( | ||
'docker-compose.yml', | ||
'services:\n' + | ||
'app:\n' + | ||
"image: ${BUILDKITE_PLUGIN_DOCKER_IMAGE:-''}\n" + | ||
'init: true\n' + | ||
'volumes:\n' + | ||
' version: 7\n' + | ||
'version: 0.2', | ||
); | ||
}); | ||
}); |
77 changes: 77 additions & 0 deletions
77
src/cli/lint/internalLints/upgrade/patches/8.2.1/patchDockerCompose.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,77 @@ | ||
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 DOCKER_COMPOSE_VERSION_REGEX = /^version: ['"]?\d+(\.\d+)*['"]?\n*/m; | ||
|
||
const fetchFiles = async (files: string[]) => | ||
Promise.all( | ||
files.map(async (file) => { | ||
const contents = await readFile(file, 'utf8'); | ||
|
||
return { | ||
file, | ||
contents, | ||
}; | ||
}), | ||
); | ||
|
||
const patchDockerComposeFiles: PatchFunction = async ({ | ||
mode, | ||
}): Promise<PatchReturnType> => { | ||
const maybeDockerComposeFiles = await Promise.resolve( | ||
fg(['docker-compose*.yml']), | ||
); | ||
|
||
if (!maybeDockerComposeFiles.length) { | ||
return { | ||
result: 'skip', | ||
reason: 'no docker-compose files found', | ||
}; | ||
} | ||
|
||
const dockerComposeFiles = await fetchFiles(maybeDockerComposeFiles); | ||
|
||
const dockerComposeFilesToPatch = dockerComposeFiles.filter(({ contents }) => | ||
contents.match(DOCKER_COMPOSE_VERSION_REGEX), | ||
); | ||
|
||
if (!dockerComposeFilesToPatch.length) { | ||
return { | ||
result: 'skip', | ||
reason: 'no docker-compose files to patch', | ||
}; | ||
} | ||
|
||
if (mode === 'lint') { | ||
return { | ||
result: 'apply', | ||
}; | ||
} | ||
|
||
await Promise.all( | ||
dockerComposeFilesToPatch.map(async ({ file, contents }) => { | ||
const patchedContents = contents.replace( | ||
DOCKER_COMPOSE_VERSION_REGEX, | ||
'', | ||
); | ||
await writeFile(file, patchedContents); | ||
}), | ||
); | ||
|
||
return { result: 'apply' }; | ||
}; | ||
|
||
export const tryPatchDockerComposeFiles: PatchFunction = async (config) => { | ||
try { | ||
return await patchDockerComposeFiles(config); | ||
} catch (err) { | ||
log.warn('Failed to patch pnpm packageManager CI configuration.'); | ||
log.subtle(inspect(err)); | ||
return { result: 'skip', reason: 'due to an error' }; | ||
} | ||
}; |