feat: implement multi-provider architecture for contribution fetching and add Docker support
This commit is contained in:
@@ -2,12 +2,11 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\GiteaProvider;
|
||||
use App\Service\GitHubProvider;
|
||||
use App\Service\GitLabProvider;
|
||||
use App\Service\ProviderInterface;
|
||||
use App\Service\SvgRenderer;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -20,30 +19,13 @@ class GraphController
|
||||
private readonly array $allowedHosts;
|
||||
|
||||
public function __construct(
|
||||
private readonly GitHubProvider $github,
|
||||
private readonly GitLabProvider $gitlab,
|
||||
private readonly GiteaProvider $gitea,
|
||||
#[TaggedIterator('app.provider')]
|
||||
private readonly iterable $providers,
|
||||
private readonly SvgRenderer $renderer,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly LoggerInterface $logger,
|
||||
#[Autowire(env: 'ALLOWED_HOSTS')]
|
||||
string $allowedHosts = '',
|
||||
#[Autowire(env: 'GITHUB_USER')]
|
||||
private readonly string $githubUser = '',
|
||||
#[Autowire(env: 'GITHUB_TOKEN')]
|
||||
private readonly string $githubToken = '',
|
||||
#[Autowire(env: 'GITLAB_USER')]
|
||||
private readonly string $gitlabUser = '',
|
||||
#[Autowire(env: 'GITLAB_TOKEN')]
|
||||
private readonly string $gitlabToken = '',
|
||||
#[Autowire(env: 'GITLAB_URL')]
|
||||
private readonly string $gitlabUrl = '',
|
||||
#[Autowire(env: 'GITEA_USER')]
|
||||
private readonly string $giteaUser = '',
|
||||
#[Autowire(env: 'GITEA_TOKEN')]
|
||||
private readonly string $giteaToken = '',
|
||||
#[Autowire(env: 'GITEA_URL')]
|
||||
private readonly string $giteaUrl = '',
|
||||
) {
|
||||
$this->allowedHosts = array_values(array_filter(array_map('trim', explode(',', $allowedHosts))));
|
||||
}
|
||||
@@ -59,16 +41,13 @@ class GraphController
|
||||
return new Response('Forbidden', 403);
|
||||
}
|
||||
|
||||
$theme = $request->query->get('theme', 'dark');
|
||||
|
||||
$theme = $request->query->get('theme', 'dark');
|
||||
$cacheKey = 'graph_' . $theme;
|
||||
|
||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($theme): string {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
$contributions = $this->fetchAllContributions();
|
||||
|
||||
return $this->renderer->render($contributions, $theme);
|
||||
return $this->renderer->render($this->fetchAllContributions(), $theme);
|
||||
});
|
||||
|
||||
return new Response($svg, 200, [
|
||||
@@ -83,51 +62,34 @@ class GraphController
|
||||
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
|
||||
/** @return array<string, int> */
|
||||
private function fetchAllContributions(): array
|
||||
{
|
||||
$contributions = [];
|
||||
|
||||
if ($this->githubUser !== '' && $this->githubToken !== '') {
|
||||
try {
|
||||
$contributions = $this->merge($contributions, $this->github->fetch($this->githubUser, $this->githubToken));
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('GitHub fetch failed: ' . $e->getMessage());
|
||||
/** @var ProviderInterface $provider */
|
||||
foreach ($this->providers as $provider) {
|
||||
if (!$provider->isConfigured()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->gitlabUser !== '' && $this->gitlabToken !== '') {
|
||||
try {
|
||||
$contributions = $this->merge($contributions, $this->gitlab->fetch(
|
||||
$this->gitlabUser,
|
||||
$this->gitlabToken,
|
||||
$this->gitlabUrl !== '' ? $this->gitlabUrl : 'https://gitlab.com'
|
||||
));
|
||||
$contributions = $this->merge($contributions, $provider->fetch());
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('GitLab fetch failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->giteaUser !== '' && $this->giteaToken !== '' && $this->giteaUrl !== '') {
|
||||
try {
|
||||
$contributions = $this->merge($contributions, $this->gitea->fetch(
|
||||
$this->giteaUser,
|
||||
$this->giteaToken,
|
||||
rtrim($this->giteaUrl, '/')
|
||||
));
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('Gitea fetch failed: ' . $e->getMessage());
|
||||
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
return $contributions;
|
||||
}
|
||||
|
||||
/** Sum contributions by date from two maps. */
|
||||
/** @param array<string, int> $base @param array<string, int> $new @return array<string, int> */
|
||||
private function merge(array $base, array $new): array
|
||||
{
|
||||
foreach ($new as $date => $count) {
|
||||
$base[$date] = ($base[$date] ?? 0) + $count;
|
||||
}
|
||||
|
||||
return $base;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,19 +11,26 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
*
|
||||
* Required token scopes: read:user
|
||||
*/
|
||||
class GitHubProvider
|
||||
class GitHubProvider implements ProviderInterface
|
||||
{
|
||||
private const GRAPHQL_URL = 'https://api.github.com/graphql';
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly GraphQLApiClientRegistryInterface $registry,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
) {}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->username !== '' && $this->token !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int> date (Y-m-d) => contribution count
|
||||
*/
|
||||
public function fetch(string $username, string $token): array
|
||||
public function fetch(): array
|
||||
{
|
||||
$from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
|
||||
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
|
||||
@@ -32,7 +39,7 @@ class GitHubProvider
|
||||
$graphqlClient = $this->registry->get('github');
|
||||
|
||||
$query = $graphqlClient->buildQuery(
|
||||
['user' => ['login' => $username]],
|
||||
['user' => ['login' => $this->username]],
|
||||
[
|
||||
'contributionsCollection' => [
|
||||
'_parameters' => ['from' => $from, 'to' => $to],
|
||||
@@ -49,7 +56,7 @@ class GitHubProvider
|
||||
// transport sends form_params, so we use Symfony HttpClient here instead.
|
||||
$response = $this->client->request('POST', self::GRAPHQL_URL, [
|
||||
'headers' => [
|
||||
'Authorization' => "Bearer $token",
|
||||
'Authorization' => "Bearer {$this->token}",
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'json' => ['query' => $query],
|
||||
|
||||
@@ -10,26 +10,36 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
* Required token scopes: read_user, read_api
|
||||
* Works with both gitlab.com and self-hosted instances.
|
||||
*/
|
||||
class GitLabProvider
|
||||
class GitLabProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(private readonly HttpClientInterface $client) {}
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly string $baseUrl = '',
|
||||
) {}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->username !== '' && $this->token !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int> date (Y-m-d) => event count
|
||||
*/
|
||||
public function fetch(string $username, string $token, string $baseUrl = 'https://gitlab.com'): array
|
||||
public function fetch(): array
|
||||
{
|
||||
$baseUrl = rtrim($baseUrl, '/');
|
||||
$baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/');
|
||||
|
||||
// Resolve numeric user ID from username
|
||||
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $token],
|
||||
'query' => ['username' => $username],
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => ['username' => $this->username],
|
||||
]);
|
||||
|
||||
$users = $userResponse->toArray();
|
||||
if (empty($users)) {
|
||||
throw new \RuntimeException("GitLab: user '$username' not found on $baseUrl");
|
||||
throw new \RuntimeException("GitLab: user '{$this->username}' not found on $baseUrl");
|
||||
}
|
||||
$userId = $users[0]['id'];
|
||||
|
||||
@@ -39,7 +49,7 @@ class GitLabProvider
|
||||
|
||||
do {
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $token],
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
|
||||
@@ -12,18 +12,28 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
*
|
||||
* Required token scopes: read:user
|
||||
*/
|
||||
class GiteaProvider
|
||||
class GiteaProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(private readonly HttpClientInterface $client) {}
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly string $baseUrl,
|
||||
) {}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->username !== '' && $this->token !== '' && $this->baseUrl !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int> date (Y-m-d) => contribution count
|
||||
*/
|
||||
public function fetch(string $username, string $token, string $baseUrl): array
|
||||
public function fetch(): array
|
||||
{
|
||||
$baseUrl = rtrim($baseUrl, '/');
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v1/users/$username/heatmap", [
|
||||
'headers' => ['Authorization' => "token $token"],
|
||||
$baseUrl = rtrim($this->baseUrl, '/');
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v1/users/{$this->username}/heatmap", [
|
||||
'headers' => ['Authorization' => "token {$this->token}"],
|
||||
]);
|
||||
|
||||
$data = $response->toArray();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
interface ProviderInterface
|
||||
{
|
||||
/** @return array<string, int> date (Y-m-d) => contribution count */
|
||||
public function fetch(): array;
|
||||
|
||||
public function isConfigured(): bool;
|
||||
}
|
||||
Reference in New Issue
Block a user