Skip to content

Commit cba9fda

Browse files
committed
Add remove-source-branch option to pull-request:merge command
1 parent 36ed198 commit cba9fda

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Command/PullRequest/PullRequestMergeCommand.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected function configure()
4242
->addOption('force-squash', null, InputOption::VALUE_NONE, 'Force squashing the PR, even if there are multiple authors (this will implicitly use --squash)')
4343
->addOption('switch', null, InputOption::VALUE_REQUIRED, 'Switch the base of the pull request before merging')
4444
->addOption('pat', null, InputOption::VALUE_REQUIRED, 'Give the PR\'s author a pat on the back after the merge')
45+
->addOption('remove-source-branch', null, InputOption::VALUE_NONE, 'Remove remote source branch after merging own pull request')
4546
->setHelp(
4647
<<<'EOF'
4748
The <info>%command.name%</info> command merges the given pull request:
@@ -99,6 +100,11 @@ protected function configure()
99100
which has precedence to the predefined configuration.
100101
101102
<comment>The whole pat configuration will be ignored and no pat will be placed if the pull request is authored by yourself!</comment>
103+
104+
If you are the author of the pull request, <comment>--remove-source-branch</comment> can be used in order to remove the remote source
105+
branch after a successful merge:
106+
107+
<info>$ gush %command.name% --remove-source-branch</info>
102108
EOF
103109
)
104110
;
@@ -141,6 +147,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
141147
$targetLabel = sprintf('Target: %s/%s', $targetRemote, $targetBranch);
142148
}
143149

150+
$authenticatedUser = $this->getParameter($input, 'authentication')['username'];
151+
$removeSourceBranch = $input->getOption('remove-source-branch');
152+
if ($removeSourceBranch && $pr['user'] !== $authenticatedUser) {
153+
throw new UserException(sprintf('`--remove-source-branch` option cannot be used with pull requests that aren\'t owned by the authenticated user (%s)', $authenticatedUser));
154+
}
155+
144156
$styleHelper->title(sprintf('Merging pull-request #%d - %s', $prNumber, $pr['title']));
145157
$styleHelper->text([sprintf('Source: %s/%s', $sourceRemote, $sourceBranch), $targetLabel]);
146158

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

200-
if ($pr['user'] !== $this->getParameter($input, 'authentication')['username']) {
201-
$patComment = $this->givePatToPullRequestAuthor($pr, $input->getOption('pat'));
202-
if ($patComment) {
203-
$styleHelper->note(sprintf('Pat given to @%s at %s.', $pr['user'], $patComment));
212+
if ($pr['user'] === $authenticatedUser) {
213+
if ($removeSourceBranch) {
214+
$adapter->removePullRequestSourceBranch($pr['number']);
215+
$styleHelper->note(sprintf('Remote source branch %s:%s has been removed.', $sourceRemote, $sourceBranch));
204216
}
217+
} elseif ($patComment = $this->givePatToPullRequestAuthor($pr, $input->getOption('pat'))) {
218+
$styleHelper->note(sprintf('Pat given to @%s at %s.', $pr['user'], $patComment));
205219
}
206220

207221
$styleHelper->success([$mergeNote, $pr['url']]);

src/ThirdParty/Github/GitHubAdapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ public function createReleaseAssets($id, $name, $contentType, $content)
625625
return $asset['id'];
626626
}
627627

628+
public function removePullRequestSourceBranch($id)
629+
{
630+
$api = $this->client->api('git_data')->references();
631+
$pr = $this->getPullRequest($id);
632+
633+
return $api->remove($pr['user'], $pr['head']['repo'], 'heads/'.$pr['head']['ref']);
634+
}
635+
628636
protected function adaptIssueStructure(array $issue)
629637
{
630638
return [

0 commit comments

Comments
 (0)