package com.natpryce.worktorule.issues.github;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.natpryce.worktorule.IssueTracker;
import com.scurrilous.uritemplate.URITemplate;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;

public class GitHubIssues implements IssueTracker {
    private static final URITemplate uriTemplate =
            new URITemplate("https://api.github.com/repos/{owner}/{repo}/issues/{issueId}");

    private final ObjectMapper objectMapper = new ObjectMapper();
    private final String owner;
    private final String repo;

    public GitHubIssues(String owner, String repo) {
        this.owner = owner;
        this.repo = repo;
    }

    @Override
    public boolean isOpen(String issueId) throws IOException {
        return Objects.equals(objectMapper.readTree(urlFor(issueId)).get("state").asText("unknown"), "open");
    }

    private URL urlFor(String issueId) {
        try {
            return uriTemplate.expand(ImmutableMap.<String,Object>of(
                    "owner", owner,
                    "repo", repo,
                    "issueId", issueId)).toURL();
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("invalid URL syntax", e);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("invalid URL syntax", e);
        }
    }
}

@@ -41,16 +41,64 @@
 ithub;%0A%0A
+import com.fasterxml.jackson.databind.JsonNode;%0A
 import c
@@ -296,19 +296,229 @@
 java
-.io.IOExcep
+x.activation.MimeType;%0Aimport javax.activation.MimeTypeParseException;%0Aimport java.io.BufferedInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.io.Reader;%0Aimport java.net.HttpURLConnec
 tion
@@ -633,22 +633,23 @@
 a.util.O
-bjects
+ptional
 ;%0A%0Apubli
@@ -1173,109 +1173,1434 @@
     
-return Objects.equals(objectMapper.readTree(urlFor(issueId)).get(%22state%22).asText(%22unknown%22), %22open%22);
+JsonNode jsonNode = getJsonFor(issueId);%0A        return jsonNode.get(%22state%22).asText(%22unknown%22).equals(%22open%22);%0A    %7D%0A%0A    private JsonNode getJsonFor(String issueId) throws IOException %7B%0A        URL issueUrl = urlFor(issueId);%0A        HttpURLConnection cx = (HttpURLConnection)issueUrl.openConnection();%0A        cx.setRequestProperty(%22Accept%22, %22application/vnd.github.v3+json%22);%0A%0A        int responseCode = cx.getResponseCode();%0A        if (responseCode %3E= 300) %7B%0A            throw new IOException(%22request to %22 + issueUrl + %22 failed with status %22 + responseCode);%0A        %7D%0A%0A        Reader r = new InputStreamReader(new BufferedInputStream(cx.getInputStream()), parseCharset(cx));%0A        try %7B%0A            return objectMapper.readTree(r);%0A        %7D%0A        finally %7B%0A            r.close();%0A        %7D%0A    %7D%0A%0A    private String parseCharset(HttpURLConnection cx) throws IOException %7B%0A        return parseCharset(parseContentType(cx));%0A    %7D%0A%0A    private String parseCharset(MimeType contentType) %7B%0A        return Optional.ofNullable(contentType.getParameter(%22charset%22)).orElse(%22utf-8%22);%0A    %7D%0A%0A    private MimeType parseContentType(HttpURLConnection cx) throws IOException %7B%0A        String contentType = cx.getContentType();%0A        try %7B%0A            return new MimeType(contentType);%0A        %7D catch (MimeTypeParseException e) %7B%0A            throw new IOException(%22failed to parse content type: %22 + contentType, e);%0A        %7D
 %0A   

Set correct Accept header for GitHub API, and use charset signalled in content type header when parsing the response