forked from googleapis/release-please
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-versions.ts
More file actions
209 lines (194 loc) · 6.81 KB
/
linked-versions.ts
File metadata and controls
209 lines (194 loc) · 6.81 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
// Copyright 2022 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 {ManifestPlugin} from '../plugin';
import {RepositoryConfig, CandidateReleasePullRequest} from '../manifest';
import {GitHub} from '../github';
import {Logger} from '../util/logger';
import {Strategy} from '../strategy';
import {Commit, parseConventionalCommits} from '../commit';
import {Release} from '../release';
import {Version} from '../version';
import {buildStrategy} from '../factory';
import {Merge} from './merge';
import {BranchName} from '../util/branch-name';
interface LinkedVersionsPluginOptions {
merge?: boolean;
groupPullRequestTitlePattern?: string;
logger?: Logger;
}
/**
* This plugin reconfigures strategies by linking multiple components
* together.
*
* Release notes are broken up using `<summary>`/`<details>` blocks.
*/
export class LinkedVersions extends ManifestPlugin {
readonly groupName: string;
readonly components: Set<string>;
readonly merge: boolean;
private groupPullRequestTitlePattern?: string;
constructor(
github: GitHub,
targetBranch: string,
repositoryConfig: RepositoryConfig,
groupName: string,
components: string[],
options: LinkedVersionsPluginOptions = {}
) {
super(github, targetBranch, repositoryConfig, options.logger);
this.groupName = groupName;
this.components = new Set(components);
this.merge = options.merge ?? true;
this.groupPullRequestTitlePattern = options.groupPullRequestTitlePattern;
}
/**
* Pre-configure strategies.
* @param {Record<string, Strategy>} strategiesByPath Strategies indexed by path
* @returns {Record<string, Strategy>} Updated strategies indexed by path
*/
async preconfigure(
strategiesByPath: Record<string, Strategy>,
commitsByPath: Record<string, Commit[]>,
releasesByPath: Record<string, Release>
): Promise<Record<string, Strategy>> {
// Find all strategies in the group
const groupStrategies: Record<string, Strategy> = {};
for (const path in strategiesByPath) {
const strategy = strategiesByPath[path];
const component = await strategy.getComponent();
if (!component) {
continue;
}
if (this.components.has(component)) {
groupStrategies[path] = strategy;
}
}
this.logger.info(
`Found ${Object.keys(groupStrategies).length} group components for ${
this.groupName
}`
);
const groupVersions: Record<string, Version> = {};
const missingReleasePaths = new Set<string>();
for (const path in groupStrategies) {
const strategy = groupStrategies[path];
const latestRelease = releasesByPath[path];
const releasePullRequest = await strategy.buildReleasePullRequest(
parseConventionalCommits(commitsByPath[path], this.logger),
latestRelease
);
if (releasePullRequest?.version) {
groupVersions[path] = releasePullRequest.version;
} else {
missingReleasePaths.add(path);
}
}
const versions = Object.values(groupVersions);
if (versions.length === 0) {
return strategiesByPath;
}
const primaryVersion = versions.reduce(
(collector, version) =>
collector.compare(version) > 0 ? collector : version,
versions[0]
);
const newStrategies: Record<string, Strategy> = {};
for (const path in strategiesByPath) {
if (path in groupStrategies) {
const component = await strategiesByPath[path].getComponent();
this.logger.info(
`Replacing strategy for path ${path} with forced version: ${primaryVersion}`
);
newStrategies[path] = await buildStrategy({
...this.repositoryConfig[path],
github: this.github,
path,
targetBranch: this.targetBranch,
releaseAs: primaryVersion.toString(),
});
if (missingReleasePaths.has(path)) {
this.logger.debug(`Appending fake commit for path: ${path}`);
commitsByPath[path].push({
sha: '',
message: `chore(${component}): Synchronize ${
this.groupName
} versions\n\nRelease-As: ${primaryVersion.toString()}`,
});
}
} else {
newStrategies[path] = strategiesByPath[path];
}
}
return newStrategies;
}
/**
* Post-process candidate pull requests.
* @param {CandidateReleasePullRequest[]} pullRequests Candidate pull requests
* @returns {CandidateReleasePullRequest[]} Updated pull requests
*/
async run(
candidates: CandidateReleasePullRequest[]
): Promise<CandidateReleasePullRequest[]> {
if (!this.merge) {
return candidates;
}
const [inScopeCandidates, outOfScopeCandidates] = candidates.reduce(
(collection, candidate) => {
if (!candidate.pullRequest.version) {
this.logger.warn('pull request missing version', candidate);
collection[1].push(candidate);
return collection;
}
if (this.components.has(candidate.config.component || '')) {
collection[0].push(candidate);
} else {
collection[1].push(candidate);
}
return collection;
},
[[], []] as CandidateReleasePullRequest[][]
);
this.logger.info(
`found ${inScopeCandidates.length} linked-versions candidates`
);
// delegate to the merge plugin and add merged pull request
if (inScopeCandidates.length > 0) {
// Use configured pattern if available, otherwise default to "libraries" for backward compatibility
let pullRequestTitlePattern = this.groupPullRequestTitlePattern
? this.groupPullRequestTitlePattern
: `chore\${scope}: release ${this.groupName} libraries`;
// Replace ${component} placeholder with the actual group name
pullRequestTitlePattern = pullRequestTitlePattern.replace(
'${component}',
this.groupName
);
const merge = new Merge(
this.github,
this.targetBranch,
this.repositoryConfig,
{
pullRequestTitlePattern,
forceMerge: true,
headBranchName: BranchName.ofGroupTargetBranch(
this.groupName,
this.targetBranch
).toString(),
}
);
const merged = await merge.run(inScopeCandidates);
outOfScopeCandidates.push(...merged);
}
return outOfScopeCandidates;
}
}