Github: use GitHub-generated html markup

* allow custom headers in PagedRequest
This commit is contained in:
Aleksey Pivovarov
2013-08-05 15:52:19 +04:00
parent 7b469c61e1
commit f2b7a90c45
7 changed files with 49 additions and 43 deletions
@@ -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<Header> 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<Header> headers,
@NotNull Collection<Header> 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<Header> headers,
@NotNull final Collection<Header> 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<T> {
@Nullable private String myNextPage;
@NotNull private final Collection<Header> myHeaders;
@NotNull private final Class<T> myResult;
@NotNull private final Class<? extends DataConstructor[]> myRawArray;
@SuppressWarnings("NullableProblems")
public PagedRequest(@NotNull String path, @NotNull Class<T> result, @NotNull Class<? extends DataConstructor[]> rawArray) {
public PagedRequest(@NotNull String path,
@NotNull Class<T> result,
@NotNull Class<? extends DataConstructor[]> 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<GithubIssueComment> request =
new PagedRequest<GithubIssueComment>(path, GithubIssueComment.class, GithubIssueCommentRaw[].class);
new PagedRequest<GithubIssueComment>(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<GithubPullRequest> request =
new PagedRequest<GithubPullRequest>(path, GithubPullRequest.class, GithubPullRequestRaw[].class);
new PagedRequest<GithubPullRequest>(path, GithubPullRequest.class, GithubPullRequestRaw[].class, ACCEPT_HTML_BODY_MARKUP);
return request.getAll(auth);
}
@@ -584,7 +589,7 @@ public class GithubApiUtil {
public static PagedRequest<GithubPullRequest> getPullRequests(@NotNull String user, @NotNull String repo) {
String path = "/repos/" + user + "/" + repo + "/pulls?per_page=100";
return new PagedRequest<GithubPullRequest>(path, GithubPullRequest.class, GithubPullRequestRaw[].class);
return new PagedRequest<GithubPullRequest>(path, GithubPullRequest.class, GithubPullRequestRaw[].class, ACCEPT_HTML_BODY_MARKUP);
}
@NotNull
@@ -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
@@ -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")
@@ -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
@@ -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());
}
@@ -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("</td></tr></table>");
builder.append(new MarkdownProcessor().markdown(getText())).append("<br>");
builder.append(getText()).append("<br>");
}
}
@@ -193,7 +193,7 @@ public class GithubRepository extends BaseRepositoryImpl {
return ContainerUtil.map2Array(result, Comment.class, new Function<GithubIssueComment, Comment>() {
@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());
}
});