UNPKG

35.6 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17 return c > 3 && r && Object.defineProperty(target, key, r), r;
18};
19var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
20 return new (P || (P = Promise))(function (resolve, reject) {
21 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
24 step((generator = generator.apply(thisArg, _arguments || [])).next());
25 });
26};
27var __generator = (this && this.__generator) || function (thisArg, body) {
28 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
29 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
30 function verb(n) { return function (v) { return step([n, v]); }; }
31 function step(op) {
32 if (f) throw new TypeError("Generator is already executing.");
33 while (_) try {
34 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
35 if (y = 0, t) op = [op[0] & 2, t.value];
36 switch (op[0]) {
37 case 0: case 1: t = op; break;
38 case 4: _.label++; return { value: op[1], done: false };
39 case 5: _.label++; y = op[1]; op = [0]; continue;
40 case 7: op = _.ops.pop(); _.trys.pop(); continue;
41 default:
42 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
43 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
44 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
45 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
46 if (t[2]) _.ops.pop();
47 _.trys.pop(); continue;
48 }
49 op = body.call(thisArg, _);
50 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
51 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
52 }
53};
54var __read = (this && this.__read) || function (o, n) {
55 var m = typeof Symbol === "function" && o[Symbol.iterator];
56 if (!m) return o;
57 var i = m.call(o), r, ar = [], e;
58 try {
59 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
60 }
61 catch (error) { e = { error: error }; }
62 finally {
63 try {
64 if (r && !r.done && (m = i["return"])) m.call(i);
65 }
66 finally { if (e) throw e.error; }
67 }
68 return ar;
69};
70var __spread = (this && this.__spread) || function () {
71 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
72 return ar;
73};
74var __importStar = (this && this.__importStar) || function (mod) {
75 if (mod && mod.__esModule) return mod;
76 var result = {};
77 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
78 result["default"] = mod;
79 return result;
80};
81var __importDefault = (this && this.__importDefault) || function (mod) {
82 return (mod && mod.__esModule) ? mod : { "default": mod };
83};
84Object.defineProperty(exports, "__esModule", { value: true });
85var _a;
86var fs = __importStar(require("fs"));
87var lodash_chunk_1 = __importDefault(require("lodash.chunk"));
88var semver_1 = require("semver");
89var util_1 = require("util");
90var typescript_memoize_1 = require("typescript-memoize");
91var changelog_1 = __importDefault(require("./changelog"));
92var log_parse_1 = __importDefault(require("./log-parse"));
93var semver_2 = __importStar(require("./semver"));
94var exec_promise_1 = __importDefault(require("./utils/exec-promise"));
95var logger_1 = require("./utils/logger");
96var make_hooks_1 = require("./utils/make-hooks");
97exports.defaultLabels = [
98 semver_2.default.major,
99 semver_2.default.minor,
100 semver_2.default.patch,
101 'skip-release',
102 'release',
103 'prerelease'
104];
105exports.isVersionLabel = function (label) {
106 return exports.defaultLabels.includes(label);
107};
108exports.defaultLabelDefinition = (_a = {},
109 _a[semver_2.default.major] = {
110 name: 'major',
111 title: '💥 Breaking Change',
112 description: 'Increment the major version when merged'
113 },
114 _a[semver_2.default.minor] = {
115 name: 'minor',
116 title: '🚀 Enhancement',
117 description: 'Increment the minor version when merged'
118 },
119 _a[semver_2.default.patch] = {
120 name: 'patch',
121 title: '🐛 Bug Fix',
122 description: 'Increment the patch version when merged'
123 },
124 _a['skip-release'] = {
125 name: 'skip-release',
126 description: 'Preserve the current version when merged'
127 },
128 _a.release = {
129 name: 'release',
130 description: 'Create a release when this pr is merged'
131 },
132 _a.prerelease = {
133 name: 'prerelease',
134 title: '🚧 Prerelease',
135 description: 'Create a pre-release version when merged'
136 },
137 _a.internal = {
138 name: 'internal',
139 title: '🏠 Internal',
140 description: 'Changes only affect the internal API'
141 },
142 _a.documentation = {
143 name: 'documentation',
144 title: '📝 Documentation',
145 description: 'Changes only affect the documentation'
146 },
147 _a);
148exports.getVersionMap = function (labels) {
149 if (labels === void 0) { labels = exports.defaultLabelDefinition; }
150 return Object.entries(labels).reduce(function (semVer, _a) {
151 var _b = __read(_a, 2), label = _b[0], labelDef = _b[1];
152 if (exports.isVersionLabel(label)) {
153 semVer.set(label, labelDef.name);
154 }
155 return semVer;
156 // tslint:disable-next-line align
157 }, new Map());
158};
159var readFile = util_1.promisify(fs.readFile);
160var writeFile = util_1.promisify(fs.writeFile);
161/**
162 * A class for interacting with the git remote
163 */
164var Release = /** @class */ (function () {
165 function Release(git, options, logger) {
166 if (options === void 0) { options = {
167 baseBranch: 'master',
168 skipReleaseLabels: [],
169 labels: exports.defaultLabelDefinition
170 }; }
171 if (logger === void 0) { logger = logger_1.dummyLog(); }
172 this.options = options;
173 this.logger = logger;
174 this.hooks = make_hooks_1.makeReleaseHooks();
175 this.versionLabels = exports.getVersionMap(options.labels);
176 this.git = git;
177 }
178 /**
179 * Generate a changelog from a range of commits.
180 *
181 * @param from sha or tag to start changelog from
182 * @param to sha or tag to end changelog at (defaults to HEAD)
183 */
184 Release.prototype.generateReleaseNotes = function (from, to) {
185 if (to === void 0) { to = 'HEAD'; }
186 return __awaiter(this, void 0, void 0, function () {
187 var commits, project, changelog;
188 return __generator(this, function (_a) {
189 switch (_a.label) {
190 case 0: return [4 /*yield*/, this.getCommitsInRelease(from, to)];
191 case 1:
192 commits = _a.sent();
193 return [4 /*yield*/, this.git.getProject()];
194 case 2:
195 project = _a.sent();
196 changelog = new changelog_1.default(this.logger, {
197 owner: this.git.options.owner,
198 repo: this.git.options.repo,
199 baseUrl: project.html_url,
200 jira: this.options.jira,
201 labels: this.options.labels,
202 baseBranch: this.options.baseBranch
203 });
204 this.hooks.onCreateChangelog.call(changelog);
205 changelog.loadDefaultHooks();
206 return [2 /*return*/, changelog.generateReleaseNotes(commits)];
207 }
208 });
209 });
210 };
211 Release.prototype.buildSearchQuery = function (commits) {
212 var repo = this.git.options.owner + "/" + this.git.options.repo;
213 var query = commits.reduce(function (q, commit) {
214 var subQuery = "repo:" + repo + " " + commit.hash;
215 return "\n " + q + "\n\n hash_" + commit.hash + ": search(query: \"" + subQuery + "\", type: ISSUE, first: 1) {\n edges {\n node {\n ... on PullRequest {\n number\n state\n body\n labels(first: 10) {\n edges {\n node {\n name\n }\n }\n }\n }\n }\n }\n }\n ";
216 }, '');
217 if (!query) {
218 return;
219 }
220 return "{\n " + query + "\n rateLimit {\n limit\n cost\n remaining\n resetAt\n }\n }";
221 };
222 Release.prototype.getCommitsInRelease = function (from, to) {
223 if (to === void 0) { to = 'HEAD'; }
224 return __awaiter(this, void 0, void 0, function () {
225 var allCommits, allPrCommits, allPrCommitHashes, labelled, commitsWithoutPR, batches, queries, data;
226 var _this = this;
227 return __generator(this, function (_a) {
228 switch (_a.label) {
229 case 0: return [4 /*yield*/, this.getCommits(from, to)];
230 case 1:
231 allCommits = _a.sent();
232 return [4 /*yield*/, Promise.all(allCommits
233 .filter(function (commit) { return commit.pullRequest; })
234 .map(function (commit) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
235 return [2 /*return*/, this.git.getCommitsForPR(Number(commit.pullRequest.number))];
236 }); }); }))];
237 case 2:
238 allPrCommits = _a.sent();
239 allPrCommitHashes = allPrCommits
240 .filter(Boolean)
241 .reduce(function (all, pr) { return __spread(all, pr.map(function (subCommit) { return subCommit.sha; })); }, []);
242 labelled = allCommits.filter(function (commit) {
243 return (commit.pullRequest || !allPrCommitHashes.includes(commit.hash)) &&
244 !commit.subject.includes('[skip ci]');
245 });
246 commitsWithoutPR = labelled.filter(function (commit) { return !commit.pullRequest; });
247 batches = lodash_chunk_1.default(commitsWithoutPR, 10);
248 return [4 /*yield*/, Promise.all(batches.map(function (batch) {
249 var batchQuery = _this.buildSearchQuery(batch);
250 if (!batchQuery) {
251 return;
252 }
253 return _this.git.graphql(batchQuery);
254 }))];
255 case 3:
256 queries = _a.sent();
257 data = queries.filter(function (q) { return Boolean(q); });
258 if (!data.length) {
259 return [2 /*return*/, labelled];
260 }
261 data.map(function (results) {
262 Object.entries(results).map(function (_a) {
263 var _b = __read(_a, 2), key = _b[0], result = _b[1];
264 var hash = key.split('hash_')[1];
265 var commit = commitsWithoutPR.find(function (commitWithoutPR) { return commitWithoutPR.hash === hash; });
266 if (!commit) {
267 return;
268 }
269 if (result.edges.length > 0) {
270 var labels = result.edges[0].node.labels
271 ? result.edges[0].node.labels.edges.map(function (edge) { return edge.node; })
272 : [];
273 commit.pullRequest = {
274 number: result.edges[0].node.number,
275 body: result.edges[0].node.body
276 };
277 commit.labels = __spread(labels.map(function (label) { return label.name; }), commit.labels);
278 }
279 else {
280 commit.labels = __spread(['pushToBaseBranch'], commit.labels);
281 }
282 commit.subject = commit.subject.split('\n')[0];
283 });
284 });
285 return [2 /*return*/, labelled];
286 }
287 });
288 });
289 };
290 /**
291 * Prepend a set of release notes to the changelog.md
292 *
293 * @param releaseNotes Release notes to prepend to the changelog
294 * @param lastRelease Last release version of the code. Could be the first commit SHA
295 * @param currentVersion Current version of the code
296 * @param message Message to commit the changelog with
297 */
298 Release.prototype.addToChangelog = function (releaseNotes, lastRelease, currentVersion, message) {
299 if (message === void 0) { message = 'Update CHANGELOG.md [skip ci]'; }
300 return __awaiter(this, void 0, void 0, function () {
301 var title, date, newChangelog, oldChangelog;
302 var _this = this;
303 return __generator(this, function (_a) {
304 switch (_a.label) {
305 case 0:
306 this.hooks.createChangelogTitle.tapPromise('Default', function () { return __awaiter(_this, void 0, void 0, function () {
307 var version, bump;
308 return __generator(this, function (_a) {
309 switch (_a.label) {
310 case 0:
311 if (!lastRelease.match(/\d+\.\d+\.\d+/)) return [3 /*break*/, 2];
312 return [4 /*yield*/, this.calcNextVersion(lastRelease)];
313 case 1:
314 version = _a.sent();
315 return [3 /*break*/, 4];
316 case 2: return [4 /*yield*/, this.getSemverBump(lastRelease)];
317 case 3:
318 bump = _a.sent();
319 version = semver_1.inc(currentVersion, bump);
320 _a.label = 4;
321 case 4:
322 this.logger.verbose.info('Calculated next version to be:', version);
323 if (!version) {
324 return [2 /*return*/, ''];
325 }
326 return [2 /*return*/, this.options.noVersionPrefix || version.startsWith('v')
327 ? version
328 : "v" + version];
329 }
330 });
331 }); });
332 this.logger.verbose.info('Adding new changes to changelog.');
333 return [4 /*yield*/, this.hooks.createChangelogTitle.promise()];
334 case 1:
335 title = _a.sent();
336 date = new Date().toDateString();
337 newChangelog = '#';
338 if (title) {
339 newChangelog += " " + title;
340 }
341 newChangelog += " (" + date + ")\n\n" + releaseNotes;
342 if (!fs.existsSync('CHANGELOG.md')) return [3 /*break*/, 3];
343 this.logger.verbose.info('Old changelog exists, prepending changes.');
344 return [4 /*yield*/, readFile('CHANGELOG.md', 'utf8')];
345 case 2:
346 oldChangelog = _a.sent();
347 newChangelog = newChangelog + "\n\n---\n\n" + oldChangelog;
348 _a.label = 3;
349 case 3: return [4 /*yield*/, writeFile('CHANGELOG.md', newChangelog)];
350 case 4:
351 _a.sent();
352 this.logger.verbose.info('Wrote new changelog to filesystem.');
353 return [4 /*yield*/, exec_promise_1.default('git', ['add', 'CHANGELOG.md'])];
354 case 5:
355 _a.sent();
356 return [4 /*yield*/, exec_promise_1.default('git', ['commit', '-m', "\"" + message + "\"", '--no-verify'])];
357 case 6:
358 _a.sent();
359 this.logger.verbose.info('Commited new changelog.');
360 return [2 /*return*/];
361 }
362 });
363 });
364 };
365 /**
366 * Get a range of commits. The commits will have PR numbers and labels attached
367 *
368 * @param from Tag or SHA to start at
369 * @param to Tage or SHA to end at (defaults to HEAD)
370 */
371 Release.prototype.getCommits = function (from, to) {
372 if (to === void 0) { to = 'HEAD'; }
373 return __awaiter(this, void 0, void 0, function () {
374 var gitlog, logParse, commits;
375 var _this = this;
376 return __generator(this, function (_a) {
377 switch (_a.label) {
378 case 0:
379 this.logger.verbose.info("Getting commits from " + from + " to " + to);
380 return [4 /*yield*/, this.git.getGitLog(from, to)];
381 case 1:
382 gitlog = _a.sent();
383 this.logger.veryVerbose.info('Got gitlog:\n', gitlog);
384 return [4 /*yield*/, this.createLogParse()];
385 case 2:
386 logParse = _a.sent();
387 return [4 /*yield*/, logParse.normalizeCommits(gitlog)];
388 case 3:
389 commits = _a.sent();
390 this.logger.veryVerbose.info('Added labels to commits:\n', commits);
391 return [4 /*yield*/, Promise.all(commits.map(function (commit) { return __awaiter(_this, void 0, void 0, function () {
392 var resolvedAuthors, prCommits, author;
393 var _this = this;
394 return __generator(this, function (_a) {
395 switch (_a.label) {
396 case 0:
397 resolvedAuthors = [];
398 if (!commit.pullRequest) return [3 /*break*/, 3];
399 return [4 /*yield*/, this.git.getCommitsForPR(Number(commit.pullRequest.number))];
400 case 1:
401 prCommits = _a.sent();
402 if (!prCommits) {
403 return [2 /*return*/];
404 }
405 return [4 /*yield*/, Promise.all(prCommits.map(function (prCommit) { return __awaiter(_this, void 0, void 0, function () {
406 return __generator(this, function (_a) {
407 if (prCommit && prCommit.author) {
408 return [2 /*return*/, this.git.getUserByUsername(prCommit.author.login)];
409 }
410 return [2 /*return*/];
411 });
412 }); }))];
413 case 2:
414 resolvedAuthors = _a.sent();
415 return [3 /*break*/, 5];
416 case 3:
417 if (!commit.authorEmail) return [3 /*break*/, 5];
418 return [4 /*yield*/, this.git.getUserByEmail(commit.authorEmail)];
419 case 4:
420 author = _a.sent();
421 resolvedAuthors.push(author);
422 _a.label = 5;
423 case 5:
424 commit.authors = resolvedAuthors.map(function (author) { return (__assign({}, author, { username: author ? author.login : undefined })); });
425 commit.authors.map(function (author) {
426 _this.logger.veryVerbose.info("Found author: " + author.username);
427 });
428 return [2 /*return*/];
429 }
430 });
431 }); }))];
432 case 4:
433 _a.sent();
434 return [2 /*return*/, commits];
435 }
436 });
437 });
438 };
439 Release.prototype.addLabelsToProject = function (labels, options) {
440 if (options === void 0) { options = {}; }
441 return __awaiter(this, void 0, void 0, function () {
442 var oldLabels, labelsToCreate, repoMetadata, justLabelNames, state, state;
443 var _this = this;
444 return __generator(this, function (_a) {
445 switch (_a.label) {
446 case 0: return [4 /*yield*/, this.git.getProjectLabels()];
447 case 1:
448 oldLabels = _a.sent();
449 labelsToCreate = Object.entries(labels).filter(function (_a) {
450 var _b = __read(_a, 2), versionLabel = _b[0], labelDef = _b[1];
451 if (!labelDef) {
452 return;
453 }
454 if (versionLabel === 'release' &&
455 !_this.options.onlyPublishWithReleaseLabel) {
456 return;
457 }
458 if (versionLabel === 'skip-release' &&
459 _this.options.onlyPublishWithReleaseLabel) {
460 return;
461 }
462 return true;
463 });
464 if (!!options.dryRun) return [3 /*break*/, 3];
465 return [4 /*yield*/, Promise.all(labelsToCreate.map(function (_a) {
466 var _b = __read(_a, 2), label = _b[0], labelDef = _b[1];
467 return __awaiter(_this, void 0, void 0, function () {
468 return __generator(this, function (_c) {
469 switch (_c.label) {
470 case 0:
471 if (!labelDef) {
472 return [2 /*return*/];
473 }
474 if (!(oldLabels && oldLabels.includes(labelDef.name))) return [3 /*break*/, 2];
475 return [4 /*yield*/, this.git.updateLabel(label, labelDef)];
476 case 1:
477 _c.sent();
478 return [3 /*break*/, 4];
479 case 2: return [4 /*yield*/, this.git.createLabel(label, labelDef)];
480 case 3:
481 _c.sent();
482 _c.label = 4;
483 case 4: return [2 /*return*/];
484 }
485 });
486 });
487 }))];
488 case 2:
489 _a.sent();
490 _a.label = 3;
491 case 3: return [4 /*yield*/, this.git.getProject()];
492 case 4:
493 repoMetadata = _a.sent();
494 justLabelNames = labelsToCreate.map(function (_a) {
495 var _b = __read(_a, 1), name = _b[0];
496 return name;
497 });
498 if (justLabelNames.length > 0) {
499 state = options.dryRun ? 'Would have created' : 'Created';
500 this.logger.log.log(state + " labels: " + justLabelNames.join(', '));
501 }
502 else {
503 state = options.dryRun ? 'would have been' : 'were';
504 this.logger.log.log("No labels " + state + " created, they must have already been present on your project.");
505 }
506 if (options.dryRun) {
507 return [2 /*return*/];
508 }
509 this.logger.log.log("\nYou can see these, and more at " + repoMetadata.html_url + "/labels");
510 return [2 /*return*/];
511 }
512 });
513 });
514 };
515 /**
516 * Calculate the SEMVER bump over a range of commits using the PR labels
517 *
518 * @param from Tag or SHA to start at
519 * @param to Tag or SHA to end at (defaults to HEAD)
520 */
521 Release.prototype.getSemverBump = function (from, to) {
522 if (to === void 0) { to = 'HEAD'; }
523 return __awaiter(this, void 0, void 0, function () {
524 var commits, labels, _a, onlyPublishWithReleaseLabel, skipReleaseLabels, options, result;
525 return __generator(this, function (_b) {
526 switch (_b.label) {
527 case 0: return [4 /*yield*/, this.getCommits(from, to)];
528 case 1:
529 commits = _b.sent();
530 labels = commits.map(function (commit) { return commit.labels; });
531 _a = this.options, onlyPublishWithReleaseLabel = _a.onlyPublishWithReleaseLabel, skipReleaseLabels = _a.skipReleaseLabels;
532 options = { onlyPublishWithReleaseLabel: onlyPublishWithReleaseLabel, skipReleaseLabels: skipReleaseLabels };
533 this.logger.verbose.info('Calculating SEMVER bump using:\n', {
534 labels: labels,
535 versionLabels: this.versionLabels,
536 options: options
537 });
538 result = semver_2.calculateSemVerBump(labels, this.versionLabels, options);
539 this.logger.verbose.success('Calculated SEMVER bump:', result);
540 return [2 /*return*/, result];
541 }
542 });
543 });
544 };
545 Release.prototype.calcNextVersion = function (lastTag) {
546 return __awaiter(this, void 0, void 0, function () {
547 var bump;
548 return __generator(this, function (_a) {
549 switch (_a.label) {
550 case 0: return [4 /*yield*/, this.getSemverBump(lastTag)];
551 case 1:
552 bump = _a.sent();
553 return [2 /*return*/, semver_1.inc(lastTag, bump)];
554 }
555 });
556 });
557 };
558 Release.prototype.createLogParse = function () {
559 return __awaiter(this, void 0, void 0, function () {
560 var logParse;
561 var _this = this;
562 return __generator(this, function (_a) {
563 logParse = new log_parse_1.default();
564 logParse.hooks.parseCommit.tapPromise('PR Information', function (commit) {
565 return _this.addPrInfoToCommit(commit);
566 });
567 logParse.hooks.parseCommit.tapPromise('PR Commits', function (commit) { return __awaiter(_this, void 0, void 0, function () {
568 var prsSinceLastRelease;
569 return __generator(this, function (_a) {
570 switch (_a.label) {
571 case 0: return [4 /*yield*/, this.getPRsSinceLastRelease()];
572 case 1:
573 prsSinceLastRelease = _a.sent();
574 return [2 /*return*/, this.getPRForRebasedCommits(commit, prsSinceLastRelease)];
575 }
576 });
577 }); });
578 this.hooks.onCreateLogParse.call(logParse);
579 return [2 /*return*/, logParse];
580 });
581 });
582 };
583 Release.prototype.getPRsSinceLastRelease = function () {
584 return __awaiter(this, void 0, void 0, function () {
585 var lastRelease, error_1, firstCommit, _a, prsSinceLastRelease, data;
586 var _this = this;
587 return __generator(this, function (_b) {
588 switch (_b.label) {
589 case 0:
590 _b.trys.push([0, 2, , 5]);
591 return [4 /*yield*/, this.git.getLatestReleaseInfo()];
592 case 1:
593 lastRelease = _b.sent();
594 return [3 /*break*/, 5];
595 case 2:
596 error_1 = _b.sent();
597 return [4 /*yield*/, this.git.getFirstCommit()];
598 case 3:
599 firstCommit = _b.sent();
600 _a = {};
601 return [4 /*yield*/, this.git.getCommitDate(firstCommit)];
602 case 4:
603 lastRelease = (_a.published_at = _b.sent(),
604 _a);
605 return [3 /*break*/, 5];
606 case 5:
607 if (!lastRelease) {
608 return [2 /*return*/, []];
609 }
610 return [4 /*yield*/, this.git.searchRepo({
611 q: "is:pr is:merged merged:>=" + lastRelease.published_at
612 })];
613 case 6:
614 prsSinceLastRelease = _b.sent();
615 if (!prsSinceLastRelease || !prsSinceLastRelease.items) {
616 return [2 /*return*/, []];
617 }
618 return [4 /*yield*/, Promise.all(prsSinceLastRelease.items.map(function (pr) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
619 return [2 /*return*/, this.git.getPullRequest(Number(pr.number))];
620 }); }); }))];
621 case 7:
622 data = _b.sent();
623 return [2 /*return*/, data.map(function (item) { return item.data; })];
624 }
625 });
626 });
627 };
628 /**
629 * Add the PR info (labels and body) to the commit
630 *
631 * @param commits Commits to modify
632 */
633 Release.prototype.addPrInfoToCommit = function (commit) {
634 return __awaiter(this, void 0, void 0, function () {
635 var info, labels;
636 return __generator(this, function (_a) {
637 switch (_a.label) {
638 case 0:
639 if (!commit.labels) {
640 commit.labels = [];
641 }
642 if (!commit.pullRequest) return [3 /*break*/, 2];
643 return [4 /*yield*/, this.git.getPr(commit.pullRequest.number)];
644 case 1:
645 info = _a.sent();
646 if (!info || !info.data) {
647 return [2 /*return*/, commit];
648 }
649 labels = info ? info.data.labels.map(function (l) { return l.name; }) : [];
650 commit.labels = __spread(labels, commit.labels);
651 commit.pullRequest.body = info.data.body;
652 _a.label = 2;
653 case 2: return [2 /*return*/, commit];
654 }
655 });
656 });
657 };
658 /**
659 * Commits from rebased PRs do not have messages that tie them to a PR
660 * Instead we have to find all PRs since the last release and try to match
661 * their merge commit SHAs.
662 *
663 * @param commits Commits to modify
664 */
665 Release.prototype.getPRForRebasedCommits = function (commit, pullRequests) {
666 var matchPr = pullRequests.find(function (pr) { return pr.merge_commit_sha === commit.hash; });
667 if (!commit.pullRequest && matchPr) {
668 var labels = matchPr.labels.map(function (label) { return label.name; }) || [];
669 commit.labels = __spread(labels, commit.labels);
670 commit.pullRequest = {
671 number: matchPr.number
672 };
673 }
674 return commit;
675 };
676 __decorate([
677 typescript_memoize_1.Memoize()
678 ], Release.prototype, "createLogParse", null);
679 __decorate([
680 typescript_memoize_1.Memoize()
681 ], Release.prototype, "getPRsSinceLastRelease", null);
682 return Release;
683}());
684exports.default = Release;
685//# sourceMappingURL=release.js.map
\No newline at end of file