Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support BEGIN_VERSION_OVERRIDE in php-yoshi #2300

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/strategies/php-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ export class PHPYoshi extends BaseStrategy {
return undefined;
}

const versionOverrides: {[key: string]: string} = {};
commits.forEach(commit => {
Object.entries(
parseVersionOverrides(commit.pullRequest?.body || '')
).forEach(([directory, version]) => {
versionOverrides[directory] = version;
});
});

const newVersion = latestRelease
? await this.versioningStrategy.bump(
latestRelease.tag.version,
Expand All @@ -100,7 +109,6 @@ export class PHPYoshi extends BaseStrategy {
this.addPath(`${directory}/VERSION`),
this.targetBranch
);
const version = Version.parse(contents.parsedContent);
const composer = await this.github.getFileJson<ComposerJson>(
this.addPath(`${directory}/composer.json`),
this.targetBranch
Expand All @@ -109,10 +117,12 @@ export class PHPYoshi extends BaseStrategy {
versionContents: contents,
composer,
};
const newVersion = await this.versioningStrategy.bump(
version,
splitCommits[directory]
);
const newVersion = versionOverrides[directory]
? Version.parse(versionOverrides[directory])
: await this.versioningStrategy.bump(
Version.parse(contents.parsedContent),
splitCommits[directory]
);
versionsMap.set(composer.name, newVersion);
const partialReleaseNotes = await this.changelogNotes.buildNotes(
splitCommits[directory],
Expand Down Expand Up @@ -273,6 +283,23 @@ export class PHPYoshi extends BaseStrategy {
}
}

function parseVersionOverrides(body: string): {[key: string]: string} {
// look for 'BEGIN_VERSION_OVERRIDE' section of pull request body
const versionOverrides: {[key: string]: string} = {};
if (body) {
const overrideMessage = (body.split('BEGIN_VERSION_OVERRIDE')[1] || '')
.split('END_VERSION_OVERRIDE')[0]
.trim();
if (overrideMessage) {
overrideMessage.split('\n').forEach(line => {
const [directory, version] = line.split(':');
versionOverrides[directory.trim()] = version.trim();
});
}
}
return versionOverrides;
}

function updatePHPChangelogEntry(
pkgKey: string,
changelogEntry: string,
Expand Down
31 changes: 30 additions & 1 deletion test/strategies/php-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {GitHub} from '../../src/github';
import {PHPYoshi} from '../../src/strategies/php-yoshi';
import * as sinon from 'sinon';
import {assertHasUpdate, buildGitHubFileRaw, dateSafe} from '../helpers';
import {buildMockConventionalCommit} from '../helpers';
import {buildMockConventionalCommit, buildMockCommit} from '../helpers';
import {TagName} from '../../src/util/tag-name';
import {Version} from '../../src/version';
import {Changelog} from '../../src/updaters/changelog';
Expand All @@ -29,6 +29,7 @@ import snapshot = require('snap-shot-it');
import {readFileSync} from 'fs';
import {resolve} from 'path';
import {FileNotFoundError} from '../../src/errors';
import {parseConventionalCommits} from '../../src/commit';

const sandbox = sinon.createSandbox();

Expand Down Expand Up @@ -239,6 +240,34 @@ describe('PHPYoshi', () => {
);
expect(newContent).to.eql('{"name":"google/client1","version":"1.2.4"}');
});
it('can override the version from BEGIN_VERSION_OVERRIDE body', async () => {
const commit = buildMockCommit('chore: some commit');
const body =
'BEGIN_VERSION_OVERRIDE\nClient1: 1.3.0\nClient2: 2.0.0-RC1\nEND_VERSION_OVERRIDE';
commit.pullRequest = {
headBranchName: 'fix-something',
baseBranchName: 'main',
number: 123,
title: 'chore: some commit',
labels: [],
files: [],
body,
};
// Add a commit with a version override
commits.push(...parseConventionalCommits([commit]));

const strategy = new PHPYoshi({
targetBranch: 'main',
github,
});

const release = await strategy.buildReleasePullRequest(commits);
const updates = release!.updates;
const client1Version = assertHasUpdate(updates, 'Client1/VERSION');
const client2Version = assertHasUpdate(updates, 'Client2/VERSION');
expect(client1Version.updater.updateContent('')).to.eql('1.3.0\n');
expect(client2Version.updater.updateContent('')).to.eql('2.0.0-RC1\n');
});
});
describe('buildRelease', () => {
it('parses the release notes', async () => {
Expand Down
Loading