From d3b9463c57a423c3eb8bd5a3ceed406f90bf07a5 Mon Sep 17 00:00:00 2001 From: ArthurErlich Date: Sat, 30 May 2026 14:13:14 +0200 Subject: [PATCH] feat(provider): implement ping and probe on GitHub, GitLab, and Gitea Add getName() and ping() to each provider and wire ProbeTrait. Make all classes final, add strict_types declarations, and replace generic RuntimeException with typed HTTP exceptions so probe() can classify auth failures and unreachable endpoints correctly. Co-Authored-By: Claude Sonnet 4.6 --- src/Service/GitHubProvider.php | 21 +++++++++++++++++++-- src/Service/GitLabProvider.php | 24 +++++++++++++++++++++--- src/Service/GiteaProvider.php | 22 ++++++++++++++++++++-- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/Service/GitHubProvider.php b/src/Service/GitHubProvider.php index ea90ea3..49bf749 100644 --- a/src/Service/GitHubProvider.php +++ b/src/Service/GitHubProvider.php @@ -1,10 +1,13 @@ username !== '' && $this->token !== ''; } + public function ping(): void + { + $this->client->request('GET', 'https://api.github.com/user', [ + 'headers' => ['Authorization' => "Bearer {$this->token}"], + ])->getContent(); + } + /** * @return array date (Y-m-d) => contribution count */ @@ -69,7 +86,7 @@ class GitHubProvider implements ProviderInterface $data = $response->toArray(); if (isset($data['errors'])) { - throw new \RuntimeException('GitHub GraphQL error: ' . json_encode($data['errors'])); + throw new ServiceUnavailableHttpException(null, 'GitHub GraphQL error: ' . json_encode($data['errors'])); } $result = []; diff --git a/src/Service/GitLabProvider.php b/src/Service/GitLabProvider.php index 802f046..d903730 100644 --- a/src/Service/GitLabProvider.php +++ b/src/Service/GitLabProvider.php @@ -1,8 +1,11 @@ username !== '' && $this->token !== ''; } + public function ping(): void + { + $baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/'); + + $this->client->request('GET', "$baseUrl/api/v4/user", [ + 'headers' => ['PRIVATE-TOKEN' => $this->token], + ])->getContent(); + } + /** * @return array date (Y-m-d) => event count */ @@ -35,7 +54,6 @@ class GitLabProvider implements ProviderInterface $this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]); - // Resolve numeric user ID from username $userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [ 'headers' => ['PRIVATE-TOKEN' => $this->token], 'query' => ['username' => $this->username], @@ -43,7 +61,7 @@ class GitLabProvider implements ProviderInterface $users = $userResponse->toArray(); if (empty($users)) { - throw new \RuntimeException("GitLab: user '{$this->username}' not found on $baseUrl"); + throw new NotFoundHttpException("GitLab: user '{$this->username}' not found on $baseUrl"); } $userId = $users[0]['id']; diff --git a/src/Service/GiteaProvider.php b/src/Service/GiteaProvider.php index c9f3c88..e3b75b1 100644 --- a/src/Service/GiteaProvider.php +++ b/src/Service/GiteaProvider.php @@ -1,5 +1,7 @@ username !== '' && $this->token !== '' && $this->baseUrl !== ''; } + public function ping(): void + { + $baseUrl = rtrim($this->baseUrl, '/'); + + $this->client->request('GET', "$baseUrl/api/v1/user", [ + 'headers' => ['Authorization' => "token {$this->token}"], + ])->getContent(); + } + /** * @return array date (Y-m-d) => contribution count */ public function fetch(): array { - $baseUrl = rtrim($this->baseUrl, '/'); + $baseUrl = rtrim($this->baseUrl, '/'); $this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);