From c7136fb4bbbcceda40ceaf2795518e6ba6432a00 Mon Sep 17 00:00:00 2001 From: Hemani Date: Fri, 20 Apr 2018 18:38:54 +0530 Subject: [PATCH] fix(tests): adds GitHub authentication to avoid rate limiting failures. (#433) --- .../GitHubProjectVersionExtractor.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/arquillian/tests/utilities/GitHubProjectVersionExtractor.java b/src/test/java/org/arquillian/tests/utilities/GitHubProjectVersionExtractor.java index 4013cd02465..65a7f8c8db8 100644 --- a/src/test/java/org/arquillian/tests/utilities/GitHubProjectVersionExtractor.java +++ b/src/test/java/org/arquillian/tests/utilities/GitHubProjectVersionExtractor.java @@ -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; @@ -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; @@ -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 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 getAuthorizationHeader() throws IOException { + Map 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; } } +