Skip to content

Commit

Permalink
Add remove-source-branch option to pull-request:merge command
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Aug 11, 2016
1 parent 36ed198 commit cba9fda
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Command/PullRequest/PullRequestMergeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function configure()
->addOption('force-squash', null, InputOption::VALUE_NONE, 'Force squashing the PR, even if there are multiple authors (this will implicitly use --squash)')
->addOption('switch', null, InputOption::VALUE_REQUIRED, 'Switch the base of the pull request before merging')
->addOption('pat', null, InputOption::VALUE_REQUIRED, 'Give the PR\'s author a pat on the back after the merge')
->addOption('remove-source-branch', null, InputOption::VALUE_NONE, 'Remove remote source branch after merging own pull request')
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command merges the given pull request:
Expand Down Expand Up @@ -99,6 +100,11 @@ protected function configure()
which has precedence to the predefined configuration.
<comment>The whole pat configuration will be ignored and no pat will be placed if the pull request is authored by yourself!</comment>
If you are the author of the pull request, <comment>--remove-source-branch</comment> can be used in order to remove the remote source
branch after a successful merge:
<info>$ gush %command.name% --remove-source-branch</info>
EOF
)
;
Expand Down Expand Up @@ -141,6 +147,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$targetLabel = sprintf('Target: %s/%s', $targetRemote, $targetBranch);
}

$authenticatedUser = $this->getParameter($input, 'authentication')['username'];
$removeSourceBranch = $input->getOption('remove-source-branch');
if ($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));
}

$styleHelper->title(sprintf('Merging pull-request #%d - %s', $prNumber, $pr['title']));
$styleHelper->text([sprintf('Source: %s/%s', $sourceRemote, $sourceBranch), $targetLabel]);

Expand Down Expand Up @@ -197,11 +209,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->addClosedPullRequestNote($pr, $mergeCommit, $squash, $input->getOption('switch'));
}

if ($pr['user'] !== $this->getParameter($input, 'authentication')['username']) {
$patComment = $this->givePatToPullRequestAuthor($pr, $input->getOption('pat'));
if ($patComment) {
$styleHelper->note(sprintf('Pat given to @%s at %s.', $pr['user'], $patComment));
if ($pr['user'] === $authenticatedUser) {
if ($removeSourceBranch) {
$adapter->removePullRequestSourceBranch($pr['number']);
$styleHelper->note(sprintf('Remote source branch %s:%s has been removed.', $sourceRemote, $sourceBranch));
}
} elseif ($patComment = $this->givePatToPullRequestAuthor($pr, $input->getOption('pat'))) {
$styleHelper->note(sprintf('Pat given to @%s at %s.', $pr['user'], $patComment));
}

$styleHelper->success([$mergeNote, $pr['url']]);
Expand Down
8 changes: 8 additions & 0 deletions src/ThirdParty/Github/GitHubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ public function createReleaseAssets($id, $name, $contentType, $content)
return $asset['id'];
}

public function removePullRequestSourceBranch($id)
{
$api = $this->client->api('git_data')->references();
$pr = $this->getPullRequest($id);

return $api->remove($pr['user'], $pr['head']['repo'], 'heads/'.$pr['head']['ref']);
}

protected function adaptIssueStructure(array $issue)
{
return [
Expand Down

0 comments on commit cba9fda

Please sign in to comment.