-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: evaluate buildpack constraints in exec (#12609)
- Loading branch information
Showing
7 changed files
with
199 additions
and
98 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
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
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,99 @@ | ||
import { mocked } from '../../../test/util'; | ||
import * as _datasource from '../../datasource'; | ||
import { generateInstallCommands, resolveConstraint } from './buildpack'; | ||
import type { ToolConstraint } from './types'; | ||
|
||
jest.mock('../../../lib/datasource'); | ||
|
||
const datasource = mocked(_datasource); | ||
|
||
describe('util/exec/buildpack', () => { | ||
describe('resolveConstraint()', () => { | ||
beforeEach(() => { | ||
datasource.getPkgReleases.mockResolvedValueOnce({ | ||
releases: [ | ||
{ version: '1.0.0' }, | ||
{ version: '1.1.0' }, | ||
{ version: '1.3.0' }, | ||
{ version: '2.0.14' }, | ||
{ version: '2.1.0' }, | ||
], | ||
}); | ||
}); | ||
it('returns from config', async () => { | ||
expect( | ||
await resolveConstraint({ toolName: 'composer', constraint: '1.1.0' }) | ||
).toBe('1.1.0'); | ||
}); | ||
|
||
it('returns from latest', async () => { | ||
expect(await resolveConstraint({ toolName: 'composer' })).toBe('2.1.0'); | ||
}); | ||
|
||
it('throws for unknown tools', async () => { | ||
datasource.getPkgReleases.mockReset(); | ||
datasource.getPkgReleases.mockResolvedValueOnce({ | ||
releases: [], | ||
}); | ||
await expect(resolveConstraint({ toolName: 'whoops' })).rejects.toThrow( | ||
'Invalid tool to install: whoops' | ||
); | ||
}); | ||
|
||
it('throws no releases', async () => { | ||
datasource.getPkgReleases.mockReset(); | ||
datasource.getPkgReleases.mockResolvedValueOnce({ | ||
releases: [], | ||
}); | ||
await expect(resolveConstraint({ toolName: 'composer' })).rejects.toThrow( | ||
'No tool releases found.' | ||
); | ||
}); | ||
|
||
it('falls back to latest version if no compatible release', async () => { | ||
datasource.getPkgReleases.mockReset(); | ||
datasource.getPkgReleases.mockResolvedValueOnce({ | ||
releases: [{ version: '1.2.3' }], | ||
}); | ||
expect( | ||
await resolveConstraint({ toolName: 'composer', constraint: '^3.1.0' }) | ||
).toBe('1.2.3'); | ||
}); | ||
|
||
it('falls back to latest version if invalid constraint', async () => { | ||
datasource.getPkgReleases.mockReset(); | ||
datasource.getPkgReleases.mockResolvedValueOnce({ | ||
releases: [{ version: '1.2.3' }], | ||
}); | ||
expect( | ||
await resolveConstraint({ toolName: 'composer', constraint: 'whoops' }) | ||
).toBe('1.2.3'); | ||
}); | ||
}); | ||
describe('generateInstallCommands()', () => { | ||
beforeEach(() => { | ||
datasource.getPkgReleases.mockResolvedValueOnce({ | ||
releases: [ | ||
{ version: '1.0.0' }, | ||
{ version: '1.1.0' }, | ||
{ version: '1.3.0' }, | ||
{ version: '2.0.14' }, | ||
{ version: '2.1.0' }, | ||
], | ||
}); | ||
}); | ||
it('returns install commands', async () => { | ||
const toolConstraints: ToolConstraint[] = [ | ||
{ | ||
toolName: 'composer', | ||
}, | ||
]; | ||
expect(await generateInstallCommands(toolConstraints)) | ||
.toMatchInlineSnapshot(` | ||
Array [ | ||
"install-tool composer 2.1.0", | ||
] | ||
`); | ||
}); | ||
}); | ||
}); |
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,75 @@ | ||
import { quote } from 'shlex'; | ||
import { getPkgReleases } from '../../datasource'; | ||
import { logger } from '../../logger'; | ||
import * as allVersioning from '../../versioning'; | ||
import { id as composerVersioningId } from '../../versioning/composer'; | ||
import type { ToolConfig, ToolConstraint } from './types'; | ||
|
||
const allToolConfig: Record<string, ToolConfig> = { | ||
composer: { | ||
datasource: 'github-releases', | ||
depName: 'composer/composer', | ||
versioning: composerVersioningId, | ||
}, | ||
}; | ||
|
||
export async function resolveConstraint( | ||
toolConstraint: ToolConstraint | ||
): Promise<string> { | ||
const { toolName } = toolConstraint; | ||
const toolConfig = allToolConfig[toolName]; | ||
if (!toolConfig) { | ||
throw new Error(`Invalid tool to install: ${toolName}`); | ||
} | ||
|
||
const versioning = allVersioning.get(toolConfig.versioning); | ||
let constraint = toolConstraint.constraint; | ||
if (constraint) { | ||
if (versioning.isValid(constraint)) { | ||
if (versioning.isSingleVersion(constraint)) { | ||
return constraint; | ||
} | ||
} else { | ||
logger.warn({ toolName, constraint }, 'Invalid tool constraint'); | ||
constraint = undefined; | ||
} | ||
} | ||
|
||
const pkgReleases = await getPkgReleases(toolConfig); | ||
if (!pkgReleases?.releases?.length) { | ||
throw new Error('No tool releases found.'); | ||
} | ||
|
||
const allVersions = pkgReleases.releases.map((r) => r.version); | ||
const matchingVersions = allVersions.filter( | ||
(v) => !constraint || versioning.matches(v, constraint) | ||
); | ||
|
||
if (matchingVersions.length) { | ||
const resolvedVersion = matchingVersions.pop(); | ||
logger.debug({ toolName, constraint, resolvedVersion }, 'Resolved version'); | ||
return resolvedVersion; | ||
} | ||
const latestVersion = allVersions.filter((v) => versioning.isStable(v)).pop(); | ||
logger.warn( | ||
{ toolName, constraint, latestVersion }, | ||
'No matching tool versions found for constraint - using latest version' | ||
); | ||
return latestVersion; | ||
} | ||
|
||
export async function generateInstallCommands( | ||
toolConstraints: ToolConstraint[] | ||
): Promise<string[]> { | ||
const installCommands = []; | ||
if (toolConstraints?.length) { | ||
for (const toolConstraint of toolConstraints) { | ||
const toolVersion = await resolveConstraint(toolConstraint); | ||
const installCommand = `install-tool ${toolConstraint.toolName} ${quote( | ||
toolVersion | ||
)}`; | ||
installCommands.push(installCommand); | ||
} | ||
} | ||
return installCommands; | ||
} |
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 @@ | ||
export interface ToolConstraint { | ||
toolName: string; | ||
constraint?: string; | ||
} | ||
|
||
export interface ToolConfig { | ||
datasource: string; | ||
depName: string; | ||
versioning: string; | ||
} |