feat: implement multi-provider architecture for contribution fetching and add Docker support

This commit is contained in:
2026-05-29 11:51:05 +02:00
parent 381b8f4489
commit 56035096c6
13 changed files with 272 additions and 89 deletions
+15 -53
View File
@@ -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;
}
}