Skip to content

Commit

Permalink
chore: ensure versioning/versioningApi used consistently (#31856)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored Oct 8, 2024
1 parent 32ecb4c commit c4d4658
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 142 deletions.
6 changes: 3 additions & 3 deletions lib/util/exec/containerbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ export function isDynamicInstall(

function isStable(
version: string,
versioning: allVersioning.VersioningApi,
versioningApi: allVersioning.VersioningApi,
latest?: string,
): boolean {
if (!versioning.isStable(version)) {
if (!versioningApi.isStable(version)) {
return false;
}
if (is.string(latest)) {
if (versioning.isGreaterThan(version, latest)) {
if (versioningApi.isGreaterThan(version, latest)) {
return false;
}
}
Expand Down
30 changes: 17 additions & 13 deletions lib/util/exec/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GlobalConfig } from '../../../config/global';
import { SYSTEM_INSUFFICIENT_MEMORY } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { getPkgReleases } from '../../../modules/datasource';
import * as versioning from '../../../modules/versioning';
import * as allVersioning from '../../../modules/versioning';
import { newlineRegex, regEx } from '../../regex';
import { uniq } from '../../uniq';
import { rawExec } from '../common';
Expand Down Expand Up @@ -76,41 +76,45 @@ function prepareCommands(commands: Opt<string>[]): string[] {
export async function getDockerTag(
packageName: string,
constraint: string,
scheme: string,
versioning: string,
): Promise<string> {
const ver = versioning.get(scheme);
const versioningApi = allVersioning.get(versioning);

if (!ver.isValid(constraint)) {
if (!versioningApi.isValid(constraint)) {
logger.warn(
{ scheme, constraint },
{ versioning, constraint },
`Invalid Docker image version constraint`,
);
return 'latest';
}

logger.debug(
{ packageName, scheme, constraint },
{ packageName, versioning, constraint },
`Found version constraint - checking for a compatible image to use`,
);
const imageReleases = await getPkgReleases({
datasource: 'docker',
packageName,
versioning: scheme,
versioning,
});
if (imageReleases?.releases) {
let versions = imageReleases.releases.map((release) => release.version);
versions = versions.filter(
(version) => ver.isVersion(version) && ver.matches(version, constraint),
(version) =>
versioningApi.isVersion(version) &&
versioningApi.matches(version, constraint),
);
// Prefer stable versions over unstable, even if the range satisfies both types
if (!versions.every((version) => ver.isStable(version))) {
if (!versions.every((version) => versioningApi.isStable(version))) {
logger.debug('Filtering out unstable versions');
versions = versions.filter((version) => ver.isStable(version));
versions = versions.filter((version) => versioningApi.isStable(version));
}
const version = versions.sort(ver.sortVersions.bind(ver)).pop();
const version = versions
.sort(versioningApi.sortVersions.bind(versioningApi))
.pop();
if (version) {
logger.debug(
{ packageName, scheme, constraint, version },
{ packageName, versioning, constraint, version },
`Found compatible image version`,
);
return version;
Expand All @@ -120,7 +124,7 @@ export async function getDockerTag(
return 'latest';
}
logger.warn(
{ packageName, constraint, scheme },
{ packageName, constraint, versioning },
'Failed to find a tag satisfying constraint, using "latest" tag instead',
);
return 'latest';
Expand Down
14 changes: 7 additions & 7 deletions lib/util/package-rules/current-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class CurrentVersionMatcher extends Matcher {
}
const isUnconstrainedValue =
!!lockedVersion && is.nullOrUndefined(currentValue);
const version = allVersioning.get(versioning);
const versioningApi = allVersioning.get(versioning);
const matchCurrentVersionStr = matchCurrentVersion.toString();
const matchCurrentVersionPred = getRegexPredicate(matchCurrentVersionStr);

Expand All @@ -31,29 +31,29 @@ export class CurrentVersionMatcher extends Matcher {
matchCurrentVersionPred(compareVersion)
);
}
if (version.isVersion(matchCurrentVersionStr)) {
if (versioningApi.isVersion(matchCurrentVersionStr)) {
try {
return (
isUnconstrainedValue ||
!!(
currentValue &&
version.isValid(currentValue) &&
version.matches(matchCurrentVersionStr, currentValue)
versioningApi.isValid(currentValue) &&
versioningApi.matches(matchCurrentVersionStr, currentValue)
)
);
} catch {
return false;
}
}

const compareVersion = version.isVersion(currentValue)
const compareVersion = versioningApi.isVersion(currentValue)
? currentValue // it's a version so we can match against it
: (lockedVersion ?? currentVersion); // need to match against this currentVersion, if available
if (is.nullOrUndefined(compareVersion)) {
return false;
}
if (version.isVersion(compareVersion)) {
return version.matches(compareVersion, matchCurrentVersion);
if (versioningApi.isVersion(compareVersion)) {
return versioningApi.matches(compareVersion, matchCurrentVersion);
}
logger.debug(
{ matchCurrentVersionStr, currentValue },
Expand Down
6 changes: 3 additions & 3 deletions lib/workers/repository/init/vulnerability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ export async function detectVulnerabilityAlerts(
};
const alertDetails = combinedAlerts[fileName][datasource][depName];
alertDetails.advisories.push(advisory);
const version = allVersioning.get(versionings[datasource]);
if (version.isVersion(firstPatchedVersion)) {
const versioningApi = allVersioning.get(versionings[datasource]);
if (versioningApi.isVersion(firstPatchedVersion)) {
if (
!alertDetails.firstPatchedVersion ||
version.isGreaterThan(
versioningApi.isGreaterThan(
firstPatchedVersion,
alertDetails.firstPatchedVersion,
)
Expand Down
12 changes: 6 additions & 6 deletions lib/workers/repository/process/lookup/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getBucket(
config: BucketConfig,
currentVersion: string,
newVersion: string,
versioning: VersioningApi,
versioningApi: VersioningApi,
): string | null {
const {
separateMajorMinor,
Expand All @@ -22,8 +22,8 @@ export function getBucket(
if (!separateMajorMinor) {
return 'latest';
}
const fromMajor = versioning.getMajor(currentVersion);
const toMajor = versioning.getMajor(newVersion);
const fromMajor = versioningApi.getMajor(currentVersion);
const toMajor = versioningApi.getMajor(newVersion);

// istanbul ignore if: error case
if (toMajor === null) {
Expand All @@ -41,8 +41,8 @@ export function getBucket(

// If we reach here then we know it's non-major

const fromMinor = versioning.getMinor(currentVersion);
const toMinor = versioning.getMinor(newVersion);
const fromMinor = versioningApi.getMinor(currentVersion);
const toMinor = versioningApi.getMinor(newVersion);

// istanbul ignore if: error case
if (fromMinor === null || toMinor === null) {
Expand All @@ -66,7 +66,7 @@ export function getBucket(

/* future option
if (separateMultiplePatch) {
const toPatch = versioning.getPatch(newVersion);
const toPatch = versioningApi.getPatch(newVersion);
if (toPatch !== null && separateMultiplePatch) {
return `v${toMajor}.${toMinor}.${toPatch}`;
}
Expand Down
18 changes: 9 additions & 9 deletions lib/workers/repository/process/lookup/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { regEx } from '../../../../util/regex';
export function getCurrentVersion(
currentValue: string,
lockedVersion: string,
versioning: VersioningApi,
versioningApi: VersioningApi,
rangeStrategy: string,
latestVersion: string,
allVersions: string[],
Expand All @@ -20,39 +20,39 @@ export function getCurrentVersion(
return currentValue;
}
let useVersions = allVersions.filter((v) =>
versioning.matches(v, currentValue),
versioningApi.matches(v, currentValue),
);
if (useVersions.length === 1) {
return useVersions[0];
}
if (latestVersion && versioning.matches(latestVersion, currentValue)) {
if (latestVersion && versioningApi.matches(latestVersion, currentValue)) {
useVersions = useVersions.filter(
(v) => !versioning.isGreaterThan(v, latestVersion),
(v) => !versioningApi.isGreaterThan(v, latestVersion),
);
}
if (rangeStrategy === 'pin') {
return (
lockedVersion ||
versioning.getSatisfyingVersion(useVersions, currentValue)
versioningApi.getSatisfyingVersion(useVersions, currentValue)
);
}
if (rangeStrategy === 'bump') {
// Use the lowest version in the current range
return versioning.minSatisfyingVersion(useVersions, currentValue);
return versioningApi.minSatisfyingVersion(useVersions, currentValue);
}
// Use the highest version in the current range
const satisfyingVersion = versioning.getSatisfyingVersion(
const satisfyingVersion = versioningApi.getSatisfyingVersion(
useVersions,
currentValue,
);
if (satisfyingVersion) {
return satisfyingVersion;
}

if (versioning.isVersion(currentValue)) {
if (versioningApi.isVersion(currentValue)) {
return currentValue;
}
if (versioning.isSingleVersion(currentValue)) {
if (versioningApi.isSingleVersion(currentValue)) {
return currentValue.replace(regEx(/=/g), '').trim();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/process/lookup/filter-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface InternalChecksResult {

export async function filterInternalChecks(
config: Partial<LookupUpdateConfig & UpdateResult>,
versioning: VersioningApi,
versioningApi: VersioningApi,
bucket: string,
sortedReleases: Release[],
): Promise<InternalChecksResult> {
Expand All @@ -43,7 +43,7 @@ export async function filterInternalChecks(
// calculate updateType and then apply it
releaseConfig.updateType = getUpdateType(
releaseConfig,
versioning,
versioningApi,
// TODO #22198
currentVersion!,
candidateRelease.version,
Expand Down
51 changes: 27 additions & 24 deletions lib/workers/repository/process/lookup/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ 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)) {
function isReleaseStable(
release: Release,
versioningApi: VersioningApi,
): boolean {
if (!versioningApi.isStable(release.version)) {
return false;
}

Expand All @@ -26,7 +29,7 @@ export function filterVersions(
currentVersion: string,
latestVersion: string,
releases: Release[],
versioning: VersioningApi,
versioningApi: VersioningApi,
): Release[] {
const { ignoreUnstable, ignoreDeprecated, respectLatest, allowedVersions } =
config;
Expand All @@ -39,17 +42,17 @@ export function filterVersions(
// Leave only versions greater than current
let filteredReleases = releases.filter(
(r) =>
versioning.isVersion(r.version) &&
versioning.isGreaterThan(r.version, currentVersion),
versioningApi.isVersion(r.version) &&
versioningApi.isGreaterThan(r.version, currentVersion),
);

const currentRelease = releases.find(
(r) =>
versioning.isValid(r.version) &&
versioning.isVersion(r.version) &&
versioning.isValid(currentVersion) &&
versioning.isVersion(currentVersion) &&
versioning.equals(r.version, currentVersion),
versioningApi.isValid(r.version) &&
versioningApi.isVersion(r.version) &&
versioningApi.isValid(currentVersion) &&
versioningApi.isVersion(currentVersion) &&
versioningApi.equals(r.version, currentVersion),
);

// Don't upgrade from non-deprecated to deprecated
Expand All @@ -71,9 +74,9 @@ export function filterVersions(
filteredReleases = filteredReleases.filter(({ version }) =>
isAllowedPred(version),
);
} else if (versioning.isValid(allowedVersions)) {
} else if (versioningApi.isValid(allowedVersions)) {
filteredReleases = filteredReleases.filter((r) =>
versioning.matches(r.version, allowedVersions),
versioningApi.matches(r.version, allowedVersions),
);
} else if (
config.versioning !== npmVersioning.id &&
Expand Down Expand Up @@ -122,42 +125,42 @@ export function filterVersions(
if (
respectLatest &&
latestVersion &&
!versioning.isGreaterThan(currentVersion, latestVersion)
!versioningApi.isGreaterThan(currentVersion, latestVersion)
) {
filteredReleases = filteredReleases.filter(
(r) => !versioning.isGreaterThan(r.version, latestVersion),
(r) => !versioningApi.isGreaterThan(r.version, latestVersion),
);
}

if (!ignoreUnstable) {
return filteredReleases;
}

if (currentRelease && isReleaseStable(currentRelease, versioning)) {
return filteredReleases.filter((r) => isReleaseStable(r, versioning));
if (currentRelease && isReleaseStable(currentRelease, versioningApi)) {
return filteredReleases.filter((r) => isReleaseStable(r, versioningApi));
}

const currentMajor = versioning.getMajor(currentVersion);
const currentMinor = versioning.getMinor(currentVersion);
const currentPatch = versioning.getPatch(currentVersion);
const currentMajor = versioningApi.getMajor(currentVersion);
const currentMinor = versioningApi.getMinor(currentVersion);
const currentPatch = versioningApi.getPatch(currentVersion);

return filteredReleases.filter((r) => {
if (isReleaseStable(r, versioning)) {
if (isReleaseStable(r, versioningApi)) {
return true;
}

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

if (major !== currentMajor) {
return false;
}

if (versioning.allowUnstableMajorUpgrades) {
if (versioningApi.allowUnstableMajorUpgrades) {
return true;
}

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

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

0 comments on commit c4d4658

Please sign in to comment.