UNPKG

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