Initialize git-contribution-graph project with Docker setup, environment configuration, and core functionality for merging contributions from GitHub, GitLab, and Gitea into an SVG heatmap.
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\GiteaProvider;
|
||||
use App\Service\GitHubProvider;
|
||||
use App\Service\GitLabProvider;
|
||||
use App\Service\SvgRenderer;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class GraphController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GitHubProvider $github,
|
||||
private readonly GitLabProvider $gitlab,
|
||||
private readonly GiteaProvider $gitea,
|
||||
private readonly SvgRenderer $renderer,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns a GitHub-style contribution graph SVG merging GitHub, GitLab and Gitea.
|
||||
*
|
||||
* Query parameters:
|
||||
* github_user string GitHub username
|
||||
* github_token string GitHub personal access token (needs read:user scope)
|
||||
* gitlab_user string GitLab username
|
||||
* gitlab_token string GitLab personal access token (needs read_user scope)
|
||||
* gitlab_url string GitLab base URL (default: https://gitlab.com)
|
||||
* gitea_user string Gitea username
|
||||
* gitea_token string Gitea personal access token
|
||||
* gitea_url string Gitea instance base URL (e.g. https://git.example.com)
|
||||
* theme string dark|light (default: dark)
|
||||
*/
|
||||
#[Route('/graph.svg', name: 'contribution_graph', methods: ['GET'])]
|
||||
public function graph(Request $request): Response
|
||||
{
|
||||
$params = $request->query->all();
|
||||
$theme = $params['theme'] ?? 'dark';
|
||||
|
||||
// Use a cache key based on the query params (minus sensitive tokens — we hash them)
|
||||
$cacheKey = 'graph_' . md5(serialize($params));
|
||||
|
||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($params, $theme): string {
|
||||
$item->expiresAfter(3600); // cache for 1 hour
|
||||
|
||||
$contributions = [];
|
||||
|
||||
if (!empty($params['github_user']) && !empty($params['github_token'])) {
|
||||
try {
|
||||
$contributions = $this->merge(
|
||||
$contributions,
|
||||
$this->github->fetch($params['github_user'], $params['github_token'])
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('GitHub fetch failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($params['gitlab_user']) && !empty($params['gitlab_token'])) {
|
||||
try {
|
||||
$contributions = $this->merge(
|
||||
$contributions,
|
||||
$this->gitlab->fetch(
|
||||
$params['gitlab_user'],
|
||||
$params['gitlab_token'],
|
||||
$params['gitlab_url'] ?? 'https://gitlab.com'
|
||||
)
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('GitLab fetch failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($params['gitea_user']) && !empty($params['gitea_token']) && !empty($params['gitea_url'])) {
|
||||
try {
|
||||
$contributions = $this->merge(
|
||||
$contributions,
|
||||
$this->gitea->fetch(
|
||||
$params['gitea_user'],
|
||||
$params['gitea_token'],
|
||||
rtrim($params['gitea_url'], '/')
|
||||
)
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('Gitea fetch failed: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->renderer->render($contributions, $theme);
|
||||
});
|
||||
|
||||
return new Response($svg, 200, [
|
||||
'Content-Type' => 'image/svg+xml',
|
||||
'Cache-Control' => 'public, max-age=3600',
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/health', name: 'health', methods: ['GET'])]
|
||||
public function health(): Response
|
||||
{
|
||||
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
|
||||
/** Sum contributions by date from two maps. */
|
||||
private function merge(array $base, array $new): array
|
||||
{
|
||||
foreach ($new as $date => $count) {
|
||||
$base[$date] = ($base[$date] ?? 0) + $count;
|
||||
}
|
||||
return $base;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user