Skip to content

Commit

Permalink
Added cache and command to get base (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Nov 8, 2020
1 parent 2a8426a commit 5fd78bc
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/Command/FindCommand.php
Expand Up @@ -65,7 +65,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$contributors[$key]['username'] = $this->usernameProvider->findUsername($c['email'], $c['name']);
}
}
$x = 2;

if ($input->getOption('pretty-print')) {
$output->writeln(json_encode($contributors, JSON_PRETTY_PRINT));
Expand Down
46 changes: 46 additions & 0 deletions src/Command/PullRequestBaseCommand.php
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/Service/BaseBranchProvider.php
@@ -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'];
}
}
12 changes: 4 additions & 8 deletions src/Service/ChangeSetProvider.php
Expand Up @@ -4,25 +4,21 @@

namespace Nyholm\GitReviewer\Service;

use Github\Client;
use Nyholm\GitReviewer\Model\Repository;
use Symfony\Component\Process\Process;

class ChangeSetProvider
{
/**
* @var Client
*/
private $github;
private $pullRequestFetcher;

public function __construct(Client $github)
public function __construct(PullRequestFetcher $pullRequestFetcher)
{
$this->github = $github;
$this->pullRequestFetcher = $pullRequestFetcher;
}

public function getChangedFiles(Repository $repository, int $number, array $ignoredPaths): array
{
$pr = $this->github->pullRequest()->show($repository->getUser(), $repository->getName(), $number);
$pr = $this->pullRequestFetcher->get($repository, $number);
$headRepoUrl = $pr['head']['repo']['ssh_url'];
$headRepoName = $pr['head']['repo']['owner']['login'];
$headCommit = $pr['head']['sha'];
Expand Down
9 changes: 9 additions & 0 deletions src/Service/GithubUsernameProvider.php
Expand Up @@ -23,6 +23,15 @@ public function __construct(Client $github, CacheInterface $cache)
}

public function findUsername(string $email, string $name): ?string
{
try {
return $this->fetchFromCache($email, $name);
} catch (\Exception $e) {
return null;
}
}

private function fetchFromCache(string $email, string $name): ?string
{
$key = 'user_'.sha1($email.$name);

Expand Down
34 changes: 34 additions & 0 deletions src/Service/PullRequestFetcher.php
@@ -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;
});
}
}
30 changes: 25 additions & 5 deletions src/Service/RepositoryProvider.php
Expand Up @@ -6,6 +6,8 @@

use Nyholm\GitReviewer\Model\Repository;
use Symfony\Component\Process\Process;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

/**
* Find repository from git root.
Expand All @@ -14,15 +16,33 @@
*/
class RepositoryProvider
{
private $cache;

public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}

public function find(string $workspace): Repository
{
$process = new Process(['git', 'remote', '-v'], $workspace);
$process->run();
$key = 'workspace_'.sha1($workspace);
$repo = $this->cache->get($key, function (ItemInterface $item) use ($workspace) {
$item->expiresAfter(60);

$process = new Process(['git', 'remote', '-v'], $workspace);
$process->run();

if (!preg_match('|origin\tgit@github.com:([a-zA-z0-9_-]+)/([a-zA-z0-9_-]+)\.git|s', $process->getOutput(), $matches)) {
return null;
}

return new Repository($matches[1], $matches[2], $workspace);
});

if (!preg_match('|origin\tgit@github.com:([a-zA-z0-9_-]+)/([a-zA-z0-9_-]+)\.git|s', $process->getOutput(), $matches)) {
throw new \RuntimeException('Could not find remote named "origin"');
if ($repo instanceof Repository) {
return $repo;
}

return new Repository($matches[1], $matches[2], $workspace);
throw new \RuntimeException('Could not find remote named "origin"');
}
}

0 comments on commit 5fd78bc

Please sign in to comment.