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,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
* Fetches the last 365 days of push/merge events from the GitLab REST API.
|
||||
*
|
||||
* Required token scopes: read_user, read_api
|
||||
* Works with both gitlab.com and self-hosted instances.
|
||||
*/
|
||||
class GitLabProvider
|
||||
{
|
||||
public function __construct(private readonly HttpClientInterface $client) {}
|
||||
|
||||
/**
|
||||
* @return array<string, int> date (Y-m-d) => event count
|
||||
*/
|
||||
public function fetch(string $username, string $token, string $baseUrl = 'https://gitlab.com'): array
|
||||
{
|
||||
$baseUrl = rtrim($baseUrl, '/');
|
||||
|
||||
// Resolve numeric user ID from username
|
||||
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $token],
|
||||
'query' => ['username' => $username],
|
||||
]);
|
||||
|
||||
$users = $userResponse->toArray();
|
||||
if (empty($users)) {
|
||||
throw new \RuntimeException("GitLab: user '$username' not found on $baseUrl");
|
||||
}
|
||||
$userId = $users[0]['id'];
|
||||
|
||||
$result = [];
|
||||
$after = (new \DateTimeImmutable('-365 days'))->format('Y-m-d');
|
||||
$page = 1;
|
||||
|
||||
do {
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
'page' => $page,
|
||||
],
|
||||
]);
|
||||
|
||||
$events = $response->toArray();
|
||||
|
||||
foreach ($events as $event) {
|
||||
$date = substr($event['created_at'], 0, 10);
|
||||
$result[$date] = ($result[$date] ?? 0) + 1;
|
||||
}
|
||||
|
||||
$page++;
|
||||
} while (count($events) === 100);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user