Skip to content
Snippets Groups Projects
Unverified Commit 80663fe1 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(maven): Enable strict null checks (#13980)

* refactor(maven): Enable strict null checks

* Fix name and cache

* Return destructuring
parent b05844bc
No related branches found
No related tags found
No related merge requests found
...@@ -114,7 +114,7 @@ async function addReleasesFromIndexPage( ...@@ -114,7 +114,7 @@ async function addReleasesFromIndexPage(
const match = line.trim().match(mavenCentralHtmlVersionRegex); const match = line.trim().match(mavenCentralHtmlVersionRegex);
if (match) { if (match) {
const { version, releaseTimestamp: timestamp } = const { version, releaseTimestamp: timestamp } =
match?.groups || {}; match?.groups ?? {};
if (version && timestamp) { if (version && timestamp) {
const date = DateTime.fromFormat(timestamp, 'yyyy-MM-dd HH:mm', { const date = DateTime.fromFormat(timestamp, 'yyyy-MM-dd HH:mm', {
zone: 'UTC', zone: 'UTC',
...@@ -234,19 +234,16 @@ async function addReleasesUsingHeadRequests( ...@@ -234,19 +234,16 @@ async function addReleasesUsingHeadRequests(
const cacheNs = 'datasource-maven:head-requests'; const cacheNs = 'datasource-maven:head-requests';
const cacheKey = `${repoUrl}${dependency.dependencyUrl}`; const cacheKey = `${repoUrl}${dependency.dependencyUrl}`;
let workingReleaseMap: ReleaseMap = await packageCache.get<ReleaseMap>( const oldReleaseMap: ReleaseMap | undefined =
cacheNs, await packageCache.get<ReleaseMap>(cacheNs, cacheKey);
cacheKey const newReleaseMap: ReleaseMap = oldReleaseMap ?? {};
);
if (!workingReleaseMap) {
workingReleaseMap = {};
if (!oldReleaseMap) {
const unknownVersions = Object.entries(releaseMap) const unknownVersions = Object.entries(releaseMap)
.filter(([version, release]) => { .filter(([version, release]) => {
const isDiscoveredOutside = !!release; const isDiscoveredOutside = !!release;
const isDiscoveredInsideAndCached = !is.undefined( const isDiscoveredInsideAndCached = !is.undefined(
workingReleaseMap[version] newReleaseMap[version]
); );
const isDiscovered = isDiscoveredOutside || isDiscoveredInsideAndCached; const isDiscovered = isDiscoveredOutside || isDiscoveredInsideAndCached;
return !isDiscovered; return !isDiscovered;
...@@ -276,26 +273,31 @@ async function addReleasesUsingHeadRequests( ...@@ -276,26 +273,31 @@ async function addReleasesUsingHeadRequests(
} }
if (res !== 'not-found' && res !== 'error') { if (res !== 'not-found' && res !== 'error') {
workingReleaseMap[version] = release; newReleaseMap[version] = release;
} }
} }
); );
await pAll(queue, { concurrency: 5 }); await pAll(queue, { concurrency: 5 });
const cacheTTL = retryEarlier ? 60 : 24 * 60; const cacheTTL = retryEarlier ? 60 : 24 * 60;
await packageCache.set(cacheNs, cacheKey, workingReleaseMap, cacheTTL); await packageCache.set(cacheNs, cacheKey, newReleaseMap, cacheTTL);
} }
} }
for (const version of Object.keys(releaseMap)) { for (const version of Object.keys(releaseMap)) {
releaseMap[version] ||= workingReleaseMap[version] ?? null; releaseMap[version] ||= newReleaseMap[version] ?? null;
} }
return releaseMap; return releaseMap;
} }
function getReleasesFromMap(releaseMap: ReleaseMap): Release[] { function getReleasesFromMap(releaseMap: ReleaseMap): Release[] {
const releases = Object.values(releaseMap).filter(Boolean); const releases: Release[] = [];
for (const release of Object.values(releaseMap)) {
if (release) {
releases.push(release);
}
}
if (releases.length) { if (releases.length) {
return releases; return releases;
} }
...@@ -306,6 +308,11 @@ export async function getReleases({ ...@@ -306,6 +308,11 @@ export async function getReleases({
lookupName, lookupName,
registryUrl, registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> { }: GetReleasesConfig): Promise<ReleaseResult | null> {
// istanbul ignore if
if (!registryUrl) {
return null;
}
const dependency = getDependencyParts(lookupName); const dependency = getDependencyParts(lookupName);
const repoUrl = ensureTrailingSlash(registryUrl); const repoUrl = ensureTrailingSlash(registryUrl);
......
...@@ -146,20 +146,20 @@ export function getMavenUrl( ...@@ -146,20 +146,20 @@ export function getMavenUrl(
dependency: MavenDependency, dependency: MavenDependency,
repoUrl: string, repoUrl: string,
path: string path: string
): url.URL | null { ): url.URL {
return new url.URL(`${dependency.dependencyUrl}/${path}`, repoUrl); return new url.URL(`${dependency.dependencyUrl}/${path}`, repoUrl);
} }
export async function downloadMavenXml( export async function downloadMavenXml(
pkgUrl: url.URL | null pkgUrl: url.URL | null
): Promise<MavenXml | null> { ): Promise<MavenXml> {
/* istanbul ignore if */ /* istanbul ignore if */
if (!pkgUrl) { if (!pkgUrl) {
return {}; return {};
} }
let rawContent: string; let rawContent: string | undefined;
let authorization: boolean; let authorization: boolean | undefined;
let statusCode: number; let statusCode: number | undefined;
switch (pkgUrl.protocol) { switch (pkgUrl.protocol) {
case 'http:': case 'http:':
case 'https:': case 'https:':
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
"lib/datasource/gitlab-tags/util.ts", "lib/datasource/gitlab-tags/util.ts",
"lib/datasource/helm/common.ts", "lib/datasource/helm/common.ts",
"lib/datasource/hex", "lib/datasource/hex",
"lib/datasource/maven",
"lib/datasource/metadata.ts", "lib/datasource/metadata.ts",
"lib/datasource/sbt-plugin/util.ts", "lib/datasource/sbt-plugin/util.ts",
"lib/globals.d.ts", "lib/globals.d.ts",
...@@ -83,6 +84,7 @@ ...@@ -83,6 +84,7 @@
"lib/datasource/go/types.ts", "lib/datasource/go/types.ts",
"lib/datasource/helm/common.ts", "lib/datasource/helm/common.ts",
"lib/datasource/hex/**/*.spec.ts", "lib/datasource/hex/**/*.spec.ts",
"lib/datasource/maven/*.spec.ts",
"lib/logger/err-serializer.spec.ts", "lib/logger/err-serializer.spec.ts",
"lib/util/cache/**/*.spec.ts", "lib/util/cache/**/*.spec.ts",
"lib/util/exec/buildpack.spec.ts", "lib/util/exec/buildpack.spec.ts",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment