Skip to content
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

fix: adds GitHub authentication to avoid rate limiting failures. #433

Merged
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.http.client.utils.URIBuilder;
import org.jboss.arquillian.drone.webdriver.utils.HttpClient;
import org.jboss.arquillian.drone.webdriver.utils.Validate;

import static org.jboss.arquillian.drone.webdriver.binary.downloading.source.GitHubSource.AUTHORIZATION_HEADER_KEY;

public class GitHubProjectVersionExtractor {

private static final Logger logger = Logger.getLogger(GitHubProjectVersionExtractor.class.getName());
private static final String OAUTH_AUTHORIZATION_HEADER_VALUE_PREFIX = "Bearer ";

private String TAGS_URL = "/tags";
private String TAG_NAME = "name";
private String project;
Expand All @@ -18,7 +30,7 @@ public GitHubProjectVersionExtractor(String project) {

public String getLatestReleaseFromGitHub() {
try {
final HttpClient.Response response = sentGetRequestWithPagination(project + TAGS_URL, 1);
final HttpClient.Response response = sentGetRequestWithPagination(project + TAGS_URL, 1, getAuthorizationHeader());
JsonArray releaseTags = new Gson().fromJson(response.getPayload(), JsonElement.class).getAsJsonArray();
if (releaseTags.size() == 0) {
return null;
Expand All @@ -29,11 +41,24 @@ public String getLatestReleaseFromGitHub() {
}
}

private HttpClient.Response sentGetRequestWithPagination(String url, int pageNumber) throws Exception {
private HttpClient.Response sentGetRequestWithPagination(String url, int pageNumber, Map<String, String> headers)
throws Exception {
final URIBuilder uriBuilder = new URIBuilder(url);
if (pageNumber != 1) {
uriBuilder.setParameter("page", String.valueOf(pageNumber));
}
return new HttpClient().get(uriBuilder.build().toString());
return new HttpClient().get(uriBuilder.build().toString(), headers);
}

private Map<String, String> getAuthorizationHeader() throws IOException {
Map<String, String> headers = new HashMap<>();
String token = new String(Files.readAllBytes(Paths.get(".github-auth"))).trim();
if (Validate.nonEmpty(token)) {
headers.put(AUTHORIZATION_HEADER_KEY, OAUTH_AUTHORIZATION_HEADER_VALUE_PREFIX + token);
} else {
logger.warning("Missing GitHub authentication configuration. Making an unauthenticated request to the GitHub API.");
}
return headers;
}
}