feat: implement multi-provider architecture for contribution fetching and add Docker support
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
.git
|
||||||
|
.gitea
|
||||||
|
var/
|
||||||
|
.env.local
|
||||||
|
.env.*.local
|
||||||
|
docker-compose.override.yml
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
# Builds and pushes a multi-arch Docker image to the Gitea container registry
|
||||||
|
# whenever a semver tag (v*.*.*) is pushed.
|
||||||
|
#
|
||||||
|
# One-time setup required:
|
||||||
|
# 1. Create a Gitea token with "package:write" scope.
|
||||||
|
# 2. Add it as a repository secret named GITEA_TOKEN
|
||||||
|
# (Repository → Settings → Secrets → Actions).
|
||||||
|
#
|
||||||
|
# After a successful run the image is available at:
|
||||||
|
# <your-gitea-host>/<owner>/<repo>:<version>
|
||||||
|
|
||||||
|
name: Docker Publish
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*.*.*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-push:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
# Strip the protocol from the server URL to get the registry hostname.
|
||||||
|
# e.g. https://gitea.example.com → gitea.example.com
|
||||||
|
- name: Derive registry hostname
|
||||||
|
run: |
|
||||||
|
echo "REGISTRY=$(echo '${{ gitea.server_url }}' | sed 's|https://||;s|http://||')" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
# Generates OCI-compliant tags and labels from the git tag.
|
||||||
|
# v1.2.3 → image tags: 1.2.3 / 1.2 / 1
|
||||||
|
- name: Extract Docker metadata
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ env.REGISTRY }}/${{ gitea.repository }}
|
||||||
|
tags: |
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
type=semver,pattern={{major}}
|
||||||
|
|
||||||
|
# QEMU enables emulation of arm64 on the amd64 runner.
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
|
# BuildKit driver required for multi-platform builds and layer caching.
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Log in to Gitea registry
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ${{ env.REGISTRY }}
|
||||||
|
username: ${{ gitea.actor }}
|
||||||
|
password: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: true
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
# Registry-based layer cache — survives between runs without a separate cache store.
|
||||||
|
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ gitea.repository }}:buildcache
|
||||||
|
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ gitea.repository }}:buildcache,mode=max
|
||||||
@@ -118,20 +118,49 @@ Add to `.claude/settings.json` to run PHPUnit automatically after every file edi
|
|||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
|
### Development
|
||||||
|
|
||||||
|
`docker-compose.override.yml` is picked up automatically and targets the `dev` stage (Xdebug + all deps, source mounted).
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Build and run
|
# Start dev container (override applied automatically)
|
||||||
docker compose up -d --build
|
docker compose up -d --build
|
||||||
|
|
||||||
# Force cache clear (clears the 1h filesystem cache)
|
# Shell into the container to run commands
|
||||||
|
docker compose exec graph sh
|
||||||
|
|
||||||
|
# Run tests inside the container
|
||||||
|
docker compose exec graph php bin/phpunit
|
||||||
|
|
||||||
|
# Disable Xdebug for faster test runs
|
||||||
|
XDEBUG_MODE=off docker compose up -d
|
||||||
|
|
||||||
|
# Force cache clear
|
||||||
docker compose exec graph rm -rf var/cache/*
|
docker compose exec graph rm -rf var/cache/*
|
||||||
|
|
||||||
# View logs
|
# View logs
|
||||||
docker compose logs -f graph
|
docker compose logs -f graph
|
||||||
```
|
```
|
||||||
|
|
||||||
There is no `composer.lock` in the repo. If you add or change dependencies, run `composer install` locally and commit the resulting lock file — without it, Docker builds resolve versions fresh each time and may produce inconsistent results.
|
**Xdebug:** listens on port `9003`. Configure your IDE to accept connections from Docker. On Linux `host.docker.internal` is resolved via `extra_hosts: host-gateway` in the override file.
|
||||||
|
|
||||||
The Dockerfile runs `composer install --no-scripts` (skipping Symfony post-install scripts) then `composer dump-autoload --optimize --no-dev` in the final stage. If `bin/console` fails with a missing class error inside the container, the most likely cause is the absent lock file causing an incomplete dependency resolution.
|
### Production
|
||||||
|
|
||||||
|
Use only the base compose file to skip the dev override:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f docker-compose.yml up -d --build
|
||||||
|
```
|
||||||
|
|
||||||
|
The `final` stage runs as a non-root `app` user and contains no Composer binary. The build pipeline is:
|
||||||
|
|
||||||
|
```
|
||||||
|
base → deps (composer install --no-dev)
|
||||||
|
→ build (copy source + dump-autoload)
|
||||||
|
→ final (copy vendor + source, chown app, USER app)
|
||||||
|
```
|
||||||
|
|
||||||
|
There is no `composer.lock` in the repo. If you add or change dependencies, run `composer install` locally and commit the resulting lock file — without it, Docker builds resolve versions fresh each time and may produce inconsistent results.
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
|
|||||||
+29
-13
@@ -1,19 +1,16 @@
|
|||||||
FROM php:8.3-cli-alpine AS base
|
FROM php:8.3-cli-alpine AS base
|
||||||
|
|
||||||
# Runtime dependencies
|
|
||||||
RUN apk add --no-cache \
|
RUN apk add --no-cache \
|
||||||
curl \
|
curl \
|
||||||
icu-libs \
|
icu-libs \
|
||||||
libzip \
|
libzip \
|
||||||
&& docker-php-ext-install opcache
|
&& docker-php-ext-install opcache
|
||||||
|
|
||||||
# Composer
|
|
||||||
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# ── deps stage ────────────────────────────────────────────────────────────────
|
# ── deps stage (prod vendor) ───────────────────────────────────────────────────
|
||||||
FROM base AS deps
|
FROM base AS deps
|
||||||
|
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
|
||||||
COPY composer.json composer.lock* ./
|
COPY composer.json composer.lock* ./
|
||||||
RUN composer install \
|
RUN composer install \
|
||||||
--no-dev \
|
--no-dev \
|
||||||
@@ -22,20 +19,39 @@ RUN composer install \
|
|||||||
--optimize-autoloader \
|
--optimize-autoloader \
|
||||||
--prefer-dist
|
--prefer-dist
|
||||||
|
|
||||||
# ── final stage ───────────────────────────────────────────────────────────────
|
# ── build stage (generate optimised classmap with source present) ──────────────
|
||||||
|
FROM deps AS build
|
||||||
|
COPY . .
|
||||||
|
RUN composer dump-autoload --optimize --no-dev --no-interaction
|
||||||
|
|
||||||
|
# ── dev stage (all deps + Xdebug, source is mounted at runtime) ───────────────
|
||||||
|
FROM base AS dev
|
||||||
|
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
|
||||||
|
RUN apk add --no-cache ${PHPIZE_DEPS} linux-headers \
|
||||||
|
&& pecl install xdebug \
|
||||||
|
&& docker-php-ext-enable xdebug \
|
||||||
|
&& apk del ${PHPIZE_DEPS}
|
||||||
|
COPY docker/php/xdebug.ini /usr/local/etc/php/conf.d/docker-xdebug.ini
|
||||||
|
COPY composer.json composer.lock* ./
|
||||||
|
RUN composer install --no-scripts --no-interaction --prefer-dist
|
||||||
|
EXPOSE 8080
|
||||||
|
ENV APP_ENV=dev APP_DEBUG=1
|
||||||
|
CMD ["php", "-S", "0.0.0.0:8080", "-t", "public", "public/index.php"]
|
||||||
|
|
||||||
|
# ── final (prod) stage — no composer binary ────────────────────────────────────
|
||||||
FROM base AS final
|
FROM base AS final
|
||||||
|
|
||||||
COPY --from=deps /app/vendor /app/vendor
|
RUN addgroup -S app && adduser -S -G app app
|
||||||
|
|
||||||
|
COPY --from=build /app/vendor /app/vendor
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN mkdir -p var/cache var/log \
|
RUN mkdir -p var/cache var/log \
|
||||||
&& chmod -R 777 var \
|
&& chown -R app:app /app
|
||||||
&& composer dump-autoload --optimize --no-dev
|
|
||||||
|
USER app
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
ENV APP_ENV=prod APP_DEBUG=0
|
||||||
|
|
||||||
ENV APP_ENV=prod \
|
|
||||||
APP_DEBUG=0
|
|
||||||
|
|
||||||
# Symfony's built-in dev server via PHP — fine for a single-purpose container
|
|
||||||
CMD ["php", "-S", "0.0.0.0:8080", "-t", "public", "public/index.php"]
|
CMD ["php", "-S", "0.0.0.0:8080", "-t", "public", "public/index.php"]
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
services:
|
||||||
|
_defaults:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
public: false
|
||||||
|
|
||||||
|
App\:
|
||||||
|
resource: '../src/'
|
||||||
|
exclude:
|
||||||
|
- '../src/Kernel.php'
|
||||||
|
|
||||||
|
_instanceof:
|
||||||
|
App\Service\ProviderInterface:
|
||||||
|
tags: ['app.provider']
|
||||||
|
|
||||||
|
App\Service\GitHubProvider:
|
||||||
|
arguments:
|
||||||
|
$username: '%env(GITHUB_USER)%'
|
||||||
|
$token: '%env(GITHUB_TOKEN)%'
|
||||||
|
|
||||||
|
App\Service\GitLabProvider:
|
||||||
|
arguments:
|
||||||
|
$username: '%env(GITLAB_USER)%'
|
||||||
|
$token: '%env(GITLAB_TOKEN)%'
|
||||||
|
$baseUrl: '%env(GITLAB_URL)%'
|
||||||
|
|
||||||
|
App\Service\GiteaProvider:
|
||||||
|
arguments:
|
||||||
|
$username: '%env(GITEA_USER)%'
|
||||||
|
$token: '%env(GITEA_TOKEN)%'
|
||||||
|
$baseUrl: '%env(GITEA_URL)%'
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# Local development overrides — picked up automatically by `docker compose up`.
|
||||||
|
# To run with production config only: docker compose -f docker-compose.yml up
|
||||||
|
services:
|
||||||
|
graph:
|
||||||
|
build:
|
||||||
|
target: dev
|
||||||
|
volumes:
|
||||||
|
- .:/app
|
||||||
|
- /app/vendor # keeps vendor from the dev image, not your local dir
|
||||||
|
environment:
|
||||||
|
APP_ENV: dev
|
||||||
|
APP_DEBUG: "1"
|
||||||
|
XDEBUG_MODE: "${XDEBUG_MODE:-debug}"
|
||||||
|
XDEBUG_CONFIG: "client_host=host.docker.internal"
|
||||||
|
# Makes host.docker.internal resolve correctly on Linux
|
||||||
|
extra_hosts:
|
||||||
|
- "host.docker.internal:host-gateway"
|
||||||
|
ports:
|
||||||
|
- "9003:9003"
|
||||||
+3
-1
@@ -1,6 +1,8 @@
|
|||||||
services:
|
services:
|
||||||
graph:
|
graph:
|
||||||
build: .
|
build:
|
||||||
|
context: .
|
||||||
|
target: final
|
||||||
container_name: git-contribution-graph
|
container_name: git-contribution-graph
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[xdebug]
|
||||||
|
; Mode is controlled by the XDEBUG_MODE env var in docker-compose.override.yml.
|
||||||
|
; Set XDEBUG_MODE=off in your shell to skip Xdebug for faster test runs.
|
||||||
|
xdebug.mode=off
|
||||||
|
xdebug.client_port=9003
|
||||||
|
xdebug.client_host=host.docker.internal
|
||||||
|
xdebug.start_with_request=yes
|
||||||
|
xdebug.log=/tmp/xdebug.log
|
||||||
@@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Service\GiteaProvider;
|
use App\Service\ProviderInterface;
|
||||||
use App\Service\GitHubProvider;
|
|
||||||
use App\Service\GitLabProvider;
|
|
||||||
use App\Service\SvgRenderer;
|
use App\Service\SvgRenderer;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -20,30 +19,13 @@ class GraphController
|
|||||||
private readonly array $allowedHosts;
|
private readonly array $allowedHosts;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly GitHubProvider $github,
|
#[TaggedIterator('app.provider')]
|
||||||
private readonly GitLabProvider $gitlab,
|
private readonly iterable $providers,
|
||||||
private readonly GiteaProvider $gitea,
|
|
||||||
private readonly SvgRenderer $renderer,
|
private readonly SvgRenderer $renderer,
|
||||||
private readonly CacheInterface $cache,
|
private readonly CacheInterface $cache,
|
||||||
private readonly LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
#[Autowire(env: 'ALLOWED_HOSTS')]
|
#[Autowire(env: 'ALLOWED_HOSTS')]
|
||||||
string $allowedHosts = '',
|
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))));
|
$this->allowedHosts = array_values(array_filter(array_map('trim', explode(',', $allowedHosts))));
|
||||||
}
|
}
|
||||||
@@ -59,16 +41,13 @@ class GraphController
|
|||||||
return new Response('Forbidden', 403);
|
return new Response('Forbidden', 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$theme = $request->query->get('theme', 'dark');
|
$theme = $request->query->get('theme', 'dark');
|
||||||
|
|
||||||
$cacheKey = 'graph_' . $theme;
|
$cacheKey = 'graph_' . $theme;
|
||||||
|
|
||||||
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($theme): string {
|
$svg = $this->cache->get($cacheKey, function (ItemInterface $item) use ($theme): string {
|
||||||
$item->expiresAfter(3600);
|
$item->expiresAfter(3600);
|
||||||
|
|
||||||
$contributions = $this->fetchAllContributions();
|
return $this->renderer->render($this->fetchAllContributions(), $theme);
|
||||||
|
|
||||||
return $this->renderer->render($contributions, $theme);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Response($svg, 200, [
|
return new Response($svg, 200, [
|
||||||
@@ -83,51 +62,34 @@ class GraphController
|
|||||||
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
|
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return array<string, int> */
|
||||||
private function fetchAllContributions(): array
|
private function fetchAllContributions(): array
|
||||||
{
|
{
|
||||||
$contributions = [];
|
$contributions = [];
|
||||||
|
|
||||||
if ($this->githubUser !== '' && $this->githubToken !== '') {
|
/** @var ProviderInterface $provider */
|
||||||
try {
|
foreach ($this->providers as $provider) {
|
||||||
$contributions = $this->merge($contributions, $this->github->fetch($this->githubUser, $this->githubToken));
|
if (!$provider->isConfigured()) {
|
||||||
} catch (\Throwable $e) {
|
continue;
|
||||||
$this->logger->warning('GitHub fetch failed: ' . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->gitlabUser !== '' && $this->gitlabToken !== '') {
|
|
||||||
try {
|
try {
|
||||||
$contributions = $this->merge($contributions, $this->gitlab->fetch(
|
$contributions = $this->merge($contributions, $provider->fetch());
|
||||||
$this->gitlabUser,
|
|
||||||
$this->gitlabToken,
|
|
||||||
$this->gitlabUrl !== '' ? $this->gitlabUrl : 'https://gitlab.com'
|
|
||||||
));
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$this->logger->warning('GitLab fetch failed: ' . $e->getMessage());
|
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $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;
|
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
|
private function merge(array $base, array $new): array
|
||||||
{
|
{
|
||||||
foreach ($new as $date => $count) {
|
foreach ($new as $date => $count) {
|
||||||
$base[$date] = ($base[$date] ?? 0) + $count;
|
$base[$date] = ($base[$date] ?? 0) + $count;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $base;
|
return $base;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,19 +11,26 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|||||||
*
|
*
|
||||||
* Required token scopes: read:user
|
* Required token scopes: read:user
|
||||||
*/
|
*/
|
||||||
class GitHubProvider
|
class GitHubProvider implements ProviderInterface
|
||||||
{
|
{
|
||||||
private const GRAPHQL_URL = 'https://api.github.com/graphql';
|
private const GRAPHQL_URL = 'https://api.github.com/graphql';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly HttpClientInterface $client,
|
private readonly HttpClientInterface $client,
|
||||||
private readonly GraphQLApiClientRegistryInterface $registry,
|
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
|
* @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');
|
$from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
|
||||||
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
|
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
|
||||||
@@ -32,7 +39,7 @@ class GitHubProvider
|
|||||||
$graphqlClient = $this->registry->get('github');
|
$graphqlClient = $this->registry->get('github');
|
||||||
|
|
||||||
$query = $graphqlClient->buildQuery(
|
$query = $graphqlClient->buildQuery(
|
||||||
['user' => ['login' => $username]],
|
['user' => ['login' => $this->username]],
|
||||||
[
|
[
|
||||||
'contributionsCollection' => [
|
'contributionsCollection' => [
|
||||||
'_parameters' => ['from' => $from, 'to' => $to],
|
'_parameters' => ['from' => $from, 'to' => $to],
|
||||||
@@ -49,7 +56,7 @@ class GitHubProvider
|
|||||||
// transport sends form_params, so we use Symfony HttpClient here instead.
|
// transport sends form_params, so we use Symfony HttpClient here instead.
|
||||||
$response = $this->client->request('POST', self::GRAPHQL_URL, [
|
$response = $this->client->request('POST', self::GRAPHQL_URL, [
|
||||||
'headers' => [
|
'headers' => [
|
||||||
'Authorization' => "Bearer $token",
|
'Authorization' => "Bearer {$this->token}",
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
],
|
],
|
||||||
'json' => ['query' => $query],
|
'json' => ['query' => $query],
|
||||||
|
|||||||
@@ -10,26 +10,36 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|||||||
* Required token scopes: read_user, read_api
|
* Required token scopes: read_user, read_api
|
||||||
* Works with both gitlab.com and self-hosted instances.
|
* 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
|
* @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
|
// Resolve numeric user ID from username
|
||||||
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
||||||
'headers' => ['PRIVATE-TOKEN' => $token],
|
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||||
'query' => ['username' => $username],
|
'query' => ['username' => $this->username],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$users = $userResponse->toArray();
|
$users = $userResponse->toArray();
|
||||||
if (empty($users)) {
|
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'];
|
$userId = $users[0]['id'];
|
||||||
|
|
||||||
@@ -39,7 +49,7 @@ class GitLabProvider
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||||
'headers' => ['PRIVATE-TOKEN' => $token],
|
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||||
'query' => [
|
'query' => [
|
||||||
'after' => $after,
|
'after' => $after,
|
||||||
'per_page' => 100,
|
'per_page' => 100,
|
||||||
|
|||||||
@@ -12,18 +12,28 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|||||||
*
|
*
|
||||||
* Required token scopes: read:user
|
* 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
|
* @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, '/');
|
$baseUrl = rtrim($this->baseUrl, '/');
|
||||||
$response = $this->client->request('GET', "$baseUrl/api/v1/users/$username/heatmap", [
|
$response = $this->client->request('GET', "$baseUrl/api/v1/users/{$this->username}/heatmap", [
|
||||||
'headers' => ['Authorization' => "token $token"],
|
'headers' => ['Authorization' => "token {$this->token}"],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$data = $response->toArray();
|
$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