Skip to content

Commit 1535817

Browse files
authored
Merge pull request #1290 from bitwiseman/task/response-to-urlconnection
Swtich from HttpConnector to GitHubConnector
2 parents 0601578 + 056d1f0 commit 1535817

File tree

51 files changed

+2025
-658
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2025
-658
lines changed

pom.xml

+22-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@
270270
</annotationProcessorPaths>
271271
</configuration>
272272
</plugin>
273-
274273
<plugin>
275274
<artifactId>maven-surefire-plugin</artifactId>
276275
<executions>
@@ -575,6 +574,28 @@
575574
<plugin>
576575
<artifactId>maven-surefire-plugin</artifactId>
577576
<executions>
577+
<execution>
578+
<id>okhttp-test</id>
579+
<phase>test</phase>
580+
<goals>
581+
<goal>test</goal>
582+
</goals>
583+
<configuration>
584+
<excludesFile>src/test/resources/slow-or-flaky-tests.txt</excludesFile>
585+
<argLine>@{jacoco.surefire.argLine} ${surefire.argLine} -Dtest.github.connector=okhttp</argLine>
586+
</configuration>
587+
</execution>
588+
<execution>
589+
<id>okhttpconnector-test</id>
590+
<phase>test</phase>
591+
<goals>
592+
<goal>test</goal>
593+
</goals>
594+
<configuration>
595+
<excludesFile>src/test/resources/slow-or-flaky-tests.txt</excludesFile>
596+
<argLine>@{jacoco.surefire.argLine} ${surefire.argLine} -Dtest.github.connector=okhttpconnector</argLine>
597+
</configuration>
598+
</execution>
578599
<execution>
579600
<id>slow-or-flaky-test</id>
580601
<phase>test</phase>

src/main/java/org/kohsuke/github/AbuseLimitHandler.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.kohsuke.github;
22

3+
import org.kohsuke.github.connector.GitHubConnectorResponse;
4+
35
import java.io.IOException;
46
import java.io.InterruptedIOException;
57
import java.net.HttpURLConnection;
68

9+
import javax.annotation.Nonnull;
10+
711
/**
812
* Pluggable strategy to determine what to do when the API abuse limit is hit.
913
*
@@ -13,6 +17,33 @@
1317
* @see RateLimitHandler
1418
*/
1519
public abstract class AbuseLimitHandler {
20+
21+
/**
22+
* Called when the library encounters HTTP error indicating that the API abuse limit is reached.
23+
*
24+
* <p>
25+
* Any exception thrown from this method will cause the request to fail, and the caller of github-api will receive
26+
* an exception. If this method returns normally, another request will be attempted. For that to make sense, the
27+
* implementation needs to wait for some time.
28+
*
29+
* @param connectorResponse
30+
* Response information for this request.
31+
* @throws IOException
32+
* on failure
33+
* @see <a href="https://developer.github.com/v3/#abuse-rate-limits">API documentation from GitHub</a>
34+
* @see <a href=
35+
* "https://developer.github.com/v3/guides/best-practices-for-integrators/#dealing-with-abuse-rate-limits">Dealing
36+
* with abuse rate limits</a>
37+
*
38+
*/
39+
public void onError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException {
40+
GHIOException e = new HttpException("Abuse limit violation",
41+
connectorResponse.statusCode(),
42+
connectorResponse.header("Status"),
43+
connectorResponse.request().url().toString()).withResponseHeaderFields(connectorResponse.allHeaders());
44+
onError(e, connectorResponse.toHttpURLConnection());
45+
}
46+
1647
/**
1748
* Called when the library encounters HTTP error indicating that the API abuse limit is reached.
1849
*
@@ -34,7 +65,9 @@ public abstract class AbuseLimitHandler {
3465
* with abuse rate limits</a>
3566
*
3667
*/
37-
public abstract void onError(IOException e, HttpURLConnection uc) throws IOException;
68+
@Deprecated
69+
public void onError(IOException e, HttpURLConnection uc) throws IOException {
70+
}
3871

3972
/**
4073
* Wait until the API abuse "wait time" is passed.

src/main/java/org/kohsuke/github/GHCompare.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.annotation.JacksonInject;
44
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
55
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
6-
import org.jetbrains.annotations.NotNull;
76

87
import java.io.IOException;
98
import java.net.URL;
@@ -174,7 +173,7 @@ public PagedIterable<Commit> listCommits() {
174173
} else {
175174
// if not using paginated commits, adapt the returned commits array
176175
return new PagedIterable<Commit>() {
177-
@NotNull
176+
@Nonnull
178177
@Override
179178
public PagedIterator<Commit> _iterator(int pageSize) {
180179
return new PagedIterator<>(Collections.singleton(commits).iterator(), null);

src/main/java/org/kohsuke/github/GHNotificationStream.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ GHThread fetch() {
192192
idx = threads.length - 1;
193193

194194
nextCheckTime = calcNextCheckTime(response);
195-
lastModified = response.headerField("Last-Modified");
195+
lastModified = response.header("Last-Modified");
196196
}
197197
} catch (IOException | InterruptedException e) {
198198
throw new RuntimeException(e);
199199
}
200200
}
201201

202202
private long calcNextCheckTime(GitHubResponse<GHThread[]> response) {
203-
String v = response.headerField("X-Poll-Interval");
203+
String v = response.header("X-Poll-Interval");
204204
if (v == null)
205205
v = "60";
206206
long seconds = Integer.parseInt(v);

src/main/java/org/kohsuke/github/GHObject.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
66
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
77
import org.apache.commons.lang3.builder.ToStringStyle;
8+
import org.kohsuke.github.connector.GitHubConnectorResponse;
89

910
import java.io.IOException;
1011
import java.lang.reflect.Field;
@@ -39,13 +40,13 @@ public abstract class GHObject extends GitHubInteractiveObject {
3940
/**
4041
* Called by Jackson
4142
*
42-
* @param responseInfo
43-
* the {@link GitHubResponse.ResponseInfo} to get headers from.
43+
* @param connectorResponse
44+
* the {@link GitHubConnectorResponse} to get headers from.
4445
*/
4546
@JacksonInject
46-
protected void setResponseHeaderFields(@CheckForNull GitHubResponse.ResponseInfo responseInfo) {
47-
if (responseInfo != null) {
48-
responseHeaderFields = responseInfo.headers();
47+
protected void setResponseHeaderFields(@CheckForNull GitHubConnectorResponse connectorResponse) {
48+
if (connectorResponse != null) {
49+
responseHeaderFields = connectorResponse.allHeaders();
4950
}
5051
}
5152

src/main/java/org/kohsuke/github/GHRateLimit.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
77
import org.apache.commons.lang3.StringUtils;
8+
import org.kohsuke.github.connector.GitHubConnectorResponse;
89

910
import java.time.Duration;
1011
import java.time.ZonedDateTime;
@@ -436,20 +437,20 @@ public Record(@JsonProperty(value = "limit", required = true) int limit,
436437
* the remaining
437438
* @param resetEpochSeconds
438439
* the reset epoch seconds
439-
* @param responseInfo
440+
* @param connectorResponse
440441
* the response info
441442
*/
442443
@JsonCreator
443444
Record(@JsonProperty(value = "limit", required = true) int limit,
444445
@JsonProperty(value = "remaining", required = true) int remaining,
445446
@JsonProperty(value = "reset", required = true) long resetEpochSeconds,
446-
@JacksonInject @CheckForNull GitHubResponse.ResponseInfo responseInfo) {
447+
@JacksonInject @CheckForNull GitHubConnectorResponse connectorResponse) {
447448
this.limit = limit;
448449
this.remaining = remaining;
449450
this.resetEpochSeconds = resetEpochSeconds;
450451
String updatedAt = null;
451-
if (responseInfo != null) {
452-
updatedAt = responseInfo.headerField("Date");
452+
if (connectorResponse != null) {
453+
updatedAt = connectorResponse.header("Date");
453454
}
454455
this.resetDate = calculateResetDate(updatedAt);
455456
}

src/main/java/org/kohsuke/github/GitHub.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.kohsuke.github.authorization.AuthorizationProvider;
3131
import org.kohsuke.github.authorization.ImmutableAuthorizationProvider;
3232
import org.kohsuke.github.authorization.UserAuthorizationProvider;
33+
import org.kohsuke.github.connector.GitHubConnector;
34+
import org.kohsuke.github.internal.GitHubConnectorHttpConnectorAdapter;
3335
import org.kohsuke.github.internal.Previews;
3436

3537
import java.io.*;
@@ -110,7 +112,7 @@ public class GitHub {
110112
* a authorization provider
111113
*/
112114
GitHub(String apiUrl,
113-
HttpConnector connector,
115+
GitHubConnector connector,
114116
RateLimitHandler rateLimitHandler,
115117
AbuseLimitHandler abuseLimitHandler,
116118
GitHubRateLimitChecker rateLimitChecker,
@@ -129,7 +131,7 @@ public class GitHub {
129131
users = new ConcurrentHashMap<>();
130132
orgs = new ConcurrentHashMap<>();
131133

132-
this.client = new GitHubHttpUrlConnectionClient(apiUrl,
134+
this.client = new GitHubClient(apiUrl,
133135
connector,
134136
rateLimitHandler,
135137
abuseLimitHandler,
@@ -441,7 +443,7 @@ public static GitHub connectToEnterpriseAnonymously(String apiUrl) throws IOExce
441443
public static GitHub offline() {
442444
try {
443445
return new GitHubBuilder().withEndpoint("https://api.github.invalid")
444-
.withConnector(HttpConnector.OFFLINE)
446+
.withConnector(GitHubConnector.OFFLINE)
445447
.build();
446448
} catch (IOException e) {
447449
throw new IllegalStateException("The offline implementation constructor should not connect", e);
@@ -470,7 +472,10 @@ public boolean isOffline() {
470472
* Gets connector.
471473
*
472474
* @return the connector
475+
* @deprecated HttpConnector has been replaced by GitHubConnector which is generally not useful outside of this
476+
* library. If you are using this method, file an issue describing your use case.
473477
*/
478+
@Deprecated
474479
public HttpConnector getConnector() {
475480
return client.getConnector();
476481
}
@@ -483,8 +488,8 @@ public HttpConnector getConnector() {
483488
* @deprecated HttpConnector should not be changed. If you find yourself needing to do this, file an issue.
484489
*/
485490
@Deprecated
486-
public void setConnector(HttpConnector connector) {
487-
client.setConnector(connector);
491+
public void setConnector(@Nonnull HttpConnector connector) {
492+
client.setConnector(GitHubConnectorHttpConnectorAdapter.adapt(connector));
488493
}
489494

490495
/**

src/main/java/org/kohsuke/github/GitHubBuilder.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.apache.commons.io.IOUtils;
44
import org.kohsuke.github.authorization.AuthorizationProvider;
55
import org.kohsuke.github.authorization.ImmutableAuthorizationProvider;
6+
import org.kohsuke.github.connector.GitHubConnector;
67
import org.kohsuke.github.extras.ImpatientHttpConnector;
8+
import org.kohsuke.github.internal.GitHubConnectorHttpConnectorAdapter;
79

810
import java.io.File;
911
import java.io.FileInputStream;
@@ -30,7 +32,7 @@ public class GitHubBuilder implements Cloneable {
3032
// default scoped so unit tests can read them.
3133
/* private */ String endpoint = GitHubClient.GITHUB_URL;
3234

33-
private HttpConnector connector;
35+
private GitHubConnector connector;
3436

3537
private RateLimitHandler rateLimitHandler = RateLimitHandler.WAIT;
3638
private AbuseLimitHandler abuseLimitHandler = AbuseLimitHandler.WAIT;
@@ -332,7 +334,18 @@ public GitHubBuilder withJwtToken(String jwtToken) {
332334
* the connector
333335
* @return the git hub builder
334336
*/
335-
public GitHubBuilder withConnector(HttpConnector connector) {
337+
public GitHubBuilder withConnector(@Nonnull HttpConnector connector) {
338+
return withConnector(GitHubConnectorHttpConnectorAdapter.adapt(connector));
339+
}
340+
341+
/**
342+
* With connector git hub builder.
343+
*
344+
* @param connector
345+
* the connector
346+
* @return the git hub builder
347+
*/
348+
public GitHubBuilder withConnector(GitHubConnector connector) {
336349
this.connector = connector;
337350
return this;
338351
}

0 commit comments

Comments
 (0)