-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathrelease.js
289 lines (245 loc) · 8.47 KB
/
release.js
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
'use strict';
const fetch = require('make-fetch-happen');
const semver = require('semver');
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
const path = require('path');
const { promises: fs } = require('fs');
const monorepoRoot = path.join(__dirname, '..');
const compassPackagePath = path.join(monorepoRoot, 'packages', 'compass');
const compassPackageJsonPath = path.join(compassPackagePath, 'package.json');
const { program } = require('commander');
const BETA_RELEASE_BRANCH = 'beta-releases';
const GA_RELEASE_BRANCH = 'ga-releases';
if (process.env.GITHUB_ACTIONS !== 'true') {
throw new Error('this script is meant to be run from CI');
}
program
.command('beta')
.description('Starts a new beta')
.option('--merge-branch <mergeBranch>', 'branch to merge', 'main')
.option('--submitter <name>', 'github username of the releaser', '')
.option(
'--next-ga [nextGa]',
'next ga version, default to the next GA version in Jira'
)
.action(async (options) => {
// can happen if `--merge-branch=` is passed as argument
if (!options.mergeBranch) {
throw new Error('mergeBranch is required');
}
if (!options.submitter) {
throw new Error('submitter is required');
}
const publisher = await getReleasePublisher(options.submitter);
const nextGa = options.nextGa || (await getNextGaVersionInJira());
if (!nextGa) {
throw new Error('next ga not found');
}
console.info(`Found ${nextGa} as next ga version in Jira`);
await gitCheckout(BETA_RELEASE_BRANCH);
const currentCompassPackageVersion = await getCompassPackageVersion();
// if the previous version is newer we fail the release:
if (semver.gte(currentCompassPackageVersion, nextGa)) {
throw new Error(
`the previous release in ${BETA_RELEASE_BRANCH} is already >= then the next GA ${nextGa}`
);
}
// if the previous version is too old (not in the range of the same minor GA)
// we can assume that we need to release a beta.0,
// otherwise we bump whatever other prerelease was in the package.json:
const nextBeta = semver.lt(currentCompassPackageVersion, `${nextGa}-beta.0`)
? `${nextGa}-beta.0`
: semver.inc(currentCompassPackageVersion, 'prerelease', 'beta');
console.info(`Promoting ${currentCompassPackageVersion} to ${nextBeta}`);
await syncWithBranch(options.mergeBranch);
await bumpAndPush(nextBeta, BETA_RELEASE_BRANCH, publisher);
});
program
.command('ga')
.description('Starts a new GA')
.option('--release-ticket <releaseTicket>')
.option('--submitter <name>', 'github username of the releaser', '')
.option(
'--merge-branch <mergeBranch>',
'branch to merge',
BETA_RELEASE_BRANCH
)
.action(async (options) => {
if (!options.releaseTicket) {
throw new Error('releaseTicket is required');
}
// can happen if `--merge-branch=` is passed as argument
if (!options.mergeBranch) {
throw new Error('mergeBranch is required');
}
if (!options.submitter) {
throw new Error('submitter is required');
}
const publisher = await getReleasePublisher(options.submitter);
const nextGa = await getReleaseVersionFromTicket(options.releaseTicket);
if (!nextGa) {
throw new Error('next ga not found');
}
console.info(`Found ${nextGa} as fixVersion in ${options.releaseTicket}`);
await gitCheckout(GA_RELEASE_BRANCH);
await syncWithBranch(options.mergeBranch);
const currentCompassPackageVersion = await getCompassPackageVersion();
if (semver.gte(currentCompassPackageVersion, nextGa)) {
throw new Error(
`Error: the previous release in the merged branch (${options.mergeBranch}) (${currentCompassPackageVersion}) is >= ${nextGa}.`
);
}
console.info(`Promoting ${currentCompassPackageVersion} to ${nextGa}`);
await bumpAndPush(nextGa, GA_RELEASE_BRANCH, publisher);
});
program.parseAsync();
// ---
// syncs the current branch with the content of the incoming branch:
// performs a merge and a clean checkout of the contents from the incoming
// branch to the current branch.
// This way we always release exactly what is in the incoming branch,
// we don't destroy the history of the release and incoming branches,
// and we never incur in conflicts.
async function syncWithBranch(branch) {
const remoteBranch = branch.startsWith('origin/')
? branch
: `origin/${branch}`;
await execFile(
'git',
['merge', '--no-ff', '--strategy-option=theirs', remoteBranch],
{
cwd: monorepoRoot,
}
);
// remove any tracked and untracked file keeping .git (this would also remove node_modules, which we don't need at this point)
// and then copies all of the files from the incoming branch.
// this seems to be the most reliable and non-destructive way to add a commit that would resync
// the target branch with the incoming branch, undoing any previous change that may have come
// from hot-fixes, and ensuring that we are releasing exactly the content of the incoming branch.
await execFile('git', ['clean', '-fdx']);
await execFile('git', ['rm', '-r', '*'], {
cwd: monorepoRoot,
});
await execFile('git', ['checkout', remoteBranch, '--', '.'], {
cwd: monorepoRoot,
});
// will be committed in bumpAndPush
await execFile('git', ['add', '.'], {
cwd: monorepoRoot,
});
}
async function getCompassPackageVersion() {
return JSON.parse(await fs.readFile(compassPackageJsonPath)).version;
}
async function gitCheckout(branchName) {
await execFile('git', ['checkout', branchName], {
cwd: monorepoRoot,
});
}
async function getReleasePublisher(submitter) {
const publisherData = (
await execFile(
'git',
['check-mailmap', `<${submitter}@users.noreply.github.com>`],
{
cwd: monorepoRoot,
encoding: 'utf8',
}
)
).stdout.trim();
if (!publisherData.match(/^[^<]+<[^@>][email protected]>/)) {
throw new Error(
`Could not translate username ${submitter} to recognized authorized email (${publisherData})`
);
}
return publisherData;
}
async function bumpAndPush(nextVersion, releaseBranch, publisher) {
await execFile('npm', ['version', '--no-git-tag-version', nextVersion], {
cwd: compassPackagePath,
});
await fs.writeFile(
compassPackageJsonPath,
JSON.stringify(
{
...JSON.parse(await fs.readFile(compassPackageJsonPath, 'utf8')),
releasePublisher: publisher,
},
null,
2
) + '\n'
);
await execFile('git', ['add', compassPackageJsonPath, `package-lock.json`], {
cwd: monorepoRoot,
});
await execFile('git', ['commit', '--no-verify', '-m', `v${nextVersion}`], {
cwd: monorepoRoot,
});
await execFile('git', ['tag', `v${nextVersion}`], { cwd: monorepoRoot });
await execFile('git', ['push', 'origin', `${releaseBranch}`], {
cwd: monorepoRoot,
});
await execFile('git', ['push', '--tags'], { cwd: monorepoRoot });
const currentBranch = (
await execFile('git', ['branch', '--show-current'], {
cwd: monorepoRoot,
encoding: 'utf8',
})
).stdout.trim();
await execFile(
'gh',
[
'workflow',
'run',
'codeql.yml',
'-R',
'mongodb-js/compass',
'-r',
currentBranch,
],
{ cwd: monorepoRoot }
);
}
// NOTE: if there are more "unreleased" versions it will
// pick the first one, not the latest.
async function getNextGaVersionInJira() {
const versions = await (
await fetch(
'https://jira.mongodb.org/rest/api/2/project/COMPASS/versions',
{
method: 'GET',
headers: {
Accept: 'application/json',
},
}
)
).json();
const nextGa = versions
.filter((v) => v.released === false && v.archived === false)
.map((v) => v.name)
.filter(semver.valid)
.filter((v) => !semver.prerelease(v))
.sort(semver.compare)[0];
return nextGa;
}
async function getReleaseVersionFromTicket(releaseTicket) {
const ticket = await (
await fetch(`https://jira.mongodb.org/rest/api/2/issue/${releaseTicket}`, {
method: 'GET',
headers: {
Accept: 'application/json',
},
})
).json();
if (ticket.fields.issuetype.name !== 'Release') {
throw new Error(`${releaseTicket} is not a Release ticket`);
}
const nextGa = ticket.fields.fixVersions?.[0]?.name;
if (!nextGa || !semver.valid(nextGa) || semver.prerelease(nextGa)) {
throw new Error(
`no valid fixVersion found on the Release ticket ${releaseTicket}`
);
}
return nextGa;
}