Skip to content

Commit

Permalink
feat(jsonata): support yaml (#33972)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jan 31, 2025
1 parent 9e51d42 commit e63ff23
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
18 changes: 17 additions & 1 deletion docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ It will be compiled using Handlebars and the regex `groups` result.
It specifies the syntax of the package file that's managed by the custom `jsonata` manager.
This setting helps the system correctly parse and interpret the configuration file's contents.

Only the `json` format is supported.
Only the `json` and `yaml` format is supported.
`yaml` files are parsed as multi document YAML files.

```json title="Parsing a JSON file with a custom manager"
{
Expand All @@ -866,6 +867,21 @@ Only the `json` format is supported.
}
```

```json title="Parsing a YAML file with a custom manager"
{
"customManagers": [
{
"customType": "jsonata",
"fileFormat": "yaml",
"fileMatch": ["file.yml"],
"matchStrings": [
"packages.{ \"depName\": package, \"currentValue\": version }"
]
}
]
}
```

### matchStrings

Each `matchStrings` must be one of the following:
Expand Down
2 changes: 1 addition & 1 deletion lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ const options: RenovateOptions[] = [
description:
'It specifies the syntax of the package file being managed by the custom JSONata manager.',
type: 'string',
allowedValues: ['json'],
allowedValues: ['json', 'yaml'],
parents: ['customManagers'],
cli: false,
env: false,
Expand Down
52 changes: 52 additions & 0 deletions lib/modules/manager/custom/jsonata/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,58 @@ describe('modules/manager/custom/jsonata/index', () => {
});
});

it('extracts yaml', async () => {
const json = codeBlock`
---
packages:
-
"dep_name": "foo"
"package_name": "fii"
"current_value": "1.2.3"
"current_digest": "1234"
"data_source": "nuget"
"versioning": "maven"
"extract_version": "custom-extract-version"
"registry_url": "https://registry.npmjs.org"
"dep_type": "dev"
---
some: true
`;
const config = {
fileFormat: 'yaml',
matchStrings: [
`packages.{
"depName": dep_name,
"packageName": package_name,
"currentValue": current_value,
"currentDigest": current_digest,
"datasource": data_source,
"versioning": versioning,
"extractVersion": extract_version,
"registryUrl": registry_url,
"depType": dep_type
}`,
],
};
const res = await extractPackageFile(json, 'unused', config);

expect(res).toMatchObject({
deps: [
{
depName: 'foo',
packageName: 'fii',
currentValue: '1.2.3',
currentDigest: '1234',
datasource: 'nuget',
versioning: 'maven',
extractVersion: 'custom-extract-version',
registryUrls: ['https://registry.npmjs.org/'],
depType: 'dev',
},
],
});
});

it('applies templates', async () => {
const json = codeBlock`
{
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/manager/custom/jsonata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import is from '@sindresorhus/is';
import type { Category } from '../../../../constants';
import { logger } from '../../../../logger';
import { parseJson } from '../../../../util/common';
import { parseYaml } from '../../../../util/yaml';
import type { PackageFileContent } from '../../types';
import type { JsonataExtractConfig } from './types';
import { handleMatching } from './utils';
Expand All @@ -25,6 +26,9 @@ export async function extractPackageFile(
case 'json':
json = parseJson(content, packageFile);
break;
case 'yaml':
json = parseYaml(content);
break;
}
} catch (err) {
logger.debug(
Expand Down
34 changes: 32 additions & 2 deletions lib/modules/manager/custom/jsonata/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
With `customManagers` using `JSONata` queries you can configure Renovate so it finds dependencies in JSON files, that are not detected by its other built-in package managers.
With `customManagers` using `JSONata` queries you can configure Renovate so it finds dependencies in JSON or YAML files, that are not detected by its other built-in package managers.

Renovate uses the `jsonata` package to process the `json` file content using the queries.
Renovate uses the `jsonata` package to process the `json` or `yaml` file content using the queries.

For more on the jsonata query language, read the [jsonata query language site](https://docs.jsonata.org/overview.html).

Expand Down Expand Up @@ -87,6 +87,8 @@ We recommend you follow these steps:

Alternatively you can "try and error" to a working config, by adjusting our examples.

YAML files are parsed as multi document files.

#### Example queries

Below are some example queries for the generic JSON manager.
Expand Down Expand Up @@ -201,3 +203,31 @@ $map($map(packages, function ($v) { $split($v, "@") }), function ($v) { { "depNa
"datasourceTemplate": "npm"
}
```

```yaml title="Dependencies in a single node, and we want to extract all of them"
packages:
- version: 1.2.3
package: foo
```
Query:
```
packages.{ "depName": package, "currentValue": version }
```
```yaml title="Dependencies in a single node in a multi document yaml, and we want to extract all of them"
packages:
- version: 1.2.3
package: foo
---
packages:
- version: 1.2.5
package: bar
```
Query:
```
packages.{ "depName": package, "currentValue": version }
```

0 comments on commit e63ff23

Please sign in to comment.