Skip to content

Commit

Permalink
feat: Use default updaters based on file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Sep 12, 2023
1 parent cd0bd85 commit e4d3041
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 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
24 changes: 24 additions & 0 deletions src/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,30 @@ export abstract class BaseStrategy implements Strategy {
);
}
}
} else if (extraFile.endsWith('.json')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new GenericJson('$.version', version),
});
} else if (extraFile.endsWith('.yaml') || extraFile.endsWith('.yml')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new GenericYaml('$.version', version),
});
} else if (extraFile.endsWith('.toml')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new GenericToml('$.version', version),
});
} else if (extraFile.endsWith('.xml')) {
extraFileUpdates.push({
path: this.addPath(extraFile),
createIfMissing: false,
updater: new GenericXml('/*/version', version),
});
} else {
extraFileUpdates.push({
path: this.addPath(extraFile),
Expand Down
64 changes: 64 additions & 0 deletions test/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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');
assertHasUpdate(updates!, 'manifest.json', GenericJson);
});
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');
assertHasUpdate(updates!, 'pubspec.yaml', GenericYaml);
});
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');
assertHasUpdate(updates!, 'foo.toml', GenericToml);
});
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');
assertHasUpdate(updates!, 'pom.xml', GenericXml);
});
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 @@ -28,6 +28,7 @@ import {TagName} from '../../src/util/tag-name';
import {Changelog} from '../../src/updaters/changelog';
import {DEFAULT_LABELS, DEFAULT_SNAPSHOT_LABELS} from '../../src/manifest';
import {Generic} from '../../src/updaters/generic';
import {GenericXml} from '../../src/updaters/generic-xml';
import {JavaReleased} from '../../src/updaters/java/java-released';

const sandbox = sinon.createSandbox();
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, GenericXml);
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', GenericXml);
});
});

Expand Down

0 comments on commit e4d3041

Please sign in to comment.