@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
1,034 lines • 99.9 kB
JavaScript
"use strict";
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGitHubRepositoryFromUrl = exports.getSprintMilestoneName = exports.RealGitHub = exports.getSprintLabels = exports.FakeGitHub = exports.FakeRepository = exports.getRepositoryBranchFullName = exports.getRepositoryBranch = exports.gitHubPullRequestGetAssignee = exports.gitHubPullRequestGetLabels = exports.gitHubPullRequestGetLabel = exports.getRepositoryFullName = exports.repositoriesAreEqual = exports.getGithubRepositoryUrl = exports.getRepository = void 0;
var tslib_1 = require("tslib");
var rest_1 = require("@octokit/rest");
var fs = tslib_1.__importStar(require("fs"));
var arrays_1 = require("./arrays");
var url_1 = require("./url");
/**
* Get a GitHubRepository object from the provided string or GitHubRepository object.
* @param repository The repository name or object.
*/
function getRepository(repository) {
var result;
if (!repository) {
result = {
name: repository,
owner: ""
};
}
else if (typeof repository === "string") {
var slashIndex = repository.indexOf("/");
if (slashIndex === -1) {
slashIndex = repository.indexOf("\\");
}
result = {
name: repository.substr(slashIndex + 1),
owner: slashIndex === -1 ? "" : repository.substr(0, slashIndex)
};
}
else {
result = repository;
}
return result;
}
exports.getRepository = getRepository;
function getGithubRepositoryUrl(repository) {
return "https://github.com/" + repository.owner + "/" + repository.name;
}
exports.getGithubRepositoryUrl = getGithubRepositoryUrl;
/**
* Get whether or not the two provided repositories are equal.
*/
function repositoriesAreEqual(lhs, rhs) {
return getRepositoryFullName(lhs).toLowerCase() === getRepositoryFullName(rhs).toLowerCase();
}
exports.repositoriesAreEqual = repositoriesAreEqual;
/**
* Get the full name of the provided repository.
* @param repository The repository to get the full name of.
*/
function getRepositoryFullName(repository) {
var result;
if (!repository) {
result = "";
}
else if (typeof repository === "string") {
result = repository;
}
else if (!repository.owner) {
result = repository.name;
}
else {
result = repository.owner + "/" + repository.name;
}
return result;
}
exports.getRepositoryFullName = getRepositoryFullName;
/**
* Get the label in the provided GitHubPullRequest that has the provided name. If no label is found,
* then undefined will be returned.
* @param githubPullRequest The pull request to look for the label in.
* @param labelName The name of the label to look for.
*/
function gitHubPullRequestGetLabel(githubPullRequest, labelName) {
return arrays_1.first(githubPullRequest.labels, function (label) { return label.name === labelName; });
}
exports.gitHubPullRequestGetLabel = gitHubPullRequestGetLabel;
function gitHubPullRequestGetLabels(githubPullRequest, labelNames) {
var labelNamesArray = (typeof labelNames === "string" ? [labelNames] : labelNames);
return arrays_1.where(githubPullRequest.labels, function (label) { return arrays_1.contains(labelNamesArray, label.name); });
}
exports.gitHubPullRequestGetLabels = gitHubPullRequestGetLabels;
function gitHubPullRequestGetAssignee(githubPullRequest, assignee) {
return arrays_1.first(githubPullRequest.assignees, function (existingAssignee) {
var isMatch;
if (!assignee) {
isMatch = false;
}
else if (typeof assignee === "number") {
isMatch = (existingAssignee.id === assignee);
}
else if (typeof assignee === "string") {
isMatch = (existingAssignee.login === assignee || existingAssignee.name === assignee);
}
else {
isMatch = (existingAssignee.id === assignee.id);
}
return isMatch;
});
}
exports.gitHubPullRequestGetAssignee = gitHubPullRequestGetAssignee;
function getPullRequestNumber(pullRequest) {
return typeof pullRequest === "number" ? pullRequest : pullRequest.number;
}
function getCommentId(comment) {
return typeof comment === "number" ? comment : comment.id;
}
/**
* Parse a ForkedRepositoryBranch reference from the provided value.
* @param repositoryBranch The string or ForkedRepositoryBranch to parse.
*/
function getRepositoryBranch(repositoryBranch) {
var result;
if (typeof repositoryBranch === "string") {
var colonIndex = repositoryBranch.indexOf(":");
if (colonIndex === -1) {
result = {
owner: "",
name: repositoryBranch
};
}
else {
var owner = repositoryBranch.substring(0, colonIndex);
var branchName = repositoryBranch.substring(colonIndex + 1);
result = {
owner: owner,
name: branchName
};
}
}
else {
result = repositoryBranch;
}
return result;
}
exports.getRepositoryBranch = getRepositoryBranch;
function getRepositoryBranchFullName(repositoryBranch) {
var result;
if (!repositoryBranch || typeof repositoryBranch === "string") {
result = repositoryBranch;
}
else if (!repositoryBranch.owner) {
result = repositoryBranch.name;
}
else {
result = getRepositoryFullName(repositoryBranch.owner) + ":" + repositoryBranch.name;
}
return result;
}
exports.getRepositoryBranchFullName = getRepositoryBranchFullName;
var FakeRepository = /** @class */ (function () {
function FakeRepository(name, forkOf) {
this.name = name;
this.forkOf = forkOf;
this.labels = [];
this.milestones = [];
this.pullRequests = [];
this.commits = [];
this.branches = [];
this.forks = [];
this.content = [];
}
/**
* Get the fork of this repository that was created by the provided owner.
* @param owner The name of the owner that created a fork of this repository.
*/
FakeRepository.prototype.getFork = function (owner) {
var forksToSearch = this.forkOf ? this.forkOf.forks : this.forks;
return arrays_1.first(forksToSearch, function (fork) { return getRepository(fork.name).owner === owner; });
};
return FakeRepository;
}());
exports.FakeRepository = FakeRepository;
var FakeGitHub = /** @class */ (function () {
function FakeGitHub() {
this.users = [];
this.repositories = [];
}
FakeGitHub.prototype.getRepository = function (repository) {
var repositoryFullName = getRepositoryFullName(repository);
var fakeRepository = arrays_1.first(this.repositories, function (fakeRepository) { return fakeRepository.name === repositoryFullName; });
if (!fakeRepository) {
throw new Error("No fake repository exists with the name \"" + repositoryFullName + "\".");
}
return fakeRepository;
};
FakeGitHub.prototype.createRepositoryInner = function (repository, forkOf) {
var repositoryFullName = getRepositoryFullName(repository);
var fakeRepository = arrays_1.first(this.repositories, function (fakeRepository) { return fakeRepository.name === repositoryFullName; });
if (fakeRepository) {
throw new Error("A fake repository with the name \"" + repositoryFullName + "\" already exists.");
}
else {
var forkOfRepository = void 0;
if (forkOf) {
forkOfRepository = this.getRepository(forkOf);
if (forkOfRepository.forkOf) {
forkOfRepository = forkOfRepository.forkOf;
}
}
fakeRepository = new FakeRepository(repositoryFullName, forkOfRepository);
if (forkOfRepository) {
forkOfRepository.forks.push(fakeRepository);
}
this.repositories.push(fakeRepository);
}
return fakeRepository;
};
FakeGitHub.prototype.createRepository = function (repository) {
return this.createRepositoryInner(repository);
};
FakeGitHub.prototype.forkRepository = function (repository, forkedRepositoryOwner) {
repository = getRepository(repository);
var forkedRepository = {
owner: forkedRepositoryOwner,
name: repository.name,
};
return this.createRepositoryInner(forkedRepository, repository);
};
FakeGitHub.prototype.deleteRepository = function (repository) {
var repositoryFullName = getRepositoryFullName(repository);
var deletedRepository = arrays_1.removeFirst(this.repositories, function (repo) { return repo.name === repositoryFullName; });
var result;
if (!deletedRepository) {
result = Promise.reject(new Error("No fake repository exists with the name \"" + repositoryFullName + "\"."));
}
else {
if (deletedRepository.forkOf) {
arrays_1.removeFirst(deletedRepository.forkOf.forks, function (fork) { return fork === deletedRepository; });
}
result = Promise.resolve();
}
return result;
};
FakeGitHub.prototype.createUser = function (username) {
var user = arrays_1.first(this.users, function (user) { return user.login === username; });
if (user) {
throw new Error("A fake user with the username \"" + username + "\" already exists.");
}
else {
user = {
id: 0,
name: "Fake User Name",
node_id: "Fake Node ID",
login: username,
url: "https://api.github.com/users/" + username,
site_admin: false
};
this.users.push(user);
}
return user;
};
FakeGitHub.prototype.getUser = function (username) {
var user = arrays_1.first(this.users, function (user) { return user.login === username; });
if (!user) {
throw new Error("No fake user with the username \"" + username + "\" exists.");
}
return user;
};
FakeGitHub.prototype.setCurrentUser = function (username) {
this.currentUser = this.getUser(username);
};
FakeGitHub.prototype.getLabel = function (repository, label) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, labels, githubLabel;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getLabels(repository)];
case 1:
labels = _a.sent();
githubLabel = arrays_1.first(labels, function (l) { return l.name === label; });
if (!githubLabel) {
result = Promise.reject(new Error("No fake label named \"" + label + "\" found in the fake repository \"" + getRepositoryFullName(repository) + "\"."));
}
else {
result = Promise.resolve(githubLabel);
}
return [2 /*return*/, result];
}
});
});
};
FakeGitHub.prototype.getCurrentUser = function () {
return this.currentUser
? Promise.resolve(this.currentUser)
: Promise.reject(new Error("No fake current user has been set."));
};
FakeGitHub.prototype.getLabels = function (repository) {
var _this = this;
return toPromise(function () { return _this.getRepository(repository).labels; });
};
FakeGitHub.prototype.getSprintLabels = function (repository) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var labels;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getLabels(repository)];
case 1:
labels = _a.sent();
return [2 /*return*/, getSprintLabels(labels)];
}
});
});
};
FakeGitHub.prototype.createLabel = function (repository, labelName, color) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, fakeRepository, label;
return tslib_1.__generator(this, function (_a) {
if (!labelName) {
result = Promise.reject(new Error("labelName cannot be undefined or empty."));
}
else if (!color) {
result = Promise.reject(new Error("label color cannot be undefined or empty."));
}
else if (color.startsWith("#")) {
result = Promise.reject(new Error("Validation Failed"));
}
else {
fakeRepository = this.getRepository(repository);
label = {
id: 0,
default: false,
node_id: "fake label node_id",
url: "fake label url",
name: labelName,
color: color,
};
fakeRepository.labels.push(label);
result = Promise.resolve(label);
}
return [2 /*return*/, result];
});
});
};
FakeGitHub.prototype.deleteLabel = function (repository, label) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var labelName, result, fakeRepository, removedLabel;
return tslib_1.__generator(this, function (_a) {
labelName = (!label || typeof label === "string") ? label : label.name;
if (!labelName) {
result = Promise.reject(new Error("label cannot be undefined or an empty string."));
}
else {
fakeRepository = this.getRepository(repository);
removedLabel = arrays_1.removeFirst(fakeRepository.labels, function (label) { return label.name === labelName; });
if (!removedLabel) {
result = Promise.reject(new Error("No label named \"" + labelName + "\" found in the fake repository \"" + getRepositoryFullName(repository) + "\"."));
}
else {
result = Promise.resolve();
}
}
return [2 /*return*/, result];
});
});
};
FakeGitHub.prototype.updateLabelColor = function (repository, labelName, newColor) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var fakeRepository, label, result;
return tslib_1.__generator(this, function (_a) {
fakeRepository = this.getRepository(repository);
label = arrays_1.first(fakeRepository.labels, function (label) { return label.name === labelName; });
if (!label) {
result = Promise.reject(new Error("No label named \"" + labelName + "\" found in the fake repository \"" + getRepositoryFullName(repository) + "\"."));
}
else {
label.color = newColor;
result = Promise.resolve();
}
return [2 /*return*/, result];
});
});
};
FakeGitHub.prototype.getMilestone = function (repository, milestone) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var milestones, result, milestoneMatch, milestoneMatch;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getMilestones(repository)];
case 1:
milestones = _a.sent();
if (typeof milestone === "string") {
milestoneMatch = arrays_1.first(milestones, function (m) { return m.title === milestone; });
if (!milestoneMatch) {
result = Promise.reject(new Error("No milestone found with the name \"" + milestone + "\" in the fake repository \"" + getRepositoryFullName(repository) + "\"."));
}
else {
result = Promise.resolve(milestoneMatch);
}
}
else {
milestoneMatch = arrays_1.first(milestones, function (m) { return m.number === milestone; });
if (!milestoneMatch) {
result = Promise.reject(new Error("No milestone found with the id number " + milestone + " in the fake repository \"" + getRepositoryFullName(repository) + "\"."));
}
else {
result = Promise.resolve(milestoneMatch);
}
}
return [2 /*return*/, result];
}
});
});
};
FakeGitHub.prototype.getMilestones = function (repository, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var fakeRepository, result;
return tslib_1.__generator(this, function (_a) {
fakeRepository = this.getRepository(repository);
result = fakeRepository.milestones;
if (options && options.open !== undefined) {
result = arrays_1.where(result, function (milestone) { return milestone.state === (options.open ? "open" : "closed"); });
}
return [2 /*return*/, result];
});
});
};
FakeGitHub.prototype.getSprintMilestones = function (repository, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var milestones;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getMilestones(repository, options)];
case 1:
milestones = _a.sent();
return [2 /*return*/, githubMilestonesToSprintMilestones(milestones)];
}
});
});
};
FakeGitHub.prototype.createMilestone = function (repository, milestoneName, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var fakeRepository, milestone;
return tslib_1.__generator(this, function (_a) {
fakeRepository = this.getRepository(repository);
milestone = {
title: milestoneName,
number: 0,
due_on: addOffset(options && options.endDate || "2000-01-02"),
state: "open",
closed_issues: 0,
open_issues: 0
};
fakeRepository.milestones.push(milestone);
return [2 /*return*/, milestone];
});
});
};
FakeGitHub.prototype.createSprintMilestone = function (repository, sprintNumber, sprintEndDate) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var milestoneName, githubMilestone;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
milestoneName = getSprintMilestoneName(sprintNumber);
return [4 /*yield*/, this.createMilestone(repository, milestoneName, { endDate: sprintEndDate })];
case 1:
githubMilestone = _a.sent();
return [2 /*return*/, githubMilestoneToSprintMilestone(githubMilestone)];
}
});
});
};
FakeGitHub.prototype.updateMilestoneEndDate = function (repository, milestoneNumber, newSprintEndDate) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var milestone;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getMilestone(repository, milestoneNumber)];
case 1:
milestone = _a.sent();
milestone.due_on = addOffset(newSprintEndDate);
return [2 /*return*/, milestone];
}
});
});
};
FakeGitHub.prototype.updateSprintMilestoneEndDate = function (repository, sprintMilestone, newSprintEndDate) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var githubMilestone;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.updateMilestoneEndDate(repository, sprintMilestone.milestoneNumber, newSprintEndDate)];
case 1:
githubMilestone = _a.sent();
return [2 /*return*/, githubMilestoneToSprintMilestone(githubMilestone)];
}
});
});
};
FakeGitHub.prototype.closeMilestone = function (repository, milestoneNumber) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var milestone;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getMilestone(repository, milestoneNumber)];
case 1:
milestone = _a.sent();
milestone.state = "closed";
return [2 /*return*/];
}
});
});
};
FakeGitHub.prototype.closeSprintMilestone = function (repository, sprintMilestone) {
return this.closeMilestone(repository, sprintMilestone.milestoneNumber);
};
FakeGitHub.prototype.createFakePullRequest = function (repository, pullRequest) {
repository = getRepository(repository);
var repositoryFullName = getRepositoryFullName(repository);
var fakeRepository = this.getRepository(repository);
var result;
var baseBranch = getRepositoryBranch(pullRequest.base.label);
if (baseBranch.owner !== repository.owner) {
throw new Error("The owner of the repository (" + repository.owner + ") must be the same owner as the base branch (" + baseBranch.owner + ").");
}
var headBranch = getRepositoryBranch(pullRequest.head.label);
if (headBranch.owner === repository.owner) {
// Pull request between branches within the same repository.
if (!arrays_1.contains(fakeRepository.branches, function (branch) { return branch.name === baseBranch.name; })) {
throw new Error("No branch exists in the fake repository \"" + repositoryFullName + "\" with the name \"" + baseBranch.name + "\".");
}
else if (!arrays_1.contains(fakeRepository.branches, function (branch) { return branch.name === headBranch.name; })) {
throw new Error("No branch exists in the fake repository \"" + repositoryFullName + "\" with the name \"" + baseBranch.name + "\".");
}
}
else {
// Pull request between branches in different repositories.
var forkedRepository = fakeRepository.getFork(headBranch.owner);
if (!forkedRepository) {
throw new Error("No fork of the fake repository \"" + getRepositoryFullName(repository) + "\" exists for the owner \"" + headBranch.owner + "\".");
}
else if (!arrays_1.contains(forkedRepository.branches, function (branch) { return branch.name === headBranch.name; })) {
throw new Error("No branch exists in the forked fake repository \"" + forkedRepository.name + "\" with the name \"" + headBranch.name + "\".");
}
}
if (pullRequest.base.label === pullRequest.head.label) {
throw new Error("The base label (\"" + pullRequest.base.label + "\") cannot be the same as the head label (\"" + pullRequest.head.label + "\").");
}
else {
var existingPullRequest = arrays_1.first(fakeRepository.pullRequests, function (pr) { return pr.number === pullRequest.number; });
if (existingPullRequest) {
throw new Error("A pull request already exists in the fake repository \"" + getRepositoryFullName(repository) + "\" with the number " + pullRequest.number + ".");
}
else {
pullRequest.body = pullRequest.body || "";
result = tslib_1.__assign(tslib_1.__assign({}, pullRequest), { comments: [] });
fakeRepository.pullRequests.push(result);
}
}
return result;
};
FakeGitHub.prototype.createPullRequest = function (repository, baseBranch, headBranch, options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var fakeRepository;
return tslib_1.__generator(this, function (_a) {
repository = getRepository(repository);
fakeRepository = this.getRepository(repository);
baseBranch = getRepositoryBranch(baseBranch);
if (baseBranch.owner) {
throw new Error("When creating a pull request, the provided baseBranch (" + getRepositoryBranchFullName(baseBranch) + ") cannot have an owner.");
}
baseBranch.owner = repository.owner;
headBranch = getRepositoryBranch(headBranch);
if (!headBranch.owner) {
headBranch.owner = repository.owner;
}
return [2 /*return*/, this.createFakePullRequest(repository, {
base: {
label: getRepositoryBranchFullName(baseBranch),
ref: baseBranch.name,
sha: "fake-base-sha",
},
diff_url: "fake-diff-url",
head: {
label: getRepositoryBranchFullName(headBranch),
ref: headBranch.name,
sha: "fake-head-sha",
},
html_url: "fake-html-url",
id: fakeRepository.pullRequests.length + 1,
labels: [],
number: fakeRepository.pullRequests.length + 1,
state: "open",
title: options.title,
url: "fake-url",
body: options.body
})];
});
});
};
FakeGitHub.prototype.updatePullRequest = function (repository, pullRequest, options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var existingPullRequest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getPullRequest(repository, getPullRequestNumber(pullRequest))];
case 1:
existingPullRequest = _a.sent();
Object.assign(existingPullRequest, options);
return [2 /*return*/, existingPullRequest];
}
});
});
};
FakeGitHub.prototype.closePullRequest = function (repository, pullRequestNumber) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var existingPullRequest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getPullRequest(repository, getPullRequestNumber(pullRequestNumber))];
case 1:
existingPullRequest = _a.sent();
existingPullRequest.state = "closed";
return [2 /*return*/];
}
});
});
};
FakeGitHub.prototype.mergePullRequest = function (repository, pullRequest, _mergeMethod) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var existingPullRequest, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getPullRequest(repository, getPullRequestNumber(pullRequest))];
case 1:
existingPullRequest = _a.sent();
if (existingPullRequest.state === "closed") {
result = Promise.reject(new Error("The pull request (" + getRepositoryFullName(repository) + "/" + existingPullRequest.number + ") is already closed."));
}
else {
existingPullRequest.state = "closed";
result = Promise.resolve();
}
return [2 /*return*/, result];
}
});
});
};
FakeGitHub.prototype.getPullRequest = function (repository, pullRequestNumber) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var pullRequests, pullRequest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getPullRequests(repository)];
case 1:
pullRequests = _a.sent();
pullRequest = arrays_1.first(pullRequests, function (pr) { return pr.number === pullRequestNumber; });
return [2 /*return*/, pullRequest
? Promise.resolve(pullRequest)
: Promise.reject(new Error("No pull request found in fake repository \"" + getRepositoryFullName(repository) + "\" with number " + pullRequestNumber + "."))];
}
});
});
};
FakeGitHub.prototype.getPullRequests = function (repository, options) {
var fakeRepository = this.getRepository(repository);
var result = fakeRepository.pullRequests;
if (options) {
if (options.open) {
result = arrays_1.where(result, function (pullRequest) { return pullRequest.state === (options.open ? "open" : "closed"); });
}
if (options.head) {
result = arrays_1.where(result, function (pullRequest) { return pullRequest.head.ref === options.head; });
}
}
return Promise.resolve(result);
};
FakeGitHub.prototype.addPullRequestAssignees = function (repository, githubPullRequest, assignees) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var pullRequestNumber, pullRequest, assignees_1, assignees_1_1, assignee, _a, _b, e_1_1;
var e_1, _c;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
pullRequestNumber = getPullRequestNumber(githubPullRequest);
return [4 /*yield*/, this.getPullRequest(repository, pullRequestNumber)];
case 1:
pullRequest = _d.sent();
if (!pullRequest.assignees) {
pullRequest.assignees = [];
}
if (!Array.isArray(assignees)) {
assignees = [assignees];
}
_d.label = 2;
case 2:
_d.trys.push([2, 8, 9, 10]);
assignees_1 = tslib_1.__values(assignees), assignees_1_1 = assignees_1.next();
_d.label = 3;
case 3:
if (!!assignees_1_1.done) return [3 /*break*/, 7];
assignee = assignees_1_1.value;
if (!(typeof assignee === "string")) return [3 /*break*/, 5];
_b = (_a = pullRequest.assignees).push;
return [4 /*yield*/, this.getUser(assignee)];
case 4:
_b.apply(_a, [_d.sent()]);
return [3 /*break*/, 6];
case 5:
pullRequest.assignees.push(assignee);
_d.label = 6;
case 6:
assignees_1_1 = assignees_1.next();
return [3 /*break*/, 3];
case 7: return [3 /*break*/, 10];
case 8:
e_1_1 = _d.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 10];
case 9:
try {
if (assignees_1_1 && !assignees_1_1.done && (_c = assignees_1.return)) _c.call(assignees_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 10: return [2 /*return*/];
}
});
});
};
FakeGitHub.prototype.addPullRequestLabels = function (repository, githubPullRequest, labelNames) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var pullRequestNumber, labelNamesArray, repositoryLabels, _loop_1, this_1, labelNamesArray_1, labelNamesArray_1_1, labelName, e_2_1, pullRequest, pullRequestLabels, pullRequestLabelNames, labelNamesAddedToPullRequest, _a, _b, _c;
var e_2, _d, _e;
var _this = this;
return tslib_1.__generator(this, function (_f) {
switch (_f.label) {
case 0:
pullRequestNumber = getPullRequestNumber(githubPullRequest);
labelNamesArray = (Array.isArray(labelNames) ? labelNames : [labelNames]);
return [4 /*yield*/, this.getLabels(repository)];
case 1:
repositoryLabels = _f.sent();
_loop_1 = function (labelName) {
var _a, _b;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!!arrays_1.contains(repositoryLabels, function (repositoryLabel) { return repositoryLabel.name === labelName; })) return [3 /*break*/, 2];
_b = (_a = repositoryLabels).push;
return [4 /*yield*/, this_1.createLabel(repository, labelName, "ededed")];
case 1:
_b.apply(_a, [_c.sent()]);
_c.label = 2;
case 2: return [2 /*return*/];
}
});
};
this_1 = this;
_f.label = 2;
case 2:
_f.trys.push([2, 7, 8, 9]);
labelNamesArray_1 = tslib_1.__values(labelNamesArray), labelNamesArray_1_1 = labelNamesArray_1.next();
_f.label = 3;
case 3:
if (!!labelNamesArray_1_1.done) return [3 /*break*/, 6];
labelName = labelNamesArray_1_1.value;
return [5 /*yield**/, _loop_1(labelName)];
case 4:
_f.sent();
_f.label = 5;
case 5:
labelNamesArray_1_1 = labelNamesArray_1.next();
return [3 /*break*/, 3];
case 6: return [3 /*break*/, 9];
case 7:
e_2_1 = _f.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 9];
case 8:
try {
if (labelNamesArray_1_1 && !labelNamesArray_1_1.done && (_d = labelNamesArray_1.return)) _d.call(labelNamesArray_1);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 9: return [4 /*yield*/, this.getPullRequest(repository, pullRequestNumber)];
case 10:
pullRequest = _f.sent();
pullRequestLabels = pullRequest.labels;
pullRequestLabelNames = arrays_1.map(pullRequestLabels, function (label) { return label.name; });
labelNamesAddedToPullRequest = arrays_1.where(labelNamesArray, function (labelName) { return !arrays_1.contains(pullRequestLabelNames, labelName); });
if (!(labelNamesAddedToPullRequest.length > 0)) return [3 /*break*/, 12];
_b = (_a = (_e = pullRequest.labels).push).apply;
_c = [_e];
return [4 /*yield*/, Promise.all(arrays_1.map(labelNamesAddedToPullRequest, function (labelName) { return _this.getLabel(repository, labelName); }))];
case 11:
_b.apply(_a, _c.concat([tslib_1.__spread.apply(void 0, [_f.sent()])]));
_f.label = 12;
case 12: return [2 /*return*/, labelNamesAddedToPullRequest];
}
});
});
};
FakeGitHub.prototype.removePullRequestLabels = function (repository, githubPullRequest, labelNames) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var pullRequestNumber, pullRequest, labelNamesToRemove, currentLabelNames, removedLabelNames;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
pullRequestNumber = getPullRequestNumber(githubPullRequest);
return [4 /*yield*/, this.getPullRequest(repository, pullRequestNumber)];
case 1:
pullRequest = _a.sent();
labelNamesToRemove = (Array.isArray(labelNames) ? labelNames : [labelNames]);
currentLabelNames = arrays_1.map(pullRequest.labels, function (label) { return label.name; });
removedLabelNames = arrays_1.where(currentLabelNames, function (labelName) { return arrays_1.contains(labelNamesToRemove, labelName); });
pullRequest.labels = arrays_1.where(pullRequest.labels, function (label) { return !arrays_1.contains(labelNamesToRemove, label.name); });
return [2 /*return*/, removedLabelNames];
}
});
});
};
FakeGitHub.prototype.setPullRequestMilestone = function (repository, githubPullRequest, milestone) {
var _this = this;
var pullRequestNumber = getPullRequestNumber(githubPullRequest);
return this.getPullRequest(repository, pullRequestNumber)
.then(function (pullRequest) {
var milestonePromise;
if (typeof milestone === "string" || typeof milestone === "number") {
milestonePromise = _this.getMilestone(repository, milestone);
}
else {
milestonePromise = Promise.resolve(milestone);
}
milestonePromise.then(function (githubMilestone) {
pullRequest.milestone = githubMilestone;
});
});
};
FakeGitHub.prototype.getPullRequestComments = function (repository, githubPullRequest) {
var pullRequestNumber = getPullRequestNumber(githubPullRequest);
return this.getPullRequest(repository, pullRequestNumber)
.then(function (fakePullRequest) { return fakePullRequest.comments; });
};
FakeGitHub.prototype.createPullRequestComment = function (repository, githubPullRequest, commentBody) {
var _this = this;
return this.getPullRequestComments(repository, githubPullRequest)
.then(function (comments) {
return _this.getCurrentUser()
.then(function (currentUser) {
var newComment = {
id: comments.length + 1,
node_id: "fake_node_id",
user: currentUser,
html_url: "fake_html_url",
url: "fake_url",
body: commentBody,
created_at: "fake_created_at",
updated_at: "fake_updated_at",
};
comments.push(newComment);
return newComment;
});
});
};
FakeGitHub.prototype.updatePullRequestComment = function (repository, githubPullRequest, comment, commentBody) {
return this.getPullRequestComments(repository, githubPullRequest)
.then(function (comments) {
var result;
var commentId = getCommentId(comment);
var commentToUpdate = arrays_1.first(comments, function (existingComment) { return existingComment.id === commentId; });
if (!commentToUpdate) {
result = Promise.reject(new Error("No comment found with the ID " + commentId + "."));
}
else {
commentToUpdate.body = commentBody;
result = Promise.resolve(commentToUpdate);
}
return result;
});
};
FakeGitHub.prototype.deletePullRequestComment = function (repository, githubPullRequest, comment) {
var pullRequestNumber = getPullRequestNumber(githubPullRequest);
return this.getPullRequest(repository, pullRequestNumber)
.then(function (pullRequest) {
var result;
var commentId = getCommentId(comment);
if (!arrays_1.contains(pullRequest.comments, function (existingComment) { return existingComment.id === commentId; })) {
result = Promise.reject(new Error("No comment was found with the id " + commentId + "."));
}
else {
pullRequest.comments = arrays_1.where(pullRequest.comments, function (existingComment) { return existingComment.id !== commentId; });
result = Promise.resolve();
}
return result;
});
};
FakeGitHub.prototype.getCommit = function (repository, commitId) {
var _this = this;
return toPromise(function () {
var fakeRepository = _this.getRepository(repository);
return arrays_1.first(fakeRepository.commits, function (commit) { return commit.sha.startsWith(commitId); });
});
};
FakeGitHub.prototype.getContents = function (repository, filepath) {
var _this = this;
if (filepath !== undefined) {
return toPromise(function () {
var fakeRepository = _this.getRepository(repository);
return arrays_1.first(fakeRepository.content);
});
}
return toPromise(function () {
var fakeRepository = _this.getRepository(repository);
return arrays_1.first(fakeRepository.content);
});
};
FakeGitHub.prototype.createCommit = function (repository, commitId, message) {
var fakeRepository = this.getRepository(repository);
fakeRepository.commits.push({
sha: commitId,
commit: {
message: message
}
});
return Promise.resolve();
};
FakeGitHub.prototype.getAllReferences = function (repository) {
var _this = this;
return toPromise(function () { return _this.getRepository(repository).branches; });
};
FakeGitHub.prototype.getAllBranches = function (repository) {
return this.getAllReferences(repository)
.then(referencesToBranches);
};
FakeGitHub.prototype.getBranch = function (repository, branchName) {
var _this = this;
return toPromise(function () {
var fakeRepository = _this.getRepository(repository);
var result = arrays_1.first(fakeRepository.branches, function (branch) { return branch.name === branchName; });
if (!result) {
throw new Error("Could not get details about the branch \"" + branchName + "\" in repository \"" + fakeRepository.name + "\" because the branch didn't exist.");
}
return result;
});
};
FakeGitHub.prototype.deleteBranch = function (repository, branchName) {
var _this = this;
return toPromise(function () {
var fakeRepository = _this.getRepository(repository);
var removedBranch = arrays_1.removeFirst(fakeRepository.branches, function (branch) { return branch.name === branchName; });
if (!removedBranch) {
throw new Error("Could not delete branch \"" + branchName + "\" from repository \"" + fakeRepository.name + "\" because the branch didn't exist.");
}
});
};
FakeGitHub.prototype.createBranch = function (repository, branchName, branchSha) {
var _this = this;
return toPromise(function () {
var fakeRepository = _this.getRepository(repository);
var result;
if (arrays_1.contains(fakeRepository.branches, function (branch) { return branch.name === branchName; })) {
throw new Error("Could not create a branch named \"" + branchName + "\" in repository \"" + fakeRepository.name + "\" because a branch with that name already exists.");
}
else if (!arrays_1.contains(fakeRepository.commits, function (commit) { return commit.sha === branchSha; })) {
throw new Error("Could not create a branch named \"" + branchName + "\" in repository \"" + fakeRepository.name + "\" with the SHA \"" + branchSha + "\" because no commit exists in the repository with the provided SHA.");
}
else {
result = {
name: branchName,
ref: "refs/heads/" + branchName,
node_id: "fake-node-id",
url: "fake-branch-url",
object: {
type: "commit",
sha: branchSha,
url: "fake-branch-commit-sha",
}
};
fakeRepository.branches.push(result);
}
return result;
});
};
return FakeGitHub;
}());
exports.FakeGitHub = FakeGitHub;
function getSprintLabels(labels) {
var e_3, _a;
var repositorySprintLabels = [];
var _loop_2 = function (repositoryLabel) {
if (repositoryLabel && repositoryLabel.name && repositoryLabel.name.includes("-Sprint-")) {
var repositoryLabelName = repositoryLabel.name;
var firstDashIndex = repositoryLabelName.indexOf("-");
var sprintLabelType = repositoryLabelName.substring(0, firstDashIndex);
var lastDashIndex = repositoryLabelName.lastIndexOf("-");
var sprintNumber_1 = parseInt(repositoryLabelName.substring(lastDashIndex + 1));
var sprintLabel = arrays_1.first(repositorySprintLabels, function (resultLabel) { return resultLabel.sprint === sprintNumber_1; });
if (sprintLabel == undefined) {
sprintLabel = {
sprint: sprintNumber_1
};
repositorySprintLabels.push(sprintLabel);
}
sprintLabel[sprintLabelType.toLowerCase() + "Color"] = repositoryLabel.color;
}
};
try {
for (var labels_1 = tslib_1.__values(labels), labels_1_1 = labels_1.next(); !labels_1_1.done; labels_1_1 = labels_1.next()) {
var repositoryLabel = labels_1_1.value;
_loop_2(repositoryLabel);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (labels_1_1 && !labels_1_1.done && (_a = labels_1.return)) _a.call(labels_1);
}
finally { if (e_3) throw e_3.error; }
}
return repositorySprintLabels;
}
exports.getSprintLabels = getSprintLabels;
/**
* A class that wraps @octokit/rest to interact with github.com.
*/
var RealGitHub = /** @class */ (function () {
function RealGitHub(githubClients) {
this.githubClients = githubClients;
}
RealGitHub.fromOctokit = function (github) {
return new RealGitHub(github);
};
RealGitHub.fromToken = function (authenticationToken) {
var options = {
auth: authenticationToken.t