Skip to content

Commit 1fccf4f

Browse files
committed
Fix API calls failing because of secondary api limits
1 parent 3043691 commit 1fccf4f

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,47 @@ public abstract class GitHubAbuseLimitHandler extends GitHubConnectorResponseErr
2828
* Signals that an I/O exception has occurred.
2929
*/
3030
@Override
31-
boolean isError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException {
32-
return connectorResponse.statusCode() == HttpURLConnection.HTTP_FORBIDDEN
33-
&& connectorResponse.header("Retry-After") != null;
31+
boolean isError(@Nonnull GitHubConnectorResponse connectorResponse) {
32+
return isForbidden(connectorResponse) && hasRetryOrLimitHeader(connectorResponse);
33+
}
34+
35+
/**
36+
* Checks if the response status code is HTTP_FORBIDDEN (403).
37+
*
38+
* @param connectorResponse
39+
* the response from the GitHub connector
40+
* @return true if the status code is HTTP_FORBIDDEN
41+
*/
42+
private boolean isForbidden(GitHubConnectorResponse connectorResponse) {
43+
return connectorResponse.statusCode() == HttpURLConnection.HTTP_FORBIDDEN;
44+
}
45+
46+
/**
47+
* Checks if the response contains either "Retry-After" or "gh-limited-by" headers. GitHub does not guarantee the
48+
* presence of the Retry-After header. However, the gh-limited-by header is included in the response when the error
49+
* is due to rate limiting
50+
*
51+
* @param connectorResponse
52+
* the response from the GitHub connector
53+
* @return true if either "Retry-After" or "gh-limited-by" headers are present
54+
* @see <a href=
55+
* "https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#handle-rate-limit-errors-appropriately</a>
56+
*/
57+
private boolean hasRetryOrLimitHeader(GitHubConnectorResponse connectorResponse) {
58+
return hasHeader(connectorResponse, "Retry-After") || hasHeader(connectorResponse, "gh-limited-by");
59+
}
60+
61+
/**
62+
* Checks if the response contains a specific header.
63+
*
64+
* @param connectorResponse
65+
* the response from the GitHub connector
66+
* @param headerName
67+
* the name of the header to check for
68+
* @return true if the specified header is present
69+
*/
70+
private boolean hasHeader(GitHubConnectorResponse connectorResponse, String headerName) {
71+
return connectorResponse.header(headerName) != null;
3472
}
3573

3674
/**

0 commit comments

Comments
 (0)