-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a github client #2747
Changes from 5 commits
eac03e5
7342592
b47e357
33cc9b8
d5a2e9a
87b2f78
fe7eec9
a7676fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ | |
public class DepsDevClient { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add Javadoc? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
private final HttpClient client; | ||
public final Gson gson; | ||
private final Gson gson; | ||
private final static String ADVISORY_URL_BASE = "https://api.deps.dev/v3/advisories/%s"; | ||
|
||
private final static String DEPENDENCY_URLBASE = "https://api.deps.dev/v3/systems/%s/packages/%s/versions/%s:dependencies"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.google.cloud.external; | ||
|
||
import com.google.cloud.model.Interval; | ||
import com.google.cloud.model.PullRequest; | ||
import com.google.cloud.model.PullRequestStatus; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.reflect.TypeToken; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class GitHubClient { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add Javadoc? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
private final HttpClient client; | ||
private final Gson gson; | ||
private final static String PULL_REQUESTS_BASE = "https://api.github.com/repos/%s/%s/pulls?state=all&per_page=100&page=%s"; | ||
private final static int MAX_PULL_REQUEST_NUM = 1000; | ||
private final static String OPEN_STATE = "open"; | ||
|
||
public GitHubClient(HttpClient client) { | ||
this.client = client; | ||
this.gson = new GsonBuilder().create(); | ||
} | ||
|
||
public PullRequestStatus listMonthlyPullRequestStatusOf(String organization, String repo) | ||
throws URISyntaxException, IOException, InterruptedException { | ||
return listPullRequestStatus(organization, repo, Interval.MONTHLY); | ||
} | ||
|
||
private PullRequestStatus listPullRequestStatus(String organization, String repo, Interval interval) | ||
throws URISyntaxException, IOException, InterruptedException { | ||
List<PullRequest> pullRequests = listPullRequests(organization, repo); | ||
ZonedDateTime now = ZonedDateTime.now(); | ||
long created = pullRequests | ||
.stream() | ||
.distinct() | ||
.filter(pullRequest -> pullRequest.state().equals(OPEN_STATE)) | ||
.filter(pullRequest -> { | ||
ZonedDateTime createdAt = utcTimeFrom(pullRequest.createdAt()); | ||
return now.minusDays(interval.getDays()).isBefore(createdAt); | ||
}) | ||
.count(); | ||
|
||
long merged = pullRequests | ||
.stream() | ||
.distinct() | ||
.filter(pullRequest -> Objects.nonNull(pullRequest.mergedAt())) | ||
.filter(pullRequest -> { | ||
ZonedDateTime createdAt = utcTimeFrom(pullRequest.mergedAt()); | ||
return now.minusDays(interval.getDays()).isBefore(createdAt); | ||
}) | ||
.count(); | ||
|
||
return new PullRequestStatus(created, merged, interval); | ||
} | ||
|
||
private List<PullRequest> listPullRequests(String organization, String repo) | ||
throws URISyntaxException, IOException, InterruptedException { | ||
List<PullRequest> pullRequests = new ArrayList<>(); | ||
int page = 1; | ||
while (pullRequests.size() < MAX_PULL_REQUEST_NUM) { | ||
HttpResponse<String> response = getResponse(getPullRequestsUrl(organization, repo, page)); | ||
pullRequests.addAll(gson.fromJson(response.body(), new TypeToken<List<PullRequest>>(){}.getType())); | ||
page++; | ||
} | ||
|
||
return pullRequests; | ||
} | ||
|
||
private String getPullRequestsUrl(String organization, String repo, int page) { | ||
return String.format(PULL_REQUESTS_BASE, organization, repo, page); | ||
} | ||
|
||
private ZonedDateTime utcTimeFrom(String time) { | ||
ZoneId zoneIdUTC = ZoneId.of("UTC"); | ||
Instant instant = Instant.parse(time); | ||
return instant.atZone(zoneIdUTC); | ||
} | ||
|
||
private HttpResponse<String> getResponse(String endpoint) | ||
throws URISyntaxException, IOException, InterruptedException { | ||
HttpRequest request = HttpRequest | ||
.newBuilder() | ||
.header("Authorization", System.getenv("GITHUB_TOKEN")) | ||
.uri(new URI(endpoint)) | ||
.GET() | ||
.build(); | ||
return client.send(request, BodyHandlers.ofString()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.google.cloud.model; | ||
|
||
public enum Interval { | ||
WEEKLY(7), | ||
MONTHLY(30); | ||
|
||
private final int days; | ||
|
||
Interval(int days) { | ||
this.days = days; | ||
} | ||
|
||
public int getDays() { | ||
return days; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.google.cloud.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public record PullRequest( | ||
String url, | ||
String state, | ||
@SerializedName("created_at") | ||
String createdAt, | ||
@SerializedName("merged_at") | ||
String mergedAt) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.google.cloud.model; | ||
|
||
public record PullRequestStatus(long created, long merged, Interval interval) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add Javadoc, especially which part of the dependency report this helps, with an example value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can change the class name to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.google.cloud.external; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.cloud.model.Interval; | ||
import com.google.cloud.model.PullRequestStatus; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandler; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
public class GitHubClientTest { | ||
|
||
private HttpResponse<String> response; | ||
private GitHubClient client; | ||
|
||
@Before | ||
public void setUp() throws IOException, InterruptedException { | ||
HttpClient httpClient = mock(HttpClient.class); | ||
client = new GitHubClient(httpClient); | ||
response = mock(HttpResponse.class); | ||
when(httpClient.send(any(HttpRequest.class), any(BodyHandler.class))) | ||
.thenReturn(response); | ||
} | ||
|
||
@Test | ||
public void testListMonthlyPullRequestStatusSucceeds() | ||
throws URISyntaxException, IOException, InterruptedException { | ||
ZonedDateTime fixedNow = ZonedDateTime.parse("2024-05-22T09:33:52Z"); | ||
ZonedDateTime lastMonth = ZonedDateTime.parse("2024-04-22T09:33:52Z"); | ||
Instant prInstant = Instant.parse("2024-05-10T09:33:52Z"); | ||
ZonedDateTime prTime = ZonedDateTime.parse("2024-05-10T09:33:52Z"); | ||
String responseBody = Files.readString( | ||
Path.of("src/test/resources/pull_request_sample_response.txt")); | ||
|
||
try (MockedStatic<ZonedDateTime> mockedLocalDateTime = Mockito.mockStatic(ZonedDateTime.class); | ||
MockedStatic<Instant> mockedInstant = Mockito.mockStatic(Instant.class)) { | ||
mockedLocalDateTime.when(ZonedDateTime::now).thenReturn(fixedNow); | ||
mockedInstant.when(() -> Instant.parse(Mockito.anyString())).thenReturn(prInstant); | ||
when(fixedNow.minusDays(30)).thenReturn(lastMonth); | ||
when(prInstant.atZone(ZoneId.of("UTC"))).thenReturn(prTime); | ||
when(response.body()).thenReturn(responseBody); | ||
String org = ""; | ||
String repo = ""; | ||
PullRequestStatus status = client.listMonthlyPullRequestStatusOf(org, repo); | ||
|
||
assertEquals(Interval.MONTHLY, status.interval()); | ||
assertEquals(3, status.created()); | ||
assertEquals(7, status.merged()); | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add source code comment how it helps tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.