UNPKG

@codecovevienna/gittt-cli

Version:

Tracking time with CLI into a git repository

415 lines (414 loc) 15 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuestionHelper = void 0; const inquirer_1 = __importDefault(require("inquirer")); const lodash_1 = __importDefault(require("lodash")); const moment_1 = __importDefault(require("moment")); const types_1 = require("../types"); const _1 = require("."); class QuestionHelper { } exports.QuestionHelper = QuestionHelper; _a = QuestionHelper; QuestionHelper.filterJiraEndpoint = (input) => { const inputString = input; // Ensure no trailing slash if (inputString[inputString.length - 1] === "/") { return inputString.slice(0, inputString.length - 1); } else { return inputString; } }; QuestionHelper.askYear = (defaultValue) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: lodash_1.default.isNumber(defaultValue) ? defaultValue : (0, moment_1.default)().year(), message: "Year", name: "choice", type: "number", validate: _1.ValidationHelper.validateYear, }, ]); return parseInt(choice.choice, 10); }); QuestionHelper.askMonth = (defaultValue) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: lodash_1.default.isNumber(defaultValue) ? defaultValue : (0, moment_1.default)().month() + 1, message: "Month", name: "choice", type: "number", validate: _1.ValidationHelper.validateMonth, }, ]); return parseInt(choice.choice, 10); }); QuestionHelper.askDay = (defaultValue) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: lodash_1.default.isNumber(defaultValue) ? defaultValue : (0, moment_1.default)().date(), message: "Day", name: "choice", type: "number", validate: _1.ValidationHelper.validateDay, }, ]); return parseInt(choice.choice, 10); }); QuestionHelper.askHour = (defaultValue) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: lodash_1.default.isNumber(defaultValue) ? defaultValue : (0, moment_1.default)().hour(), message: "Hour", name: "choice", type: "number", validate: _1.ValidationHelper.validateHour, }, ]); return parseInt(choice.choice, 10); }); QuestionHelper.askMinute = (defaultValue) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: lodash_1.default.isNumber(defaultValue) ? defaultValue : (0, moment_1.default)().minute(), message: "Minute", name: "choice", type: "number", validate: _1.ValidationHelper.validateMinute, }, ]); return parseInt(choice.choice, 10); }); QuestionHelper.askAmount = (oldAmount) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: lodash_1.default.isNumber(oldAmount) ? oldAmount : undefined, message: "Amount", name: "choice", type: "number", validate: _1.ValidationHelper.validateAmount, }, ]); return parseFloat(choice.choice); }); QuestionHelper.askMessage = (defaultValue) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { default: defaultValue ? defaultValue : undefined, message: "Message", name: "choice", type: "input", }, ]); return choice.choice.length > 0 ? choice.choice : undefined; }); QuestionHelper.askGitUrl = () => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { message: "Git Repository URL:", name: "choice", type: "input", validate: _1.ValidationHelper.validateGitUrl, }, ]); return choice.choice; }); QuestionHelper.askJiraLink = (project, prevData, endpointVersion = "latest") => __awaiter(void 0, void 0, void 0, function* () { const jiraAnswers = yield inquirer_1.default.prompt([ { default: prevData ? prevData.host : "https://jira.gittt.org", filter: QuestionHelper.filterJiraEndpoint, message: "Jira host", name: "host", type: "input", validate: _1.ValidationHelper.validateJiraEndpoint, }, { default: prevData ? prevData.username : undefined, message: "Jira username", name: "username", type: "input", // TODO validate }, { message: prevData ? "Jira password (leave empty if not changed)" : "Jira password", name: "password", type: "password", // TODO validate }, { default: prevData ? prevData.key : undefined, message: "Jira project key (e.g. GITTT)", name: "key", type: "input", validate: _1.ValidationHelper.validateJiraKey, }, { default: prevData ? prevData.issue : undefined, message: "Jira issue key (e.g. EPIC-1 or STORY-1337), may be empty", name: "issue", type: "input", validate: _1.ValidationHelper.validateJiraIssueKey, }, ]); const { host, key, issue, username, password } = jiraAnswers; let hash; // Assuming edit mode, use hash from prevData if (!password && prevData) { hash = prevData.hash; } else { hash = Buffer .from(`${username}:${password}`) .toString("base64"); } const projectName = project.name; const link = { host, endpoint: `/rest/gittt/${endpointVersion}/`, hash, key, issue, linkType: "Jira", projectName, username, }; return link; }); QuestionHelper.askMultipieLink = (project, prevData) => __awaiter(void 0, void 0, void 0, function* () { const multipieAnswers = yield inquirer_1.default.prompt([ { default: prevData ? prevData.endpoint : "https://multipie.gittt.org/v1/publish", // also works for generic hosts filter: QuestionHelper.filterJiraEndpoint, message: "Multipie publish endpoint", name: "endpoint", type: "input", validate: _1.ValidationHelper.validateJiraEndpoint, }, { default: prevData ? prevData.rolesEndpoint : "https://multipie.gittt.org/v1/roles", // also works for generic hosts filter: QuestionHelper.filterJiraEndpoint, message: "Multipie roles endpoint", name: "rolesEndpoint", type: "input", validate: _1.ValidationHelper.validateJiraEndpoint, }, { message: "gittt-cli client secret", name: "clientSecret", type: "input", validate: _1.ValidationHelper.validateClientSecret, }, { message: "Multipie username", name: "username", type: "input", validate: _1.ValidationHelper.validateUsername }, { message: "Multipie password", name: "password", type: "password", validate: _1.ValidationHelper.validatePassword }, ]); const { endpoint, rolesEndpoint, username, password, clientSecret } = multipieAnswers; const projectName = project.name; const link = { endpoint, rolesEndpoint, linkType: "Multipie", projectName, username, password, clientSecret, }; return link; }); QuestionHelper.chooseRecord = (records) => __awaiter(void 0, void 0, void 0, function* () { const choice = yield inquirer_1.default.prompt([ { choices: records.map((rc) => { return { name: `${(0, moment_1.default)(rc.end).format("DD.MM.YYYY, HH:mm:ss")}: ${rc.amount} ${rc.type} as "${rc.role}" - "${lodash_1.default. truncate(rc.message)}"`, value: rc.guid, }; }), message: "List of records", name: "choice", type: "list", }, ]); const chosenRecords = records.filter((rc) => { return rc.guid === choice.choice; }); const [chosenRecord] = chosenRecords; return chosenRecord; }); QuestionHelper.chooseType = (oldType) => __awaiter(void 0, void 0, void 0, function* () { const choices = [ { name: types_1.RECORD_TYPES.Time, value: types_1.RECORD_TYPES.Time, }, ]; const question = { choices, message: "Type", name: "choice", type: "list", }; if (oldType) { question.default = oldType; } const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.chooseRole = (project, record, oldRole) => __awaiter(void 0, void 0, void 0, function* () { const multipieHelper = new _1.MultipieHelper(); const availableRoles = yield multipieHelper.getValidRoles(project, record, oldRole); const question = { choices: availableRoles, message: "Role", name: "choice", type: "list", }; if (oldRole) { question.default = oldRole; } const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.chooseIntegration = () => __awaiter(void 0, void 0, void 0, function* () { const choices = [ "Jira", "Multipie", ]; const question = { choices, message: "What integration should be used?", name: "choice", type: "list", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.chooseDomain = (domains) => __awaiter(void 0, void 0, void 0, function* () { const question = { choices: domains, message: "What domain should be used?", name: "choice", type: "list", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.chooseProjectFile = (projects) => __awaiter(void 0, void 0, void 0, function* () { const question = { choices: projects.map((project) => { if (project.meta) { const { host, port } = project.meta; return { name: `${host}${port ? `:${port}` : ""} ${project.name}`, value: _1.ProjectHelper.getProjectPath(project), }; } else { return { name: `${project.name}`, value: _1.ProjectHelper.getProjectPath(project), }; } }), message: "Choose a project", name: "choice", type: "list", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.chooseOverrideLocalChanges = () => __awaiter(void 0, void 0, void 0, function* () { const question = { choices: [ { name: "Override local config file", value: 0 }, { name: "Override remote config file", value: 1 }, { name: "Exit", value: 2 }, ], message: `Remote repo is not empty, override local changes?`, name: "choice", type: "list", }; const choice = yield inquirer_1.default.prompt([question]); return parseInt(choice.choice, 10); }); QuestionHelper.confirmMigration = () => __awaiter(void 0, void 0, void 0, function* () { const question = { message: `Do you want to migrate from an existing project?`, name: "choice", type: "confirm", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.confirmLinkCreation = () => __awaiter(void 0, void 0, void 0, function* () { const question = { message: `Do you want to setup a new link for this project?`, name: "choice", type: "confirm", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.confirmPushLocalChanges = () => __awaiter(void 0, void 0, void 0, function* () { const question = { message: `Found local changes, they have to be pushed before publishing`, name: "choice", type: "confirm", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.confirmSetup = () => __awaiter(void 0, void 0, void 0, function* () { const question = { message: `Looks like you never used gittt before, should it be set up?`, name: "choice", type: "confirm", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.confirmInit = () => __awaiter(void 0, void 0, void 0, function* () { const question = { message: `This will reset the project if it is already initialized, are you sure?`, name: "choice", type: "confirm", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; }); QuestionHelper.confirmTicketNumber = (ticketNumber) => __awaiter(void 0, void 0, void 0, function* () { const question = { message: `Ticket number (${ticketNumber}) found in branch name, should it be added to the commit message?`, name: "choice", type: "confirm", }; const choice = yield inquirer_1.default.prompt([question]); return choice.choice; });