UNPKG

bitbucket-server-utils-cli

Version:

[![NPM](https://img.shields.io/npm/v/bitbucket-server-utils-cli.svg?style=flat-square)](https://www.npmjs.com/package/bitbucket-server-utils-cli)

378 lines 21 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var axios_1 = __importDefault(require("axios")); var log_1 = __importDefault(require("../utils/log")); var sleep_1 = __importDefault(require("../utils/sleep")); function authString(settings) { if (settings.personalAccessToken) { return "Bearer ".concat(settings.personalAccessToken); } if (settings.username && settings.password) { var userAndPass = "".concat(settings.username, ":").concat(settings.password); var authString_1 = Buffer.from(userAndPass, 'binary').toString('base64'); return "Basic ".concat(authString_1); } throw Error("No credentials supplied"); } var BitbucketService = /** @class */ (function () { function BitbucketService(settings) { this.settings = settings; var maskedSettings = JSON.parse(JSON.stringify(settings)); maskedSettings.personalAccessToken = '<masked>'; maskedSettings.username = '<masked>'; maskedSettings.password = '<masked>'; (0, log_1.default)('DEBUG', maskedSettings); var authorization = authString(settings); this.config = { headers: { Authorization: authorization, }, }; } BitbucketService.prototype.getRepositories = function (projects) { return __awaiter(this, void 0, void 0, function () { var categories, _loop_1, this_1, _i, projects_1, project; return __generator(this, function (_a) { switch (_a.label) { case 0: categories = []; _loop_1 = function (project) { var url, response; return __generator(this, function (_b) { switch (_b.label) { case 0: url = "".concat(this_1.settings.url, "/projects/").concat(project, "/repos?limit=9999"); (0, log_1.default)('DEBUG', '> ' + url); return [4 /*yield*/, axios_1.default.get(url, this_1.config)]; case 1: response = _b.sent(); return [4 /*yield*/, (0, sleep_1.default)(this_1.settings.sleepTime)]; case 2: _b.sent(); //log('DEBUG','response: ' + JSON.stringify(response.data)); categories.push(response.data.values.map(function (it) { return { slug: { projectSlug: project, repoSlug: it.slug, }, id: it.id, state: it.state, cloneUrl: it.links.clone // // Sort ssh before https .sort(function (a, b) { return b.name.localeCompare(a.name); })[0].href, }; })); return [2 /*return*/]; } }); }; this_1 = this; _i = 0, projects_1 = projects; _a.label = 1; case 1: if (!(_i < projects_1.length)) return [3 /*break*/, 4]; project = projects_1[_i]; return [5 /*yield**/, _loop_1(project)]; case 2: _a.sent(); _a.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/, categories .flat() .sort(function (a, b) { return "".concat(a.projectSlug, "-").concat(a.repoSlug).localeCompare("".concat(b.projectSlug, "-").concat(b.repoSlug)); })]; } }); }); }; BitbucketService.prototype.getBranches = function (repo) { return __awaiter(this, void 0, void 0, function () { var url, response; return __generator(this, function (_a) { switch (_a.label) { case 0: url = "".concat(this.settings.url, "/projects/").concat(repo.projectSlug, "/repos/").concat(repo.repoSlug, "/branches?limit=9999"); (0, log_1.default)('DEBUG', '> ' + url); return [4 /*yield*/, axios_1.default.get(url, this.config)]; case 1: response = _a.sent(); return [4 /*yield*/, (0, sleep_1.default)(this.settings.sleepTime)]; case 2: _a.sent(); return [2 /*return*/, response.data.values .map(function (data) { return { displayId: data.displayId, latestCommit: data.latestCommit, isDefault: data.isDefault, }; }) .sort(function (a, b) { return "".concat(a.displayId, "-").concat(a.displayId).localeCompare("".concat(b.displayId, "-").concat(b.displayId)); })]; } }); }); }; BitbucketService.prototype.getPullRequests = function (repo) { return __awaiter(this, void 0, void 0, function () { var url, response; return __generator(this, function (_a) { switch (_a.label) { case 0: url = "".concat(this.settings.url, "/projects/").concat(repo.projectSlug, "/repos/").concat(repo.repoSlug, "/pull-requests?limit=9999"); (0, log_1.default)('DEBUG', '> ' + url); return [4 /*yield*/, axios_1.default.get(url, this.config)]; case 1: response = _a.sent(); return [4 /*yield*/, (0, sleep_1.default)(this.settings.sleepTime)]; case 2: _a.sent(); return [2 /*return*/, response.data.values .map(function (data) { return { id: data.id, title: data.title, state: data.state, createdDate: data.createdDate, updatedDate: data.updatedDate, author: { name: data.author.user.name, emailAddress: data.author.user.emailAddress, displayName: data.author.user.displayName, slug: data.author.user.slug, }, repository: { projectSlug: repo.projectSlug, repoSlug: repo.repoSlug, }, reviewers: data.reviewers.map(function (reviewer) { return { user: { displayName: reviewer.user.displayName, emailAddress: reviewer.user.emailAddress, name: reviewer.user.name, slug: reviewer.user.slug, }, role: reviewer.role, status: reviewer.status, }; }), fromRef: { displayId: data.fromRef.displayId, latestCommit: data.fromRef.latestCommit, }, }; }) .sort()]; } }); }); }; BitbucketService.prototype.getCommit = function (repo, commit) { return __awaiter(this, void 0, void 0, function () { var url, response; return __generator(this, function (_a) { switch (_a.label) { case 0: url = "".concat(this.settings.url, "/projects/").concat(repo.projectSlug, "/repos/").concat(repo.repoSlug, "/commits/").concat(commit); (0, log_1.default)('DEBUG', '> ' + url); return [4 /*yield*/, axios_1.default.get(url, this.config)]; case 1: response = _a.sent(); return [4 /*yield*/, (0, sleep_1.default)(this.settings.sleepTime)]; case 2: _a.sent(); return [2 /*return*/, { displayId: response.data.displayId, authorTimestamp: response.data.authorTimestamp, committerTimestamp: response.data.committerTimestamp, author: { name: response.data.author.name, emailAddress: response.data.author.emailAddress, displayName: response.data.author.displayName, slug: response.data.author.slug, }, }]; } }); }); }; BitbucketService.prototype.findCommentsByCommentKey = function (repo, pullRequest, commentKey) { return __awaiter(this, void 0, void 0, function () { var urlActivities, response; return __generator(this, function (_a) { switch (_a.label) { case 0: urlActivities = "".concat(this.settings.url, "/projects/").concat(repo.projectSlug, "/repos/").concat(repo.repoSlug, "/pull-requests/").concat(pullRequest, "/activities?limit=9999"); (0, log_1.default)('DEBUG', '> ' + urlActivities); return [4 /*yield*/, axios_1.default.get(urlActivities, this.config)]; case 1: response = _a.sent(); return [4 /*yield*/, (0, sleep_1.default)(this.settings.sleepTime)]; case 2: _a.sent(); return [2 /*return*/, response.data.values .filter(function (activity) { return (activity.action == 'COMMENTED' && activity.comment.text.indexOf(commentKey) != -1); }) .map(function (activity) { return activity.comment; })]; } }); }); }; BitbucketService.prototype.deletePullRequestCommentById = function (repo, pullRequest, comment) { return __awaiter(this, void 0, void 0, function () { var urlDelete, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: urlDelete = "".concat(this.settings.url, "/projects/").concat(repo.projectSlug, "/repos/").concat(repo.repoSlug, "/pull-requests/").concat(pullRequest, "/comments/").concat(comment.id, "?version=").concat(comment.version); _a.label = 1; case 1: _a.trys.push([1, 5, , 6]); if (!!this.settings.dryRun) return [3 /*break*/, 3]; return [4 /*yield*/, axios_1.default.delete(urlDelete, this.config)]; case 2: _a.sent(); _a.label = 3; case 3: return [4 /*yield*/, (0, sleep_1.default)(this.settings.sleepTime)]; case 4: _a.sent(); return [3 /*break*/, 6]; case 5: e_1 = _a.sent(); (0, log_1.default)('ERROR', "Was unable to delete ".concat(urlDelete, " ").concat(e_1)); return [3 /*break*/, 6]; case 6: return [2 /*return*/]; } }); }); }; BitbucketService.prototype.deletePullRequestCommentByCommentKey = function (repo, pullRequest, commentKey) { return __awaiter(this, void 0, void 0, function () { var existingComments, _i, existingComments_1, comment; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.findCommentsByCommentKey(repo, pullRequest, commentKey)]; case 1: existingComments = _a.sent(); _i = 0, existingComments_1 = existingComments; _a.label = 2; case 2: if (!(_i < existingComments_1.length)) return [3 /*break*/, 5]; comment = existingComments_1[_i]; (0, log_1.default)('DEBUG', "Deleting old comment ".concat(comment.id)); return [4 /*yield*/, this.deletePullRequestCommentById(repo, pullRequest, comment)]; case 3: _a.sent(); _a.label = 4; case 4: _i++; return [3 /*break*/, 2]; case 5: return [2 /*return*/]; } }); }); }; BitbucketService.prototype.postPullRequestComment = function (config) { return __awaiter(this, void 0, void 0, function () { var commentMessage, existingComments, notIdentialComments, _i, notIdentialComments_1, comment, commentsUrl, postContent; return __generator(this, function (_a) { switch (_a.label) { case 0: (0, log_1.default)('DEBUG', "Commenting ".concat(JSON.stringify(config))); commentMessage = config.message; if (!config.commentKey) return [3 /*break*/, 6]; commentMessage = config.message + '\n\n' + config.commentKey; return [4 /*yield*/, this.findCommentsByCommentKey(config.repo, config.pullRequest, config.commentKey)]; case 1: existingComments = _a.sent(); notIdentialComments = existingComments.filter(function (comment) { var _a, _b; return ((_b = (_a = comment.text) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '').indexOf(commentMessage.trim()) == -1; }); _i = 0, notIdentialComments_1 = notIdentialComments; _a.label = 2; case 2: if (!(_i < notIdentialComments_1.length)) return [3 /*break*/, 5]; comment = notIdentialComments_1[_i]; (0, log_1.default)('DEBUG', "Deleting not identical comment ".concat(JSON.stringify(comment))); return [4 /*yield*/, this.deletePullRequestCommentById(config.repo, config.pullRequest, comment)]; case 3: _a.sent(); _a.label = 4; case 4: _i++; return [3 /*break*/, 2]; case 5: if (notIdentialComments.length != existingComments.length) { (0, log_1.default)('DEBUG', "Identical comment found, not commenting again"); return [2 /*return*/]; } _a.label = 6; case 6: commentsUrl = "".concat(this.settings.url, "/projects/").concat(config.repo.projectSlug, "/repos/").concat(config.repo.repoSlug, "/pull-requests/").concat(config.pullRequest, "/comments"); postContent = { severity: config.severity, text: commentMessage, }; (0, log_1.default)('DEBUG', '> ' + commentsUrl); if (!!this.settings.dryRun) return [3 /*break*/, 8]; return [4 /*yield*/, axios_1.default.post(commentsUrl, postContent, this.config)]; case 7: _a.sent(); _a.label = 8; case 8: return [4 /*yield*/, (0, sleep_1.default)(this.settings.sleepTime)]; case 9: _a.sent(); return [2 /*return*/]; } }); }); }; return BitbucketService; }()); exports.default = BitbucketService; //# sourceMappingURL=bitbucket-service.js.map