Skip to content

Commit

Permalink
fix: GenericJSON updater uses regex to find version in matching entry (
Browse files Browse the repository at this point in the history
…#2253)

Co-authored-by: Jeff Ching <chingor@google.com>
  • Loading branch information
0xdbe and chingor13 authored Sep 11, 2024
1 parent eb968c8 commit acc3242
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
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

0 comments on commit acc3242

Please sign in to comment.