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: Simplify lambda function in lookup result filtering #30018

Merged
Merged
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
39 changes: 21 additions & 18 deletions lib/workers/repository/process/lookup/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import * as poetryVersioning from '../../../../modules/versioning/poetry';
import { getRegexPredicate } from '../../../../util/string-match';
import type { FilterConfig } from './types';

function isReleaseStable(release: Release, versioning: VersioningApi): boolean {
if (!versioning.isStable(release.version)) {
return false;
}

if (release.isStable === false) {
return false;
}

return true;
}

export function filterVersions(
config: FilterConfig,
currentVersion: string,
Expand All @@ -18,17 +30,7 @@ export function filterVersions(
): Release[] {
const { ignoreUnstable, ignoreDeprecated, respectLatest, allowedVersions } =
config;
function isVersionStable(version: string): boolean {
if (!versioning.isStable(version)) {
return false;
}
// Check if the datasource returned isStable = false
const release = releases.find((r) => r.version === version);
if (release?.isStable === false) {
return false;
}
return true;
}

// istanbul ignore if: shouldn't happen
if (!currentVersion) {
return [];
Expand Down Expand Up @@ -125,18 +127,19 @@ export function filterVersions(
return filteredVersions;
}

if (isVersionStable(currentVersion)) {
return filteredVersions.filter((v) => isVersionStable(v.version));
const currentRelease = releases.find((r) => r.version === currentVersion);
if (currentRelease && isReleaseStable(currentRelease, versioning)) {
return filteredVersions.filter((r) => isReleaseStable(r, versioning));
}

// if current is unstable then allow unstable in the current major only
// Allow unstable only in current major
return filteredVersions.filter((v) => {
if (isVersionStable(v.version)) {
return filteredVersions.filter((r) => {
if (isReleaseStable(r, versioning)) {
return true;
}
if (
versioning.getMajor(v.version) !== versioning.getMajor(currentVersion)
versioning.getMajor(r.version) !== versioning.getMajor(currentVersion)
) {
return false;
}
Expand All @@ -145,8 +148,8 @@ export function filterVersions(
return true;
}
return (
versioning.getMinor(v.version) === versioning.getMinor(currentVersion) &&
versioning.getPatch(v.version) === versioning.getPatch(currentVersion)
versioning.getMinor(r.version) === versioning.getMinor(currentVersion) &&
versioning.getPatch(r.version) === versioning.getPatch(currentVersion)
);
});
}