Add environment variables for GitHub, GitLab, Gitea, and allowed hosts configuration
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Service\GitHubProvider;
|
||||
use App\Service\GitLabProvider;
|
||||
use App\Service\SvgRenderer;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -15,6 +16,9 @@ use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
class GraphController
|
||||
{
|
||||
/** @var list<string> */
|
||||
private readonly array $allowedHosts;
|
||||
|
||||
public function __construct(
|
||||
private readonly GitHubProvider $github,
|
||||
private readonly GitLabProvider $gitlab,
|
||||
@@ -22,76 +26,47 @@ class GraphController
|
||||
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))));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* 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';
|
||||
if ($this->allowedHosts !== [] && !in_array($request->getHost(), $this->allowedHosts, true)) {
|
||||
return new Response('Forbidden', 403);
|
||||
}
|
||||
|
||||
// Use a cache key based on the query params (minus sensitive tokens — we hash them)
|
||||
$cacheKey = 'graph_' . md5(serialize($params));
|
||||
$theme = $request->query->get('theme', 'dark');
|
||||
|
||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($params, $theme): string {
|
||||
$item->expiresAfter(3600); // cache for 1 hour
|
||||
$cacheKey = 'graph_' . $theme;
|
||||
|
||||
$contributions = [];
|
||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($theme): string {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
$contributions = $this->fetchAllContributions();
|
||||
|
||||
return $this->renderer->render($contributions, $theme);
|
||||
});
|
||||
@@ -108,6 +83,45 @@ class GraphController
|
||||
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->gitlabUser !== '' && $this->gitlabToken !== '') {
|
||||
try {
|
||||
$contributions = $this->merge($contributions, $this->gitlab->fetch(
|
||||
$this->gitlabUser,
|
||||
$this->gitlabToken,
|
||||
$this->gitlabUrl !== '' ? $this->gitlabUrl : 'https://gitlab.com'
|
||||
));
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
|
||||
return $contributions;
|
||||
}
|
||||
|
||||
/** Sum contributions by date from two maps. */
|
||||
private function merge(array $base, array $new): array
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user