Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(maven): Simplify metadata representation #32786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,31 +98,19 @@ 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);
await packageCache.set(cacheNamespace, cacheKey, versions, 30);
}
return releaseMap;
}

getReleasesFromMap(releaseMap: ReleaseMap): Release[] {
const releases = Object.values(releaseMap).filter(is.truthy);
// istanbul ignore if: will be removed
if (releases.length) {
return releases;
}
return Object.keys(releaseMap).map((version) => ({ version }));
return versions;
}

async getReleases({
Expand All @@ -139,14 +127,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
4 changes: 1 addition & 3 deletions lib/modules/datasource/maven/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { XmlDocument } from 'xmldoc';
import type { Release, ReleaseResult } from '../types';
import type { ReleaseResult } from '../types';

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

export type ReleaseMap = Record<string, Release | null>;
viceice marked this conversation as resolved.
Show resolved Hide resolved

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

export type DependencyInfo = Pick<
Expand Down