-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathjava-yoshi.ts
More file actions
364 lines (336 loc) · 11 KB
/
java-yoshi.ts
File metadata and controls
364 lines (336 loc) · 11 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// 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 {Update} from '../update';
import {VersionsManifest} from '../updaters/java/versions-manifest';
import {Version, VersionsMap} from '../version';
import {JavaUpdate} from '../updaters/java/java-update';
import {BaseStrategy, BuildUpdatesOptions, BaseStrategyOptions} from './base';
import {Changelog} from '../updaters/changelog';
import {GitHubFileContents} from '../util/file-cache';
import {JavaSnapshot} from '../versioning-strategies/java-snapshot';
import {GitHubAPIError, MissingRequiredFileError} from '../errors';
import {Commit, ConventionalCommit} from '../commit';
import {Release} from '../release';
import {ReleasePullRequest} from '../release-pull-request';
import {logger} from '../util/logger';
import {PullRequestTitle} from '../util/pull-request-title';
import {BranchName} from '../util/branch-name';
import {PullRequestBody} from '../util/pull-request-body';
import {VersioningStrategy} from '../versioning-strategy';
import {DefaultVersioningStrategy} from '../versioning-strategies/default';
import {JavaAddSnapshot} from '../versioning-strategies/java-add-snapshot';
const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
{type: 'fix', section: 'Bug Fixes'},
{type: 'perf', section: 'Performance Improvements'},
{type: 'deps', section: 'Dependencies'},
{type: 'revert', section: 'Reverts'},
{type: 'docs', section: 'Documentation'},
{type: 'style', section: 'Styles', hidden: true},
{type: 'chore', section: 'Miscellaneous Chores', hidden: true},
{type: 'refactor', section: 'Code Refactoring', hidden: true},
{type: 'test', section: 'Tests', hidden: true},
{type: 'build', section: 'Build System', hidden: true},
{type: 'ci', section: 'Continuous Integration', hidden: true},
];
interface JavaBuildUpdatesOption extends BuildUpdatesOptions {
isSnapshot?: boolean;
}
export class JavaYoshi extends BaseStrategy {
private versionsContent?: GitHubFileContents;
private snapshotVersioning: VersioningStrategy;
constructor(options: BaseStrategyOptions) {
options.changelogSections = options.changelogSections ?? CHANGELOG_SECTIONS;
// wrap the configured versioning strategy with snapshotting
const parentVersioningStrategy =
options.versioningStrategy || new DefaultVersioningStrategy();
options.versioningStrategy = new JavaSnapshot(parentVersioningStrategy);
super(options);
this.snapshotVersioning = new JavaAddSnapshot(parentVersioningStrategy);
}
async buildReleasePullRequest(
commits: Commit[],
latestRelease?: Release,
draft?: boolean,
labels: string[] = []
): Promise<ReleasePullRequest | undefined> {
if (await this.needsSnapshot()) {
logger.info('Repository needs a snapshot bump.');
return await this.buildSnapshotPullRequest(latestRelease);
}
logger.info('No Java snapshot needed');
return await super.buildReleasePullRequest(
commits,
latestRelease,
draft,
labels
);
}
private async buildSnapshotPullRequest(
latestRelease?: Release
): Promise<ReleasePullRequest> {
const component = await this.getComponent();
const newVersion = latestRelease
? await this.snapshotVersioning.bump(latestRelease.tag.version, [])
: this.initialReleaseVersion();
const versionsMap = await this.buildVersionsMap();
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.snapshotVersioning.bump(version, []);
versionsMap.set(versionKey, newVersion);
}
const pullRequestTitle = PullRequestTitle.ofComponentTargetBranchVersion(
component || '',
this.targetBranch,
newVersion
);
const branchName = component
? BranchName.ofComponentTargetBranch(component, this.targetBranch)
: BranchName.ofTargetBranch(this.targetBranch);
const notes =
'### Updating meta-information for bleeding-edge SNAPSHOT release.';
const pullRequestBody = new PullRequestBody([
{
component,
version: newVersion,
notes,
},
]);
const updates = await this.buildUpdates({
newVersion,
versionsMap,
changelogEntry: notes,
isSnapshot: true,
});
return {
title: pullRequestTitle,
body: pullRequestBody,
updates,
labels: [],
headRefName: branchName.toString(),
version: newVersion,
draft: false,
};
}
/**
* Override this method to post process commits
* @param {ConventionalCommit[]} commits parsed commits
* @returns {ConventionalCommit[]} modified commits
*/
protected async postProcessCommits(
commits: ConventionalCommit[]
): Promise<ConventionalCommit[]> {
if (commits.length === 0) {
// For Java commits, push a fake commit so we force a
// SNAPSHOT release
commits.push({
type: 'fake',
bareMessage: 'fake commit',
message: 'fake commit',
breaking: false,
scope: null,
notes: [],
files: [],
references: [],
sha: 'fake',
});
}
return commits;
}
private async needsSnapshot(): Promise<boolean> {
return VersionsManifest.needsSnapshot(
(await this.getVersionsContent()).parsedContent
);
}
protected async buildVersionsMap(): Promise<VersionsMap> {
this.versionsContent = await this.getVersionsContent();
return VersionsManifest.parseVersions(this.versionsContent.parsedContent);
}
protected async getVersionsContent(): Promise<GitHubFileContents> {
if (!this.versionsContent) {
try {
this.versionsContent = await this.github.getFileContentsOnBranch(
this.addPath('versions.txt'),
this.targetBranch
);
} catch (err) {
if (err instanceof GitHubAPIError) {
throw new MissingRequiredFileError(
this.addPath('versions.txt'),
JavaYoshi.name,
`${this.repository.owner}/${this.repository.repo}`
);
}
throw err;
}
}
return this.versionsContent;
}
protected async buildUpdates(
options: JavaBuildUpdatesOption
): Promise<Update[]> {
const updates: Update[] = [];
const version = options.newVersion;
const versionsMap = options.versionsMap;
updates.push({
path: this.addPath('versions.txt'),
createIfMissing: false,
cachedFileContents: this.versionsContent,
updater: new VersionsManifest({
version,
versionsMap,
}),
});
const pomFilesSearch = this.github.findFilesByFilenameAndRef(
'pom.xml',
this.targetBranch,
this.path
);
const buildFilesSearch = this.github.findFilesByFilenameAndRef(
'build.gradle',
this.targetBranch,
this.path
);
const dependenciesSearch = this.github.findFilesByFilenameAndRef(
'dependencies.properties',
this.targetBranch,
this.path
);
const pomFiles = await pomFilesSearch;
pomFiles.forEach(path => {
updates.push({
path: this.addPath(path),
createIfMissing: false,
updater: new JavaUpdate({
version,
versionsMap,
isSnapshot: options.isSnapshot,
}),
});
});
const buildFiles = await buildFilesSearch;
buildFiles.forEach(path => {
updates.push({
path: this.addPath(path),
createIfMissing: false,
updater: new JavaUpdate({
version,
versionsMap,
isSnapshot: options.isSnapshot,
}),
});
});
const dependenciesFiles = await dependenciesSearch;
dependenciesFiles.forEach(path => {
updates.push({
path: this.addPath(path),
createIfMissing: false,
updater: new JavaUpdate({
version,
versionsMap,
isSnapshot: options.isSnapshot,
}),
});
});
this.extraFiles.forEach(path => {
updates.push({
path,
createIfMissing: false,
updater: new JavaUpdate({
version,
versionsMap,
isSnapshot: options.isSnapshot,
}),
});
});
if (!options.isSnapshot) {
updates.push({
path: this.addPath(this.changelogPath),
createIfMissing: true,
updater: new Changelog({
version,
changelogEntry: options.changelogEntry,
}),
});
}
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';
}