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 <noreply@anthropic.com>
83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
/**
|
|
* Fetches contribution data from the Gitea heatmap endpoint.
|
|
*
|
|
* Endpoint: GET /api/v1/users/{username}/heatmap
|
|
* Returns: [{"timestamp": 1234567890, "contributions": 3}, ...]
|
|
*
|
|
* Required token scopes: read:user
|
|
*/
|
|
final class GiteaProvider implements ProviderInterface
|
|
{
|
|
use ProbeTrait;
|
|
|
|
public function __construct(
|
|
private readonly HttpClientInterface $client,
|
|
private readonly string $username,
|
|
private readonly string $token,
|
|
private readonly string $baseUrl,
|
|
private readonly LoggerInterface $logger,
|
|
) {}
|
|
|
|
public function getName(): string
|
|
{
|
|
return 'gitea';
|
|
}
|
|
|
|
public function isConfigured(): bool
|
|
{
|
|
return $this->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<string, int> date (Y-m-d) => contribution count
|
|
*/
|
|
public function fetch(): array
|
|
{
|
|
$baseUrl = rtrim($this->baseUrl, '/');
|
|
|
|
$this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
|
|
|
|
$response = $this->client->request('GET', "$baseUrl/api/v1/users/{$this->username}/heatmap", [
|
|
'headers' => ['Authorization' => "token {$this->token}"],
|
|
]);
|
|
|
|
$data = $response->toArray();
|
|
$cutoff = (new \DateTimeImmutable('-365 days'))->getTimestamp();
|
|
$result = [];
|
|
|
|
foreach ($data as $entry) {
|
|
if ($entry['timestamp'] < $cutoff) {
|
|
continue;
|
|
}
|
|
$date = date('Y-m-d', $entry['timestamp']);
|
|
$result[$date] = ($result[$date] ?? 0) + (int) $entry['contributions'];
|
|
}
|
|
|
|
$this->logger->info('GiteaProvider: fetched contributions', [
|
|
'user' => $this->username,
|
|
'days' => count($result),
|
|
'total' => array_sum($result),
|
|
]);
|
|
|
|
return $result;
|
|
}
|
|
}
|