Skip to content

Fix API calls failing because of secondary api limits #1853

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

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/main/java/org/kohsuke/github/GitHubAbuseLimitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,47 @@ public abstract class GitHubAbuseLimitHandler extends GitHubConnectorResponseErr
* Signals that an I/O exception has occurred.
*/
@Override
boolean isError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException {
return connectorResponse.statusCode() == HttpURLConnection.HTTP_FORBIDDEN
&& connectorResponse.header("Retry-After") != null;
boolean isError(@Nonnull GitHubConnectorResponse connectorResponse) {
return isForbidden(connectorResponse) && hasRetryOrLimitHeader(connectorResponse);
}

/**
* Checks if the response status code is HTTP_FORBIDDEN (403).
*
* @param connectorResponse
* the response from the GitHub connector
* @return true if the status code is HTTP_FORBIDDEN
*/
private boolean isForbidden(GitHubConnectorResponse connectorResponse) {
return connectorResponse.statusCode() == HttpURLConnection.HTTP_FORBIDDEN;
}

/**
* Checks if the response contains either "Retry-After" or "gh-limited-by" headers. GitHub does not guarantee the
* presence of the Retry-After header. However, the gh-limited-by header is included in the response when the error
* is due to rate limiting
*
* @param connectorResponse
* the response from the GitHub connector
* @return true if either "Retry-After" or "gh-limited-by" headers are present
* @see <a href=
* "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>
*/
private boolean hasRetryOrLimitHeader(GitHubConnectorResponse connectorResponse) {
return hasHeader(connectorResponse, "Retry-After") || hasHeader(connectorResponse, "gh-limited-by");
}

/**
* Checks if the response contains a specific header.
*
* @param connectorResponse
* the response from the GitHub connector
* @param headerName
* the name of the header to check for
* @return true if the specified header is present
*/
private boolean hasHeader(GitHubConnectorResponse connectorResponse, String headerName) {
return connectorResponse.header(headerName) != null;
}

/**
Expand Down