Skip to content

Commit

Permalink
refactor: Reuse datasource filtering (#23716)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Aug 7, 2023
1 parent 6c42022 commit 17bf7d7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
23 changes: 10 additions & 13 deletions lib/modules/datasource/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,28 @@ describe('modules/datasource/common', () => {

describe('applyExtractVersion', () => {
it('should return the same release result if extractVersion is not defined', () => {
const config = {};
const releaseResult: ReleaseResult = {
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
};
const res = applyExtractVersion(config, releaseResult);
const res = applyExtractVersion(releaseResult, undefined);
expect(res).toBe(releaseResult);
});

it('should extract version from release using provided regex', () => {
const config = { extractVersion: '^v(?<version>.+)$' };
const releaseResult: ReleaseResult = {
releases: [{ version: 'v1.0.0' }, { version: 'v2.0.0' }],
};
const res = applyExtractVersion(config, releaseResult);
const res = applyExtractVersion(releaseResult, '^v(?<version>.+)$');
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});

it('should return null for releases with invalid version', () => {
const config = { extractVersion: '^v(?<version>.+)$' };
const releaseResult: ReleaseResult = {
releases: [{ version: 'v1.0.0' }, { version: 'invalid' }],
};
const result = applyExtractVersion(config, releaseResult);
const result = applyExtractVersion(releaseResult, '^v(?<version>.+)$');
expect(result).toEqual({
releases: [{ version: '1.0.0' }],
});
Expand All @@ -131,23 +128,23 @@ describe('modules/datasource/common', () => {

it('should filter out invalid versions', () => {
const config = { datasource: 'npm' };
const res = filterValidVersions(config, releaseResult);
const res = filterValidVersions(releaseResult, config);
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});

it('should use default versioning if none is specified', () => {
const config = { datasource: 'foobar' };
const res = filterValidVersions(config, releaseResult);
const res = filterValidVersions(releaseResult, config);
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});

it('should use specified versioning if provided', () => {
const config = { datasource: 'npm', versioning: 'semver' };
const res = filterValidVersions(config, releaseResult);
const res = filterValidVersions(releaseResult, config);
expect(res).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
Expand All @@ -172,7 +169,7 @@ describe('modules/datasource/common', () => {
{ version: '3.0.0' },
],
};
const result = sortAndRemoveDuplicates(config, releaseResult);
const result = sortAndRemoveDuplicates(releaseResult, config);
expect(result).toEqual(expected);
});

Expand All @@ -181,7 +178,7 @@ describe('modules/datasource/common', () => {
const releaseResult: ReleaseResult = {
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
};
const result = sortAndRemoveDuplicates(config, releaseResult);
const result = sortAndRemoveDuplicates(releaseResult, config);
expect(result).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
Expand All @@ -205,7 +202,7 @@ describe('modules/datasource/common', () => {
{ version: '2.0.0', constraints: { foo: ['^2.0.0'] } },
],
};
expect(applyConstraintsFiltering(config, releaseResult)).toEqual({
expect(applyConstraintsFiltering(releaseResult, config)).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
Expand All @@ -224,7 +221,7 @@ describe('modules/datasource/common', () => {
{ version: '3.0.0', constraints: { baz: ['^0.9.0'] } },
],
};
expect(applyConstraintsFiltering(config, releaseResult)).toEqual({
expect(applyConstraintsFiltering(releaseResult, config)).toEqual({
releases: [{ version: '1.0.0' }, { version: '2.0.0' }],
});
});
Expand Down
17 changes: 9 additions & 8 deletions lib/modules/datasource/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ export function isGetPkgReleasesConfig(
);
}

export function applyExtractVersion<
Config extends Pick<GetPkgReleasesConfig, 'extractVersion'>
>(config: Config, releaseResult: ReleaseResult): ReleaseResult {
if (!config.extractVersion) {
export function applyExtractVersion(
releaseResult: ReleaseResult,
extractVersion: string | undefined
): ReleaseResult {
if (!extractVersion) {
return releaseResult;
}

const extractVersionRegEx = regEx(config.extractVersion);
const extractVersionRegEx = regEx(extractVersion);
releaseResult.releases = filterMap(releaseResult.releases, (release) => {
const version = extractVersionRegEx.exec(release.version)?.groups?.version;
if (!version) {
Expand All @@ -76,7 +77,7 @@ export function applyExtractVersion<

export function filterValidVersions<
Config extends Pick<GetPkgReleasesConfig, 'versioning' | 'datasource'>
>(config: Config, releaseResult: ReleaseResult): ReleaseResult {
>(releaseResult: ReleaseResult, config: Config): ReleaseResult {
const versioningName =
config.versioning ?? getDefaultVersioning(config.datasource);
const versioning = allVersioning.get(versioningName);
Expand All @@ -90,7 +91,7 @@ export function filterValidVersions<

export function sortAndRemoveDuplicates<
Config extends Pick<GetPkgReleasesConfig, 'versioning' | 'datasource'>
>(config: Config, releaseResult: ReleaseResult): ReleaseResult {
>(releaseResult: ReleaseResult, config: Config): ReleaseResult {
const versioningName =
config.versioning ?? getDefaultVersioning(config.datasource);
const versioning = allVersioning.get(versioningName);
Expand Down Expand Up @@ -121,7 +122,7 @@ export function applyConstraintsFiltering<
| 'constraints'
| 'packageName'
>
>(config: Config, releaseResult: ReleaseResult): ReleaseResult {
>(releaseResult: ReleaseResult, config: Config): ReleaseResult {
if (config?.constraintsFiltering !== 'strict') {
for (const release of releaseResult.releases) {
delete release.constraints;
Expand Down
21 changes: 15 additions & 6 deletions lib/modules/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async function fetchReleases(
return dep;
}

function getRawReleases(
function fetchCachedReleases(
config: GetReleasesInternalConfig
): Promise<ReleaseResult | null> {
const { datasource, packageName, registryUrls } = config;
Expand Down Expand Up @@ -345,7 +345,7 @@ export function getRawPkgReleases(
return AsyncResult.err('no-package-name');
}

return Result.wrapNullable(getRawReleases(config), 'no-result' as const)
return Result.wrapNullable(fetchCachedReleases(config), 'no-result' as const)
.catch((e) => {
if (e instanceof ExternalHostError) {
e.hostType = config.datasource;
Expand All @@ -356,14 +356,23 @@ export function getRawPkgReleases(
.transform(clone);
}

export function applyDatasourceFilters(
releaseResult: ReleaseResult,
config: GetPkgReleasesConfig
): ReleaseResult {
let res = releaseResult;
res = applyExtractVersion(res, config.extractVersion);
res = filterValidVersions(res, config);
res = sortAndRemoveDuplicates(res, config);
res = applyConstraintsFiltering(res, config);
return res;
}

export async function getPkgReleases(
config: GetPkgReleasesConfig
): Promise<ReleaseResult | null> {
const { val = null, err } = await getRawPkgReleases(config)
.transform((res) => applyExtractVersion(config, res))
.transform((res) => filterValidVersions(config, res))
.transform((res) => sortAndRemoveDuplicates(config, res))
.transform((res) => applyConstraintsFiltering(config, res))
.transform((res) => applyDatasourceFilters(res, config))
.unwrap();

if (err instanceof Error) {
Expand Down
25 changes: 13 additions & 12 deletions lib/workers/repository/process/lookup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { logger } from '../../../../logger';
import {
Release,
ReleaseResult,
applyDatasourceFilters,
getDigest,
getPkgReleases,
getRawPkgReleases,
isGetPkgReleasesConfig,
supportsDigests,
} from '../../../../modules/datasource';
Expand All @@ -19,10 +20,8 @@ import { getRangeStrategy } from '../../../../modules/manager';
import * as allVersioning from '../../../../modules/versioning';
import { ExternalHostError } from '../../../../types/errors/external-host-error';
import { assignKeys } from '../../../../util/assign-keys';
import { clone } from '../../../../util/clone';
import { applyPackageRules } from '../../../../util/package-rules';
import { regEx } from '../../../../util/regex';
import { Result } from '../../../../util/result';
import { getBucket } from './bucket';
import { getCurrentVersion } from './current';
import { filterVersions } from './filter';
Expand Down Expand Up @@ -84,14 +83,17 @@ export async function lookupUpdates(
return res;
}

const { val: lookupValue, err: lookupError } = await Result.wrapNullable(
getPkgReleases(config),
'no-releases' as const
const { val: releaseResult, err: lookupError } = await getRawPkgReleases(
config
)
.transform((x) => Result.ok(clone(x)))
.transform((res) => applyDatasourceFilters(res, config))
.unwrap();

if (lookupError === 'no-releases') {
if (lookupError instanceof Error) {
throw lookupError;
}

if (lookupError) {
// If dependency lookup fails then warn and return
const warning: ValidationMessage = {
topic: packageName,
Expand All @@ -101,18 +103,17 @@ export async function lookupUpdates(
// TODO: return warnings in own field
res.warnings.push(warning);
return res;
} else if (lookupError) {
throw lookupError;
}

dependency = lookupValue;
dependency = releaseResult;

if (dependency.deprecationMessage) {
logger.debug(
`Found deprecationMessage for ${datasource} package ${packageName}`
);
}

assignKeys(res, lookupValue, [
assignKeys(res, dependency, [
'deprecationMessage',
'sourceUrl',
'registryUrl',
Expand Down
1 change: 1 addition & 0 deletions lib/workers/repository/process/lookup/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface LookupUpdateConfig
replacementName?: string;
replacementNameTemplate?: string;
replacementVersion?: string;
extractVersion?: string;
}

export interface UpdateResult {
Expand Down

0 comments on commit 17bf7d7

Please sign in to comment.