Skip to content

Commit

Permalink
Add option --remove-source-branch to pull-request:close command
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Aug 11, 2016
1 parent 640bea2 commit 9ef9c42
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Command/PullRequest/PullRequestCloseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ protected function configure()
->setDescription('Closes a pull request')
->addArgument('pr_number', InputArgument::REQUIRED, 'Pull Request number to be closed')
->addOption('message', 'm', InputOption::VALUE_REQUIRED, 'Closing comment')
->addOption('remove-source-branch', null, InputOption::VALUE_REQUIRED, 'Remove remote source branch after closing own pull request', 'no')
->setHelp(
<<<EOF
The <info>%command.name%</info> command closes a Pull Request for either the current or the given organization
and repository:
<info>$ gush %command.name% 12 -m"let's try to keep it low profile guys."</info>
<info>$ gush %command.name% 12 -m"let's try to keep it low profile guys." --remove-source-branch=yes</info>
EOF
)
Expand All @@ -47,11 +48,16 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$prNumber = $input->getArgument('pr_number');
$closingComment = $input->getOption('message');

$adapter = $this->getAdapter();
$prNumber = $input->getArgument('pr_number');
$pr = $adapter->getPullRequest($prNumber);
$authenticatedUser = $this->getParameter($input, 'authentication')['username'];
$removeSourceBranch = $input->getOption('remove-source-branch');
if ('yes' === $removeSourceBranch && $pr['user'] !== $authenticatedUser) {
throw new UserException(sprintf('`--remove-source-branch` option cannot be used with pull requests that aren\'t owned by the authenticated user (%s)', $authenticatedUser));
}

$closingComment = $input->getOption('message');
$adapter->closePullRequest($prNumber);

if ($input->getOption('message')) {
Expand All @@ -61,6 +67,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
$url = $adapter->getPullRequest($prNumber)['url'];
$this->getHelper('gush_style')->success("Closed {$url}");

// Post close options
if ($pr['user'] === $authenticatedUser) {
if ('yes' !== $removeSourceBranch) {
$removeSourceBranch = $this->getHelper('gush_style')->choice('Delete source branch?', ['yes', 'no'], 'no');
}
if ('yes' === $removeSourceBranch) {
$adapter->removePullRequestSourceBranch($pr['number']);
$this->getHelper('gush_style')->note(sprintf('Remote source branch %s:%s has been removed.', $pr['head']['user'], $pr['head']['ref']));
}
}

return self::COMMAND_SUCCESS;
}
}

0 comments on commit 9ef9c42

Please sign in to comment.