Skip to content

Commit

Permalink
refactor(custom/datasource): prepare for additional format types (#25640
Browse files Browse the repository at this point in the history
)
  • Loading branch information
secustor committed Nov 8, 2023
1 parent 6b46237 commit d10dcff
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
23 changes: 23 additions & 0 deletions lib/modules/datasource/custom/formats/plain.ts
@@ -0,0 +1,23 @@
import type { Http } from '../../../../util/http';
import { newlineRegex } from '../../../../util/regex';
import type { ReleaseResult } from '../../types';

export async function fetch(
http: Http,
url: string,
): Promise<ReleaseResult | null> {
const response = await http.getPlain(url);
const contentType = response.headers['content-type'];
if (!contentType?.startsWith('text/')) {
return null;
}
const lines = response.body.split(newlineRegex).map((line) => line.trim());

const versions = lines.map((value) => {
return { version: value };
});

return {
releases: versions,
};
}
52 changes: 9 additions & 43 deletions lib/modules/datasource/custom/index.ts
@@ -1,11 +1,11 @@
import is from '@sindresorhus/is';
import jsonata from 'jsonata';
import { logger } from '../../../logger';
import { newlineRegex } from '../../../util/regex';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { fetch as plainFetch } from './formats/plain';
import { ReleaseResultZodSchema } from './schema';
import { massageCustomDatasourceConfig } from './utils';
import { getCustomConfig } from './utils';

export class CustomDatasource extends Datasource {
static readonly id = 'custom';
Expand All @@ -19,34 +19,20 @@ export class CustomDatasource extends Datasource {
async getReleases(
getReleasesConfig: GetReleasesConfig,
): Promise<ReleaseResult | null> {
const customDatasourceName = getReleasesConfig.datasource?.replace(
'custom.',
'',
);

if (!is.nonEmptyString(customDatasourceName)) {
logger.debug(
`No datasource has been supplied while looking up ${getReleasesConfig.packageName}`,
);
return null;
}

const config = massageCustomDatasourceConfig(
customDatasourceName,
getReleasesConfig,
);
const config = getCustomConfig(getReleasesConfig);
if (is.nullOrUndefined(config)) {
return null;
}

const { defaultRegistryUrlTemplate, transformTemplates, format } = config;
// TODO add here other format options than JSON and "plain"
let response: unknown;
try {
if (format === 'plain') {
response = await this.fetchPlainFormat(defaultRegistryUrlTemplate);
} else {
response = (await this.http.getJson(defaultRegistryUrlTemplate)).body;
switch (format) {
case 'plain':
response = await plainFetch(this.http, defaultRegistryUrlTemplate);
break;
case 'json':
response = (await this.http.getJson(defaultRegistryUrlTemplate)).body;
}
} catch (e) {
this.handleHttpErrors(e);
Expand All @@ -69,24 +55,4 @@ export class CustomDatasource extends Datasource {
return null;
}
}

private async fetchPlainFormat(url: string): Promise<unknown> {
const response = await this.http.get(url, {
headers: {
Accept: 'text/plain',
},
});
const contentType = response.headers['content-type'];
if (!contentType?.startsWith('text/')) {
return null;
}
const versions = response.body.split(newlineRegex).map((version) => {
return {
version: version.trim(),
};
});
return {
releases: versions,
};
}
}
18 changes: 18 additions & 0 deletions lib/modules/datasource/custom/utils.ts
Expand Up @@ -44,3 +44,21 @@ export function massageCustomDatasourceConfig(
transformTemplates: transform,
};
}

export function getCustomConfig(
getReleasesConfig: GetReleasesConfig,
): Required<CustomDatasourceConfig> | null {
const customDatasourceName = getReleasesConfig.datasource?.replace(
'custom.',
'',
);

if (!is.nonEmptyString(customDatasourceName)) {
logger.debug(
`No datasource has been supplied while looking up ${getReleasesConfig.packageName}`,
);
return null;
}

return massageCustomDatasourceConfig(customDatasourceName, getReleasesConfig);
}
3 changes: 1 addition & 2 deletions lib/modules/platform/gitlab/index.ts
Expand Up @@ -18,8 +18,7 @@ import {
import { logger } from '../../../logger';
import type { BranchStatus } from '../../../types';
import { coerceArray } from '../../../util/array';
import { noLeadingAtSymbol } from '../../../util/common';
import { parseJson } from '../../../util/common';
import { noLeadingAtSymbol, parseJson } from '../../../util/common';
import * as git from '../../../util/git';
import * as hostRules from '../../../util/host-rules';
import { setBaseUrl } from '../../../util/http/gitlab';
Expand Down
10 changes: 10 additions & 0 deletions lib/util/http/index.ts
Expand Up @@ -332,6 +332,16 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
return res;
}

async getPlain(url: string, options?: Opts): Promise<HttpResponse> {
const opt = options ?? {};
return await this.get(url, {
headers: {
Accept: 'text/plain',
},
...opt,
});
}

getJson<ResT>(
url: string,
options?: Opts & HttpRequestOptions<ResT>,
Expand Down

0 comments on commit d10dcff

Please sign in to comment.