diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java index 554216db2743..60ebc3ec3b4b 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java @@ -36,6 +36,7 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.NoSuchElementException; /** * @author Kirill Likhodedov @@ -76,34 +77,6 @@ public class GithubApiUtil { return request(auth, path, null, HttpVerb.GET).getJsonElement(); } - @Nullable - private static ResponsePage getPagedRequest(@NotNull GithubAuthData auth, @NotNull String path) throws IOException { - return request(auth, path, null, HttpVerb.GET); - } - - @Nullable - private static JsonArray getAllRequest(@NotNull GithubAuthData auth, @NotNull String path) throws IOException { - JsonArray ret = new JsonArray(); - String next = path; - - while (next != null) { - ResponsePage response = request(auth, next, null, HttpVerb.GET); - - if (response.getJsonElement() == null) { - return null; - } - - if (!response.getJsonElement().isJsonArray()) { - throw new JsonException("Wrong json type: expected JsonArray"); - } - - next = response.getNextPage(); - ret.addAll(response.getJsonElement().getAsJsonArray()); - } - - return ret; - } - @NotNull private static ResponsePage request(@NotNull GithubAuthData auth, @NotNull String path, @@ -295,8 +268,8 @@ public class GithubApiUtil { } } - /* - * Github API + /* + * Json API */ static Result createDataFromRaw(@NotNull Raw rawObject, @NotNull Class resultClass) @@ -309,29 +282,58 @@ public class GithubApiUtil { } } - @NotNull - public static Collection getTokenScopes(@NotNull GithubAuthData auth) throws IOException { - HttpMethod method = null; - try { - method = doREST(auth, "", null, HttpVerb.HEAD); + static class PagedRequest { + @NotNull private GithubAuthData myAuth; + @Nullable private String myNextPage; + @NotNull private Class myResult; + @NotNull private Class myRawArray; - checkStatusCode(method); - - Header header = method.getResponseHeader("X-OAuth-Scopes"); - if (header == null) { - throw new HttpException("No scopes header"); - } - - Collection scopes = new ArrayList(); - for (HeaderElement elem : header.getElements()) { - scopes.add(elem.getName()); - } - return scopes; + public PagedRequest(@NotNull GithubAuthData auth, @NotNull String path, @NotNull Class result, @NotNull Class rawArray) { + myAuth = auth; + myNextPage = path; + myResult = result; + myRawArray = rawArray; } - finally { - if (method != null) { - method.releaseConnection(); + + @NotNull + public List next() throws IOException { + if (myNextPage == null) { + throw new NoSuchElementException(); } + + String page = myNextPage; + myNextPage = null; + + ResponsePage response = request(myAuth, page, null, HttpVerb.GET); + + if (response.getJsonElement() == null) { + throw new HttpException("Empty response"); + } + + if (!response.getJsonElement().isJsonArray()) { + throw new JsonException("Wrong json type: expected JsonArray"); + } + + myNextPage = response.getNextPage(); + + List result = new ArrayList(); + for (R raw : fromJson(response.getJsonElement().getAsJsonArray(), myRawArray)) { + result.add(createDataFromRaw(raw, myResult)); + } + return result; + } + + public boolean hasNext() { + return myNextPage != null; + } + + @NotNull + public List getAll() throws IOException { + List result = new ArrayList(); + while (hasNext()) { + result.addAll(next()); + } + return result; } } @@ -359,6 +361,36 @@ public class GithubApiUtil { return res; } + /* + * Github API + */ + + @NotNull + public static Collection getTokenScopes(@NotNull GithubAuthData auth) throws IOException { + HttpMethod method = null; + try { + method = doREST(auth, "", null, HttpVerb.HEAD); + + checkStatusCode(method); + + Header header = method.getResponseHeader("X-OAuth-Scopes"); + if (header == null) { + throw new HttpException("No scopes header"); + } + + Collection scopes = new ArrayList(); + for (HeaderElement elem : header.getElements()) { + scopes.add(elem.getName()); + } + return scopes; + } + finally { + if (method != null) { + method.releaseConnection(); + } + } + } + @NotNull public static String getScopedToken(@NotNull GithubAuthData auth, @NotNull Collection scopes, @Nullable String note) throws IOException { @@ -389,16 +421,13 @@ public class GithubApiUtil { @NotNull private static List doGetAvailableRepos(@NotNull GithubAuthData auth, @Nullable String user) throws IOException { - String request = user == null ? "/user/repos" : "/users/" + user + "/repos?per_page=100"; - JsonElement result = getAllRequest(auth, request); + String path = user == null ? "/user/repos" : "/users/" + user + "/repos?per_page=100"; - GithubRepoRaw[] rawRepos = fromJson(result, GithubRepoRaw[].class); - List repos = new ArrayList(); - for (GithubRepoRaw raw : rawRepos) { - repos.add(createDataFromRaw(raw, GithubRepo.class)); - } - return repos; + PagedRequest request = + new PagedRequest(auth, path, GithubRepo.class, GithubRepoRaw[].class); + + return request.getAll(); } @NotNull @@ -464,15 +493,10 @@ public class GithubApiUtil { path = "/repos/" + user + "/" + repo + "/issues?per_page=100"; } - JsonElement result = getAllRequest(auth, path); + PagedRequest request = + new PagedRequest(auth, path, GithubIssue.class, GithubIssueRaw[].class); - GithubIssueRaw[] rawIssues = fromJson(result, GithubIssueRaw[].class); - - List issues = new ArrayList(); - for (GithubIssueRaw raw : rawIssues) { - issues.add(createDataFromRaw(raw, GithubIssue.class)); - } - return issues; + return request.getAll(); } @NotNull @@ -490,15 +514,10 @@ public class GithubApiUtil { throws IOException { String path = "/repos/" + user + "/" + repo + "/issues/" + id + "/comments?per_page=100"; - JsonElement result = getAllRequest(auth, path); + PagedRequest request = + new PagedRequest(auth, path, GithubIssueComment.class, GithubIssueCommentRaw[].class); - GithubIssueCommentRaw[] rawComments = fromJson(result, GithubIssueCommentRaw[].class); - - List comments = new ArrayList(); - for (GithubIssueCommentRaw raw : rawComments) { - comments.add(createDataFromRaw(raw, GithubIssueComment.class)); - } - return comments; + return request.getAll(); } @NotNull @@ -524,15 +543,10 @@ public class GithubApiUtil { throws IOException { String path = "/repos/" + user + "/" + repo + "/pulls?per_page=100"; - JsonElement result = getAllRequest(auth, path); + PagedRequest request = + new PagedRequest(auth, path, GithubPullRequest.class, GithubPullRequestRaw[].class); - GithubPullRequestRaw[] rawRequests = fromJson(result, GithubPullRequestRaw[].class); - - List requests = new ArrayList(); - for (GithubPullRequestRaw raw : rawRequests) { - requests.add(createDataFromRaw(raw, GithubPullRequest.class)); - } - return requests; + return request.getAll(); } @NotNull @@ -540,15 +554,10 @@ public class GithubApiUtil { throws IOException { String path = "/repos/" + user + "/" + repo + "/pulls/" + id + "/commits?per_page=100"; - JsonElement result = getAllRequest(auth, path); + PagedRequest request = + new PagedRequest(auth, path, GithubCommit.class, GithubCommitRaw[].class); - GithubCommitRaw[] rawCommits = fromJson(result, GithubCommitRaw[].class); - - List commits = new ArrayList(); - for (GithubCommitRaw raw : rawCommits) { - commits.add(createDataFromRaw(raw, GithubCommit.class)); - } - return commits; + return request.getAll(); } @NotNull @@ -556,14 +565,9 @@ public class GithubApiUtil { throws IOException { String path = "/repos/" + user + "/" + repo + "/pulls/" + id + "/files?per_page=100"; - JsonElement result = getAllRequest(auth, path); + PagedRequest request = + new PagedRequest(auth, path, GithubFile.class, GithubFileRaw[].class); - GithubFileRaw[] rawFiles = fromJson(result, GithubFileRaw[].class); - - List files = new ArrayList(); - for (GithubFileRaw raw : rawFiles) { - files.add(createDataFromRaw(raw, GithubFile.class)); - } - return files; + return request.getAll(); } }