refactor(maven): Clarify HTTP resource check function (#12640)

This commit is contained in:
Sergei Zharinov 2021-11-14 14:16:54 +03:00 committed by GitHub
parent 811f81d3a2
commit 60d03add71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 15 deletions

View file

@ -10,11 +10,11 @@ import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { MAVEN_REPO } from './common';
import type { MavenDependency, ReleaseMap } from './types';
import {
checkHttpResource,
downloadMavenXml,
getDependencyInfo,
getDependencyParts,
getMavenUrl,
isHttpResourceExists,
} from './util';
export { id } from './common';
@ -198,19 +198,21 @@ async function addReleasesUsingHeadRequests(
repoUrl
);
const artifactUrl = getMavenUrl(dependency, repoUrl, pomUrl);
const res = await isHttpResourceExists(artifactUrl);
const release: Release = { version };
if (is.string(res)) {
release.releaseTimestamp = res;
}
const res = await checkHttpResource(artifactUrl);
// Retry earlier for error status other than 404
if (res === null) {
if (res === 'error') {
retryEarlier = true;
}
workingReleaseMap[version] = res ? release : null;
if (is.date(res)) {
release.releaseTimestamp = res.toISOString();
}
if (res !== 'not-found' && res !== 'error') {
workingReleaseMap[version] = release;
}
}
);

View file

@ -14,3 +14,5 @@ export interface MavenXml {
}
export type ReleaseMap = Record<string, Release | null>;
export type HttpResourceCheckResult = 'found' | 'not-found' | 'error' | Date;

View file

@ -1,14 +1,20 @@
import url from 'url';
import { DateTime } from 'luxon';
import { XmlDocument } from 'xmldoc';
import { HOST_DISABLED } from '../../constants/error-messages';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { Http, HttpResponse } from '../../util/http';
import { regEx } from '../../util/regex';
import { normalizeDate } from '../metadata';
import type { ReleaseResult } from '../types';
import { MAVEN_REPO, id } from './common';
import type { MavenDependency, MavenXml } from './types';
import type {
HttpResourceCheckResult,
MavenDependency,
MavenXml,
} from './types';
const http: Record<string, Http> = {};
@ -100,18 +106,27 @@ export async function downloadHttpProtocol(
}
}
export async function isHttpResourceExists(
export async function checkHttpResource(
pkgUrl: url.URL | string,
hostType = id
): Promise<boolean | string | null> {
): Promise<HttpResourceCheckResult> {
try {
const httpClient = httpByHostType(hostType);
const res = await httpClient.head(pkgUrl.toString());
const timestamp = res?.headers?.['last-modified'] as string;
return timestamp || true;
if (timestamp) {
const isoTimestamp = normalizeDate(timestamp);
if (isoTimestamp) {
const releaseDate = DateTime.fromISO(isoTimestamp, {
zone: 'UTC',
}).toJSDate();
return releaseDate;
}
}
return 'found';
} catch (err) {
if (isNotFoundError(err)) {
return false;
return 'not-found';
}
const failedUrl = pkgUrl.toString();
@ -119,7 +134,7 @@ export async function isHttpResourceExists(
{ failedUrl, statusCode: err.statusCode },
`Can't check HTTP resource existence`
);
return null;
return 'error';
}
}

View file

@ -126,7 +126,7 @@ function massageGitlabUrl(url: string): string {
.replace('.git', '');
}
function normalizeDate(input: any): string | null {
export function normalizeDate(input: any): string | null {
if (
typeof input === 'number' &&
!Number.isNaN(input) &&