Skip to content
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

feat(manager/pip-compile): Handle isLockfileUpdate in updateArtifacts #27353

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion lib/modules/manager/pip-compile/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { logger } from '../../../logger';
import * as docker from '../../../util/exec/docker';
import type { StatusResult } from '../../../util/git/types';
import * as _datasource from '../../datasource';
import type { UpdateArtifactsConfig } from '../types';
import type { UpdateArtifactsConfig, Upgrade } from '../types';
import { constructPipCompileCmd } from './artifacts';
import { updateArtifacts } from '.';

Expand Down Expand Up @@ -272,6 +272,33 @@ describe('modules/manager/pip-compile/artifacts', () => {
]);
});

it('uses --upgrade-package only for isLockfileUpdate', async () => {
fs.readLocalFile.mockResolvedValueOnce(simpleHeader);
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValue(
partial<StatusResult>({
modified: ['requirements.txt'],
}),
);
fs.readLocalFile.mockResolvedValueOnce('New requirements.txt');
expect(
await updateArtifacts({
packageFileName: 'requirements.in',
updatedDeps: [
{ depName: 'foo', newVersion: '1.0.2', isLockfileUpdate: true },
{ depName: 'bar', newVersion: '2.0.0' },
] satisfies Upgrade[],
newPackageFileContent: '{}',
config: { ...lockMaintenanceConfig, lockFiles: ['requirements.txt'] },
}),
).not.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: 'pip-compile --no-emit-index-url requirements.in --upgrade-package=foo==1.0.2',
},
]);
});

it('uses pip-compile version from config', async () => {
fs.readLocalFile.mockResolvedValueOnce(simpleHeader);
GlobalConfig.set(dockerAdminConfig);
Expand Down Expand Up @@ -408,5 +435,23 @@ describe('modules/manager/pip-compile/artifacts', () => {
),
).toThrow(/custom/);
});

it('add --upgrade-package to command if Upgrade[] passed', () => {
expect(
constructPipCompileCmd(
getCommandInHeader(
'pip-compile --output-file=requirements.txt requirements.in',
),
'subdir/requirements.txt',
false,
[
{ depName: 'foo', newVersion: '1.0.2' },
{ depName: 'bar', newVersion: '2.0.0' },
] satisfies Upgrade[],
),
).toBe(
'pip-compile --no-emit-index-url --output-file=requirements.txt requirements.in --upgrade-package=foo==1.0.2 --upgrade-package=bar==2.0.0',
);
});
});
});
11 changes: 10 additions & 1 deletion lib/modules/manager/pip-compile/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../../util/fs';
import { getRepoStatus } from '../../../util/git';
import * as pipRequirements from '../pip_requirements';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
import type { UpdateArtifact, UpdateArtifactsResult, Upgrade } from '../types';
import {
extractHeaderCommand,
getExecOptions,
Expand All @@ -21,6 +21,7 @@ export function constructPipCompileCmd(
content: string,
outputFileName: string,
haveCredentials: boolean,
upgradePackages: Upgrade[] = [],
): string {
const compileArgs = extractHeaderCommand(content, outputFileName);
if (compileArgs.isCustomCommand) {
Expand Down Expand Up @@ -56,12 +57,18 @@ export function constructPipCompileCmd(
) {
compileArgs.argv.splice(1, 0, '--no-emit-index-url');
}
for (const dep of upgradePackages) {
compileArgs.argv.push(
`--upgrade-package=${quote(dep.depName + '==' + dep.newVersion)}`,
);
}
return compileArgs.argv.map(quote).join(' ');
}

export async function updateArtifacts({
packageFileName: inputFileName,
newPackageFileContent: newInputContent,
updatedDeps,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
if (!config.lockFiles) {
Expand Down Expand Up @@ -89,12 +96,14 @@ export async function updateArtifacts({
if (config.isLockFileMaintenance) {
await deleteLocalFile(outputFileName);
}
const upgradePackages = updatedDeps.filter((dep) => dep.isLockfileUpdate);
const packageFile = pipRequirements.extractPackageFile(newInputContent);
const registryUrlVars = getRegistryUrlVarsFromPackageFile(packageFile);
const cmd = constructPipCompileCmd(
existingOutput,
outputFileName,
registryUrlVars.haveCredentials,
upgradePackages,
);
const execOptions = await getExecOptions(
config,
Expand Down
Loading