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/composer): support updates with minimal changes #34218

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/modules/manager/composer/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type { AuthJson } from './types';
import {
extractConstraints,
getComposerArguments,
getComposerUpdateArguments,
getPhpConstraint,
isArtifactAuthEnabled,
requireComposerDependencyInstallation,
Expand Down Expand Up @@ -191,7 +192,7 @@ export async function updateArtifacts({
.join(' ')
).trim() + ' --with-dependencies';
}
args += getComposerArguments(config, composerToolConstraint);
args += getComposerUpdateArguments(config, composerToolConstraint)
logger.trace({ cmd, args }, 'composer command');
commands.push(`${cmd} ${args}`);

Expand Down
51 changes: 51 additions & 0 deletions lib/modules/manager/composer/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Lockfile, PackageFile } from './schema';
import {
extractConstraints,
getComposerArguments,
getComposerUpdateArguments,
requireComposerDependencyInstallation,
} from './utils';

Expand Down Expand Up @@ -277,6 +278,56 @@ describe('modules/manager/composer/utils', () => {
});
});

describe('getComposerUpdateArguments', () => {
it('does not request an update with minimal changes with 2.6.0', () => {
expect(
getComposerUpdateArguments({}, { toolName: 'composer', constraint: '2.6.0' }),
).toBe(
' --no-ansi --no-interaction --no-scripts --no-autoloader --no-plugins',
);
});

it('requests an update with minimal changes with 2.7.0', () => {
expect(
getComposerUpdateArguments({}, { toolName: 'composer', constraint: '2.7.0' }),
).toBe(
" --no-ansi --no-interaction --no-scripts --no-autoloader --no-plugins --minimal-changes",
);
});

it('requests an update with minimal changes with 2.8.0', () => {
expect(
getComposerUpdateArguments({}, { toolName: 'composer', constraint: '2.8.0' }),
).toBe(
" --no-ansi --no-interaction --no-scripts --no-autoloader --no-plugins --minimal-changes",
);
});

it('requests an update with minimal changes with ^2.6', () => {
expect(
getComposerUpdateArguments({}, { toolName: 'composer', constraint: '^2.6' }),
).toBe(
" --no-ansi --no-interaction --no-scripts --no-autoloader --no-plugins --minimal-changes",
);
});

it('requests an update with minimal changes with ^2.7', () => {
expect(
getComposerUpdateArguments({}, { toolName: 'composer', constraint: '^2.7' }),
).toBe(
" --no-ansi --no-interaction --no-scripts --no-autoloader --no-plugins --minimal-changes",
);
});

it('requests an update with minimal changes with ^2.8', () => {
expect(
getComposerUpdateArguments({}, { toolName: 'composer', constraint: '^2.8' }),
).toBe(
" --no-ansi --no-interaction --no-scripts --no-autoloader --no-plugins --minimal-changes",
);
});
});

describe('requireComposerDependencyInstallation', () => {
it('returns true when symfony/flex has been installed', () => {
const lockfile = Lockfile.parse({
Expand Down
17 changes: 17 additions & 0 deletions lib/modules/manager/composer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { coerceNumber } from '../../../util/number';
import { api, id as composerVersioningId } from '../../versioning/composer';
import type { UpdateArtifactsConfig } from '../types';
import type { Lockfile, PackageFile } from './schema';
import is from '@sindresorhus/is';

export { composerVersioningId };

Expand Down Expand Up @@ -46,6 +47,22 @@ export function getComposerArguments(
return args;
}

export function getComposerUpdateArguments(
config: UpdateArtifactsConfig,
toolConstraint: ToolConstraint,
): string {
let args = getComposerArguments(config, toolConstraint);

if (
is.string(toolConstraint.constraint) &&
api.intersects!(toolConstraint.constraint, '^2.7')
) {
args += ' --minimal-changes';
}
Comment on lines +56 to +61
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If api.intersects() can ever throw, it would be best to wrap this block in a try/catch and emit a logger.warn() if it ever throws (and then continue with default args)


return args;
}

export function getPhpConstraint(
constraints: Record<string, string>,
): string | null {
Expand Down
21 changes: 21 additions & 0 deletions lib/modules/versioning/composer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ describe('modules/versioning/composer/index', () => {
expect(semver.subset!(a, b)).toBe(expected);
});

it.each`
a | b | expected
${'1.0.0'} | ${'1.0.0'} | ${true}
${'1.0.0'} | ${'>=1.0.0'} | ${true}
${'1.1.0'} | ${'^1.0.0'} | ${true}
${'>=1.0.0'} | ${'>=1.0.0'} | ${true}
${'~1.0.0'} | ${'~1.0.0'} | ${true}
${'^1.0.0'} | ${'^1.0.0'} | ${true}
${'>=1.0.0'} | ${'>=1.1.0'} | ${true}
${'~1.0.0'} | ${'~1.1.0'} | ${false}
${'^1.0.0'} | ${'^1.1.0'} | ${true}
${'>=1.0.0'} | ${'<1.0.0'} | ${false}
${'~1.0.0'} | ${'~0.9.0'} | ${false}
${'^1.0.0'} | ${'^0.9.0'} | ${false}
${'^1.1.0 || ^2.0.0'} | ${'^1.0.0 || ^2.0.0'} | ${true}
${'^1.0.0 || ^2.0.0'} | ${'^1.1.0 || ^2.0.0'} | ${true}
${'^7.0.0'} | ${'<8.0-DEV'} | ${true}
`('intersects("$a", "$b") === $expected', ({ a, b, expected }) => {
expect(semver.intersects!(a, b)).toBe(expected);
});

it.each`
currentValue | rangeStrategy | currentVersion | newVersion | expected
${'~1.0'} | ${'pin'} | ${'1.0'} | ${'V1.1'} | ${'V1.1'}
Expand Down
10 changes: 10 additions & 0 deletions lib/modules/versioning/composer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ function subset(subRange: string, superRange: string): boolean | undefined {
}
}

function intersects(subRange: string, superRange: string): boolean {
try {
return npm.intersects!(composer2npm(subRange), composer2npm(superRange));
} catch (err) {
logger.trace({ err }, 'composer.intersects error');
return false;
}
}

function getNewValue({
currentValue,
rangeStrategy,
Expand Down Expand Up @@ -393,5 +402,6 @@ export const api: VersioningApi = {
getNewValue,
sortVersions,
subset,
intersects,
};
export default api;
20 changes: 20 additions & 0 deletions lib/modules/versioning/npm/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ describe('modules/versioning/npm/index', () => {
expect(semver.subset!(a, b)).toBe(expected);
});

it.each`
a | b | expected
${'1.0.0'} | ${'1.0.0'} | ${true}
${'1.0.0'} | ${'>=1.0.0'} | ${true}
${'1.1.0'} | ${'^1.0.0'} | ${true}
${'>=1.0.0'} | ${'>=1.0.0'} | ${true}
${'~1.0.0'} | ${'~1.0.0'} | ${true}
${'^1.0.0'} | ${'^1.0.0'} | ${true}
${'>=1.0.0'} | ${'>=1.1.0'} | ${true}
${'~1.0.0'} | ${'~1.1.0'} | ${false}
${'^1.0.0'} | ${'^1.1.0'} | ${true}
${'>=1.0.0'} | ${'<1.0.0'} | ${false}
${'~1.0.0'} | ${'~0.9.0'} | ${false}
${'^1.0.0'} | ${'^0.9.0'} | ${false}
${'^1.1.0 || ^2.0.0'} | ${'^1.0.0 || ^2.0.0'} | ${true}
${'^1.0.0 || ^2.0.0'} | ${'^1.1.0 || ^2.0.0'} | ${true}
`('intersects("$a", "$b") === $expected', ({ a, b, expected }) => {
expect(semver.intersects!(a, b)).toBe(expected);
});

it.each`
currentValue | rangeStrategy | currentVersion | newVersion | expected
${'=1.0.0'} | ${'bump'} | ${'1.0.0'} | ${'1.1.0'} | ${'=1.1.0'}
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/versioning/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
gt: isGreaterThan,
eq: equals,
subset,
intersects,
} = semver;

// If this is left as an alias, inputs like "17.04.0" throw errors
Expand Down Expand Up @@ -65,6 +66,7 @@ export const api: VersioningApi = {
minSatisfyingVersion,
sortVersions,
subset,
intersects,
};

export default api;
5 changes: 5 additions & 0 deletions lib/modules/versioning/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export interface VersioningApi {
*/
subset?(subRange: string, superRange: string): boolean | undefined;

/**
* Checks whether subRange intersects superRange.
*/
intersects?(subRange: string, superRange: string): boolean;

/**
* Return whether unstable-to-unstable upgrades within the same major version are allowed.
*/
Expand Down