appcenter-cli
Version:
Command line tool for Visual Studio App Center
113 lines (112 loc) • 6.39 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const commandline_1 = require("../../../util/commandline");
const interaction_1 = require("../../../util/interaction");
const util_1 = require("util");
const _ = require("lodash");
const list_of_users_helper_1 = require("../../../util/misc/list-of-users-helper");
const debug = require("debug")("appcenter-cli:commands:distribute:groups:create");
let CreateDistributionGroupCommand = class CreateDistributionGroupCommand extends commandline_1.AppCommand {
run(client) {
return __awaiter(this, void 0, void 0, function* () {
const app = this.app;
// validate that 'testers' and 'testers-file' are not specified simultaneously
this.validateParameters();
// getting string with testers' emails
debug("Getting list of testers");
const testersEmails = yield interaction_1.out.progress("Loading testers list file...", list_of_users_helper_1.getUsersList(this.testers, this.testersListFile, debug));
debug("Creating distribution group");
yield this.createDistributionGroup(client, app);
// add testers if any were specified
if (testersEmails.length) {
debug("Adding testers to the distribution group");
const addTestersResult = yield this.addTestersToDistributionGroup(client, app, testersEmails);
// filtering users which were actually added
const addedUsers = addTestersResult.filter((userResult) => userResult.status < 400);
interaction_1.out.text((obj) => `Successfully created the ${obj.distributionGroupName} distribution group with ${obj.testersAdded.length} testers`, { distributionGroupName: this.distributionGroup, testersAdded: addedUsers });
}
else {
interaction_1.out.text((obj) => `Successfully created the ${obj.distributionGroupName} distribution group`, {
distributionGroupName: this.distributionGroup,
testersAdded: [],
});
}
return commandline_1.success();
});
}
validateParameters() {
if (!_.isNil(this.testers) && !_.isNil(this.testersListFile)) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "parameters 'testers' and 'testers-file' are mutually exclusive");
}
}
createDistributionGroup(client, app) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield interaction_1.out.progress("Creating distribution group...", client.distributionGroups.create(app.ownerName, app.appName, this.distributionGroup));
}
catch (error) {
if (error === 409) {
throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${this.distributionGroup} already exists`);
}
else {
debug(`Failed to create distribution group ${this.distributionGroup} - ${util_1.inspect(error)}`);
throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to create distribution group");
}
}
});
}
addTestersToDistributionGroup(client, app, users) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield interaction_1.out.progress("Adding testers to the distribution group...", client.distributionGroups.addUser(app.ownerName, app.appName, this.distributionGroup, {
userEmails: users,
}));
}
catch (error) {
if (error.response.statusCode >= 400) {
throw error.response.statusCode;
}
debug(`Failed to add testers to the new distribution group ${this.distributionGroup} - ${util_1.inspect(error)}`);
throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to add testers to the new distribution group");
}
});
}
};
__decorate([
commandline_1.help("Distribution group name"),
commandline_1.shortName("n"),
commandline_1.longName("name"),
commandline_1.required,
commandline_1.hasArg
], CreateDistributionGroupCommand.prototype, "distributionGroup", void 0);
__decorate([
commandline_1.help("List of testers (space-separated list of e-mails)"),
commandline_1.shortName("t"),
commandline_1.longName("testers"),
commandline_1.hasArg
], CreateDistributionGroupCommand.prototype, "testers", void 0);
__decorate([
commandline_1.help("Path to file containing list of testers"),
commandline_1.shortName("T"),
commandline_1.longName("testers-file"),
commandline_1.hasArg
], CreateDistributionGroupCommand.prototype, "testersListFile", void 0);
CreateDistributionGroupCommand = __decorate([
commandline_1.help("Create new distribution group")
], CreateDistributionGroupCommand);
exports.default = CreateDistributionGroupCommand;