From 70143876684bef3bf3cd4a598f7e99b90378dbe3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 4 Jun 2024 12:42:47 -0700 Subject: [PATCH 1/2] feat: support BEGIN_VERSION_OVERRIDE in php-yoshi --- src/strategies/php-yoshi.ts | 38 +++++++++++++++++++++++++++++++----- test/strategies/php-yoshi.ts | 31 ++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/strategies/php-yoshi.ts b/src/strategies/php-yoshi.ts index fddf2c203..8eeb171d8 100644 --- a/src/strategies/php-yoshi.ts +++ b/src/strategies/php-yoshi.ts @@ -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, @@ -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( this.addPath(`${directory}/composer.json`), this.targetBranch @@ -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], @@ -273,6 +283,24 @@ 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; + } + } + return versionOverrides; +} + function updatePHPChangelogEntry( pkgKey: string, changelogEntry: string, diff --git a/test/strategies/php-yoshi.ts b/test/strategies/php-yoshi.ts index df01cdfbf..a3cc8fb4f 100644 --- a/test/strategies/php-yoshi.ts +++ b/test/strategies/php-yoshi.ts @@ -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'; @@ -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(); @@ -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 () => { From 34ec418f7bad1a97ecffe697a149160b2d492d4e Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 4 Jun 2024 15:04:36 -0700 Subject: [PATCH 2/2] remove unneeded extra return --- src/strategies/php-yoshi.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/strategies/php-yoshi.ts b/src/strategies/php-yoshi.ts index 8eeb171d8..ed31687b9 100644 --- a/src/strategies/php-yoshi.ts +++ b/src/strategies/php-yoshi.ts @@ -295,7 +295,6 @@ function parseVersionOverrides(body: string): {[key: string]: string} { const [directory, version] = line.split(':'); versionOverrides[directory.trim()] = version.trim(); }); - return versionOverrides; } } return versionOverrides;