UNPKG

36.5 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15var __assign = (this && this.__assign) || function () {
16 __assign = Object.assign || function(t) {
17 for (var s, i = 1, n = arguments.length; i < n; i++) {
18 s = arguments[i];
19 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20 t[p] = s[p];
21 }
22 return t;
23 };
24 return __assign.apply(this, arguments);
25};
26var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
27 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
28 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
29 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;
30 return c > 3 && r && Object.defineProperty(target, key, r), r;
31};
32var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
33 return new (P || (P = Promise))(function (resolve, reject) {
34 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
35 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
36 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
37 step((generator = generator.apply(thisArg, _arguments || [])).next());
38 });
39};
40var __generator = (this && this.__generator) || function (thisArg, body) {
41 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
42 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
43 function verb(n) { return function (v) { return step([n, v]); }; }
44 function step(op) {
45 if (f) throw new TypeError("Generator is already executing.");
46 while (_) try {
47 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;
48 if (y = 0, t) op = [op[0] & 2, t.value];
49 switch (op[0]) {
50 case 0: case 1: t = op; break;
51 case 4: _.label++; return { value: op[1], done: false };
52 case 5: _.label++; y = op[1]; op = [0]; continue;
53 case 7: op = _.ops.pop(); _.trys.pop(); continue;
54 default:
55 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
56 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
57 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
58 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
59 if (t[2]) _.ops.pop();
60 _.trys.pop(); continue;
61 }
62 op = body.call(thisArg, _);
63 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
64 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
65 }
66};
67var __read = (this && this.__read) || function (o, n) {
68 var m = typeof Symbol === "function" && o[Symbol.iterator];
69 if (!m) return o;
70 var i = m.call(o), r, ar = [], e;
71 try {
72 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
73 }
74 catch (error) { e = { error: error }; }
75 finally {
76 try {
77 if (r && !r.done && (m = i["return"])) m.call(i);
78 }
79 finally { if (e) throw e.error; }
80 }
81 return ar;
82};
83var __importDefault = (this && this.__importDefault) || function (mod) {
84 return (mod && mod.__esModule) ? mod : { "default": mod };
85};
86Object.defineProperty(exports, "__esModule", { value: true });
87var graphql_1 = __importDefault(require("@octokit/graphql"));
88var plugin_enterprise_compatibility_1 = __importDefault(require("@octokit/plugin-enterprise-compatibility"));
89var plugin_retry_1 = __importDefault(require("@octokit/plugin-retry"));
90var plugin_throttling_1 = __importDefault(require("@octokit/plugin-throttling"));
91var rest_1 = __importDefault(require("@octokit/rest"));
92var gitlog_1 = __importDefault(require("gitlog"));
93var tinycolor2_1 = __importDefault(require("tinycolor2"));
94var util_1 = require("util");
95var typescript_memoize_1 = require("typescript-memoize");
96var exec_promise_1 = __importDefault(require("./utils/exec-promise"));
97var logger_1 = require("./utils/logger");
98var gitlog = util_1.promisify(gitlog_1.default);
99var GitAPIError = /** @class */ (function (_super) {
100 __extends(GitAPIError, _super);
101 function GitAPIError(api, args, origError) {
102 return _super.call(this, "Error calling github: " + api + "\n\twith: " + JSON.stringify(args) + ".\n\t" + origError.message) || this;
103 }
104 return GitAPIError;
105}(Error));
106var makeIdentifier = function (type, context) {
107 return "<!-- GITHUB_RELEASE " + type + ": " + context + " -->";
108};
109var makeCommentIdentifier = function (context) {
110 return makeIdentifier('COMMENT', context);
111};
112var makePrBodyIdentifier = function (context) {
113 return makeIdentifier('PR BODY', context);
114};
115// A class to interact with the local git instance and the git remote.
116// currently it only interfaces with GitHub.
117var Git = /** @class */ (function () {
118 function Git(options, logger) {
119 if (logger === void 0) { logger = logger_1.dummyLog(); }
120 var _this = this;
121 this.logger = logger;
122 this.options = options;
123 this.baseUrl = this.options.baseUrl || 'https://api.github.com';
124 this.graphqlBaseUrl = this.options.graphqlBaseUrl || this.baseUrl;
125 this.logger.veryVerbose.info("Initializing GitHub with: " + this.baseUrl);
126 var gitHub = rest_1.default.plugin(plugin_enterprise_compatibility_1.default)
127 .plugin(plugin_retry_1.default)
128 .plugin(plugin_throttling_1.default);
129 this.ghub = new gitHub({
130 baseUrl: this.baseUrl,
131 auth: this.options.token,
132 previews: ['symmetra-preview'],
133 throttle: {
134 onRateLimit: function (retryAfter, opts) {
135 _this.logger.log.warn("Request quota exhausted for request " + opts.method + " " + opts.url);
136 if (opts.request.retryCount < 5) {
137 _this.logger.verbose.log("Retrying after " + retryAfter + " seconds!");
138 return true;
139 }
140 },
141 onAbuseLimit: function (retryAfter, opts) {
142 // does not retry, only logs an error
143 _this.logger.log.error("Went over abuse rate limit " + opts.method + " " + opts.url);
144 }
145 }
146 });
147 this.ghub.hook.error('request', function (error) {
148 if (error && error.headers && error.headers.authorization) {
149 delete error.headers.authorization;
150 }
151 throw error;
152 });
153 }
154 Git.prototype.getLatestReleaseInfo = function () {
155 return __awaiter(this, void 0, void 0, function () {
156 var latestRelease;
157 return __generator(this, function (_a) {
158 switch (_a.label) {
159 case 0: return [4 /*yield*/, this.ghub.repos.getLatestRelease({
160 owner: this.options.owner,
161 repo: this.options.repo
162 })];
163 case 1:
164 latestRelease = _a.sent();
165 return [2 /*return*/, latestRelease.data];
166 }
167 });
168 });
169 };
170 Git.prototype.getLatestRelease = function () {
171 return __awaiter(this, void 0, void 0, function () {
172 var latestRelease, e_1;
173 return __generator(this, function (_a) {
174 switch (_a.label) {
175 case 0:
176 _a.trys.push([0, 2, , 3]);
177 return [4 /*yield*/, this.getLatestReleaseInfo()];
178 case 1:
179 latestRelease = _a.sent();
180 this.logger.veryVerbose.info('Got response for "getLatestRelease":\n', latestRelease);
181 this.logger.verbose.info('Got latest release:\n', latestRelease);
182 return [2 /*return*/, latestRelease.tag_name];
183 case 2:
184 e_1 = _a.sent();
185 if (e_1.status === 404) {
186 this.logger.verbose.info("Couldn't find latest release on GitHub, using first commit.");
187 return [2 /*return*/, this.getFirstCommit()];
188 }
189 throw e_1;
190 case 3: return [2 /*return*/];
191 }
192 });
193 });
194 };
195 Git.prototype.getCommitDate = function (sha) {
196 return __awaiter(this, void 0, void 0, function () {
197 var date, _a, day, time, timezone;
198 return __generator(this, function (_b) {
199 switch (_b.label) {
200 case 0: return [4 /*yield*/, exec_promise_1.default('git', ['show', '-s', '--format=%ci', sha])];
201 case 1:
202 date = _b.sent();
203 _a = __read(date.split(' '), 3), day = _a[0], time = _a[1], timezone = _a[2];
204 return [2 /*return*/, day + "T" + time + timezone];
205 }
206 });
207 });
208 };
209 Git.prototype.getFirstCommit = function () {
210 return __awaiter(this, void 0, void 0, function () {
211 var list;
212 return __generator(this, function (_a) {
213 switch (_a.label) {
214 case 0: return [4 /*yield*/, exec_promise_1.default('git', ['rev-list', 'HEAD'])];
215 case 1:
216 list = _a.sent();
217 return [2 /*return*/, list.split('\n').pop()];
218 }
219 });
220 });
221 };
222 /**
223 * Get the SHA of the latest commit
224 */
225 Git.prototype.getSha = function (short) {
226 return __awaiter(this, void 0, void 0, function () {
227 var result;
228 return __generator(this, function (_a) {
229 switch (_a.label) {
230 case 0: return [4 /*yield*/, exec_promise_1.default('git', [
231 'rev-parse',
232 short && '--short',
233 'HEAD'
234 ])];
235 case 1:
236 result = _a.sent();
237 this.logger.verbose.info("Got commit SHA from HEAD: " + result);
238 return [2 /*return*/, result];
239 }
240 });
241 });
242 };
243 Git.prototype.getLabels = function (prNumber) {
244 return __awaiter(this, void 0, void 0, function () {
245 var args, labels, e_2;
246 return __generator(this, function (_a) {
247 switch (_a.label) {
248 case 0:
249 this.logger.verbose.info("Getting labels for PR: " + prNumber);
250 args = {
251 owner: this.options.owner,
252 repo: this.options.repo,
253 issue_number: prNumber
254 };
255 this.logger.verbose.info('Getting issue labels using:', args);
256 _a.label = 1;
257 case 1:
258 _a.trys.push([1, 3, , 4]);
259 return [4 /*yield*/, this.ghub.issues.listLabelsOnIssue(args)];
260 case 2:
261 labels = _a.sent();
262 this.logger.veryVerbose.info('Got response for "listLabelsOnIssue":\n', labels);
263 this.logger.verbose.info('Found labels on PR:\n', labels.data);
264 return [2 /*return*/, labels.data.map(function (l) { return l.name; })];
265 case 3:
266 e_2 = _a.sent();
267 throw new GitAPIError('listLabelsOnIssue', args, e_2);
268 case 4: return [2 /*return*/];
269 }
270 });
271 });
272 };
273 Git.prototype.getProjectLabels = function () {
274 return __awaiter(this, void 0, void 0, function () {
275 var args, labels, e_3;
276 return __generator(this, function (_a) {
277 switch (_a.label) {
278 case 0:
279 this.logger.verbose.info("Getting labels for project: " + this.options.repo);
280 args = {
281 owner: this.options.owner,
282 repo: this.options.repo
283 };
284 _a.label = 1;
285 case 1:
286 _a.trys.push([1, 3, , 4]);
287 return [4 /*yield*/, this.ghub.issues.listLabelsForRepo(args)];
288 case 2:
289 labels = _a.sent();
290 this.logger.veryVerbose.info('Got response for "getProjectLabels":\n', labels);
291 this.logger.verbose.info('Found labels on project:\n', labels.data);
292 return [2 /*return*/, labels.data.map(function (l) { return l.name; })];
293 case 3:
294 e_3 = _a.sent();
295 throw new GitAPIError('getProjectLabels', args, e_3);
296 case 4: return [2 /*return*/];
297 }
298 });
299 });
300 };
301 Git.prototype.getGitLog = function (start, end) {
302 if (end === void 0) { end = 'HEAD'; }
303 return __awaiter(this, void 0, void 0, function () {
304 var log;
305 return __generator(this, function (_a) {
306 switch (_a.label) {
307 case 0: return [4 /*yield*/, gitlog({
308 repo: process.cwd(),
309 number: Number.MAX_SAFE_INTEGER,
310 fields: ['hash', 'authorName', 'authorEmail', 'rawBody'],
311 branch: start.trim() + ".." + end.trim(),
312 execOptions: { maxBuffer: 1000 * 1024 }
313 })];
314 case 1:
315 log = _a.sent();
316 return [2 /*return*/, log.map(function (commit) { return ({
317 hash: commit.hash,
318 authorName: commit.authorName,
319 authorEmail: commit.authorEmail,
320 subject: commit.rawBody
321 }); })];
322 }
323 });
324 });
325 };
326 Git.prototype.getUserByEmail = function (email) {
327 return __awaiter(this, void 0, void 0, function () {
328 var search, error_1;
329 return __generator(this, function (_a) {
330 switch (_a.label) {
331 case 0:
332 _a.trys.push([0, 2, , 3]);
333 return [4 /*yield*/, this.ghub.search.users({
334 q: "in:email " + email
335 })];
336 case 1:
337 search = (_a.sent()).data;
338 return [2 /*return*/, search && search.items.length > 0
339 ? search.items[0]
340 : { login: email }];
341 case 2:
342 error_1 = _a.sent();
343 this.logger.verbose.warn("Could not find user by email: " + email);
344 return [3 /*break*/, 3];
345 case 3: return [2 /*return*/];
346 }
347 });
348 });
349 };
350 Git.prototype.getUserByUsername = function (username) {
351 return __awaiter(this, void 0, void 0, function () {
352 var user, error_2;
353 return __generator(this, function (_a) {
354 switch (_a.label) {
355 case 0:
356 _a.trys.push([0, 2, , 3]);
357 return [4 /*yield*/, this.ghub.users.getByUsername({
358 username: username
359 })];
360 case 1:
361 user = _a.sent();
362 return [2 /*return*/, user.data];
363 case 2:
364 error_2 = _a.sent();
365 this.logger.verbose.warn("Could not find user by username: " + username);
366 return [3 /*break*/, 3];
367 case 3: return [2 /*return*/];
368 }
369 });
370 });
371 };
372 Git.prototype.getPullRequest = function (pr) {
373 return __awaiter(this, void 0, void 0, function () {
374 var args, result;
375 return __generator(this, function (_a) {
376 switch (_a.label) {
377 case 0:
378 this.logger.verbose.info("Getting Pull Request: " + pr);
379 args = {
380 owner: this.options.owner,
381 repo: this.options.repo,
382 pull_number: pr
383 };
384 this.logger.verbose.info('Getting pull request info using:', args);
385 return [4 /*yield*/, this.ghub.pulls.get(args)];
386 case 1:
387 result = _a.sent();
388 this.logger.veryVerbose.info('Got pull request data\n', result);
389 this.logger.verbose.info('Got pull request info');
390 return [2 /*return*/, result];
391 }
392 });
393 });
394 };
395 Git.prototype.searchRepo = function (options) {
396 return __awaiter(this, void 0, void 0, function () {
397 var repo, result;
398 return __generator(this, function (_a) {
399 switch (_a.label) {
400 case 0:
401 repo = "repo:" + this.options.owner + "/" + this.options.repo;
402 options.q = repo + " " + options.q;
403 this.logger.verbose.info('Searching repo using:\n', options);
404 return [4 /*yield*/, this.ghub.search.issuesAndPullRequests(options)];
405 case 1:
406 result = _a.sent();
407 this.logger.veryVerbose.info('Got response from search\n', result);
408 this.logger.verbose.info('Searched repo on GitHub.');
409 return [2 /*return*/, result.data];
410 }
411 });
412 });
413 };
414 Git.prototype.graphql = function (query) {
415 return __awaiter(this, void 0, void 0, function () {
416 var data;
417 return __generator(this, function (_a) {
418 switch (_a.label) {
419 case 0:
420 this.logger.verbose.info('Querying Github using GraphQL:\n', query);
421 return [4 /*yield*/, graphql_1.default(query, {
422 baseUrl: this.graphqlBaseUrl,
423 headers: {
424 authorization: "token " + this.options.token
425 }
426 })];
427 case 1:
428 data = _a.sent();
429 this.logger.veryVerbose.info('Got response from query\n', data);
430 return [2 /*return*/, data];
431 }
432 });
433 });
434 };
435 Git.prototype.createStatus = function (prInfo) {
436 return __awaiter(this, void 0, void 0, function () {
437 var args, result;
438 return __generator(this, function (_a) {
439 switch (_a.label) {
440 case 0:
441 args = __assign({}, prInfo, { owner: this.options.owner, repo: this.options.repo });
442 this.logger.verbose.info('Creating status using:\n', args);
443 return [4 /*yield*/, this.ghub.repos.createStatus(args)];
444 case 1:
445 result = _a.sent();
446 this.logger.veryVerbose.info('Got response from createStatues\n', result);
447 this.logger.verbose.info('Created status on GitHub.');
448 return [2 /*return*/, result];
449 }
450 });
451 });
452 };
453 Git.prototype.createLabel = function (name, label) {
454 return __awaiter(this, void 0, void 0, function () {
455 var color, result;
456 return __generator(this, function (_a) {
457 switch (_a.label) {
458 case 0:
459 this.logger.verbose.info("Creating \"" + name + "\" label :\n" + label.name);
460 color = label.color
461 ? tinycolor2_1.default(label.color).toString('hex6')
462 : tinycolor2_1.default.random().toString('hex6');
463 return [4 /*yield*/, this.ghub.issues.createLabel({
464 name: label.name,
465 owner: this.options.owner,
466 repo: this.options.repo,
467 color: color.replace('#', ''),
468 description: label.description
469 })];
470 case 1:
471 result = _a.sent();
472 this.logger.veryVerbose.info('Got response from createLabel\n', result);
473 this.logger.verbose.info('Created label on GitHub.');
474 return [2 /*return*/, result];
475 }
476 });
477 });
478 };
479 Git.prototype.updateLabel = function (name, label) {
480 return __awaiter(this, void 0, void 0, function () {
481 var color, result;
482 return __generator(this, function (_a) {
483 switch (_a.label) {
484 case 0:
485 this.logger.verbose.info("Updating \"" + name + "\" label :\n" + label.name);
486 color = label.color
487 ? tinycolor2_1.default(label.color).toString('hex6')
488 : tinycolor2_1.default.random().toString('hex6');
489 return [4 /*yield*/, this.ghub.issues.updateLabel({
490 current_name: label.name,
491 owner: this.options.owner,
492 repo: this.options.repo,
493 color: color.replace('#', ''),
494 description: label.description
495 })];
496 case 1:
497 result = _a.sent();
498 this.logger.veryVerbose.info('Got response from updateLabel\n', result);
499 this.logger.verbose.info('Updated label on GitHub.');
500 return [2 /*return*/, result];
501 }
502 });
503 });
504 };
505 Git.prototype.addLabelToPr = function (pr, label) {
506 return __awaiter(this, void 0, void 0, function () {
507 var result;
508 return __generator(this, function (_a) {
509 switch (_a.label) {
510 case 0:
511 this.logger.verbose.info("Creating \"" + label + "\" label to PR " + pr);
512 return [4 /*yield*/, this.ghub.issues.addLabels({
513 // Type definitions do not seem to match
514 // issue_number: pr,
515 number: pr,
516 owner: this.options.owner,
517 repo: this.options.repo,
518 labels: [label]
519 })];
520 case 1:
521 result = _a.sent();
522 this.logger.veryVerbose.info('Got response from addLabels\n', result);
523 this.logger.verbose.info('Added labels on Pull Request.');
524 return [2 /*return*/, result];
525 }
526 });
527 });
528 };
529 Git.prototype.lockIssue = function (issue) {
530 return __awaiter(this, void 0, void 0, function () {
531 var result;
532 return __generator(this, function (_a) {
533 switch (_a.label) {
534 case 0:
535 this.logger.verbose.info("Locking #" + issue + " issue...");
536 return [4 /*yield*/, this.ghub.issues.lock({
537 issue_number: issue,
538 owner: this.options.owner,
539 repo: this.options.repo
540 })];
541 case 1:
542 result = _a.sent();
543 this.logger.veryVerbose.info('Got response from lock\n', result);
544 this.logger.verbose.info('Locked issue.');
545 return [2 /*return*/, result];
546 }
547 });
548 });
549 };
550 Git.prototype.getProject = function () {
551 return __awaiter(this, void 0, void 0, function () {
552 var result;
553 return __generator(this, function (_a) {
554 switch (_a.label) {
555 case 0:
556 this.logger.verbose.info('Getting project from GitHub');
557 return [4 /*yield*/, this.ghub.repos.get({
558 owner: this.options.owner,
559 repo: this.options.repo
560 })];
561 case 1:
562 result = (_a.sent()).data;
563 this.logger.veryVerbose.info('Got response from repos\n', result);
564 this.logger.verbose.info('Got project information.');
565 return [2 /*return*/, result];
566 }
567 });
568 });
569 };
570 Git.prototype.getPullRequests = function (options) {
571 return __awaiter(this, void 0, void 0, function () {
572 var result;
573 return __generator(this, function (_a) {
574 switch (_a.label) {
575 case 0:
576 this.logger.verbose.info('Getting pull requests...');
577 return [4 /*yield*/, this.ghub.pulls.list(__assign({ owner: this.options.owner.toLowerCase(), repo: this.options.repo.toLowerCase() }, options))];
578 case 1:
579 result = (_a.sent()).data;
580 this.logger.veryVerbose.info('Got response from pull requests', result);
581 this.logger.verbose.info('Got pull request');
582 return [2 /*return*/, result];
583 }
584 });
585 });
586 };
587 Git.prototype.getCommitsForPR = function (pr) {
588 return __awaiter(this, void 0, void 0, function () {
589 var result;
590 return __generator(this, function (_a) {
591 switch (_a.label) {
592 case 0:
593 this.logger.verbose.info("Getting commits for PR #" + pr);
594 return [4 /*yield*/, this.ghub.pulls.listCommits({
595 owner: this.options.owner.toLowerCase(),
596 repo: this.options.repo.toLowerCase(),
597 pull_number: pr
598 })];
599 case 1:
600 result = (_a.sent()).data;
601 this.logger.veryVerbose.info("Got response from PR #" + pr + "\n", result);
602 this.logger.verbose.info("Got commits for PR #" + pr + ".");
603 return [2 /*return*/, result];
604 }
605 });
606 });
607 };
608 Git.prototype.createComment = function (message, pr, context) {
609 if (context === void 0) { context = 'default'; }
610 return __awaiter(this, void 0, void 0, function () {
611 var commentIdentifier, comments, oldMessage, result;
612 return __generator(this, function (_a) {
613 switch (_a.label) {
614 case 0:
615 commentIdentifier = makeCommentIdentifier(context);
616 this.logger.verbose.info('Using comment identifier:', commentIdentifier);
617 this.logger.verbose.info('Getting previous comments on:', pr);
618 return [4 /*yield*/, this.ghub.issues.listComments({
619 owner: this.options.owner,
620 repo: this.options.repo,
621 issue_number: pr
622 })];
623 case 1:
624 comments = _a.sent();
625 this.logger.veryVerbose.info('Got PR comments\n', comments);
626 oldMessage = comments.data.find(function (comment) {
627 return comment.body.includes(commentIdentifier);
628 });
629 if (!oldMessage) return [3 /*break*/, 3];
630 this.logger.verbose.info('Found previous message from same scope.');
631 this.logger.verbose.info('Deleting previous comment');
632 return [4 /*yield*/, this.ghub.issues.deleteComment({
633 owner: this.options.owner,
634 repo: this.options.repo,
635 comment_id: oldMessage.id
636 })];
637 case 2:
638 _a.sent();
639 this.logger.verbose.info('Successfully deleted previous comment');
640 _a.label = 3;
641 case 3:
642 this.logger.verbose.info('Creating new comment');
643 return [4 /*yield*/, this.ghub.issues.createComment({
644 owner: this.options.owner,
645 repo: this.options.repo,
646 issue_number: pr,
647 body: commentIdentifier + "\n" + message
648 })];
649 case 4:
650 result = _a.sent();
651 this.logger.veryVerbose.info('Got response from creating comment\n', result);
652 this.logger.verbose.info('Successfully posted comment to PR');
653 return [2 /*return*/, result];
654 }
655 });
656 });
657 };
658 Git.prototype.addToPrBody = function (message, pr, context) {
659 if (context === void 0) { context = 'default'; }
660 return __awaiter(this, void 0, void 0, function () {
661 var id, issue, regex, body, result;
662 return __generator(this, function (_a) {
663 switch (_a.label) {
664 case 0:
665 id = makePrBodyIdentifier(context);
666 this.logger.verbose.info('Using PR body identifier:', id);
667 this.logger.verbose.info('Getting previous pr body on:', pr);
668 return [4 /*yield*/, this.ghub.issues.get({
669 owner: this.options.owner,
670 repo: this.options.repo,
671 issue_number: pr
672 })];
673 case 1:
674 issue = _a.sent();
675 this.logger.veryVerbose.info('Got PR description\n', issue.data.body);
676 regex = new RegExp("(" + id + ")\\s(.+)\\s(" + id + ")");
677 body = issue.data.body;
678 if (body.match(regex)) {
679 this.logger.verbose.info('Found previous message from same scope.');
680 this.logger.verbose.info('Replacing pr body comment');
681 body = body.replace(regex, "$1\n" + message + "\n$3");
682 }
683 else {
684 body += "\n" + id + "\n" + message + "\n" + id + "\n";
685 }
686 this.logger.verbose.info('Creating new pr body');
687 return [4 /*yield*/, this.ghub.issues.update({
688 owner: this.options.owner,
689 repo: this.options.repo,
690 issue_number: pr,
691 body: body
692 })];
693 case 2:
694 result = _a.sent();
695 this.logger.veryVerbose.info('Got response from updating body\n', result);
696 this.logger.verbose.info("Successfully updated body of PR #" + pr);
697 return [2 /*return*/, result];
698 }
699 });
700 });
701 };
702 Git.prototype.publish = function (releaseNotes, tag) {
703 return __awaiter(this, void 0, void 0, function () {
704 var result;
705 return __generator(this, function (_a) {
706 switch (_a.label) {
707 case 0:
708 this.logger.verbose.info('Creating release on GitHub for tag:', tag);
709 return [4 /*yield*/, this.ghub.repos.createRelease({
710 owner: this.options.owner,
711 repo: this.options.repo,
712 tag_name: tag,
713 body: releaseNotes
714 })];
715 case 1:
716 result = _a.sent();
717 this.logger.veryVerbose.info('Got response from createRelease\n', result);
718 this.logger.verbose.info('Created GitHub release.');
719 return [2 /*return*/, result];
720 }
721 });
722 });
723 };
724 Git.prototype.getLatestTagInBranch = function () {
725 return __awaiter(this, void 0, void 0, function () {
726 return __generator(this, function (_a) {
727 return [2 /*return*/, exec_promise_1.default('git', ['describe', '--tags', '--abbrev=0'])];
728 });
729 });
730 };
731 __decorate([
732 typescript_memoize_1.Memoize()
733 ], Git.prototype, "getLatestReleaseInfo", null);
734 __decorate([
735 typescript_memoize_1.Memoize()
736 ], Git.prototype, "getLatestRelease", null);
737 __decorate([
738 typescript_memoize_1.Memoize()
739 ], Git.prototype, "getLabels", null);
740 __decorate([
741 typescript_memoize_1.Memoize()
742 ], Git.prototype, "getGitLog", null);
743 __decorate([
744 typescript_memoize_1.Memoize()
745 ], Git.prototype, "getUserByEmail", null);
746 __decorate([
747 typescript_memoize_1.Memoize()
748 ], Git.prototype, "getUserByUsername", null);
749 __decorate([
750 typescript_memoize_1.Memoize()
751 ], Git.prototype, "getPullRequest", null);
752 __decorate([
753 typescript_memoize_1.Memoize()
754 ], Git.prototype, "getProject", null);
755 __decorate([
756 typescript_memoize_1.Memoize()
757 ], Git.prototype, "getCommitsForPR", null);
758 return Git;
759}());
760exports.default = Git;
761//# sourceMappingURL=git.js.map
\No newline at end of file