diff --git a/src/Service/Github/GithubClientService.php b/src/Service/Github/GithubClientService.php index d2d7188..57ad900 100644 --- a/src/Service/Github/GithubClientService.php +++ b/src/Service/Github/GithubClientService.php @@ -8,6 +8,7 @@ use App\Service\User\UserService; use Github\AuthMethod; use Github\Client; +use Symfony\Component\Cache\Adapter\FilesystemAdapter; final class GithubClientService { @@ -15,7 +16,8 @@ final class GithubClientService 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; diff --git a/src/Service/Github/PullRequestFilterService.php b/src/Service/Github/PullRequestFilterService.php index b6f99c6..ee8eb7b 100644 --- a/src/Service/Github/PullRequestFilterService.php +++ b/src/Service/Github/PullRequestFilterService.php @@ -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) { @@ -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'); @@ -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)) { @@ -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[] */ diff --git a/src/Service/Github/PullRequestLabelService.php b/src/Service/Github/PullRequestLabelService.php index 0ba3cc6..1f21f79 100644 --- a/src/Service/Github/PullRequestLabelService.php +++ b/src/Service/Github/PullRequestLabelService.php @@ -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) { @@ -115,25 +115,13 @@ 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] + ) + ; } /** @@ -141,7 +129,7 @@ protected function getAll(string $username, string $repository, array $params): * * @return array[] */ - protected function sortByLabel(array $pullRequests): array + private function sortByLabel(array $pullRequests): array { $pullRequestsSorted = [ Label::REVIEW_NEEDED->value => [],