Skip to content

Commit

Permalink
Feature: add ResultPager (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
babeuloula authored Jul 5, 2022
1 parent cb29d2d commit bbccf93
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 64 deletions.
4 changes: 3 additions & 1 deletion src/Service/Github/GithubClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use App\Service\User\UserService;
use Github\AuthMethod;
use Github\Client;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

final class GithubClientService
{
private Client $client;

public function __construct(UserService $userService)
{
$this->client = new Client();
$this->client = new Client(apiVersion: 'v4');
$this->client->addCache(new FilesystemAdapter());

if (false === $userService->getUser() instanceof User) {
return;
Expand Down
51 changes: 16 additions & 35 deletions src/Service/Github/PullRequestFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
use App\Model\AbstractPullRequestService;
use App\Service\User\UserService;
use Github\Api\PullRequest as PullRequestApi;
use Github\Api\Search;
use Github\Client;
use Github\ResultPager;

final class PullRequestFilterService extends AbstractPullRequestService implements PullRequestServiceInterface
{
protected Client $client;
private Client $client;

/** @var string[] */
protected array $githubRepos;
private array $githubRepos;

/** @var string[] */
protected array $githubFilters;
private array $githubFilters;

/** @var int[] */
protected array $openCount = [];
private array $openCount = [];

public function __construct(GithubClientService $client, UserService $userService)
{
Expand Down Expand Up @@ -59,12 +59,8 @@ public function getOpenCount(): array
return $this->openCount;
}

/**
* @param mixed[] $params
*
* @return array[]
*/
protected function search(array $params = []): array
/** @return array[] */
private function search(): array
{
/** @var PullRequestApi $pullRequestApi */
$pullRequestApi = $this->client->api('pullRequest');
Expand All @@ -84,7 +80,7 @@ protected function search(array $params = []): array
continue;
}

foreach ($this->getAll($username, $repository, $filter, $params) as $pullRequest) {
foreach ($this->getAll($username, $repository, $filter) as $pullRequest) {
$pullRequest = $pullRequestApi->show($username, $repository, $pullRequest['number']);

if (true === \is_array($pullRequest)) {
Expand All @@ -102,34 +98,19 @@ protected function search(array $params = []): array
}

/** @return array[] */
protected function getAll(string $username, string $repository, string $filter, array $params): array
private function getAll(string $username, string $repository, string $filter): array
{
/** @var Search $searchApi */
$searchApi = $this->client->api('search');

if (0 === \preg_match("/repo\:[a-zA-Z0-9\/-]+/", $filter)) {
$filter = "repo:$username/$repository $filter";
}

// Issues and PRs use the same method
$pullRequests = $searchApi->issues($filter)['items'];

// Github does not offer a system indicating the total number of PRs.
// We are therefore obliged to detect the number of returns.
// If we have 30, it's because there's a next page. If we have less, we are on the last page.
if (30 === \count($pullRequests)) {
$pullRequests = \array_merge(
$this->getAll(
$username,
$repository,
$filter,
\array_merge($params, ['page' => ($params['page'] ?? 1) + 1])
),
$pullRequests
);
}

return $pullRequests;
return (new ResultPager($this->client))
->fetchAll(
$this->client->api('search'),
'issues',
[$filter]
)
;
}

/** @return array[] */
Expand Down
44 changes: 16 additions & 28 deletions src/Service/Github/PullRequestLabelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
use App\Enum\Label;
use App\Model\AbstractPullRequestService;
use App\Service\User\UserService;
use Github\Api\PullRequest as PullRequestApi;
use Github\Client;
use Github\ResultPager;

final class PullRequestLabelService extends AbstractPullRequestService implements PullRequestServiceInterface
{
protected Client $client;
private Client $client;

/** @var string[] */
protected array $githubRepos;
private array $githubRepos;

/** @var string[] */
protected array $labelsReviewNeeded;
private array $labelsReviewNeeded;

/** @var string[] */
protected array $labelsChangesRequested;
private array $labelsChangesRequested;

/** @var string[] */
protected array $labelsAccepted;
private array $labelsAccepted;

/** @var string[] */
protected array $labelsWip;
private array $labelsWip;

/** @var array[] */
protected array $openCount = [];
private array $openCount = [];

public function __construct(GithubClientService $client, UserService $userService)
{
Expand Down Expand Up @@ -115,33 +115,21 @@ protected function search(array $params = []): array
*/
protected function getAll(string $username, string $repository, array $params): array
{
/** @var PullRequestApi $pullRequestApi */
$pullRequestApi = $this->client->api('pullRequest');
$pullRequest = $pullRequestApi->all($username, $repository, $params);

// Github does not offer a system indicating the total number of PRs.
// We are therefore obliged to detect the number of returns.
// If we have 30, it's because there's a next page. If we have less, we are on the last page.
if (30 === \count($pullRequest)) {
$pullRequest = \array_merge(
$this->getAll(
$username,
$repository,
\array_merge($params, ['page' => ($params['page'] ?? 1) + 1])
),
$pullRequest
);
}

return $pullRequest;
return (new ResultPager($this->client))
->fetchAll(
$this->client->api('pullRequest'),
'all',
[$username, $repository, $params]
)
;
}

/**
* @param array[] $pullRequests
*
* @return array[]
*/
protected function sortByLabel(array $pullRequests): array
private function sortByLabel(array $pullRequests): array
{
$pullRequestsSorted = [
Label::REVIEW_NEEDED->value => [],
Expand Down

0 comments on commit bbccf93

Please sign in to comment.