Skip to content

Commit

Permalink
feat(datasource/custom): allow to fetch from YAML data endpoint (#25695)
Browse files Browse the repository at this point in the history
  • Loading branch information
nSimonFR committed Nov 13, 2023
1 parent 2ce4fc5 commit 53401eb
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/config/types.ts
Expand Up @@ -264,7 +264,7 @@ export interface RenovateConfig

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

Expand Down
9 changes: 9 additions & 0 deletions lib/modules/datasource/custom/formats/yaml.ts
@@ -0,0 +1,9 @@
import yaml from 'js-yaml';

import type { Http } from '../../../../util/http';

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

return yaml.load(response.body);
}
41 changes: 41 additions & 0 deletions lib/modules/datasource/custom/index.spec.ts
@@ -1,3 +1,4 @@
import { codeBlock } from 'common-tags';
import { getPkgReleases } from '..';
import * as httpMock from '../../../../test/http-mock';
import { CustomDatasource } from './index';
Expand Down Expand Up @@ -198,6 +199,46 @@ describe('modules/datasource/custom/index', () => {
expect(result).toBeNull();
});

it('return releases for yaml API directly exposing in Renovate format', async () => {
const expected = {
releases: [
{
version: '1.0.0',
},
{
version: '2.0.0',
},
{
version: '3.0.0',
},
],
};

const yaml = codeBlock`
releases:
- version: 1.0.0
- version: 2.0.0
- version: 3.0.0
`;

httpMock.scope('https://example.com').get('/v1').reply(200, yaml, {
'Content-Type': 'text/yaml',
});

const result = await getPkgReleases({
datasource: `${CustomDatasource.id}.foo`,
packageName: 'myPackage',
customDatasources: {
foo: {
defaultRegistryUrlTemplate: 'https://example.com/v1',
format: 'yaml',
},
},
});

expect(result).toEqual(expected);
});

it('return release when templating registryUrl', async () => {
const expected = {
releases: [
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/datasource/custom/index.ts
Expand Up @@ -4,6 +4,7 @@ import { logger } from '../../../logger';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { fetch as plainFetch } from './formats/plain';
import { fetch as yamlFetch } from './formats/yaml';
import { ReleaseResultZodSchema } from './schema';
import { getCustomConfig } from './utils';

Expand Down Expand Up @@ -31,6 +32,9 @@ export class CustomDatasource extends Datasource {
case 'plain':
response = await plainFetch(this.http, defaultRegistryUrlTemplate);
break;
case 'yaml':
response = await yamlFetch(this.http, defaultRegistryUrlTemplate);
break;
case 'json':
response = (await this.http.getJson(defaultRegistryUrlTemplate)).body;
}
Expand Down
33 changes: 33 additions & 0 deletions lib/modules/datasource/custom/readme.md
Expand Up @@ -118,6 +118,39 @@ When Renovate receives this response with the `plain` format, it will convert it

After the conversion, any `jsonata` rules defined in the `transformTemplates` section will be applied as usual to further process the JSON data.

### Yaml

If `yaml` is used, response is parsed and converted into JSON for further processing.

Suppose the body of the HTTP response is as follows:

```yaml
releases:
- version: 1.0.0
- version: 2.0.0
- version: 3.0.0
```

When Renovate receives this response with the `yaml` format, it will convert it into the following:

```json
{
"releases": [
{
"version": "1.0.0"
},
{
"version": "2.0.0"
},
{
"version": "3.0.0"
}
]
}
```

After the conversion, any `jsonata` rules defined in the `transformTemplates` section will be applied as usual to further process the JSON data.

## Examples

### K3s
Expand Down

0 comments on commit 53401eb

Please sign in to comment.