-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --remove-source-branch
/--no-remove-source-branch
options to pull-request:merge
command
#619
base: master
Are you sure you want to change the base?
Changes from all commits
6a6e0a8
b7507eb
99de267
6011a3a
9aad20d
a6bdd79
341830d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ 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') | ||
->addOption('no-remove-source-branch', null, InputOption::VALUE_NONE, 'Don\'t remove remote source branch after merging own pull request') | ||
->setHelp( | ||
<<<'EOF' | ||
The <info>%command.name%</info> command merges the given pull request: | ||
|
@@ -99,6 +101,14 @@ 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, you'll be prompted if the remote source branch will be removed after a successful merge. | ||
Also, you can pass your choice for this action with the <comment>--remove-source-branch</comment> and <comment>--no-remove-source-branch</comment> | ||
options: | ||
|
||
<info>$ gush %command.name% --remove-source-branch</info> | ||
|
||
<info>$ gush %command.name% --no-remove-source-branch</info> | ||
EOF | ||
) | ||
; | ||
|
@@ -141,6 +151,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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a problem for GitLab CE where there are no forks for pull-requests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sstok, I don't know if I got your point. I think this restriction isn't driven by permissions, but for respect to PR author. By instance, in my private organization I have access to delete branches from another author than me, but I think this isn't a good practice or something we should allow. IMO, the responsibility over each branch belongs only to their author, regardless your access level as merger. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, no problem 👍 |
||
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]); | ||
|
||
|
@@ -197,15 +213,18 @@ 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)); | ||
$styleHelper->success([$mergeNote, $pr['url']]); | ||
|
||
// Post merge options | ||
if ($pr['user'] === $authenticatedUser) { | ||
if ($removeSourceBranch || (!$input->getOption('no-remove-source-branch') && 'yes' === $this->getHelper('gush_style')->choice('Delete source branch?', ['yes', 'no'], 'no'))) { | ||
$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']]); | ||
|
||
return self::COMMAND_SUCCESS; | ||
} catch (CannotSquashMultipleAuthors $e) { | ||
$styleHelper->error([ | ||
|
@@ -415,4 +434,13 @@ private function givePatToPullRequestAuthor(array $pr, $pat) | |
return $this->getAdapter()->createComment($pr['number'], $patMessage); | ||
} | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output) | ||
{ | ||
parent::initialize($input, $output); | ||
|
||
if ($input->getOption('remove-source-branch') && $input->getOption('no-remove-source-branch')) { | ||
throw new UserException('Options `--remove-source-branch` and `--no-remove-source-branch` cannot be used toghether'); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe do a
strtolower
here, just for safety sake for the checks below. Or maybe the option should be changed to not take a value, so it is set totrue
when passed throughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, value should not be required. That's the common pattern for bool options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a
--no-remove-source-branch
option then in order to let the user express its choice to not delete the source branch without being prompted?