-
Notifications
You must be signed in to change notification settings - Fork 336
/
branch-name.ts
246 lines (231 loc) · 7.57 KB
/
branch-name.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Version} from '../version';
import {logger as defaultLogger, Logger} from './logger';
// cannot import from '..' - transpiled code references to RELEASE_PLEASE
// at the script level are undefined, they are only defined inside function
// or instance methods/properties.
// import {RELEASE_PLEASE} from '../constants';
const RELEASE_PLEASE = 'release-please';
type BranchNameType = typeof BranchName;
function getAllResourceNames(): BranchNameType[] {
return [
AutoreleaseBranchName,
ComponentBranchName,
GroupBranchName,
DefaultBranchName,
V12ComponentBranchName,
V12DefaultBranchName,
];
}
export class BranchName {
component?: string;
targetBranch?: string;
version?: Version;
static parse(
branchName: string,
logger: Logger = defaultLogger
): BranchName | undefined {
try {
const branchNameClass = getAllResourceNames().find(clazz => {
return clazz.matches(branchName);
});
if (!branchNameClass) {
return undefined;
}
return new branchNameClass(branchName);
} catch (e) {
logger.warn(`Error parsing branch name: ${branchName}`, e);
return undefined;
}
}
static ofComponentVersion(
branchPrefix: string,
version: Version
): BranchName {
return new AutoreleaseBranchName(`release-${branchPrefix}-v${version}`);
}
static ofVersion(version: Version): BranchName {
return new AutoreleaseBranchName(`release-v${version}`);
}
static ofTargetBranch(targetBranch: string): BranchName {
return new DefaultBranchName(
`${RELEASE_PLEASE}--branches--${targetBranch}`
);
}
static ofComponentTargetBranch(
component: string,
targetBranch: string
): BranchName {
return new ComponentBranchName(
`${RELEASE_PLEASE}--branches--${targetBranch}--components--${component}`
);
}
static ofGroupTargetBranch(group: string, targetBranch: string): BranchName {
return new GroupBranchName(
`${RELEASE_PLEASE}--branches--${targetBranch}--groups--${safeBranchName(
group
)}`
);
}
constructor(_branchName: string) {}
static matches(_branchName: string): boolean {
return false;
}
getTargetBranch(): string | undefined {
return this.targetBranch;
}
getComponent(): string | undefined {
return this.component;
}
getVersion(): Version | undefined {
return this.version;
}
toString(): string {
return '';
}
}
/**
* 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].*)$/;
const RELEASE_PLEASE_BRANCH_PREFIX = 'release-please--branches';
class AutoreleaseBranchName extends BranchName {
static matches(branchName: string): boolean {
if (branchName.startsWith(RELEASE_PLEASE_BRANCH_PREFIX)) {
return false;
}
return !!branchName.match(AUTORELEASE_PATTERN);
}
constructor(branchName: string) {
super(branchName);
const match = branchName.match(AUTORELEASE_PATTERN);
if (match?.groups) {
this.component = match.groups['component'];
this.version = Version.parse(match.groups['version']);
}
}
toString(): string {
if (this.component) {
return `release-${this.component}-v${this.version?.toString()}`;
}
return `release-v${this.version?.toString()}`;
}
}
/**
* 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);
}
constructor(branchName: string) {
super(branchName);
const match = branchName.match(DEFAULT_PATTERN);
if (match?.groups) {
this.targetBranch = match.groups['branch'];
}
}
toString(): string {
return `${RELEASE_PLEASE}--branches--${this.targetBranch}`;
}
}
const COMPONENT_PATTERN = `^${RELEASE_PLEASE}--branches--(?<branch>.+)--components--(?<component>.+)$`;
class ComponentBranchName extends BranchName {
static matches(branchName: string): boolean {
return !!branchName.match(COMPONENT_PATTERN);
}
constructor(branchName: string) {
super(branchName);
const match = branchName.match(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 GROUP_PATTERN = `^${RELEASE_PLEASE}--branches--(?<branch>.+)--groups--(?<group>.+)$`;
class GroupBranchName extends BranchName {
static matches(branchName: string): boolean {
return !!branchName.match(GROUP_PATTERN);
}
constructor(branchName: string) {
super(branchName);
const match = branchName.match(GROUP_PATTERN);
if (match?.groups) {
this.targetBranch = match.groups['branch'];
this.component = match.groups['group'];
}
}
toString(): string {
return `${RELEASE_PLEASE}--branches--${this.targetBranch}--groups--${this.component}`;
}
}
function safeBranchName(branchName: string): string {
// convert disallowed characters in branch names, replacing them with '-'.
// replace multiple consecutive '-' with a single '-' to avoid interfering with
// our regexes for parsing the branch names
return branchName.replace(/[^\w\d]/g, '-').replace(/-+/g, '-');
}