Skip to content

Commit

Permalink
feat: reimplement Java 1.0.0 special version bumping (#1126)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
chingor13 and bcoe committed Nov 30, 2021
1 parent 1470661 commit 28bc76b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 14 deletions.
69 changes: 68 additions & 1 deletion src/strategies/java-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {Changelog} from '../updaters/changelog';
import {GitHubFileContents} from '../github';
import {JavaSnapshot} from '../versioning-strategies/java-snapshot';
import {GitHubAPIError, MissingRequiredFileError} from '../errors';
import {Commit} from '../commit';
import {Commit, ConventionalCommit} from '../commit';
import {Release} from '../release';
import {ReleasePullRequest} from '../release-pull-request';
import {logger} from '../util/logger';
Expand Down Expand Up @@ -260,7 +260,74 @@ export class JavaYoshi extends Strategy {
return updates;
}

protected async updateVersionsMap(
versionsMap: VersionsMap,
conventionalCommits: ConventionalCommit[]
): Promise<VersionsMap> {
let isPromotion = false;
const modifiedCommits: ConventionalCommit[] = [];
for (const commit of conventionalCommits) {
if (isPromotionCommit(commit)) {
isPromotion = true;
modifiedCommits.push({
...commit,
notes: commit.notes.filter(note => !isPromotionNote(note)),
});
} else {
modifiedCommits.push(commit);
}
}
for (const versionKey of versionsMap.keys()) {
const version = versionsMap.get(versionKey);
if (!version) {
logger.warn(`didn't find version for ${versionKey}`);
continue;
}
if (isPromotion && isStableArtifact(versionKey)) {
versionsMap.set(versionKey, Version.parse('1.0.0'));
} else {
const newVersion = await this.versioningStrategy.bump(
version,
modifiedCommits
);
versionsMap.set(versionKey, newVersion);
}
}
return versionsMap;
}

protected initialReleaseVersion(): Version {
return Version.parse('0.1.0');
}
}

const VERSIONED_ARTIFACT_REGEX = /^.*-(v\d+[^-]*)$/;
const VERSION_REGEX = /^v\d+(.*)$/;

/**
* Returns true if the artifact should be considered stable
* @param artifact name of the artifact to check
*/
function isStableArtifact(artifact: string): boolean {
const match = artifact.match(VERSIONED_ARTIFACT_REGEX);
if (!match) {
// The artifact does not have a version qualifier at the end
return true;
}

const versionMatch = match[1].match(VERSION_REGEX);
if (versionMatch && versionMatch[1]) {
// The version is not stable (probably alpha/beta/rc)
return false;
}

return true;
}

function isPromotionCommit(commit: ConventionalCommit): boolean {
return commit.notes.some(isPromotionNote);
}

function isPromotionNote(note: {title: string; text: string}): boolean {
return note.title === 'RELEASE AS' && note.text === '1.0.0';
}
36 changes: 23 additions & 13 deletions src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,10 @@ export abstract class Strategy {
conventionalCommits,
latestRelease
);
const versionsMap = await this.buildVersionsMap(conventionalCommits);
for (const versionKey of versionsMap.keys()) {
const version = versionsMap.get(versionKey);
if (!version) {
logger.warn(`didn't find version for ${versionKey}`);
continue;
}
const newVersion = await this.versioningStrategy.bump(
version,
conventionalCommits
);
versionsMap.set(versionKey, newVersion);
}
const versionsMap = await this.updateVersionsMap(
await this.buildVersionsMap(conventionalCommits),
conventionalCommits
);
const component = await this.getComponent();
logger.debug('component:', component);

Expand Down Expand Up @@ -266,6 +257,25 @@ export abstract class Strategy {
return changelogEntry.split('\n').length <= 1;
}

protected async updateVersionsMap(
versionsMap: VersionsMap,
conventionalCommits: ConventionalCommit[]
): Promise<VersionsMap> {
for (const versionKey of versionsMap.keys()) {
const version = versionsMap.get(versionKey);
if (!version) {
logger.warn(`didn't find version for ${versionKey}`);
continue;
}
const newVersion = await this.versioningStrategy.bump(
version,
conventionalCommits
);
versionsMap.set(versionKey, newVersion);
}
return versionsMap;
}

protected async buildNewVersion(
conventionalCommits: ConventionalCommit[],
latestRelease?: Release
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Format:
# module:released-version:current-version

google-cloud-trace:0.108.0-beta:0.108.1-beta-SNAPSHOT
grpc-google-cloud-trace-v1:0.73.0:0.73.1-SNAPSHOT
grpc-google-cloud-trace-v1beta1:0.73.0:0.73.1-SNAPSHOT
proto-google-cloud-trace-v1:0.73.0:0.73.1-SNAPSHOT
proto-google-cloud-trace-v2beta1:0.73.0:0.73.1-SNAPSHOT
46 changes: 46 additions & 0 deletions test/strategies/java-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,52 @@ describe('JavaYoshi', () => {
);
expect(release!.version?.toString()).to.eql(expectedVersion);
});
it('handles promotion to 1.0.0', async () => {
const commits = [
buildMockCommit('feat: promote to 1.0.0\n\nRelease-As: 1.0.0'),
];
const expectedVersion = '1.0.0';
const strategy = new JavaYoshi({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
});
sandbox.stub(github, 'findFilesByFilename').resolves([]);
const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('versions.txt', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'versions-with-beta-artifacts.txt'
)
);
const latestRelease = {
tag: new TagName(Version.parse('0.123.4'), 'google-cloud-automl'),
sha: 'abc123',
notes: 'some notes',
};
const releasePullRequest = await strategy.buildReleasePullRequest(
commits,
latestRelease
);
expect(releasePullRequest!.version?.toString()).to.eql(expectedVersion);
const update = assertHasUpdate(
releasePullRequest!.updates,
'versions.txt',
VersionsManifest
);
const versionsMap = (update.updater as VersionsManifest).versionsMap!;
expect(versionsMap.get('grpc-google-cloud-trace-v1')?.toString()).to.eql(
'1.0.0'
);
expect(
versionsMap.get('grpc-google-cloud-trace-v1beta1')?.toString()
).to.eql('0.74.0');
});
});
describe('buildUpdates', () => {
it('builds common files', async () => {
Expand Down

0 comments on commit 28bc76b

Please sign in to comment.