UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

237 lines (236 loc) 13.2 kB
"use strict"; 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:update"); let UpdateDistributionGroupCommand = class UpdateDistributionGroupCommand extends commandline_1.AppCommand { run(client) { return __awaiter(this, void 0, void 0, function* () { const app = this.app; // validate that string and file properties are not specified simultaneously this.validateParameters(); // validating parameters and loading provided files (if any) const testersToAdd = list_of_users_helper_1.getUsersList(this.testersToAdd, this.testersToAddListFile, debug); const testersToDelete = list_of_users_helper_1.getUsersList(this.testersToDelete, this.testersToDeleteListFile, debug); const newDistributionGroupNameValidation = this.isDistributionGroupNameFree(client, app, this.newDistributionGroupName); // showing spinner while parameters are validated const [testersToAddEmails, testersToDeleteEmails] = yield interaction_1.out.progress("Validating parameters...", Promise.all([testersToAdd, testersToDelete, newDistributionGroupNameValidation])); let deletedTestersEmails; if (testersToDeleteEmails.length) { debug("Deleting testers from distribution group"); deletedTestersEmails = yield this.deleteTestersFromDistributionGroup(client, app, testersToDeleteEmails); } else { deletedTestersEmails = []; } let addedTestersEmails; if (testersToAddEmails.length) { debug("Adding testers to distribution group"); addedTestersEmails = yield this.addTestersToDistributionGroup(client, app, testersToAddEmails); } else { addedTestersEmails = []; } if (deletedTestersEmails.length !== testersToDeleteEmails.length || addedTestersEmails.length !== testersToAddEmails.length) { interaction_1.out.text("Updating the list of testers was partially successful"); } let currentGroupName; const options = {}; if (!_.isNil(this.newDistributionGroupName)) { debug("Renaming the distribution group"); options.name = this.newDistributionGroupName; currentGroupName = this.newDistributionGroupName; } else { currentGroupName = this.distributionGroup; } if (this.makePublic) { debug("Setting distribution group public status to true"); options.isPublic = true; } if (this.makePrivate) { debug("Setting distribution group public status to false"); options.isPublic = false; } if (!_.isNil(options.name) || !_.isNil(options.isPublic)) { yield this.updateDistributionGroup(client, app, options); } interaction_1.out.text((result) => `Distribution group ${result.name} was successfully updated`, { name: currentGroupName, addedTesters: addedTestersEmails, deletedTesters: deletedTestersEmails, }); return commandline_1.success(); }); } validateParameters() { if (_.isNil(this.newDistributionGroupName) && _.isNil(this.testersToAdd) && _.isNil(this.testersToAddListFile) && _.isNil(this.testersToDelete) && _.isNil(this.testersToDeleteListFile) && _.isNil(this.makePublic) && _.isNil(this.makePrivate)) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "nothing to update"); } if (!_.isNil(this.testersToAdd) && !_.isNil(this.testersToAddListFile)) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "parameters 'add-testers' and 'add-testers-file' are mutually exclusive"); } if (!_.isNil(this.testersToDelete) && !_.isNil(this.testersToDeleteListFile)) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "parameters 'delete-testers' and 'delete-testers-file' are mutually exclusive"); } if (this.makePublic && this.makePrivate) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, "parameters 'public' and 'private' are mutually exclusive"); } } isDistributionGroupNameFree(client, app, name) { return __awaiter(this, void 0, void 0, function* () { if (!_.isNil(name)) { try { yield client.distributionGroups.get(app.ownerName, app.appName, name); // Throw an exception if 404 error was not thrown during clientRequest. // In particular case existance of the group means it is "not free". throw 200; } catch (error) { if (error && error.response && error.response.status === 404) { // 404 is correct status code for this case return; } if (error === 200) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${name} already exists`); } else { debug(`Failed to check if the distribution group ${name} already exists - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, `failed to check if the distribution group ${name} already exists - ${util_1.inspect(error)}`); } } } }); } deleteTestersFromDistributionGroup(client, app, userEmails) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield interaction_1.out.progress("Deleting testers from the distribution group...", client.distributionGroups.removeUser(app.ownerName, app.appName, this.distributionGroup, { userEmails, })); return result.filter((result) => result.status < 400).map((result) => result.userEmail); } catch (error) { if (error === 404) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${this.distributionGroup} doesn't exist`); } else { debug(`Failed to delete testers from the distribution group ${this.distributionGroup} - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, `failed to delete testers from the distribution group`); } } }); } addTestersToDistributionGroup(client, app, userEmails) { return __awaiter(this, void 0, void 0, function* () { try { const result = yield interaction_1.out.progress("Adding testers to the distribution group...", client.distributionGroups.addUser(app.ownerName, app.appName, this.distributionGroup, { userEmails, })); return result.filter((result) => result.status < 400).map((result) => result.userEmail); } catch (error) { if (error === 404) { throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${this.distributionGroup} doesn't exist`); } else { debug(`Failed to add testers to the distribution group ${this.distributionGroup} - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, "failed to add testers to the distribution group"); } } }); } updateDistributionGroup(client, app, options) { return __awaiter(this, void 0, void 0, function* () { try { yield interaction_1.out.progress("Updating the distribution group...", client.distributionGroups.update(app.ownerName, app.appName, this.distributionGroup, options)); return; } catch (error) { switch (error) { case 400: throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `Can't update ${this.distributionGroup} group`); case 404: throw commandline_1.failure(commandline_1.ErrorCodes.InvalidParameter, `distribution group ${this.distributionGroup} doesn't exist`); default: debug(`Failed to update distribution group ${this.distributionGroup} - ${util_1.inspect(error)}`); throw commandline_1.failure(commandline_1.ErrorCodes.Exception, `failed to update the distribution group : ${util_1.inspect(error)}`); } } }); } }; __decorate([ commandline_1.help("Distribution group name"), commandline_1.shortName("g"), commandline_1.longName("group"), commandline_1.required, commandline_1.hasArg ], UpdateDistributionGroupCommand.prototype, "distributionGroup", void 0); __decorate([ commandline_1.help("New distribution group name"), commandline_1.shortName("n"), commandline_1.longName("name"), commandline_1.hasArg ], UpdateDistributionGroupCommand.prototype, "newDistributionGroupName", void 0); __decorate([ commandline_1.help("List of testers to add (use space-separated list of e-mails)"), commandline_1.shortName("t"), commandline_1.longName("add-testers"), commandline_1.hasArg ], UpdateDistributionGroupCommand.prototype, "testersToAdd", void 0); __decorate([ commandline_1.help("List of testers to delete (use space-separated list of e-mails)"), commandline_1.shortName("d"), commandline_1.longName("delete-testers"), commandline_1.hasArg ], UpdateDistributionGroupCommand.prototype, "testersToDelete", void 0); __decorate([ commandline_1.help("Path to file containing list of testers to add"), commandline_1.shortName("T"), commandline_1.longName("add-testers-file"), commandline_1.hasArg ], UpdateDistributionGroupCommand.prototype, "testersToAddListFile", void 0); __decorate([ commandline_1.help("Path to file containing list of testers to delete"), commandline_1.shortName("D"), commandline_1.longName("delete-testers-file"), commandline_1.hasArg ], UpdateDistributionGroupCommand.prototype, "testersToDeleteListFile", void 0); __decorate([ commandline_1.help("Make the distribution group public (allowing anyone to download the releases). Don't use with opposite --private."), commandline_1.shortName("p"), commandline_1.longName("public") ], UpdateDistributionGroupCommand.prototype, "makePublic", void 0); __decorate([ commandline_1.help("Make the distribution group private (allowing only members to download the releases). Don't use with opposite --public."), commandline_1.longName("private") ], UpdateDistributionGroupCommand.prototype, "makePrivate", void 0); UpdateDistributionGroupCommand = __decorate([ commandline_1.help("Update existing distribution group") ], UpdateDistributionGroupCommand); exports.default = UpdateDistributionGroupCommand;