Skip to content

Commit acc3242

Browse files
0xdbechingor13
andauthored
fix: GenericJSON updater uses regex to find version in matching entry (#2253)
Co-authored-by: Jeff Ching <chingor@google.com>
1 parent eb968c8 commit acc3242

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

__snapshots__/generic-json.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@ exports['GenericJson updateContent updates matching entry 1'] = `
4646
}
4747
4848
`
49+
50+
exports['GenericJson updateContent updates substring in matching entry 1'] = `
51+
{
52+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
53+
"extends": [
54+
"github>abc/foo:xyz/sub#2.3.4",
55+
"github>abc/bar:xyz/sub#2.3.4"
56+
]
57+
}
58+
59+
`

src/updaters/generic-json.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import * as jp from 'jsonpath';
1818
import {jsonStringify} from '../util/json-stringify';
1919
import {logger as defaultLogger, Logger} from '../util/logger';
2020

21+
const VERSION_REGEX =
22+
/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-(?<preRelease>[\w.]+))?(\+(?<build>[-\w.]+))?/;
23+
2124
export class GenericJson implements Updater {
2225
readonly jsonpath: string;
2326
readonly version: Version;
@@ -33,8 +36,16 @@ export class GenericJson implements Updater {
3336
*/
3437
updateContent(content: string, logger: Logger = defaultLogger): string {
3538
const data = JSON.parse(content);
36-
const nodes = jp.apply(data, this.jsonpath, _val => {
37-
return this.version.toString();
39+
const nodes = jp.apply(data, this.jsonpath, value => {
40+
if (typeof value !== 'string') {
41+
logger.warn(`No string in ${this.jsonpath}. Skipping.`);
42+
return value;
43+
}
44+
if (!value.match(VERSION_REGEX)) {
45+
logger.warn(`No version found in ${this.jsonpath}. Skipping.`);
46+
return value;
47+
}
48+
return value.replace(VERSION_REGEX, this.version.toString());
3849
});
3950
if (!nodes) {
4051
logger.warn(`No entries modified in ${this.jsonpath}`);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": [
4+
"github>abc/foo:xyz/sub#1.2.3",
5+
"github>abc/bar:xyz/sub#1.2.3"
6+
]
7+
}

test/updaters/generic-json.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ describe('GenericJson', () => {
4545
const newContent = updater.updateContent(oldContent);
4646
snapshot(newContent);
4747
});
48+
it('updates substring in matching entry', async () => {
49+
const oldContent = readFileSync(
50+
resolve(fixturesPath, './renovate-shared-preset.json'),
51+
'utf8'
52+
).replace(/\r\n/g, '\n');
53+
const updater = new GenericJson('$.extends.*', Version.parse('v2.3.4'));
54+
const newContent = updater.updateContent(oldContent);
55+
snapshot(newContent);
56+
});
4857
it('ignores non-matching entry', async () => {
4958
const oldContent = readFileSync(
5059
resolve(fixturesPath, './esy.json'),
@@ -54,6 +63,24 @@ describe('GenericJson', () => {
5463
const newContent = updater.updateContent(oldContent);
5564
expect(newContent).to.eql(oldContent);
5665
});
66+
it('ignore array entry', async () => {
67+
const oldContent = readFileSync(
68+
resolve(fixturesPath, './renovate-shared-preset.json'),
69+
'utf8'
70+
).replace(/\r\n/g, '\n');
71+
const updater = new GenericJson('$.extends', Version.parse('v2.3.4'));
72+
const newContent = updater.updateContent(oldContent);
73+
expect(newContent).to.eql(oldContent);
74+
});
75+
it('ignore non-matching string', async () => {
76+
const oldContent = readFileSync(
77+
resolve(fixturesPath, './esy.json'),
78+
'utf8'
79+
).replace(/\r\n/g, '\n');
80+
const updater = new GenericJson('$.author', Version.parse('v2.3.4'));
81+
const newContent = updater.updateContent(oldContent);
82+
expect(newContent).to.eql(oldContent);
83+
});
5784
it('warns on invalid jsonpath', async () => {
5885
const oldContent = readFileSync(
5986
resolve(fixturesPath, './esy.json'),

0 commit comments

Comments
 (0)