username !== '' && $this->token !== ''; } public function ping(): void { $baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/'); $this->client->request('GET', "$baseUrl/api/v4/user", [ 'headers' => ['PRIVATE-TOKEN' => $this->token], ])->getContent(); } /** * @return array date (Y-m-d) => event count */ public function fetch(): array { $baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/'); $this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]); $userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [ 'headers' => ['PRIVATE-TOKEN' => $this->token], 'query' => ['username' => $this->username], ]); $users = $userResponse->toArray(); if (empty($users)) { throw new NotFoundHttpException("GitLab: user '{$this->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' => $this->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); $this->logger->info('GitLabProvider: fetched contributions', [ 'user' => $this->username, 'pages' => $page - 1, 'days' => count($result), 'total' => array_sum($result), ]); return $result; } }