Skip to content

Commit

Permalink
fix: adds GitHub authentication to avoid rate limiting failures.
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanik committed Apr 6, 2018
1 parent e792799 commit 8469672
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
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 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 String OAUTH_AUTHORIZATION_HEADER_VALUE_PREFIX = "Bearer ";

private String TAGS_URL = "/tags";
private String TAG_NAME = "name";
private String project;
Expand All @@ -18,7 +28,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 +39,21 @@ 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);
}
return headers;
}
}

0 comments on commit 8469672

Please sign in to comment.