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

fix: use regex to replace version in GenericJSON updater #2253

Merged
merged 2 commits into from
Sep 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions __snapshots__/generic-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ exports['GenericJson updateContent updates matching entry 1'] = `
}

`

exports['GenericJson updateContent updates substring in matching entry 1'] = `
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"github>abc/foo:xyz/sub#2.3.4",
"github>abc/bar:xyz/sub#2.3.4"
]
}

`
15 changes: 13 additions & 2 deletions src/updaters/generic-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import * as jp from 'jsonpath';
import {jsonStringify} from '../util/json-stringify';
import {logger as defaultLogger, Logger} from '../util/logger';

const VERSION_REGEX =
/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-(?<preRelease>[\w.]+))?(\+(?<build>[-\w.]+))?/;

export class GenericJson implements Updater {
readonly jsonpath: string;
readonly version: Version;
Expand All @@ -33,8 +36,16 @@ export class GenericJson implements Updater {
*/
updateContent(content: string, logger: Logger = defaultLogger): string {
const data = JSON.parse(content);
const nodes = jp.apply(data, this.jsonpath, _val => {
return this.version.toString();
const nodes = jp.apply(data, this.jsonpath, value => {
if (typeof value !== 'string') {
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
return value;
}
if (!value.match(VERSION_REGEX)) {
logger.warn(`No version found in ${this.jsonpath}. Skipping.`);
return value;
}
return value.replace(VERSION_REGEX, this.version.toString());
});
if (!nodes) {
logger.warn(`No entries modified in ${this.jsonpath}`);
Expand Down
7 changes: 7 additions & 0 deletions test/updaters/fixtures/renovate-shared-preset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"github>abc/foo:xyz/sub#1.2.3",
"github>abc/bar:xyz/sub#1.2.3"
]
}
27 changes: 27 additions & 0 deletions test/updaters/generic-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ describe('GenericJson', () => {
const newContent = updater.updateContent(oldContent);
snapshot(newContent);
});
it('updates substring in matching entry', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './renovate-shared-preset.json'),
'utf8'
).replace(/\r\n/g, '\n');
const updater = new GenericJson('$.extends.*', Version.parse('v2.3.4'));
const newContent = updater.updateContent(oldContent);
snapshot(newContent);
});
it('ignores non-matching entry', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './esy.json'),
Expand All @@ -54,6 +63,24 @@ describe('GenericJson', () => {
const newContent = updater.updateContent(oldContent);
expect(newContent).to.eql(oldContent);
});
it('ignore array entry', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './renovate-shared-preset.json'),
'utf8'
).replace(/\r\n/g, '\n');
const updater = new GenericJson('$.extends', Version.parse('v2.3.4'));
const newContent = updater.updateContent(oldContent);
expect(newContent).to.eql(oldContent);
});
it('ignore non-matching string', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './esy.json'),
'utf8'
).replace(/\r\n/g, '\n');
const updater = new GenericJson('$.author', Version.parse('v2.3.4'));
const newContent = updater.updateContent(oldContent);
expect(newContent).to.eql(oldContent);
});
it('warns on invalid jsonpath', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './esy.json'),
Expand Down
Loading