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

refactor: Lookup filtering of unstable releases #30054

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 51 additions & 27 deletions lib/workers/repository/process/lookup/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import { partial } from '../../../../../test/util';
import { Release } from '../../../../modules/datasource';

Check failure on line 2 in lib/workers/repository/process/lookup/filter.spec.ts

View workflow job for this annotation

GitHub Actions / lint-other

This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'.
import * as allVersioning from '../../../../modules/versioning';
import { filterVersions } from './filter';
import type { FilterConfig } from './types';

const versioning = allVersioning.get('semver');

const releases = [
{
version: '1.0.1',
releaseTimestamp: '2021-01-01T00:00:01.000Z',
},
{
version: '1.2.0',
releaseTimestamp: '2021-01-03T00:00:00.000Z',
},
{
version: '2.0.0',
releaseTimestamp: '2021-01-05T00:00:00.000Z',
},
{
version: '2.1.0',
releaseTimestamp: '2021-01-07T00:00:00.000Z',
},
// for coverage
{
version: 'invalid.version',
releaseTimestamp: '2021-01-07T00:00:00.000Z',
},
];

describe('workers/repository/process/lookup/filter', () => {
describe('.filterVersions()', () => {
it('should filter versions allowed by semver syntax when allowedVersions is not valid version, range or pypi syntax', () => {
const releases = [
{
version: '1.0.1',
releaseTimestamp: '2021-01-01T00:00:01.000Z',
},
{
version: '1.2.0',
releaseTimestamp: '2021-01-03T00:00:00.000Z',
},
{
version: '2.0.0',
releaseTimestamp: '2021-01-05T00:00:00.000Z',
},
{
version: '2.1.0',
releaseTimestamp: '2021-01-07T00:00:00.000Z',
},
// for coverage
{
version: 'invalid.version',
releaseTimestamp: '2021-01-07T00:00:00.000Z',
},
] satisfies Release[];

const config = partial<FilterConfig>({
ignoreUnstable: false,
ignoreDeprecated: false,
Expand All @@ -41,9 +42,6 @@
const currentVersion = '1.0.0';
const latestVersion = '2.0.0';

jest.spyOn(versioning, 'isVersion').mockReturnValue(true);
jest.spyOn(versioning, 'isGreaterThan').mockReturnValue(true);

const filteredVersions = filterVersions(
config,
currentVersion,
Expand All @@ -57,5 +55,31 @@
{ version: '2.1.0', releaseTimestamp: '2021-01-07T00:00:00.000Z' },
]);
});

it('allows unstable major upgrades', () => {
const nodeVersioning = allVersioning.get('node');

const releases = [
{ version: '1.0.0-alpha' },
{ version: '1.2.3-beta' },
] satisfies Release[];

const config = partial<FilterConfig>({
ignoreUnstable: true,
ignoreDeprecated: true,
});
const currentVersion = '1.0.0-alpha';
const latestVersion = '1.2.3-beta';

const filteredVersions = filterVersions(
config,
currentVersion,
latestVersion,
releases,
nodeVersioning,
);

expect(filteredVersions).toEqual([{ version: '1.2.3-beta' }]);
});
});
});
24 changes: 14 additions & 10 deletions lib/workers/repository/process/lookup/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,28 @@ export function filterVersions(
return filteredReleases.filter((r) => isReleaseStable(r, versioning));
}

// if current is unstable then allow unstable in the current major only
// Allow unstable only in current major
const currentMajor = versioning.getMajor(currentVersion);
const currentMinor = versioning.getMinor(currentVersion);
const currentPatch = versioning.getPatch(currentVersion);

return filteredReleases.filter((r) => {
if (isReleaseStable(r, versioning)) {
return true;
}
if (
versioning.getMajor(r.version) !== versioning.getMajor(currentVersion)
) {

const major = versioning.getMajor(r.version);

if (major !== currentMajor) {
return false;
}
// istanbul ignore if: test passes without touching this

if (versioning.allowUnstableMajorUpgrades) {
return true;
}
return (
versioning.getMinor(r.version) === versioning.getMinor(currentVersion) &&
versioning.getPatch(r.version) === versioning.getPatch(currentVersion)
);

const minor = versioning.getMinor(r.version);
const patch = versioning.getPatch(r.version);

return minor === currentMinor && patch === currentPatch;
});
}
Loading