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

feat(datasource/custom): allow to fetch from YAML data endpoint #25695

Merged
merged 4 commits into from
Nov 13, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/config/types.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be at h4 instead of h3 to be on the same as JSON and plain.

Line 13 misses yaml as a valid option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to open a PR to fix it 🤗


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