From 06f6d759a8139a6f416f74bf4ff14089a2f92be7 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Thu, 21 Nov 2024 17:12:05 -0300 Subject: [PATCH] Simpify code --- lib/modules/datasource/maven/index.ts | 34 +++++++++------------------ lib/modules/datasource/maven/types.ts | 3 --- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/lib/modules/datasource/maven/index.ts b/lib/modules/datasource/maven/index.ts index 7affe848ffabf0..9c50e90a95b185 100644 --- a/lib/modules/datasource/maven/index.ts +++ b/lib/modules/datasource/maven/index.ts @@ -18,7 +18,7 @@ import type { ReleaseResult, } from '../types'; import { MAVEN_REPO } from './common'; -import type { MavenDependency, ReleaseMap } from './types'; +import type { MavenDependency } from './types'; import { checkResource, createUrlForDependencyPom, @@ -76,15 +76,15 @@ export class MavenDatasource extends Datasource { super(id); } - async fetchReleasesFromMetadata( + async fetchVersionsFromMetadata( dependency: MavenDependency, repoUrl: string, - ): Promise { + ): Promise { const metadataUrl = getMavenUrl(dependency, repoUrl, 'maven-metadata.xml'); const cacheNamespace = 'datasource-maven:metadata-xml'; - const cacheKey = metadataUrl.toString(); - const cachedVersions = await packageCache.get( + const cacheKey = `v2:${metadataUrl}`; + const cachedVersions = await packageCache.get( cacheNamespace, cacheKey, ); @@ -98,30 +98,18 @@ export class MavenDatasource extends Datasource { metadataUrl, ); if (!mavenMetadata) { - return {}; + return []; } const versions = extractVersions(mavenMetadata); - const releaseMap = versions.reduce( - (acc, version) => ({ ...acc, [version]: null }), - {}, - ); const cachePrivatePackages = GlobalConfig.get( 'cachePrivatePackages', false, ); if (cachePrivatePackages || isCacheable) { - await packageCache.set(cacheNamespace, cacheKey, releaseMap, 30); - } - return releaseMap; - } - - getReleasesFromMap(releaseMap: ReleaseMap): Release[] { - const releases = Object.values(releaseMap).filter(is.truthy); - if (releases.length) { - return releases; + await packageCache.set(cacheNamespace, cacheKey, versions, 30); } - return Object.keys(releaseMap).map((version) => ({ version })); + return versions; } async getReleases({ @@ -138,14 +126,14 @@ export class MavenDatasource extends Datasource { logger.debug(`Looking up ${dependency.display} in repository ${repoUrl}`); - const releaseMap = await this.fetchReleasesFromMetadata( + const metadataVersions = await this.fetchVersionsFromMetadata( dependency, repoUrl, ); - const releases = this.getReleasesFromMap(releaseMap); - if (!releases?.length) { + if (!metadataVersions?.length) { return null; } + const releases = metadataVersions.map((version) => ({ version })); logger.debug( `Found ${releases.length} new releases for ${dependency.display} in repository ${repoUrl}`, diff --git a/lib/modules/datasource/maven/types.ts b/lib/modules/datasource/maven/types.ts index 1a24f174d36ecb..e9dc4b49da7567 100644 --- a/lib/modules/datasource/maven/types.ts +++ b/lib/modules/datasource/maven/types.ts @@ -1,5 +1,4 @@ import type { XmlDocument } from 'xmldoc'; -import type { Release } from '../types'; export interface MavenDependency { display: string; @@ -13,6 +12,4 @@ export interface MavenXml { xml?: XmlDocument; } -export type ReleaseMap = Record; - export type HttpResourceCheckResult = 'found' | 'not-found' | 'error' | Date;