Skip to content

Commit

Permalink
fix: switch branch delimiter to -- (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Nov 29, 2021
1 parent 2f3e84c commit 26442f1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
76 changes: 69 additions & 7 deletions src/util/branch-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const RELEASE_PLEASE = 'release-please';
type BranchNameType = typeof BranchName;

function getAllResourceNames(): BranchNameType[] {
return [AutoreleaseBranchName, ComponentBranchName, DefaultBranchName];
return [
AutoreleaseBranchName,
ComponentBranchName,
DefaultBranchName,
V12ComponentBranchName,
V12DefaultBranchName,
];
}

export class BranchName {
Expand All @@ -51,14 +57,16 @@ export class BranchName {
return new AutoreleaseBranchName(`release-v${version}`);
}
static ofTargetBranch(targetBranch: string): BranchName {
return new DefaultBranchName(`${RELEASE_PLEASE}/branches/${targetBranch}`);
return new DefaultBranchName(
`${RELEASE_PLEASE}--branches--${targetBranch}`
);
}
static ofComponentTargetBranch(
component: string,
targetBranch: string
): BranchName {
return new ComponentBranchName(
`${RELEASE_PLEASE}/branches/${targetBranch}/components/${component}`
`${RELEASE_PLEASE}--branches--${targetBranch}--components--${component}`
);
}
constructor(_branchName: string) {}
Expand All @@ -80,6 +88,11 @@ export class BranchName {
}
}

/**
* This is the legacy branch pattern used by releasetool
*
* @see https://github.com/googleapis/releasetool
*/
const AUTORELEASE_PATTERN =
/^release-?(?<component>[\w-.]*)?-v(?<version>[0-9].*)$/;
class AutoreleaseBranchName extends BranchName {
Expand All @@ -102,7 +115,56 @@ class AutoreleaseBranchName extends BranchName {
}
}

const DEFAULT_PATTERN = `^${RELEASE_PLEASE}/branches/(?<branch>[^/]+)$`;
/**
* This is a parsable branch pattern used by release-please v12.
* It has potential issues due to git treating `/` like directories.
* This should be removed at some point in the future.
*
* @see https://github.com/googleapis/release-please/issues/1024
*/
const V12_DEFAULT_PATTERN = `^${RELEASE_PLEASE}/branches/(?<branch>[^/]+)$`;
class V12DefaultBranchName extends BranchName {
static matches(branchName: string): boolean {
return !!branchName.match(V12_DEFAULT_PATTERN);
}
constructor(branchName: string) {
super(branchName);
const match = branchName.match(V12_DEFAULT_PATTERN);
if (match?.groups) {
this.targetBranch = match.groups['branch'];
}
}
toString(): string {
return `${RELEASE_PLEASE}/branches/${this.targetBranch}`;
}
}

/**
* This is a parsable branch pattern used by release-please v12.
* It has potential issues due to git treating `/` like directories.
* This should be removed at some point in the future.
*
* @see https://github.com/googleapis/release-please/issues/1024
*/
const V12_COMPONENT_PATTERN = `^${RELEASE_PLEASE}/branches/(?<branch>[^/]+)/components/(?<component>.+)$`;
class V12ComponentBranchName extends BranchName {
static matches(branchName: string): boolean {
return !!branchName.match(V12_COMPONENT_PATTERN);
}
constructor(branchName: string) {
super(branchName);
const match = branchName.match(V12_COMPONENT_PATTERN);
if (match?.groups) {
this.targetBranch = match.groups['branch'];
this.component = match.groups['component'];
}
}
toString(): string {
return `${RELEASE_PLEASE}/branches/${this.targetBranch}/components/${this.component}`;
}
}

const DEFAULT_PATTERN = `^${RELEASE_PLEASE}--branches--(?<branch>.+)$`;
class DefaultBranchName extends BranchName {
static matches(branchName: string): boolean {
return !!branchName.match(DEFAULT_PATTERN);
Expand All @@ -115,11 +177,11 @@ class DefaultBranchName extends BranchName {
}
}
toString(): string {
return `${RELEASE_PLEASE}/branches/${this.targetBranch}`;
return `${RELEASE_PLEASE}--branches--${this.targetBranch}`;
}
}

const COMPONENT_PATTERN = `^${RELEASE_PLEASE}/branches/(?<branch>[^/]+)/components/(?<component>.+)$`;
const COMPONENT_PATTERN = `^${RELEASE_PLEASE}--branches--(?<branch>.+)--components--(?<component>.+)$`;
class ComponentBranchName extends BranchName {
static matches(branchName: string): boolean {
return !!branchName.match(COMPONENT_PATTERN);
Expand All @@ -133,6 +195,6 @@ class ComponentBranchName extends BranchName {
}
}
toString(): string {
return `${RELEASE_PLEASE}/branches/${this.targetBranch}/components/${this.component}`;
return `${RELEASE_PLEASE}--branches--${this.targetBranch}--components--${this.component}`;
}
}
6 changes: 6 additions & 0 deletions test/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ describe('Manifest', () => {
assertHasUpdate(pullRequest.updates, 'CHANGELOG.md');
assertHasUpdate(pullRequest.updates, 'version.txt');
assertHasUpdate(pullRequest.updates, '.release-please-manifest.json');
expect(pullRequest.headRefName).to.eql(
'release-please--branches--main'
);
});

it('should create a draft pull request', async () => {
Expand Down Expand Up @@ -467,6 +470,9 @@ describe('Manifest', () => {
expect(pullRequests).lengthOf(1);
const pullRequest = pullRequests[0];
expect(pullRequest.version?.toString()).to.eql('1.0.1');
expect(pullRequest.headRefName).to.eql(
'release-please--branches--main--components--pkg1'
);
});

it('should handle multiple package repository', async () => {
Expand Down
30 changes: 26 additions & 4 deletions test/util/branch-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,30 @@ describe('BranchName', () => {
expect(branchName?.toString()).to.eql(name);
});
});
describe('v12 format', () => {
it('parses a target branch', () => {
const name = 'release-please/branches/main';
const branchName = BranchName.parse(name);
expect(branchName).to.not.be.undefined;
expect(branchName?.getTargetBranch()).to.eql('main');
expect(branchName?.getComponent()).to.be.undefined;
expect(branchName?.getVersion()).to.be.undefined;
expect(branchName?.toString()).to.eql(name);
});

it('parses a target branch and component', () => {
const name = 'release-please/branches/main/components/storage';
const branchName = BranchName.parse(name);
expect(branchName).to.not.be.undefined;
expect(branchName?.getTargetBranch()).to.eql('main');
expect(branchName?.getComponent()).to.eql('storage');
expect(branchName?.getVersion()).to.be.undefined;
expect(branchName?.toString()).to.eql(name);
});
});

it('parses a target branch', () => {
const name = 'release-please/branches/main';
const name = 'release-please--branches--main';
const branchName = BranchName.parse(name);
expect(branchName).to.not.be.undefined;
expect(branchName?.getTargetBranch()).to.eql('main');
Expand All @@ -50,7 +72,7 @@ describe('BranchName', () => {
});

it('parses a target branch and component', () => {
const name = 'release-please/branches/main/components/storage';
const name = 'release-please--branches--main--components--storage';
const branchName = BranchName.parse(name);
expect(branchName).to.not.be.undefined;
expect(branchName?.getTargetBranch()).to.eql('main');
Expand Down Expand Up @@ -82,14 +104,14 @@ describe('BranchName', () => {
describe('ofTargetBranch', () => {
it('builds branchname with only target branch', () => {
const branchName = BranchName.ofTargetBranch('main');
expect(branchName.toString()).to.eql('release-please/branches/main');
expect(branchName.toString()).to.eql('release-please--branches--main');
});
});
describe('ofComponentTargetBranch', () => {
it('builds branchname with target branch and component', () => {
const branchName = BranchName.ofComponentTargetBranch('foo', 'main');
expect(branchName.toString()).to.eql(
'release-please/branches/main/components/foo'
'release-please--branches--main--components--foo'
);
});
});
Expand Down

0 comments on commit 26442f1

Please sign in to comment.