Skip to content

Commit

Permalink
fix(tests): adds GitHub authentication to avoid rate limiting failure…
Browse files Browse the repository at this point in the history
…s. (#433)
  • Loading branch information
hemanik authored and MatousJobanek committed Apr 20, 2018
1 parent e792799 commit c7136fb
Showing 1 changed file with 28 additions and 3 deletions.
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;
}
}

0 comments on commit c7136fb

Please sign in to comment.