Skip to content
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

Ensure upstream exists for pull-request:open #468

Merged
merged 2 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Command/PullRequest/PullRequestCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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),
]
);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}
}
35 changes: 20 additions & 15 deletions src/Helper/GitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 8 additions & 11 deletions tests/Command/PullRequest/PullRequestCreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
);

Expand Down Expand Up @@ -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());
}
);

Expand Down Expand Up @@ -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());
}
);

Expand All @@ -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(
Expand All @@ -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;
}
Expand Down