Skip to content

Commit

Permalink
Simpify code
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Nov 21, 2024
1 parent 774451a commit 06f6d75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
34 changes: 11 additions & 23 deletions lib/modules/datasource/maven/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -76,15 +76,15 @@ export class MavenDatasource extends Datasource {
super(id);
}

async fetchReleasesFromMetadata(
async fetchVersionsFromMetadata(
dependency: MavenDependency,
repoUrl: string,
): Promise<ReleaseMap> {
): Promise<string[]> {
const metadataUrl = getMavenUrl(dependency, repoUrl, 'maven-metadata.xml');

const cacheNamespace = 'datasource-maven:metadata-xml';
const cacheKey = metadataUrl.toString();
const cachedVersions = await packageCache.get<ReleaseMap>(
const cacheKey = `v2:${metadataUrl}`;
const cachedVersions = await packageCache.get<string[]>(
cacheNamespace,
cacheKey,
);
Expand All @@ -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({
Expand All @@ -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}`,
Expand Down
3 changes: 0 additions & 3 deletions lib/modules/datasource/maven/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { XmlDocument } from 'xmldoc';
import type { Release } from '../types';

export interface MavenDependency {
display: string;
Expand All @@ -13,6 +12,4 @@ export interface MavenXml {
xml?: XmlDocument;
}

export type ReleaseMap = Record<string, Release | null>;

export type HttpResourceCheckResult = 'found' | 'not-found' | 'error' | Date;

0 comments on commit 06f6d75

Please sign in to comment.