UNPKG

38.7 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.getPr = function (prNumber) {
274 return __awaiter(this, void 0, void 0, function () {
275 var args, info, e_3;
276 return __generator(this, function (_a) {
277 switch (_a.label) {
278 case 0:
279 this.logger.verbose.info("Getting info for PR: " + prNumber);
280 args = {
281 owner: this.options.owner,
282 repo: this.options.repo,
283 issue_number: prNumber
284 };
285 this.logger.verbose.info('Getting issue info using:', args);
286 _a.label = 1;
287 case 1:
288 _a.trys.push([1, 3, , 4]);
289 return [4 /*yield*/, this.ghub.issues.get(args)];
290 case 2:
291 info = _a.sent();
292 this.logger.veryVerbose.info('Got response for "issues.get":\n', info);
293 return [2 /*return*/, info];
294 case 3:
295 e_3 = _a.sent();
296 throw new GitAPIError('getPr', args, e_3);
297 case 4: return [2 /*return*/];
298 }
299 });
300 });
301 };
302 Git.prototype.getProjectLabels = function () {
303 return __awaiter(this, void 0, void 0, function () {
304 var args, labels, e_4;
305 return __generator(this, function (_a) {
306 switch (_a.label) {
307 case 0:
308 this.logger.verbose.info("Getting labels for project: " + this.options.repo);
309 args = {
310 owner: this.options.owner,
311 repo: this.options.repo
312 };
313 _a.label = 1;
314 case 1:
315 _a.trys.push([1, 3, , 4]);
316 return [4 /*yield*/, this.ghub.issues.listLabelsForRepo(args)];
317 case 2:
318 labels = _a.sent();
319 this.logger.veryVerbose.info('Got response for "getProjectLabels":\n', labels);
320 this.logger.verbose.info('Found labels on project:\n', labels.data);
321 return [2 /*return*/, labels.data.map(function (l) { return l.name; })];
322 case 3:
323 e_4 = _a.sent();
324 throw new GitAPIError('getProjectLabels', args, e_4);
325 case 4: return [2 /*return*/];
326 }
327 });
328 });
329 };
330 Git.prototype.getGitLog = function (start, end) {
331 if (end === void 0) { end = 'HEAD'; }
332 return __awaiter(this, void 0, void 0, function () {
333 var log;
334 return __generator(this, function (_a) {
335 switch (_a.label) {
336 case 0: return [4 /*yield*/, gitlog({
337 repo: process.cwd(),
338 number: Number.MAX_SAFE_INTEGER,
339 fields: ['hash', 'authorName', 'authorEmail', 'rawBody'],
340 branch: start.trim() + ".." + end.trim(),
341 execOptions: { maxBuffer: 1000 * 1024 }
342 })];
343 case 1:
344 log = _a.sent();
345 return [2 /*return*/, log.map(function (commit) { return ({
346 hash: commit.hash,
347 authorName: commit.authorName,
348 authorEmail: commit.authorEmail,
349 subject: commit.rawBody
350 }); })];
351 }
352 });
353 });
354 };
355 Git.prototype.getUserByEmail = function (email) {
356 return __awaiter(this, void 0, void 0, function () {
357 var search, error_1;
358 return __generator(this, function (_a) {
359 switch (_a.label) {
360 case 0:
361 _a.trys.push([0, 2, , 3]);
362 return [4 /*yield*/, this.ghub.search.users({
363 q: "in:email " + email
364 })];
365 case 1:
366 search = (_a.sent()).data;
367 return [2 /*return*/, search && search.items.length > 0
368 ? search.items[0]
369 : { login: email }];
370 case 2:
371 error_1 = _a.sent();
372 this.logger.verbose.warn("Could not find user by email: " + email);
373 return [3 /*break*/, 3];
374 case 3: return [2 /*return*/];
375 }
376 });
377 });
378 };
379 Git.prototype.getUserByUsername = function (username) {
380 return __awaiter(this, void 0, void 0, function () {
381 var user, error_2;
382 return __generator(this, function (_a) {
383 switch (_a.label) {
384 case 0:
385 _a.trys.push([0, 2, , 3]);
386 return [4 /*yield*/, this.ghub.users.getByUsername({
387 username: username
388 })];
389 case 1:
390 user = _a.sent();
391 return [2 /*return*/, user.data];
392 case 2:
393 error_2 = _a.sent();
394 this.logger.verbose.warn("Could not find user by username: " + username);
395 return [3 /*break*/, 3];
396 case 3: return [2 /*return*/];
397 }
398 });
399 });
400 };
401 Git.prototype.getPullRequest = function (pr) {
402 return __awaiter(this, void 0, void 0, function () {
403 var args, result;
404 return __generator(this, function (_a) {
405 switch (_a.label) {
406 case 0:
407 this.logger.verbose.info("Getting Pull Request: " + pr);
408 args = {
409 owner: this.options.owner,
410 repo: this.options.repo,
411 pull_number: pr
412 };
413 this.logger.verbose.info('Getting pull request info using:', args);
414 return [4 /*yield*/, this.ghub.pulls.get(args)];
415 case 1:
416 result = _a.sent();
417 this.logger.veryVerbose.info('Got pull request data\n', result);
418 this.logger.verbose.info('Got pull request info');
419 return [2 /*return*/, result];
420 }
421 });
422 });
423 };
424 Git.prototype.searchRepo = function (options) {
425 return __awaiter(this, void 0, void 0, function () {
426 var repo, result;
427 return __generator(this, function (_a) {
428 switch (_a.label) {
429 case 0:
430 repo = "repo:" + this.options.owner + "/" + this.options.repo;
431 options.q = repo + " " + options.q;
432 this.logger.verbose.info('Searching repo using:\n', options);
433 return [4 /*yield*/, this.ghub.search.issuesAndPullRequests(options)];
434 case 1:
435 result = _a.sent();
436 this.logger.veryVerbose.info('Got response from search\n', result);
437 this.logger.verbose.info('Searched repo on GitHub.');
438 return [2 /*return*/, result.data];
439 }
440 });
441 });
442 };
443 Git.prototype.graphql = function (query) {
444 return __awaiter(this, void 0, void 0, function () {
445 var data;
446 return __generator(this, function (_a) {
447 switch (_a.label) {
448 case 0:
449 this.logger.verbose.info('Querying Github using GraphQL:\n', query);
450 return [4 /*yield*/, graphql_1.default(query, {
451 baseUrl: this.graphqlBaseUrl,
452 headers: {
453 authorization: "token " + this.options.token
454 }
455 })];
456 case 1:
457 data = _a.sent();
458 this.logger.veryVerbose.info('Got response from query\n', data);
459 return [2 /*return*/, data];
460 }
461 });
462 });
463 };
464 Git.prototype.createStatus = function (prInfo) {
465 return __awaiter(this, void 0, void 0, function () {
466 var args, result;
467 return __generator(this, function (_a) {
468 switch (_a.label) {
469 case 0:
470 args = __assign({}, prInfo, { owner: this.options.owner, repo: this.options.repo });
471 this.logger.verbose.info('Creating status using:\n', args);
472 return [4 /*yield*/, this.ghub.repos.createStatus(args)];
473 case 1:
474 result = _a.sent();
475 this.logger.veryVerbose.info('Got response from createStatues\n', result);
476 this.logger.verbose.info('Created status on GitHub.');
477 return [2 /*return*/, result];
478 }
479 });
480 });
481 };
482 Git.prototype.createLabel = function (name, label) {
483 return __awaiter(this, void 0, void 0, function () {
484 var color, result;
485 return __generator(this, function (_a) {
486 switch (_a.label) {
487 case 0:
488 this.logger.verbose.info("Creating \"" + name + "\" label :\n" + label.name);
489 color = label.color
490 ? tinycolor2_1.default(label.color).toString('hex6')
491 : tinycolor2_1.default.random().toString('hex6');
492 return [4 /*yield*/, this.ghub.issues.createLabel({
493 name: label.name,
494 owner: this.options.owner,
495 repo: this.options.repo,
496 color: color.replace('#', ''),
497 description: label.description
498 })];
499 case 1:
500 result = _a.sent();
501 this.logger.veryVerbose.info('Got response from createLabel\n', result);
502 this.logger.verbose.info('Created label on GitHub.');
503 return [2 /*return*/, result];
504 }
505 });
506 });
507 };
508 Git.prototype.updateLabel = function (name, label) {
509 return __awaiter(this, void 0, void 0, function () {
510 var color, result;
511 return __generator(this, function (_a) {
512 switch (_a.label) {
513 case 0:
514 this.logger.verbose.info("Updating \"" + name + "\" label :\n" + label.name);
515 color = label.color
516 ? tinycolor2_1.default(label.color).toString('hex6')
517 : tinycolor2_1.default.random().toString('hex6');
518 return [4 /*yield*/, this.ghub.issues.updateLabel({
519 current_name: label.name,
520 owner: this.options.owner,
521 repo: this.options.repo,
522 color: color.replace('#', ''),
523 description: label.description
524 })];
525 case 1:
526 result = _a.sent();
527 this.logger.veryVerbose.info('Got response from updateLabel\n', result);
528 this.logger.verbose.info('Updated label on GitHub.');
529 return [2 /*return*/, result];
530 }
531 });
532 });
533 };
534 Git.prototype.addLabelToPr = function (pr, label) {
535 return __awaiter(this, void 0, void 0, function () {
536 var result;
537 return __generator(this, function (_a) {
538 switch (_a.label) {
539 case 0:
540 this.logger.verbose.info("Creating \"" + label + "\" label to PR " + pr);
541 return [4 /*yield*/, this.ghub.issues.addLabels({
542 // Type definitions do not seem to match
543 // issue_number: pr,
544 number: pr,
545 owner: this.options.owner,
546 repo: this.options.repo,
547 labels: [label]
548 })];
549 case 1:
550 result = _a.sent();
551 this.logger.veryVerbose.info('Got response from addLabels\n', result);
552 this.logger.verbose.info('Added labels on Pull Request.');
553 return [2 /*return*/, result];
554 }
555 });
556 });
557 };
558 Git.prototype.lockIssue = function (issue) {
559 return __awaiter(this, void 0, void 0, function () {
560 var result;
561 return __generator(this, function (_a) {
562 switch (_a.label) {
563 case 0:
564 this.logger.verbose.info("Locking #" + issue + " issue...");
565 return [4 /*yield*/, this.ghub.issues.lock({
566 issue_number: issue,
567 owner: this.options.owner,
568 repo: this.options.repo
569 })];
570 case 1:
571 result = _a.sent();
572 this.logger.veryVerbose.info('Got response from lock\n', result);
573 this.logger.verbose.info('Locked issue.');
574 return [2 /*return*/, result];
575 }
576 });
577 });
578 };
579 Git.prototype.getProject = function () {
580 return __awaiter(this, void 0, void 0, function () {
581 var result;
582 return __generator(this, function (_a) {
583 switch (_a.label) {
584 case 0:
585 this.logger.verbose.info('Getting project from GitHub');
586 return [4 /*yield*/, this.ghub.repos.get({
587 owner: this.options.owner,
588 repo: this.options.repo
589 })];
590 case 1:
591 result = (_a.sent()).data;
592 this.logger.veryVerbose.info('Got response from repos\n', result);
593 this.logger.verbose.info('Got project information.');
594 return [2 /*return*/, result];
595 }
596 });
597 });
598 };
599 Git.prototype.getPullRequests = function (options) {
600 return __awaiter(this, void 0, void 0, function () {
601 var result;
602 return __generator(this, function (_a) {
603 switch (_a.label) {
604 case 0:
605 this.logger.verbose.info('Getting pull requests...');
606 return [4 /*yield*/, this.ghub.pulls.list(__assign({ owner: this.options.owner.toLowerCase(), repo: this.options.repo.toLowerCase() }, options))];
607 case 1:
608 result = (_a.sent()).data;
609 this.logger.veryVerbose.info('Got response from pull requests', result);
610 this.logger.verbose.info('Got pull request');
611 return [2 /*return*/, result];
612 }
613 });
614 });
615 };
616 Git.prototype.getCommitsForPR = function (pr) {
617 return __awaiter(this, void 0, void 0, function () {
618 var result;
619 return __generator(this, function (_a) {
620 switch (_a.label) {
621 case 0:
622 this.logger.verbose.info("Getting commits for PR #" + pr);
623 return [4 /*yield*/, this.ghub.pulls.listCommits({
624 owner: this.options.owner.toLowerCase(),
625 repo: this.options.repo.toLowerCase(),
626 pull_number: pr
627 })];
628 case 1:
629 result = (_a.sent()).data;
630 this.logger.veryVerbose.info("Got response from PR #" + pr + "\n", result);
631 this.logger.verbose.info("Got commits for PR #" + pr + ".");
632 return [2 /*return*/, result];
633 }
634 });
635 });
636 };
637 Git.prototype.deleteComment = function (pr, context) {
638 if (context === void 0) { context = 'default'; }
639 return __awaiter(this, void 0, void 0, function () {
640 var commentIdentifier, comments, oldMessage, commentId;
641 return __generator(this, function (_a) {
642 switch (_a.label) {
643 case 0:
644 commentIdentifier = makeCommentIdentifier(context);
645 this.logger.verbose.info('Getting previous comments on:', pr);
646 return [4 /*yield*/, this.ghub.issues.listComments({
647 owner: this.options.owner,
648 repo: this.options.repo,
649 issue_number: pr
650 })];
651 case 1:
652 comments = _a.sent();
653 this.logger.veryVerbose.info('Got PR comments\n', comments);
654 oldMessage = comments.data.find(function (comment) {
655 return comment.body.includes(commentIdentifier);
656 });
657 if (!oldMessage) {
658 return [2 /*return*/];
659 }
660 this.logger.verbose.info('Found previous message from same scope.');
661 commentId = oldMessage.id;
662 this.logger.verbose.info("Deleting comment: " + commentId);
663 return [4 /*yield*/, this.ghub.issues.deleteComment({
664 owner: this.options.owner,
665 repo: this.options.repo,
666 comment_id: commentId
667 })];
668 case 2:
669 _a.sent();
670 this.logger.verbose.info("Successfully deleted comment: " + commentId);
671 return [2 /*return*/];
672 }
673 });
674 });
675 };
676 Git.prototype.createComment = function (message, pr, context) {
677 if (context === void 0) { context = 'default'; }
678 return __awaiter(this, void 0, void 0, function () {
679 var commentIdentifier, result;
680 return __generator(this, function (_a) {
681 switch (_a.label) {
682 case 0:
683 commentIdentifier = makeCommentIdentifier(context);
684 this.logger.verbose.info('Using comment identifier:', commentIdentifier);
685 return [4 /*yield*/, this.deleteComment(pr, context)];
686 case 1:
687 _a.sent();
688 this.logger.verbose.info('Creating new comment');
689 return [4 /*yield*/, this.ghub.issues.createComment({
690 owner: this.options.owner,
691 repo: this.options.repo,
692 issue_number: pr,
693 body: commentIdentifier + "\n" + message
694 })];
695 case 2:
696 result = _a.sent();
697 this.logger.veryVerbose.info('Got response from creating comment\n', result);
698 this.logger.verbose.info('Successfully posted comment to PR');
699 return [2 /*return*/, result];
700 }
701 });
702 });
703 };
704 Git.prototype.addToPrBody = function (message, pr, context) {
705 if (context === void 0) { context = 'default'; }
706 return __awaiter(this, void 0, void 0, function () {
707 var id, issue, regex, body, result;
708 return __generator(this, function (_a) {
709 switch (_a.label) {
710 case 0:
711 id = makePrBodyIdentifier(context);
712 this.logger.verbose.info('Using PR body identifier:', id);
713 this.logger.verbose.info('Getting previous pr body on:', pr);
714 return [4 /*yield*/, this.ghub.issues.get({
715 owner: this.options.owner,
716 repo: this.options.repo,
717 issue_number: pr
718 })];
719 case 1:
720 issue = _a.sent();
721 this.logger.veryVerbose.info('Got PR description\n', issue.data.body);
722 regex = new RegExp("(" + id + ")\\s(.+)\\s(" + id + ")");
723 body = issue.data.body;
724 if (body.match(regex)) {
725 this.logger.verbose.info('Found previous message from same scope.');
726 this.logger.verbose.info('Replacing pr body comment');
727 body = body.replace(regex, message ? "$1\n" + message + "\n$3" : '');
728 }
729 else {
730 body += message ? "\n" + id + "\n" + message + "\n" + id + "\n" : '';
731 }
732 this.logger.verbose.info('Creating new pr body');
733 return [4 /*yield*/, this.ghub.issues.update({
734 owner: this.options.owner,
735 repo: this.options.repo,
736 issue_number: pr,
737 body: body
738 })];
739 case 2:
740 result = _a.sent();
741 this.logger.veryVerbose.info('Got response from updating body\n', result);
742 this.logger.verbose.info("Successfully updated body of PR #" + pr);
743 return [2 /*return*/, result];
744 }
745 });
746 });
747 };
748 Git.prototype.publish = function (releaseNotes, tag) {
749 return __awaiter(this, void 0, void 0, function () {
750 var result;
751 return __generator(this, function (_a) {
752 switch (_a.label) {
753 case 0:
754 this.logger.verbose.info('Creating release on GitHub for tag:', tag);
755 return [4 /*yield*/, this.ghub.repos.createRelease({
756 owner: this.options.owner,
757 repo: this.options.repo,
758 tag_name: tag,
759 body: releaseNotes
760 })];
761 case 1:
762 result = _a.sent();
763 this.logger.veryVerbose.info('Got response from createRelease\n', result);
764 this.logger.verbose.info('Created GitHub release.');
765 return [2 /*return*/, result];
766 }
767 });
768 });
769 };
770 Git.prototype.getLatestTagInBranch = function () {
771 return __awaiter(this, void 0, void 0, function () {
772 return __generator(this, function (_a) {
773 return [2 /*return*/, exec_promise_1.default('git', ['describe', '--tags', '--abbrev=0'])];
774 });
775 });
776 };
777 __decorate([
778 typescript_memoize_1.Memoize()
779 ], Git.prototype, "getLatestReleaseInfo", null);
780 __decorate([
781 typescript_memoize_1.Memoize()
782 ], Git.prototype, "getLatestRelease", null);
783 __decorate([
784 typescript_memoize_1.Memoize()
785 ], Git.prototype, "getLabels", null);
786 __decorate([
787 typescript_memoize_1.Memoize()
788 ], Git.prototype, "getPr", null);
789 __decorate([
790 typescript_memoize_1.Memoize()
791 ], Git.prototype, "getGitLog", null);
792 __decorate([
793 typescript_memoize_1.Memoize()
794 ], Git.prototype, "getUserByEmail", null);
795 __decorate([
796 typescript_memoize_1.Memoize()
797 ], Git.prototype, "getUserByUsername", null);
798 __decorate([
799 typescript_memoize_1.Memoize()
800 ], Git.prototype, "getPullRequest", null);
801 __decorate([
802 typescript_memoize_1.Memoize()
803 ], Git.prototype, "getProject", null);
804 __decorate([
805 typescript_memoize_1.Memoize()
806 ], Git.prototype, "getCommitsForPR", null);
807 return Git;
808}());
809exports.default = Git;
810//# sourceMappingURL=git.js.map
\No newline at end of file