UNPKG

cli-stash

Version:

CLI application to manage and work with Atlassian Stash. Work with your Stash project and repositories from Command lines.

120 lines (119 loc) 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const stash_connector_1 = require("stash-connector"); const baseCommand_1 = require("../../../../libs/core/baseCommand"); const stashResponse_1 = require("../../../../libs/core/stashResponse"); const tables_1 = require("../../../../libs/core/tables"); const ux_1 = require("../../../../libs/core/ux"); class Update extends baseCommand_1.BaseCommand { async run() { const response = new stashResponse_1.StashCLIResponse(); const connector = new stash_connector_1.StashConnector(this.localConfig.getConnectorOptions(this.flags.alias)); try { let inputData; if (this.hasInputData()) { inputData = this.getInputData(); } else { inputData = { description: this.flags.description, title: this.flags.title, toRef: { id: '', repository: { slug: this.flags.slug, project: { key: this.flags.project } } }, reviewers: this.flags.reviewers, }; const targetBranchResult = await connector.projects.repos(this.flags.project).branches(this.flags.slug).list({ filterText: this.flags.target, }); if (targetBranchResult.values.length === 0) { throw new Error('From branch not found (' + this.flags.from + ')'); } if (targetBranchResult.values.length > 1) { throw new Error('Found more than one branch as target with name (' + this.flags.target + ')'); } const targetBranch = targetBranchResult.values[0]; if (inputData.toRef) { inputData.toRef.id = targetBranch.id; } } const pullRequest = await connector.projects.repos(this.flags.project).pullRequests(this.flags.slug).update(this.flags.pull, inputData); response.result = pullRequest; response.status = 0; response.message = this.getRecordCreatedText('Pull Request'); this.ux.log(response.message); this.ux.table([pullRequest], tables_1.PullRequestColumns, { csv: this.flags.csv, extended: this.flags.extended || this.flags.csv }); } catch (error) { this.processError(response, error); } return response; } } exports.default = Update; Update.description = 'Update the title, description, reviewers or destination branch of an existing pull request. ' + ux_1.UX.processDocumentation('<doc:PullRequest>'); Update.examples = [ `$ stash projects:repos:pulls:update -a MyStashAlias --project "ProjectKey" --slug "MyRepoSlug" --pull 1234 --data "{'title':'<Title>','description':'<description>','toRef':{'id':'refs/heads/master','repository':{'slug':'MyRepoSlug','name':null,'project':{'key':'ProjectKey'}}},'reviewers':[{'user':{'name':'jonh.smith'}}]}" --json`, `$ stash projects:repos:pulls:update -a MyStashAlias --project "ProjectKey" --slug "MyRepoSlug" --pull 1234 --file "path/to/json/data/file" --csv`, `$ stash projects:repos:pulls:update -a MyStashAlias --project "ProjectKey" --slug "MyRepoSlug" --pull 1234 --title "Title" --description "desc" --to "main" --reviewers "john.smith, jenny.smith"`, ]; Update.flags = { ...baseCommand_1.BaseCommand.flags, csv: baseCommand_1.BuildFlags.csv, extended: baseCommand_1.BuildFlags.extended, alias: baseCommand_1.BuildFlags.alias, data: baseCommand_1.BuildFlags.input.jsonData('<doc:PullRequestInput>', false, ['from', 'title', 'description', 'reviewers']), file: baseCommand_1.BuildFlags.input.jsonFile('<doc:PullRequestInput>', false, ['from', 'title', 'description', 'reviewers']), project: core_1.Flags.string({ description: 'The Project Key (or user slug like ~userSlug) to update the pull request', required: true, name: 'Project' }), slug: core_1.Flags.string({ description: 'The Repository slug to update the pull request', required: true, name: 'Slug' }), pull: core_1.Flags.integer({ description: 'The Pull Request Id to update', required: true, name: 'Pull Request Id', }), title: core_1.Flags.string({ description: 'The Pull Request Title', required: false, name: 'Title', exclusive: ['data', 'file'], }), description: core_1.Flags.string({ description: 'The Pull Request Description', required: false, name: 'Description', exclusive: ['data', 'file'], }), target: core_1.Flags.string({ description: 'The target (to ref) branch to create the pull request', required: false, name: 'Target', exclusive: ['data', 'file'], }), reviewers: core_1.Flags.string({ description: 'Comma separated pull requests reviewers names', required: false, name: 'Target', exclusive: ['data', 'file'], parse: (input) => { return baseCommand_1.BuildFlags.parseArray(input); } }), };