Skip to content

Commit

Permalink
feat: Use default updaters based on file extension (#2072)
Browse files Browse the repository at this point in the history
* feat: Use default updaters based on file extension

* use CompositeUpdater

* improve things

---------

Co-authored-by: Jeff Ching <chingor@google.com>
  • Loading branch information
regseb and chingor13 authored Sep 18, 2023
1 parent c71f2d7 commit 1ee162b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
9 changes: 9 additions & 0 deletions docs/customizing.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ the block, we will attempt to replace version values.

## Updating arbitrary JSON files

For files with the `.xml` extension, the `version` property is updated.

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

## Updating arbitrary XML files

For files with the `.xml` extension, the `version` element is updated.

For most release strategies, you can provide additional files to update
using the [GenericXml](/src/updaters/generic-xml.ts) updater. You can
specify a configuration object in the `extra-files` option in the manifest
Expand All @@ -216,6 +220,9 @@ configuration.

## Updating arbitrary YAML files

For files with the `.yaml` or `.yml` extension, the `version` property is
updated.

For most release strategies, you can provide additional files to update
using the [GenericYaml](/src/updaters/generic-yaml.ts) updater. You can
specify a configuration object in the `extra-files` option in the manifest
Expand All @@ -235,6 +242,8 @@ configuration.

## Updating arbitrary TOML files

For files with the `.toml` extension, the `version` property is updated.

For most release strategies, you can provide additional files to update
using the [GenericToml](/src/updaters/generic-toml.ts) updater. You can
specify a configuration object in the `extra-files` option in the manifest
Expand Down
39 changes: 38 additions & 1 deletion src/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {PullRequestTitle} from '../util/pull-request-title';
import {BranchName} from '../util/branch-name';
import {PullRequestBody, ReleaseData} from '../util/pull-request-body';
import {PullRequest} from '../pull-request';
import {mergeUpdates} from '../updaters/composite';
import {CompositeUpdater, mergeUpdates} from '../updaters/composite';
import {Generic} from '../updaters/generic';
import {GenericJson} from '../updaters/generic-json';
import {GenericXml} from '../updaters/generic-xml';
Expand Down Expand Up @@ -428,6 +428,43 @@ export abstract class BaseStrategy implements Strategy {
);
}
}
} else if (extraFile.endsWith('.json')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new CompositeUpdater(
new GenericJson('$.version', version),
new Generic({version, versionsMap})
),
});
} else if (extraFile.endsWith('.yaml') || extraFile.endsWith('.yml')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new CompositeUpdater(
new GenericYaml('$.version', version),
new Generic({version, versionsMap})
),
});
} else if (extraFile.endsWith('.toml')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new CompositeUpdater(
new GenericToml('$.version', version),
new Generic({version, versionsMap})
),
});
} else if (extraFile.endsWith('.xml')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new CompositeUpdater(
// Updates "version" element that is a child of the root element.
new GenericXml('/*/version', version),
new Generic({version, versionsMap})
),
});
} else {
extraFileUpdates.push({
path: this.addPath(extraFile),
Expand Down
65 changes: 65 additions & 0 deletions test/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import snapshot = require('snap-shot-it');
import {
dateSafe,
assertHasUpdate,
assertHasUpdates,
buildMockConventionalCommit,
} from '../helpers';
import {GenericJson} from '../../src/updaters/generic-json';
Expand Down Expand Up @@ -112,6 +113,70 @@ describe('Strategy', () => {
])
.and.not.include('foo/baz/bar/', 'expected file but got directory');
});
it('updates extra JSON files with default', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
extraFiles: ['manifest.json'],
});
const pullRequest = await strategy.buildReleasePullRequest(
buildMockConventionalCommit('fix: a bugfix'),
undefined
);
expect(pullRequest).to.exist;
const updates = pullRequest?.updates;
expect(updates).to.be.an('array');
assertHasUpdates(updates!, 'manifest.json', GenericJson, Generic);
});
it('updates extra YAML files with default', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
extraFiles: ['pubspec.yaml'],
});
const pullRequest = await strategy.buildReleasePullRequest(
buildMockConventionalCommit('fix: a bugfix'),
undefined
);
expect(pullRequest).to.exist;
const updates = pullRequest?.updates;
expect(updates).to.be.an('array');
assertHasUpdates(updates!, 'pubspec.yaml', GenericYaml, Generic);
});
it('updates extra TOML files with default', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
extraFiles: ['foo.toml'],
});
const pullRequest = await strategy.buildReleasePullRequest(
buildMockConventionalCommit('fix: a bugfix'),
undefined
);
expect(pullRequest).to.exist;
const updates = pullRequest?.updates;
expect(updates).to.be.an('array');
assertHasUpdates(updates!, 'foo.toml', GenericToml, Generic);
});
it('updates extra Xml files with default', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
extraFiles: ['pom.xml'],
});
const pullRequest = await strategy.buildReleasePullRequest(
buildMockConventionalCommit('fix: a bugfix'),
undefined
);
expect(pullRequest).to.exist;
const updates = pullRequest?.updates;
expect(updates).to.be.an('array');
assertHasUpdates(updates!, 'pom.xml', GenericXml, Generic);
});
it('updates extra JSON files', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
Expand Down
5 changes: 3 additions & 2 deletions test/strategies/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {Version} from '../../src/version';
import {TagName} from '../../src/util/tag-name';
import {Changelog} from '../../src/updaters/changelog';
import {DEFAULT_LABELS, DEFAULT_SNAPSHOT_LABELS} from '../../src/manifest';
import {CompositeUpdater} from '../../src/updaters/composite';
import {Generic} from '../../src/updaters/generic';
import {JavaReleased} from '../../src/updaters/java/java-released';

Expand Down Expand Up @@ -262,7 +263,7 @@ describe('Java', () => {

const updates = release!.updates;
assertHasUpdate(updates, 'CHANGELOG.md', Changelog);
assertHasUpdates(updates, 'pom.xml', JavaReleased, Generic);
assertHasUpdates(updates, 'pom.xml', JavaReleased, CompositeUpdater);
assertHasUpdates(updates, 'foo/bar.java', JavaReleased, Generic);
});

Expand All @@ -286,7 +287,7 @@ describe('Java', () => {
const updates = release!.updates;
assertNoHasUpdate(updates, 'CHANGELOG.md');
assertHasUpdate(updates, 'foo/bar.java', Generic);
assertHasUpdate(updates, 'pom.xml', Generic);
assertHasUpdate(updates, 'pom.xml', CompositeUpdater);
});
});

Expand Down

0 comments on commit 1ee162b

Please sign in to comment.