diff --git a/src/Command/PullRequest/PullRequestCreateCommand.php b/src/Command/PullRequest/PullRequestCreateCommand.php index 9f5d1289..57bc240a 100644 --- a/src/Command/PullRequest/PullRequestCreateCommand.php +++ b/src/Command/PullRequest/PullRequestCreateCommand.php @@ -123,6 +123,7 @@ public function getTemplateDefault() protected function execute(InputInterface $input, OutputInterface $output) { $org = $input->getOption('org'); + $repo = $input->getOption('repo'); $template = $input->getOption('template'); $sourceOrg = $input->getOption('source-org'); @@ -146,11 +147,12 @@ protected function execute(InputInterface $input, OutputInterface $output) /** @var StyleHelper $styleHelper */ $styleHelper = $this->getHelper('gush_style'); + $this->guardRemoteUpdated($org, $repo); - $styleHelper->title(sprintf('Open request on %s/%s', $org, $input->getOption('repo'))); + $styleHelper->title(sprintf('Open request on %s/%s', $org, $repo)); $styleHelper->text( [ - sprintf('This pull-request will be opened on "%s/%s".', $org, $input->getOption('repo')), + sprintf('This pull-request will be opened on "%s/%s".', $org, $repo), sprintf('The source branch is "%s" on "%s".', $sourceBranch, $sourceOrg), ] ); @@ -211,9 +213,7 @@ private function guardRemoteBranchExist($org, $repo, $branch, StyleHelper $style } if ($gitHelper->branchExists($branch)) { - $gitConfigHelper->ensureRemoteExists($org, $repo); - - $gitHelper->remoteUpdate($org); + $this->guardRemoteUpdated($org, $repo); $gitHelper->pushToRemote($org, $branch, true); $styleHelper->note(sprintf('Branch "%s" was pushed to "%s".', $branch, $org)); @@ -225,4 +225,19 @@ private function guardRemoteBranchExist($org, $repo, $branch, StyleHelper $style sprintf('Cannot open pull-request, remote branch "%s" does not exist in "%s/%s".', $branch, $org, $repo) ); } + + /** + * @param string $org + * @param string $repo + */ + private function guardRemoteUpdated($org, $repo) + { + /** @var GitConfigHelper $gitConfigHelper */ + $gitConfigHelper = $this->getHelper('git_config'); + /** @var GitHelper $gitHelper */ + $gitHelper = $this->getHelper('git'); + + $gitConfigHelper->ensureRemoteExists($org, $repo); + $gitHelper->remoteUpdate($org); + } } diff --git a/src/Helper/GitHelper.php b/src/Helper/GitHelper.php index 7c280b5c..16999bb5 100644 --- a/src/Helper/GitHelper.php +++ b/src/Helper/GitHelper.php @@ -294,26 +294,31 @@ public function getIssueNumber() * @param string $sourceBranch The source branch name * * @return string The title of the first commit on sourceBranch off of base + * or an empty string in the case of an error */ public function getFirstCommitTitle($base, $sourceBranch) { - $forkPoint = $this->processHelper->runCommand( - sprintf( - 'git merge-base --fork-point %s %s', - $base, - $sourceBranch - ) - ); + try { + $forkPoint = $this->processHelper->runCommand( + sprintf( + 'git merge-base --fork-point %s %s', + $base, + $sourceBranch + ) + ); - $lines = $this->processHelper->runCommand( - sprintf( - 'git rev-list %s..%s --reverse --oneline', - $forkPoint, - $sourceBranch - ) - ); + $lines = $this->processHelper->runCommand( + sprintf( + 'git rev-list %s..%s --reverse --oneline', + $forkPoint, + $sourceBranch + ) + ); - return substr(strtok($lines, "\n"), 8); + return substr(strtok($lines, "\n"), 8); + } catch (\RuntimeException $e) { + return ''; + } } public function createTempBranch($originalBranch) diff --git a/tests/Command/PullRequest/PullRequestCreateCommandTest.php b/tests/Command/PullRequest/PullRequestCreateCommandTest.php index a2c843c3..337cae9d 100644 --- a/tests/Command/PullRequest/PullRequestCreateCommandTest.php +++ b/tests/Command/PullRequest/PullRequestCreateCommandTest.php @@ -109,7 +109,7 @@ public function testOpenPullRequestWithSourceOptionsProvided() null, function (HelperSet $helperSet) { $helperSet->set($this->getLocalGitHelper('sstok', 'gush', 'feat-adapters')->reveal()); - $helperSet->set($this->getGitConfigHelper(true, 'sstok')->reveal()); + $helperSet->set($this->getGitConfigHelper('sstok')->reveal()); } ); @@ -216,7 +216,7 @@ public function testOpenPullRequestAutoPushMissingBranch() null, function (HelperSet $helperSet) { $helperSet->set($this->getLocalGitHelper('someone')->reveal()); - $helperSet->set($this->getGitConfigHelper(true, 'someone')->reveal()); + $helperSet->set($this->getGitConfigHelper('someone')->reveal()); } ); @@ -259,7 +259,7 @@ public function testCannotOpenPullRequestForNonExistentBranch() null, function (HelperSet $helperSet) { $helperSet->set($this->getLocalGitHelper('someone')->reveal()); - $helperSet->set($this->getGitConfigHelper(true, 'someone')->reveal()); + $helperSet->set($this->getGitConfigHelper('someone')->reveal()); } ); @@ -286,6 +286,7 @@ private function getLocalGitHelper($sourceOrg = 'cordoval', $sourceRepo = 'gush' $helper->remoteBranchExists(Argument::any(), Argument::any())->willReturn(false); $helper->remoteBranchExists('git@github.com:cordoval/gush.git', $branch)->willReturn(true); + $helper->remoteUpdate('gushphp')->shouldBeCalled(); $helper->branchExists(Argument::any())->willReturn(false); $helper->branchExists($branch)->will( @@ -300,17 +301,13 @@ function () use ($helper, $sourceOrg, $sourceRepo, $branch) { return $helper; } - protected function getGitConfigHelper($remoteExists = true, $sourceOrg = 'cordoval', $sourceRepo = 'gush') + protected function getGitConfigHelper($sourceOrg = 'cordoval', $sourceRepo = 'gush') { $helper = parent::getGitConfigHelper(); + $helper->ensureRemoteExists('gushphp', 'gush')->shouldBeCalled(); - if ($remoteExists) { - $helper->remoteExists($sourceOrg, $sourceRepo)->willReturn(); - $helper->ensureRemoteExists($sourceOrg, $sourceRepo)->willReturn(); - } else { - $helper->remoteExists($sourceOrg, $sourceRepo)->shouldNotBeCalled(); - $helper->ensureRemoteExists($sourceOrg, $sourceRepo)->shouldNotBeCalled(); - } + $helper->remoteExists($sourceOrg, $sourceRepo)->willReturn(); + $helper->ensureRemoteExists($sourceOrg, $sourceRepo)->willReturn(); return $helper; }