Skip to content

Commit 1ee162b

Browse files
regsebchingor13
andauthored
feat: Use default updaters based on file extension (#2072)
* feat: Use default updaters based on file extension * use CompositeUpdater * improve things --------- Co-authored-by: Jeff Ching <chingor@google.com>
1 parent c71f2d7 commit 1ee162b

4 files changed

Lines changed: 115 additions & 3 deletions

File tree

docs/customizing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ the block, we will attempt to replace version values.
174174

175175
## Updating arbitrary JSON files
176176

177+
For files with the `.xml` extension, the `version` property is updated.
178+
177179
For most release strategies, you can provide additional files to update
178180
using the [GenericJson](/src/updaters/generic-json.ts) updater. You can
179181
specify a configuration object in the `extra-files` option in the manifest
@@ -197,6 +199,8 @@ informs release-please on which JSON field to update with the new version.
197199

198200
## Updating arbitrary XML files
199201

202+
For files with the `.xml` extension, the `version` element is updated.
203+
200204
For most release strategies, you can provide additional files to update
201205
using the [GenericXml](/src/updaters/generic-xml.ts) updater. You can
202206
specify a configuration object in the `extra-files` option in the manifest
@@ -216,6 +220,9 @@ configuration.
216220

217221
## Updating arbitrary YAML files
218222

223+
For files with the `.yaml` or `.yml` extension, the `version` property is
224+
updated.
225+
219226
For most release strategies, you can provide additional files to update
220227
using the [GenericYaml](/src/updaters/generic-yaml.ts) updater. You can
221228
specify a configuration object in the `extra-files` option in the manifest
@@ -235,6 +242,8 @@ configuration.
235242

236243
## Updating arbitrary TOML files
237244

245+
For files with the `.toml` extension, the `version` property is updated.
246+
238247
For most release strategies, you can provide additional files to update
239248
using the [GenericToml](/src/updaters/generic-toml.ts) updater. You can
240249
specify a configuration object in the `extra-files` option in the manifest

src/strategies/base.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {PullRequestTitle} from '../util/pull-request-title';
3535
import {BranchName} from '../util/branch-name';
3636
import {PullRequestBody, ReleaseData} from '../util/pull-request-body';
3737
import {PullRequest} from '../pull-request';
38-
import {mergeUpdates} from '../updaters/composite';
38+
import {CompositeUpdater, mergeUpdates} from '../updaters/composite';
3939
import {Generic} from '../updaters/generic';
4040
import {GenericJson} from '../updaters/generic-json';
4141
import {GenericXml} from '../updaters/generic-xml';
@@ -428,6 +428,43 @@ export abstract class BaseStrategy implements Strategy {
428428
);
429429
}
430430
}
431+
} else if (extraFile.endsWith('.json')) {
432+
extraFileUpdates.push({
433+
path: this.addPath(extraFile),
434+
createIfMissing: false,
435+
updater: new CompositeUpdater(
436+
new GenericJson('$.version', version),
437+
new Generic({version, versionsMap})
438+
),
439+
});
440+
} else if (extraFile.endsWith('.yaml') || extraFile.endsWith('.yml')) {
441+
extraFileUpdates.push({
442+
path: this.addPath(extraFile),
443+
createIfMissing: false,
444+
updater: new CompositeUpdater(
445+
new GenericYaml('$.version', version),
446+
new Generic({version, versionsMap})
447+
),
448+
});
449+
} else if (extraFile.endsWith('.toml')) {
450+
extraFileUpdates.push({
451+
path: this.addPath(extraFile),
452+
createIfMissing: false,
453+
updater: new CompositeUpdater(
454+
new GenericToml('$.version', version),
455+
new Generic({version, versionsMap})
456+
),
457+
});
458+
} else if (extraFile.endsWith('.xml')) {
459+
extraFileUpdates.push({
460+
path: this.addPath(extraFile),
461+
createIfMissing: false,
462+
updater: new CompositeUpdater(
463+
// Updates "version" element that is a child of the root element.
464+
new GenericXml('/*/version', version),
465+
new Generic({version, versionsMap})
466+
),
467+
});
431468
} else {
432469
extraFileUpdates.push({
433470
path: this.addPath(extraFile),

test/strategies/base.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import snapshot = require('snap-shot-it');
2323
import {
2424
dateSafe,
2525
assertHasUpdate,
26+
assertHasUpdates,
2627
buildMockConventionalCommit,
2728
} from '../helpers';
2829
import {GenericJson} from '../../src/updaters/generic-json';
@@ -112,6 +113,70 @@ describe('Strategy', () => {
112113
])
113114
.and.not.include('foo/baz/bar/', 'expected file but got directory');
114115
});
116+
it('updates extra JSON files with default', async () => {
117+
const strategy = new TestStrategy({
118+
targetBranch: 'main',
119+
github,
120+
component: 'google-cloud-automl',
121+
extraFiles: ['manifest.json'],
122+
});
123+
const pullRequest = await strategy.buildReleasePullRequest(
124+
buildMockConventionalCommit('fix: a bugfix'),
125+
undefined
126+
);
127+
expect(pullRequest).to.exist;
128+
const updates = pullRequest?.updates;
129+
expect(updates).to.be.an('array');
130+
assertHasUpdates(updates!, 'manifest.json', GenericJson, Generic);
131+
});
132+
it('updates extra YAML files with default', async () => {
133+
const strategy = new TestStrategy({
134+
targetBranch: 'main',
135+
github,
136+
component: 'google-cloud-automl',
137+
extraFiles: ['pubspec.yaml'],
138+
});
139+
const pullRequest = await strategy.buildReleasePullRequest(
140+
buildMockConventionalCommit('fix: a bugfix'),
141+
undefined
142+
);
143+
expect(pullRequest).to.exist;
144+
const updates = pullRequest?.updates;
145+
expect(updates).to.be.an('array');
146+
assertHasUpdates(updates!, 'pubspec.yaml', GenericYaml, Generic);
147+
});
148+
it('updates extra TOML files with default', async () => {
149+
const strategy = new TestStrategy({
150+
targetBranch: 'main',
151+
github,
152+
component: 'google-cloud-automl',
153+
extraFiles: ['foo.toml'],
154+
});
155+
const pullRequest = await strategy.buildReleasePullRequest(
156+
buildMockConventionalCommit('fix: a bugfix'),
157+
undefined
158+
);
159+
expect(pullRequest).to.exist;
160+
const updates = pullRequest?.updates;
161+
expect(updates).to.be.an('array');
162+
assertHasUpdates(updates!, 'foo.toml', GenericToml, Generic);
163+
});
164+
it('updates extra Xml files with default', async () => {
165+
const strategy = new TestStrategy({
166+
targetBranch: 'main',
167+
github,
168+
component: 'google-cloud-automl',
169+
extraFiles: ['pom.xml'],
170+
});
171+
const pullRequest = await strategy.buildReleasePullRequest(
172+
buildMockConventionalCommit('fix: a bugfix'),
173+
undefined
174+
);
175+
expect(pullRequest).to.exist;
176+
const updates = pullRequest?.updates;
177+
expect(updates).to.be.an('array');
178+
assertHasUpdates(updates!, 'pom.xml', GenericXml, Generic);
179+
});
115180
it('updates extra JSON files', async () => {
116181
const strategy = new TestStrategy({
117182
targetBranch: 'main',

test/strategies/java.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {Version} from '../../src/version';
2727
import {TagName} from '../../src/util/tag-name';
2828
import {Changelog} from '../../src/updaters/changelog';
2929
import {DEFAULT_LABELS, DEFAULT_SNAPSHOT_LABELS} from '../../src/manifest';
30+
import {CompositeUpdater} from '../../src/updaters/composite';
3031
import {Generic} from '../../src/updaters/generic';
3132
import {JavaReleased} from '../../src/updaters/java/java-released';
3233

@@ -262,7 +263,7 @@ describe('Java', () => {
262263

263264
const updates = release!.updates;
264265
assertHasUpdate(updates, 'CHANGELOG.md', Changelog);
265-
assertHasUpdates(updates, 'pom.xml', JavaReleased, Generic);
266+
assertHasUpdates(updates, 'pom.xml', JavaReleased, CompositeUpdater);
266267
assertHasUpdates(updates, 'foo/bar.java', JavaReleased, Generic);
267268
});
268269

@@ -286,7 +287,7 @@ describe('Java', () => {
286287
const updates = release!.updates;
287288
assertNoHasUpdate(updates, 'CHANGELOG.md');
288289
assertHasUpdate(updates, 'foo/bar.java', Generic);
289-
assertHasUpdate(updates, 'pom.xml', Generic);
290+
assertHasUpdate(updates, 'pom.xml', CompositeUpdater);
290291
});
291292
});
292293

0 commit comments

Comments
 (0)