diff --git a/lib/versioning/nuget/index.ts b/lib/versioning/nuget/index.ts index bb0e3ebb7e56734bc31329feb885de0bf60950ca..64e9c74f5d5aeeea509340e52b02dd703317f07d 100644 --- a/lib/versioning/nuget/index.ts +++ b/lib/versioning/nuget/index.ts @@ -13,7 +13,7 @@ export const supportsRanges = false; const pattern = regEx(/^(\d+(?:\.\d+)*)(-[^+]+)?(\+.*)?$/); class NugetVersioningApi extends GenericVersioningApi { - protected _parse(version: string): GenericVersion { + protected _parse(version: string): GenericVersion | null { const matches = pattern.exec(version); if (!matches) { return null; diff --git a/lib/versioning/regex/index.spec.ts b/lib/versioning/regex/index.spec.ts index de59d98bd10ba3030234987bc52fe65830a9a87d..de2e577bfd88bafa1a1a759f27191c0cf0a8b6aa 100644 --- a/lib/versioning/regex/index.spec.ts +++ b/lib/versioning/regex/index.spec.ts @@ -247,6 +247,7 @@ describe('versioning/regex/index', () => { ${['2.1.5', '2.1.6a1', '2.1.6', '2.1.6-foo']} | ${'2.1.6-foo'} | ${'2.1.6'} ${['2.1.5-foo', '2.1.6']} | ${'2.1.6-foo'} | ${'2.1.6'} ${['1.2.3', '1.2.4']} | ${'3.5.0'} | ${null} + ${['1.2.3', '1.2.4']} | ${'!@#'} | ${null} `( 'getSatisfyingVersion($versions, "$range") === $expected', ({ versions, range, expected }) => { @@ -260,6 +261,7 @@ describe('versioning/regex/index', () => { ${['2.1.5', '2.1.6a1', '2.1.6', '2.1.6-foo']} | ${'2.1.6-foo'} | ${'2.1.6'} ${['2.1.5', '2.1.6-foo']} | ${'2.1.5-foo'} | ${'2.1.5'} ${['1.2.3', '1.2.4']} | ${'3.5.0'} | ${null} + ${['1.2.3', '1.2.4']} | ${'!@#'} | ${null} `( 'minSatisfyingVersion($versions, "$range") === "$expected"', ({ versions, range, expected }) => { diff --git a/lib/versioning/regex/index.ts b/lib/versioning/regex/index.ts index 782aad65cfbfbdeb002eb94aa6b4f6808411b68f..eba5c900edf312dda029e01d46545a0c43665d78 100644 --- a/lib/versioning/regex/index.ts +++ b/lib/versioning/regex/index.ts @@ -39,11 +39,11 @@ export class RegExpVersioningApi extends GenericVersioningApi<RegExpVersion> { // RegExp('^(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)(?<prerelease>[^.-]+)?(-(?<compatibility>.*))?$'); // * matches the versioning approach used by the Bitnami images on DockerHub: // RegExp('^(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)(:?-(?<compatibility>.*-r)(?<build>\\d+))?$'); - private _config: RegExp = null; + private _config: RegExp | null = null; - constructor(_new_config: string) { + constructor(_new_config: string | undefined) { super(); - const new_config = _new_config || '^(?<major>\\d+)?$'; + const new_config = _new_config ?? '^(?<major>\\d+)?$'; // without at least one of {major, minor, patch} specified in the regex, // this versioner will not work properly @@ -66,66 +66,93 @@ export class RegExpVersioningApi extends GenericVersioningApi<RegExpVersion> { // convenience method for passing a string into a Version given current config. protected _parse(version: string): RegExpVersion | null { - const match = this._config.exec(version); - if (match === null) { + const groups = this._config?.exec(version)?.groups; + if (!groups) { return null; } - const groups = match.groups; + const { major, minor, patch, build, prerelease, compatibility } = groups; const release = [ - typeof groups.major === 'undefined' ? 0 : Number(groups.major), - typeof groups.minor === 'undefined' ? 0 : Number(groups.minor), - typeof groups.patch === 'undefined' ? 0 : Number(groups.patch), + typeof major === 'undefined' ? 0 : Number.parseInt(major, 10), + typeof minor === 'undefined' ? 0 : Number.parseInt(minor, 10), + typeof patch === 'undefined' ? 0 : Number.parseInt(patch, 10), ]; - if (groups.build) { - release.push(Number(groups.build)); + if (build) { + release.push(Number.parseInt(build, 10)); } return { release, - prerelease: groups.prerelease, - compatibility: groups.compatibility, + prerelease: prerelease, + compatibility: compatibility, }; } override isCompatible(version: string, current: string): boolean { - return ( - this._parse(version).compatibility === this._parse(current).compatibility + const parsedVersion = this._parse(version); + const parsedCurrent = this._parse(current); + return !!( + parsedVersion && + parsedCurrent && + parsedVersion.compatibility === parsedCurrent.compatibility ); } override isLessThanRange(version: string, range: string): boolean { - return semver.ltr( - asSemver(this._parse(version)), - asSemver(this._parse(range)) + const parsedVersion = this._parse(version); + const parsedRange = this._parse(range); + return !!( + parsedVersion && + parsedRange && + semver.ltr(asSemver(parsedVersion), asSemver(parsedRange)) ); } + private _getSemverVersions(versions: string[]): string[] { + const parsedVersions: string[] = []; + versions.forEach((v) => { + const parsedVersion = this._parse(v); + if (parsedVersion) { + parsedVersions.push(asSemver(parsedVersion)); + } + }); + return parsedVersions; + } + override getSatisfyingVersion( versions: string[], range: string ): string | null { - return semver.maxSatisfying( - versions.map((v) => asSemver(this._parse(v))), - asSemver(this._parse(range)) - ); + const parsedRange = this._parse(range); + return parsedRange + ? semver.maxSatisfying( + this._getSemverVersions(versions), + asSemver(parsedRange) + ) + : null; } override minSatisfyingVersion( versions: string[], range: string ): string | null { - return semver.minSatisfying( - versions.map((v) => asSemver(this._parse(v))), - asSemver(this._parse(range)) - ); + const parsedRange = this._parse(range); + return parsedRange + ? semver.minSatisfying( + this._getSemverVersions(versions), + asSemver(parsedRange) + ) + : null; } override matches(version: string, range: string): boolean { - return semver.satisfies( - asSemver(this._parse(version)), - asSemver(this._parse(range)) + const parsedVersion = this._parse(version); + const parsedRange = this._parse(range); + return !!( + parsedVersion && + parsedRange && + semver.satisfies(asSemver(parsedVersion), asSemver(parsedRange)) ); } } diff --git a/tsconfig.strict.json b/tsconfig.strict.json index 791fd35945013cc8ea98fa8f1b4cdb0894a4384c..5a35b006c4955f4c39d93deac43a1df068e0483f 100644 --- a/tsconfig.strict.json +++ b/tsconfig.strict.json @@ -395,11 +395,9 @@ "lib/versioning/hashicorp/index.ts", "lib/versioning/helm/index.ts", "lib/versioning/index.ts", - "lib/versioning/nuget/index.ts", "lib/versioning/poetry/index.ts", "lib/versioning/poetry/patterns.ts", "lib/versioning/poetry/transform.ts", - "lib/versioning/regex/index.ts", "lib/versioning/types.ts", "lib/workers/branch/artifacts.ts", "lib/workers/branch/auto-replace.ts",