baseUrl = rtrim($baseUrl, '/'); } public function getName(): string { return 'gitea'; } public function isConfigured(): bool { return $this->username !== '' && $this->token !== '' && $this->baseUrl !== ''; } public function ping(): void { $this->client->request('GET', "$this->baseUrl/api/v1/user", [ 'headers' => ['Authorization' => "token {$this->token}"], ])->getContent(); } /** * Fires a single heatmap request; $since/$until are carried through unused for client-side filtering in resolveFetch(). * * @return array{response: ResponseInterface, since: ?\DateTimeImmutable, until: ?\DateTimeImmutable} */ public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): array { $this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $this->baseUrl]); $response = $this->client->request('GET', "$this->baseUrl/api/v1/users/{$this->username}/heatmap", [ 'headers' => ['Authorization' => "token {$this->token}"], ]); return ['response' => $response, 'since' => $since, 'until' => $until]; } /** * Filters the heatmap entries to the [$since, $until] window and sums contributions per day. * * @param array{response: ResponseInterface, since: ?\DateTimeImmutable, until: ?\DateTimeImmutable} $handle * @return array date (Y-m-d) => contribution count */ public function resolveFetch(mixed $handle): array { ['response' => $response, 'since' => $since, 'until' => $until] = $handle; /** @var ResponseInterface $response */ $data = $response->toArray(); $cutoff = ($since ?? new \DateTimeImmutable('-365 days'))->getTimestamp(); $ceiling = $until?->getTimestamp(); $result = []; foreach ($data as $entry) { if ($entry['timestamp'] < $cutoff) { continue; } if ($ceiling !== null && $entry['timestamp'] > $ceiling) { continue; } $date = date('Y-m-d', $entry['timestamp']); $result[$date] = ($result[$date] ?? 0) + (int) $entry['contributions']; } $this->logger->info('GiteaProvider: fetched contributions', [ 'user' => $this->username, 'days' => count($result), 'total' => array_sum($result), ]); return $result; } }