UNPKG

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