Skip to content

Commit

Permalink
refactor(maven): Simplify downloadHttpProtocol return type (#31641)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Sep 26, 2024
1 parent 8cbe0f3 commit e7543eb
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 130 deletions.
31 changes: 18 additions & 13 deletions lib/modules/datasource/maven/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,24 @@ export class MavenDatasource extends Datasource {
try {
const indexUrl = getMavenUrl(dependency, repoUrl, 'index.html');
const res = await downloadHttpProtocol(this.http, indexUrl);
const { body = '' } = res;
for (const line of body.split(newlineRegex)) {
const match = line.trim().match(mavenCentralHtmlVersionRegex);
if (match) {
const { version, releaseTimestamp: timestamp } =
match?.groups ?? /* istanbul ignore next: hard to test */ {};
if (version && timestamp) {
const date = DateTime.fromFormat(timestamp, 'yyyy-MM-dd HH:mm', {
zone: 'UTC',
});
if (date.isValid) {
const releaseTimestamp = date.toISO();
workingReleaseMap[version] = { version, releaseTimestamp };
if (res) {
for (const line of res.body.split(newlineRegex)) {
const match = line.trim().match(mavenCentralHtmlVersionRegex);
if (match) {
const { version, releaseTimestamp: timestamp } =
match?.groups ?? /* istanbul ignore next: hard to test */ {};
if (version && timestamp) {
const date = DateTime.fromFormat(
timestamp,
'yyyy-MM-dd HH:mm',
{
zone: 'UTC',
},
);
if (date.isValid) {
const releaseTimestamp = date.toISO();
workingReleaseMap[version] = { version, releaseTimestamp };
}
}
}
}
Expand Down
65 changes: 45 additions & 20 deletions lib/modules/datasource/maven/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type Request from 'got/dist/source/core';
import { partial } from '../../../../test/util';
import { HOST_DISABLED } from '../../../constants/error-messages';
import type { Http } from '../../../util/http';
import { type Http, HttpError } from '../../../util/http';
import { parseUrl } from '../../../util/url';
import {
checkResource,
Expand All @@ -9,6 +10,38 @@ import {
downloadS3Protocol,
} from './util';

function httpError({
name,
message = 'unknown error',
code,
request = {},
response,
}: {
name?: string;
message?: string;
code?: HttpError['code'];
request?: Partial<Request>;
response?: Partial<Response>;
}): HttpError {
type Writeable<T> = { -readonly [P in keyof T]: T[P] };

const err = new HttpError(
message,
{ code },
request as never,
) as Writeable<HttpError>;

if (name) {
err.name = name;
}

if (response) {
err.response = response as never;
}

return err;
}

describe('modules/datasource/maven/util', () => {
describe('downloadMavenXml', () => {
it('returns empty object for unsupported protocols', async () => {
Expand Down Expand Up @@ -39,51 +72,43 @@ describe('modules/datasource/maven/util', () => {
describe('downloadHttpProtocol', () => {
it('returns empty for HOST_DISABLED error', async () => {
const http = partial<Http>({
get: () =>
Promise.reject(
Object.assign(new Error(), { message: HOST_DISABLED }),
),
get: () => Promise.reject(httpError({ message: HOST_DISABLED })),
});
const res = await downloadHttpProtocol(http, 'some://');
expect(res).toStrictEqual({});
expect(res).toBeNull();
});

it('returns empty for host error', async () => {
const http = partial<Http>({
get: () =>
Promise.reject(Object.assign(new Error(), { code: 'ETIMEDOUT' })),
get: () => Promise.reject(httpError({ code: 'ETIMEDOUT' })),
});
const res = await downloadHttpProtocol(http, 'some://');
expect(res).toStrictEqual({});
expect(res).toBeNull();
});

it('returns empty for temporal error', async () => {
it('returns empty for temporary error', async () => {
const http = partial<Http>({
get: () =>
Promise.reject(Object.assign(new Error(), { code: 'ECONNRESET' })),
get: () => Promise.reject(httpError({ code: 'ECONNRESET' })),
});
const res = await downloadHttpProtocol(http, 'some://');
expect(res).toStrictEqual({});
expect(res).toBeNull();
});

it('returns empty for connection error', async () => {
const http = partial<Http>({
get: () =>
Promise.reject(Object.assign(new Error(), { code: 'ECONNREFUSED' })),
get: () => Promise.reject(httpError({ code: 'ECONNREFUSED' })),
});
const res = await downloadHttpProtocol(http, 'some://');
expect(res).toStrictEqual({});
expect(res).toBeNull();
});

it('returns empty for unsupported error', async () => {
const http = partial<Http>({
get: () =>
Promise.reject(
Object.assign(new Error(), { name: 'UnsupportedProtocolError' }),
),
Promise.reject(httpError({ name: 'UnsupportedProtocolError' })),
});
const res = await downloadHttpProtocol(http, 'some://');
expect(res).toStrictEqual({});
expect(res).toBeNull();
});
});

Expand Down
Loading

0 comments on commit e7543eb

Please sign in to comment.