diff --git a/lib/datasource/maven/index.ts b/lib/datasource/maven/index.ts index 5d6666377c755905bec0372edbf3fb000a2298cb..acbc41ab7141f6027b824711d8b6c5bf3fae4fb2 100644 --- a/lib/datasource/maven/index.ts +++ b/lib/datasource/maven/index.ts @@ -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; + } } ); diff --git a/lib/datasource/maven/types.ts b/lib/datasource/maven/types.ts index 780360f247bc0c8c2348aba9d5ef441ea4bd9a50..c66c311f1604b82eea4a28759924089b71431b44 100644 --- a/lib/datasource/maven/types.ts +++ b/lib/datasource/maven/types.ts @@ -14,3 +14,5 @@ export interface MavenXml { } export type ReleaseMap = Record<string, Release | null>; + +export type HttpResourceCheckResult = 'found' | 'not-found' | 'error' | Date; diff --git a/lib/datasource/maven/util.ts b/lib/datasource/maven/util.ts index 1a637553523eb085194de7d89021a4836cf92fa7..a0e60610143242e746750485fecdf5d104da23a7 100644 --- a/lib/datasource/maven/util.ts +++ b/lib/datasource/maven/util.ts @@ -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'; } } diff --git a/lib/datasource/metadata.ts b/lib/datasource/metadata.ts index 25103f1927058ae2e46a4936969fd28d689935b7..95b611da71d0c8b5f2271ec9031a4dbb52a87178 100644 --- a/lib/datasource/metadata.ts +++ b/lib/datasource/metadata.ts @@ -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) &&