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 downloadHttpProtocol return type #31641

Merged
merged 3 commits into from
Sep 26, 2024
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
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