From f2b7a90c455083b96bf6ae411bae55998e5069c2 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 5 Aug 2013 14:45:44 +0400 Subject: [PATCH] Github: use GitHub-generated html markup * allow custom headers in PagedRequest --- .../plugins/github/api/GithubApiUtil.java | 61 ++++++++++--------- .../github/api/GithubIssueComment.java | 10 +-- .../github/api/GithubIssueCommentRaw.java | 3 +- .../plugins/github/api/GithubPullRequest.java | 10 +-- .../github/api/GithubPullRequestRaw.java | 3 +- .../plugins/github/tasks/GithubComment.java | 3 +- .../github/tasks/GithubRepository.java | 2 +- 7 files changed, 49 insertions(+), 43 deletions(-) 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 900ecc5edeac..cb5ba3bac36f 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java @@ -45,6 +45,9 @@ public class GithubApiUtil { private static final int CONNECTION_TIMEOUT = 5000; private static final Logger LOG = GithubUtil.LOG; + private static final Header ACCEPT_HTML_BODY_MARKUP = new Header("Accept", "application/vnd.github.v3.html+json"); + private static final Header ACCEPT_NEW_SEARCH_API = new Header("Accept", "application/vnd.github.preview"); + @NotNull private static final Gson gson = initGson(); private static Gson initGson() { @@ -59,32 +62,29 @@ public class GithubApiUtil { } @Nullable - private static JsonElement postRequest(@NotNull GithubAuthData auth, @NotNull String path, @Nullable String requestBody) + private static JsonElement postRequest(@NotNull GithubAuthData auth, + @NotNull String path, + @Nullable String requestBody, + @NotNull Header... headers) throws IOException { + return request(auth, path, requestBody, Arrays.asList(headers), HttpVerb.POST).getJsonElement(); + } + + @Nullable + private static JsonElement deleteRequest(@NotNull GithubAuthData auth, @NotNull String path, @NotNull Header... headers) throws IOException { - return request(auth, path, requestBody, null, HttpVerb.POST).getJsonElement(); + return request(auth, path, null, Arrays.asList(headers), HttpVerb.DELETE).getJsonElement(); } @Nullable - private static JsonElement deleteRequest(@NotNull GithubAuthData auth, @NotNull String path) throws IOException { - return request(auth, path, null, null, HttpVerb.DELETE).getJsonElement(); - } - - @Nullable - private static JsonElement getRequest(@NotNull GithubAuthData auth, @NotNull String path) throws IOException { - return request(auth, path, null, null, HttpVerb.GET).getJsonElement(); - } - - @Nullable - private static JsonElement getRequest(@NotNull GithubAuthData auth, @NotNull String path, @Nullable Collection
headers) - throws IOException { - return request(auth, path, null, headers, HttpVerb.GET).getJsonElement(); + private static JsonElement getRequest(@NotNull GithubAuthData auth, @NotNull String path, @NotNull Header... headers) throws IOException { + return request(auth, path, null, Arrays.asList(headers), HttpVerb.GET).getJsonElement(); } @NotNull private static ResponsePage request(@NotNull GithubAuthData auth, @NotNull String path, @Nullable String requestBody, - @Nullable Collection
headers, + @NotNull Collection
headers, @NotNull HttpVerb verb) throws IOException { HttpMethod method = null; try { @@ -128,7 +128,7 @@ public class GithubApiUtil { private static HttpMethod doREST(@NotNull final GithubAuthData auth, @NotNull String path, @Nullable final String requestBody, - @Nullable final Collection
headers, + @NotNull final Collection
headers, @NotNull final HttpVerb verb) throws IOException { HttpClient client = getHttpClient(auth.getBasicAuth()); String uri = GithubUrlUtil.getApiUrl(auth.getHost()) + path; @@ -160,10 +160,8 @@ public class GithubApiUtil { if (tokenAuth != null) { method.addRequestHeader("Authorization", "token " + tokenAuth.getToken()); } - if (headers != null) { - for (Header header : headers) { - method.addRequestHeader(header); - } + for (Header header : headers) { + method.addRequestHeader(header); } return method; } @@ -286,14 +284,19 @@ public class GithubApiUtil { public static class PagedRequest { @Nullable private String myNextPage; + @NotNull private final Collection
myHeaders; @NotNull private final Class myResult; @NotNull private final Class myRawArray; @SuppressWarnings("NullableProblems") - public PagedRequest(@NotNull String path, @NotNull Class result, @NotNull Class rawArray) { + public PagedRequest(@NotNull String path, + @NotNull Class result, + @NotNull Class rawArray, + @NotNull Header... headers) { myNextPage = path; myResult = result; myRawArray = rawArray; + myHeaders = Arrays.asList(headers); } @NotNull @@ -305,7 +308,7 @@ public class GithubApiUtil { String page = myNextPage; myNextPage = null; - ResponsePage response = request(auth, page, null, null, HttpVerb.GET); + ResponsePage response = request(auth, page, null, myHeaders, HttpVerb.GET); if (response.getJsonElement() == null) { throw new HttpException("Empty response"); @@ -525,7 +528,8 @@ public class GithubApiUtil { String path = "/search/issues?q=" + query; //TODO: remove header after end of preview period. ~ october 2013 - JsonElement result = getRequest(auth, path, Collections.singletonList(new Header("Accept", "application/vnd.github.preview"))); + //TODO: Use bodyHtml for issues - preview does not support this feature + JsonElement result = getRequest(auth, path, ACCEPT_NEW_SEARCH_API); return createDataFromRaw(fromJson(result, GithubIssuesSearchResultRaw.class), GithubIssuesSearchResult.class).getIssues(); } @@ -546,7 +550,7 @@ public class GithubApiUtil { String path = "/repos/" + user + "/" + repo + "/issues/" + id + "/comments?per_page=100"; PagedRequest request = - new PagedRequest(path, GithubIssueComment.class, GithubIssueCommentRaw[].class); + new PagedRequest(path, GithubIssueComment.class, GithubIssueCommentRaw[].class, ACCEPT_HTML_BODY_MARKUP); return request.getAll(auth); } @@ -566,7 +570,8 @@ public class GithubApiUtil { public static GithubPullRequest getPullRequest(@NotNull GithubAuthData auth, @NotNull String user, @NotNull String repo, int id) throws IOException { String path = "/repos/" + user + "/" + repo + "/pulls/" + id; - return createDataFromRaw(fromJson(getRequest(auth, path), GithubPullRequestRaw.class), GithubPullRequest.class); + return createDataFromRaw(fromJson(getRequest(auth, path, ACCEPT_HTML_BODY_MARKUP), GithubPullRequestRaw.class), + GithubPullRequest.class); } @NotNull @@ -575,7 +580,7 @@ public class GithubApiUtil { String path = "/repos/" + user + "/" + repo + "/pulls?per_page=100"; PagedRequest request = - new PagedRequest(path, GithubPullRequest.class, GithubPullRequestRaw[].class); + new PagedRequest(path, GithubPullRequest.class, GithubPullRequestRaw[].class, ACCEPT_HTML_BODY_MARKUP); return request.getAll(auth); } @@ -584,7 +589,7 @@ public class GithubApiUtil { public static PagedRequest getPullRequests(@NotNull String user, @NotNull String repo) { String path = "/repos/" + user + "/" + repo + "/pulls?per_page=100"; - return new PagedRequest(path, GithubPullRequest.class, GithubPullRequestRaw[].class); + return new PagedRequest(path, GithubPullRequest.class, GithubPullRequestRaw[].class, ACCEPT_HTML_BODY_MARKUP); } @NotNull diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueComment.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueComment.java index 0e005c3a9d86..5faf9a416585 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueComment.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueComment.java @@ -26,7 +26,7 @@ public class GithubIssueComment { private long myId; @NotNull private String myHtmlUrl; - @NotNull private String myBody; + @NotNull private String myBodyHtml; @NotNull private Date myCreatedAt; @NotNull private Date myUpdatedAt; @@ -35,13 +35,13 @@ public class GithubIssueComment { public GithubIssueComment(long id, @NotNull String htmlUrl, - @NotNull String body, + @NotNull String bodyHtml, @NotNull Date createdAt, @NotNull Date updatedAt, @NotNull GithubUser user) { myId = id; myHtmlUrl = htmlUrl; - myBody = body; + myBodyHtml = bodyHtml; myCreatedAt = createdAt; myUpdatedAt = updatedAt; myUser = user; @@ -57,8 +57,8 @@ public class GithubIssueComment { } @NotNull - public String getBody() { - return myBody; + public String getBodyHtml() { + return myBodyHtml; } @NotNull diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueCommentRaw.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueCommentRaw.java index 9bef7d7d174f..c8ef08a2b632 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueCommentRaw.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubIssueCommentRaw.java @@ -30,6 +30,7 @@ class GithubIssueCommentRaw implements DataConstructor { @Nullable public String url; @Nullable public String htmlUrl; @Nullable public String body; + @Nullable public String bodyHtml; @Nullable public Date createdAt; @Nullable public Date updatedAt; @@ -39,7 +40,7 @@ class GithubIssueCommentRaw implements DataConstructor { @SuppressWarnings("ConstantConditions") @NotNull public GithubIssueComment createIssueComment() { - return new GithubIssueComment(id, htmlUrl, body, createdAt, updatedAt, user.createUser()); + return new GithubIssueComment(id, htmlUrl, bodyHtml, createdAt, updatedAt, user.createUser()); } @SuppressWarnings("unchecked") diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequest.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequest.java index 7abcb10eaab3..1a8facb768d6 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequest.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequest.java @@ -29,7 +29,7 @@ public class GithubPullRequest { private long myNumber; @NotNull private String myState; @NotNull private String myTitle; - @NotNull private String myBody; + @NotNull private String myBodyHtml; @NotNull private String myHtmlUrl; @NotNull private String myDiffUrl; @@ -91,7 +91,7 @@ public class GithubPullRequest { public GithubPullRequest(long number, @NotNull String state, @NotNull String title, - @Nullable String body, + @Nullable String bodyHtml, @NotNull String htmlUrl, @NotNull String diffUrl, @NotNull String patchUrl, @@ -106,7 +106,7 @@ public class GithubPullRequest { myNumber = number; myState = state; myTitle = title; - myBody = StringUtil.notNullize(body); + myBodyHtml = StringUtil.notNullize(bodyHtml); myHtmlUrl = htmlUrl; myDiffUrl = diffUrl; myPatchUrl = patchUrl; @@ -135,8 +135,8 @@ public class GithubPullRequest { } @NotNull - public String getBody() { - return myBody; + public String getBodyHtml() { + return myBodyHtml; } @NotNull diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequestRaw.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequestRaw.java index 5f81f66ecd7b..069688be5090 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequestRaw.java +++ b/plugins/github/src/org/jetbrains/plugins/github/api/GithubPullRequestRaw.java @@ -29,6 +29,7 @@ class GithubPullRequestRaw implements DataConstructor { @Nullable public String state; @Nullable public String title; @Nullable public String body; + @Nullable public String bodyHtml; @Nullable public String url; @Nullable public String htmlUrl; @@ -71,7 +72,7 @@ class GithubPullRequestRaw implements DataConstructor { @NotNull public GithubPullRequest createPullRequest() { - return new GithubPullRequest(number, state, title, body, htmlUrl, diffUrl, patchUrl, issueUrl, createdAt, updatedAt, closedAt, mergedAt, + return new GithubPullRequest(number, state, title, bodyHtml, htmlUrl, diffUrl, patchUrl, issueUrl, createdAt, updatedAt, closedAt, mergedAt, user.createUser(), head.create(), base.create()); } diff --git a/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubComment.java b/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubComment.java index 9c1ac8c891d1..d65a95044a9c 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubComment.java +++ b/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubComment.java @@ -17,7 +17,6 @@ package org.jetbrains.plugins.github.tasks; import com.intellij.tasks.impl.SimpleComment; import com.intellij.util.text.DateFormatUtil; -import com.petebevin.markdown.MarkdownProcessor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,6 +55,6 @@ public class GithubComment extends SimpleComment { } builder.append(""); - builder.append(new MarkdownProcessor().markdown(getText())).append("
"); + builder.append(getText()).append("
"); } } diff --git a/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepository.java b/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepository.java index e5c8e66e75c9..d2d8700c00fd 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepository.java +++ b/plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepository.java @@ -193,7 +193,7 @@ public class GithubRepository extends BaseRepositoryImpl { return ContainerUtil.map2Array(result, Comment.class, new Function() { @Override public Comment fun(GithubIssueComment comment) { - return new GithubComment(comment.getCreatedAt(), comment.getUser().getLogin(), comment.getBody(), comment.getUser().getGravatarId(), + return new GithubComment(comment.getCreatedAt(), comment.getUser().getLogin(), comment.getBodyHtml(), comment.getUser().getGravatarId(), comment.getUser().getHtmlUrl()); } });