3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.alm.client.github;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.function.Function;
26 import javax.annotation.Nullable;
27 import org.kohsuke.github.GHRateLimit;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.sonar.alm.client.github.security.AccessToken;
31 import org.sonar.api.ce.ComputeEngineSide;
32 import org.sonar.api.server.ServerSide;
34 import static java.lang.String.format;
38 public class GithubPaginatedHttpClientImpl implements GithubPaginatedHttpClient {
40 private static final Logger LOG = LoggerFactory.getLogger(GithubPaginatedHttpClientImpl.class);
41 private final GithubApplicationHttpClient appHttpClient;
42 private final RatioBasedRateLimitChecker rateLimitChecker;
44 public GithubPaginatedHttpClientImpl(GithubApplicationHttpClient appHttpClient, RatioBasedRateLimitChecker rateLimitChecker) {
45 this.appHttpClient = appHttpClient;
46 this.rateLimitChecker = rateLimitChecker;
50 public <E> List<E> get(String appUrl, AccessToken token, String query, Function<String, List<E>> responseDeserializer) throws IOException {
51 List<E> results = new ArrayList<>();
52 String nextEndpoint = query + "?per_page=100";
53 if (query.contains("?")) {
54 nextEndpoint = query + "&per_page=100";
56 GithubApplicationHttpClient.RateLimit rateLimit = null;
57 while (nextEndpoint != null) {
58 checkRateLimit(rateLimit);
59 GithubApplicationHttpClient.GetResponse response = executeCall(appUrl, token, nextEndpoint);
61 .ifPresent(content -> results.addAll(responseDeserializer.apply(content)));
62 nextEndpoint = response.getNextEndPoint().orElse(null);
63 rateLimit = response.getRateLimit();
68 private void checkRateLimit(@Nullable GithubApplicationHttpClient.RateLimit rateLimit) {
69 if (rateLimit == null) {
73 GHRateLimit.Record rateLimitRecord = new GHRateLimit.Record(rateLimit.limit(), rateLimit.remaining(), rateLimit.reset());
74 rateLimitChecker.checkRateLimit(rateLimitRecord, 0);
75 } catch (InterruptedException e) {
76 Thread.currentThread().interrupt();
77 LOG.warn(format("Thread interrupted: %s", e.getMessage()), e);
81 private GithubApplicationHttpClient.GetResponse executeCall(String appUrl, AccessToken token, String endpoint) throws IOException {
82 GithubApplicationHttpClient.GetResponse response = appHttpClient.get(appUrl, token, endpoint);
83 if (response.getCode() < 200 || response.getCode() >= 300) {
84 throw new IllegalStateException(
85 format("Error while executing a call to GitHub. Return code %s. Error message: %s.", response.getCode(), response.getContent().orElse("")));