-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from akeneo/TIP-1391
TIP-1391: auto merge Nelson's PRs
- Loading branch information
Showing
11 changed files
with
236 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace spec\Akeneo\Git; | ||
|
||
use Github\Api\PullRequest; | ||
use Github\Client; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class PullRequestMergerSpec extends ObjectBehavior | ||
{ | ||
public function let( | ||
Client $client, | ||
EventDispatcherInterface $eventDispatcher | ||
) { | ||
$this->beConstructedWith($client, $eventDispatcher); | ||
} | ||
|
||
public function it_merges_a_pull_request(Client $client, PullRequest $githubMerger) | ||
{ | ||
$client->api('pull_request')->willReturn($githubMerger); | ||
$githubMerger->merge('nelson', 'akeneo/repo', 78556, 'Merge pull request #78556')->shouldBeCalled(); | ||
|
||
$this->mergePullRequest([ | ||
'number' => 78556, | ||
'base' => [ | ||
'user' => ['login' => 'nelson'], | ||
'repo' => ['name' => 'akeneo/repo'] | ||
] | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Akeneo\Git; | ||
|
||
use Akeneo\Event\Events; | ||
use Akeneo\System\Executor; | ||
use Exception; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
|
||
class DiffChecker | ||
{ | ||
/** @var Executor */ | ||
private $executor; | ||
|
||
/** @var EventDispatcherInterface */ | ||
private $eventDispatcher; | ||
|
||
public function __construct( | ||
Executor $executor, | ||
EventDispatcherInterface $eventDispatcher | ||
) { | ||
$this->executor = $executor; | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
/** | ||
* Check if current repository has diff, so we know if we have to create PR or not. | ||
* | ||
* @param string $projectDir | ||
* | ||
* @return bool | ||
* @throws Exception | ||
*/ | ||
public function haveDiff($projectDir) | ||
{ | ||
$this->eventDispatcher->dispatch(Events::PRE_GITHUB_CHECK_DIFF); | ||
|
||
$commands = [ | ||
sprintf('cd %s && git diff|wc -l', $projectDir), | ||
sprintf('cd %s && git ls-files --others --exclude-standard|wc -l', $projectDir), | ||
]; | ||
$diff = 0; | ||
foreach ($commands as $command) { | ||
$result = $this->executor->execute($command, true); | ||
$matches = null; | ||
preg_match('/^(?P<diff>\d+)\\n$/', $result[0], $matches); | ||
$diff += intval($matches['diff']); | ||
} | ||
|
||
$this->eventDispatcher->dispatch(Events::POST_GITHUB_CHECK_DIFF, new GenericEvent($this, [ | ||
'diff' => $diff | ||
])); | ||
|
||
return intval(0 !== $diff); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Akeneo\Git; | ||
|
||
use Akeneo\Event\Events; | ||
use Github\Client; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
|
||
class PullRequestMerger | ||
{ | ||
/** @var Client */ | ||
private $client; | ||
|
||
/** @var EventDispatcherInterface */ | ||
private $eventDispatcher; | ||
|
||
public function __construct( | ||
Client $client, | ||
EventDispatcherInterface $eventDispatcher | ||
) { | ||
$this->client = $client; | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
public function mergePullRequest(array $pullRequest): void | ||
{ | ||
$this->waitForGithubCheckMergeableBranch(); | ||
|
||
$mergeTitle = sprintf('Merge pull request #%s', $pullRequest['number']); | ||
|
||
$this->eventDispatcher->dispatch(Events::PRE_GITHUB_MERGE_PR, new GenericEvent($this, [ | ||
'number' => $pullRequest['number'], | ||
])); | ||
|
||
$this->client->api('pull_request')->merge( | ||
$pullRequest['base']['user']['login'], | ||
$pullRequest['base']['repo']['name'], | ||
$pullRequest['number'], | ||
$mergeTitle | ||
); | ||
|
||
$this->eventDispatcher->dispatch(Events::POST_GITHUB_MERGE_PR, new GenericEvent($this, [ | ||
'number' => $pullRequest['number'], | ||
])); | ||
} | ||
|
||
/** | ||
* cf. https://github.community/t5/GitHub-API-Development-and/Merging-via-REST-API-returns-405-Base-branch-was-modified-Review/td-p/19281 | ||
*/ | ||
private function waitForGithubCheckMergeableBranch(): void | ||
{ | ||
sleep(3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.