Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cache and command to get base (#2)
- Loading branch information
Showing
7 changed files
with
142 additions
and
14 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
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nyholm\GitReviewer\Command; | ||
|
||
use Nyholm\GitReviewer\Service\BaseBranchProvider; | ||
use Nyholm\GitReviewer\Service\RepositoryProvider; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class PullRequestBaseCommand extends Command | ||
{ | ||
protected static $defaultName = 'pull-request:base'; | ||
private $baseBranchProvider; | ||
private $repositoryProvider; | ||
|
||
public function __construct(RepositoryProvider $repositoryProvider, BaseBranchProvider $baseBranchProvider) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->baseBranchProvider = $baseBranchProvider; | ||
$this->repositoryProvider = $repositoryProvider; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this->setDescription('Find the base branch for a pull request'); | ||
$this->addArgument('pull-request', InputArgument::REQUIRED, 'The pull request number'); | ||
$this->addArgument('workspace', InputArgument::REQUIRED, 'The path to the workspace, ie where the project is cloned'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$repository = $this->repositoryProvider->find($input->getArgument('workspace')); | ||
|
||
// find the changed files in pull request | ||
$pullRequest = (int) $input->getArgument('pull-request'); | ||
$branch = $this->baseBranchProvider->getBaseBranch($repository, $pullRequest); | ||
$output->writeln($branch); | ||
|
||
return 0; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nyholm\GitReviewer\Service; | ||
|
||
use Nyholm\GitReviewer\Model\Repository; | ||
|
||
class BaseBranchProvider | ||
{ | ||
private $pullRequestFetcher; | ||
|
||
public function __construct(PullRequestFetcher $pullRequestFetcher) | ||
{ | ||
$this->pullRequestFetcher = $pullRequestFetcher; | ||
} | ||
|
||
public function getBaseBranch(Repository $repository, int $number): string | ||
{ | ||
$pr = $this->pullRequestFetcher->get($repository, $number); | ||
|
||
return $pr['base']['ref']; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nyholm\GitReviewer\Service; | ||
|
||
use Github\Client; | ||
use Nyholm\GitReviewer\Model\Repository; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
use Symfony\Contracts\Cache\ItemInterface; | ||
|
||
class PullRequestFetcher | ||
{ | ||
private $github; | ||
private $cache; | ||
|
||
public function __construct(Client $github, CacheInterface $cache) | ||
{ | ||
$this->github = $github; | ||
$this->cache = $cache; | ||
} | ||
|
||
public function get(Repository $repository, int $number): array | ||
{ | ||
$key = 'pull_request'.sha1($repository->getFullName().':'.$number); | ||
|
||
return $this->cache->get($key, function (ItemInterface $item) use ($repository, $number) { | ||
$pr = $this->github->pullRequest()->show($repository->getUser(), $repository->getName(), $number); | ||
$item->set(300); | ||
|
||
return $pr; | ||
}); | ||
} | ||
} |
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