Skip to content

Commit

Permalink
refactor(datasource/custom): use class based fetcher to simplify code (
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Nov 25, 2023
1 parent 3228c12 commit 95ad0d0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 60 deletions.
5 changes: 4 additions & 1 deletion lib/config/types.ts
Expand Up @@ -262,9 +262,12 @@ export interface RenovateConfig
customizeDashboard?: Record<string, string>;
}

const CustomDatasourceFormats = ['json', 'plain', 'yaml'] as const;
export type CustomDatasourceFormats = (typeof CustomDatasourceFormats)[number];

export interface CustomDatasourceConfig {
defaultRegistryUrlTemplate?: string;
format?: 'json' | 'plain' | 'yaml';
format?: CustomDatasourceFormats;
transformTemplates?: string[];
}

Expand Down
14 changes: 14 additions & 0 deletions lib/modules/datasource/custom/formats/index.ts
@@ -0,0 +1,14 @@
import type { CustomDatasourceFormats } from '../../../../config/types';
import { JSONFetcher } from './json';
import { PlainFetcher } from './plain';
import type { CustomDatasourceFetcher } from './types';
import { YamlFetcher } from './yaml';

export const fetchers: Record<
CustomDatasourceFormats,
CustomDatasourceFetcher
> = {
json: new JSONFetcher(),
plain: new PlainFetcher(),
yaml: new YamlFetcher(),
};
17 changes: 10 additions & 7 deletions lib/modules/datasource/custom/formats/json.ts
@@ -1,13 +1,16 @@
import { readLocalFile } from '../../../../util/fs';
import type { Http } from '../../../../util/http';
import type { CustomDatasourceFetcher } from './types';

export async function fetch(http: Http, url: string): Promise<unknown> {
const response = await http.getJson(url);
return response.body;
}
export class JSONFetcher implements CustomDatasourceFetcher {
async fetch(http: Http, registryURL: string): Promise<unknown> {
const response = await http.getJson(registryURL);
return response.body;
}

export async function read(path: string): Promise<unknown> {
const fileContent = await readLocalFile(path, 'utf8');
async readFile(registryURL: string): Promise<unknown> {
const fileContent = await readLocalFile(registryURL, 'utf8');

return JSON.parse(fileContent!);
return JSON.parse(fileContent!);
}
}
28 changes: 14 additions & 14 deletions lib/modules/datasource/custom/formats/plain.ts
Expand Up @@ -2,6 +2,7 @@ import { readLocalFile } from '../../../../util/fs';
import type { Http } from '../../../../util/http';
import { newlineRegex } from '../../../../util/regex';
import type { ReleaseResult } from '../../types';
import type { CustomDatasourceFetcher } from './types';

function convertLinesToVersions(content: string): ReleaseResult {
const lines = content.split(newlineRegex).map((line) => line.trim());
Expand All @@ -15,22 +16,21 @@ function convertLinesToVersions(content: string): ReleaseResult {
};
}

export async function fetch(
http: Http,
url: string,
): Promise<ReleaseResult | null> {
const response = await http.getPlain(url);
export class PlainFetcher implements CustomDatasourceFetcher {
async fetch(http: Http, registryURL: string): Promise<unknown> {
const response = await http.getPlain(registryURL);

const contentType = response.headers['content-type'];
if (!contentType?.startsWith('text/')) {
return null;
}
const contentType = response.headers['content-type'];
if (!contentType?.startsWith('text/')) {
return null;
}

return convertLinesToVersions(response.body);
}
return convertLinesToVersions(response.body);
}

export async function read(path: string): Promise<ReleaseResult | null> {
const fileContent = await readLocalFile(path, 'utf8');
async readFile(registryURL: string): Promise<unknown> {
const fileContent = await readLocalFile(registryURL, 'utf8');

return fileContent ? convertLinesToVersions(fileContent) : null;
return fileContent ? convertLinesToVersions(fileContent) : null;
}
}
6 changes: 6 additions & 0 deletions lib/modules/datasource/custom/formats/types.ts
@@ -0,0 +1,6 @@
import type { Http } from '../../../../util/http';

export interface CustomDatasourceFetcher {
fetch(http: Http, registryURL: string): Promise<unknown>;
readFile(registryURL: string): Promise<unknown>;
}
17 changes: 10 additions & 7 deletions lib/modules/datasource/custom/formats/yaml.ts
@@ -1,15 +1,18 @@
import yaml from 'js-yaml';
import { readLocalFile } from '../../../../util/fs';
import type { Http } from '../../../../util/http';
import type { CustomDatasourceFetcher } from './types';

export async function fetch(http: Http, url: string): Promise<unknown> {
const response = await http.get(url);
export class YamlFetcher implements CustomDatasourceFetcher {
async fetch(http: Http, registryURL: string): Promise<unknown> {
const response = await http.get(registryURL);

return yaml.load(response.body);
}
return yaml.load(response.body);
}

export async function read(path: string): Promise<unknown> {
const fileContent = await readLocalFile(path, 'utf8');
async readFile(registryURL: string): Promise<unknown> {
const fileContent = await readLocalFile(registryURL, 'utf8');

return yaml.load(fileContent!);
return yaml.load(fileContent!);
}
}
41 changes: 10 additions & 31 deletions lib/modules/datasource/custom/index.ts
Expand Up @@ -3,9 +3,7 @@ import jsonata from 'jsonata';
import { logger } from '../../../logger';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { fetch as jsonFetch, read as jsonRead } from './formats/json';
import { fetch as plainFetch, read as plainRead } from './formats/plain';
import { fetch as yamlFetch, read as yamlRead } from './formats/yaml';
import { fetchers } from './formats';
import { ReleaseResultZodSchema } from './schema';
import { getCustomConfig } from './utils';

Expand All @@ -28,38 +26,19 @@ export class CustomDatasource extends Datasource {

const { defaultRegistryUrlTemplate, transformTemplates, format } = config;

const fetcher = fetchers[format];
const isLocalRegistry = defaultRegistryUrlTemplate.startsWith('file://');

let data: unknown;
if (isLocalRegistry) {
switch (format) {
case 'plain':
data = await plainRead(defaultRegistryUrlTemplate);
break;
case 'yaml':
data = await yamlRead(defaultRegistryUrlTemplate);
break;
case 'json':
data = await jsonRead(defaultRegistryUrlTemplate);
break;
}
} else {
try {
switch (format) {
case 'plain':
data = await plainFetch(this.http, defaultRegistryUrlTemplate);
break;
case 'yaml':
data = await yamlFetch(this.http, defaultRegistryUrlTemplate);
break;
case 'json':
data = await jsonFetch(this.http, defaultRegistryUrlTemplate);
break;
}
} catch (e) {
this.handleHttpErrors(e);
return null;
try {
if (isLocalRegistry) {
data = await fetcher.readFile(defaultRegistryUrlTemplate);
} else {
data = await fetcher.fetch(this.http, defaultRegistryUrlTemplate);
}
} catch (e) {
this.handleHttpErrors(e);
return null;
}

for (const transformTemplate of transformTemplates) {
Expand Down

0 comments on commit 95ad0d0

Please sign in to comment.