|
| 1 | +package com.google.cloud.external; |
| 2 | + |
| 3 | +import com.google.cloud.model.Interval; |
| 4 | +import com.google.cloud.model.PullRequest; |
| 5 | +import com.google.cloud.model.PullRequestStatistics; |
| 6 | +import com.google.gson.Gson; |
| 7 | +import com.google.gson.GsonBuilder; |
| 8 | +import com.google.gson.reflect.TypeToken; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.URI; |
| 11 | +import java.net.URISyntaxException; |
| 12 | +import java.net.http.HttpClient; |
| 13 | +import java.net.http.HttpRequest; |
| 14 | +import java.net.http.HttpResponse; |
| 15 | +import java.net.http.HttpResponse.BodyHandlers; |
| 16 | +import java.time.Instant; |
| 17 | +import java.time.ZoneId; |
| 18 | +import java.time.ZonedDateTime; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +/** |
| 24 | + * GitHubClient is a class that sends HTTP requests to the GitHub RESTful API. It provides methods |
| 25 | + * for interacting with various GitHub resources such as repositories, issues, users, etc. |
| 26 | + * |
| 27 | + * <p>This class simplifies the process of making API calls by handling authentication, request |
| 28 | + * construction, and response parsing. It uses the {@link java.net.http.HttpClient} for sending |
| 29 | + * requests and {@link com.google.gson.Gson} for handling JSON serialization/deserialization. |
| 30 | + */ |
| 31 | +public class GitHubClient { |
| 32 | + private final HttpClient client; |
| 33 | + private final Gson gson; |
| 34 | + private static final String PULL_REQUESTS_BASE = |
| 35 | + "https://api.github.com/repos/%s/%s/pulls?state=all&per_page=100&page=%s"; |
| 36 | + private static final int MAX_PULL_REQUEST_NUM = 1000; |
| 37 | + private static final String OPEN_STATE = "open"; |
| 38 | + |
| 39 | + public GitHubClient(HttpClient client) { |
| 40 | + this.client = client; |
| 41 | + this.gson = new GsonBuilder().create(); |
| 42 | + } |
| 43 | + |
| 44 | + public PullRequestStatistics listMonthlyPullRequestStatusOf(String organization, String repo) |
| 45 | + throws URISyntaxException, IOException, InterruptedException { |
| 46 | + return listPullRequestStatus(organization, repo, Interval.MONTHLY); |
| 47 | + } |
| 48 | + |
| 49 | + private PullRequestStatistics listPullRequestStatus( |
| 50 | + String organization, String repo, Interval interval) |
| 51 | + throws URISyntaxException, IOException, InterruptedException { |
| 52 | + List<PullRequest> pullRequests = listPullRequests(organization, repo); |
| 53 | + ZonedDateTime now = ZonedDateTime.now(); |
| 54 | + long created = |
| 55 | + pullRequests.stream() |
| 56 | + .distinct() |
| 57 | + .filter(pullRequest -> pullRequest.state().equals(OPEN_STATE)) |
| 58 | + .filter( |
| 59 | + pullRequest -> { |
| 60 | + ZonedDateTime createdAt = utcTimeFrom(pullRequest.createdAt()); |
| 61 | + return now.minusDays(interval.getDays()).isBefore(createdAt); |
| 62 | + }) |
| 63 | + .count(); |
| 64 | + |
| 65 | + long merged = |
| 66 | + pullRequests.stream() |
| 67 | + .distinct() |
| 68 | + .filter(pullRequest -> Objects.nonNull(pullRequest.mergedAt())) |
| 69 | + .filter( |
| 70 | + pullRequest -> { |
| 71 | + ZonedDateTime createdAt = utcTimeFrom(pullRequest.mergedAt()); |
| 72 | + return now.minusDays(interval.getDays()).isBefore(createdAt); |
| 73 | + }) |
| 74 | + .count(); |
| 75 | + |
| 76 | + return new PullRequestStatistics(created, merged, interval); |
| 77 | + } |
| 78 | + |
| 79 | + private List<PullRequest> listPullRequests(String organization, String repo) |
| 80 | + throws URISyntaxException, IOException, InterruptedException { |
| 81 | + List<PullRequest> pullRequests = new ArrayList<>(); |
| 82 | + int page = 1; |
| 83 | + while (pullRequests.size() < MAX_PULL_REQUEST_NUM) { |
| 84 | + HttpResponse<String> response = getResponse(getPullRequestsUrl(organization, repo, page)); |
| 85 | + pullRequests.addAll( |
| 86 | + gson.fromJson(response.body(), new TypeToken<List<PullRequest>>() {}.getType())); |
| 87 | + page++; |
| 88 | + } |
| 89 | + |
| 90 | + return pullRequests; |
| 91 | + } |
| 92 | + |
| 93 | + private String getPullRequestsUrl(String organization, String repo, int page) { |
| 94 | + return String.format(PULL_REQUESTS_BASE, organization, repo, page); |
| 95 | + } |
| 96 | + |
| 97 | + private ZonedDateTime utcTimeFrom(String time) { |
| 98 | + ZoneId zoneIdUTC = ZoneId.of("UTC"); |
| 99 | + Instant instant = Instant.parse(time); |
| 100 | + return instant.atZone(zoneIdUTC); |
| 101 | + } |
| 102 | + |
| 103 | + private HttpResponse<String> getResponse(String endpoint) |
| 104 | + throws URISyntaxException, IOException, InterruptedException { |
| 105 | + HttpRequest request = |
| 106 | + HttpRequest.newBuilder() |
| 107 | + .header("Authorization", System.getenv("GITHUB_TOKEN")) |
| 108 | + .uri(new URI(endpoint)) |
| 109 | + .GET() |
| 110 | + .build(); |
| 111 | + return client.send(request, BodyHandlers.ofString()); |
| 112 | + } |
| 113 | +} |
0 commit comments