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): add toml support #28594

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions lib/modules/datasource/custom/formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JSONFetcher } from './json';
import { PlainFetcher } from './plain';
import type { CustomDatasourceFetcher } from './types';
import { YamlFetcher } from './yaml';
import { TomlFetcher } from "./toml";

export const fetchers: Record<
CustomDatasourceFormats,
Expand All @@ -12,5 +13,6 @@ export const fetchers: Record<
html: new HtmlFetcher(),
json: new JSONFetcher(),
plain: new PlainFetcher(),
toml: new TomlFetcher(),
yaml: new YamlFetcher(),
};
23 changes: 23 additions & 0 deletions lib/modules/datasource/custom/formats/toml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readLocalFile } from '../../../../util/fs';
import { parse as parseToml } from '../../../../util/toml';
import type { Http } from '../../../../util/http';
import type { CustomDatasourceFetcher } from './types';

export class TomlFetcher implements CustomDatasourceFetcher {
async fetch(http: Http, registryURL: string): Promise<unknown> {
const response = await http.getToml(registryURL)

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

return parseToml(response.body);
}

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

return parseToml(fileContent!);
}
}
87 changes: 86 additions & 1 deletion lib/modules/datasource/custom/index.spec.ts
Copy link
Member

Choose a reason for hiding this comment

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

We should split this file because it's becoming too big. Should of cause be done in a separate PR.

Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('modules/datasource/custom/index', () => {
expect(result).toBeNull();
});

it('return releases for yaml API directly exposing in Renovate format', async () => {
it('returns releases for yaml API directly exposing in Renovate format', async () => {
const expected = {
releases: [
{
Expand Down Expand Up @@ -309,6 +309,91 @@ describe('modules/datasource/custom/index', () => {
expect(result).toEqual(expected);
});

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

const toml = codeBlock`
[[releases]]
version = "1.0.0"

[[releases]]
version = "2.0.0"

[[releases]]
version = "3.0.0"
`;

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

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

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

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

fs.readLocalFile.mockResolvedValueOnce(codeBlock`
[[releases]]
version = "1.0.0"

[[releases]]
version = "2.0.0"

[[releases]]
version = "3.0.0"
`);

const result = await getPkgReleases({
datasource: `${CustomDatasource.id}.foo`,
packageName: 'myPackage',
customDatasources: {
foo: {
defaultRegistryUrlTemplate: 'file://test.toml',
format: 'toml',
},
},
});

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


it('return releases for json file directly exposing in Renovate format', async () => {
const expected = {
releases: [
Expand Down
40 changes: 38 additions & 2 deletions lib/modules/datasource/custom/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ This example shows how to update the `k3s.version` file with a custom datasource
Options:

| option | default | description |
| -------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| -------------------------- | -------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| defaultRegistryUrlTemplate | `""` | URL used if no `registryUrl` is provided when looking up new releases |
| format | `"json"` | format used by the API. Available values are: `json`, `plain`, `yaml`, `html` |
| format | `"json"` | format used by the API. Available values are: `json`, `plain`, `yaml`, `html`, `toml` |
| transformTemplates | `[]` | [JSONata rules](https://docs.jsonata.org/simple) to transform the API output. Each rule will be evaluated after another and the result will be used as input to the next |

Available template variables:
Expand Down Expand Up @@ -153,6 +153,42 @@ When Renovate receives this response with the `yaml` 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.

#### TOML

If `toml` is used, response is parsed and converted into TOML for further processing.

The below TOML document

```toml
[[releases]]
version = "1.0.0"

[[releases]]
version = "2.0.0"

[[releases]]
version = "3.0.0"
```

Will convert applying any `jsonata` rules defined in the `transformTemplates` section will be applied.

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


#### HTML

If the format is set to `html`, Renovate will call the HTTP endpoint with the `Accept` header value `text/html`.
Expand Down
10 changes: 10 additions & 0 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
});
}

async getToml(url: string, options?: Opts): Promise<HttpResponse> {
Copy link
Member

Choose a reason for hiding this comment

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

Needs a separate test in lib/util/http/index.spec.ts

It should also parse the toml and support optional schema validation. please do it like the getJson.
It should be a separate PR.

Copy link
Author

Choose a reason for hiding this comment

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

Gotcha, I understand now how the JSON side works. Just to make sure I understand, you want two PRs.

Copy link
Member

Choose a reason for hiding this comment

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

yes, the split can be done last after this pr

Copy link
Author

Choose a reason for hiding this comment

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

I did some more digging and had some more downtime to look into this.

I may be missing something, but I can't do it precisely like getJson since the internals of lib.util.http delegate to got for parsing the body.

The best I can do is to use a more basic type like text in the InternalHttpOptions.responseType, and then once I have something back from await this.request<ResT>(url, opts) I can parse the TOML and then optionally apply the schema.

Still working on the util.http.index.spec but it feels like I am finally making progress with my understanding of this area.

It appears some of the other JSON support floating around is for interfacing with other systems / clients.

Copy link
Member

Choose a reason for hiding this comment

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

yes, do the parsing in Http class

const opt = options ?? {};
return await this.get(url, {
headers: {
Accept: 'application/toml'
},
...opt,
})
}

getJson<ResT>(url: string, options?: Opts): Promise<HttpResponse<ResT>>;
getJson<ResT, Schema extends ZodType<ResT> = ZodType<ResT>>(
url: string,
Expand Down