|
| 1 | +package dev.robocode.rumble.client; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import com.google.gson.JsonParser; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.http.HttpClient; |
| 11 | +import java.net.http.HttpRequest; |
| 12 | +import java.net.http.HttpResponse; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.UUID; |
| 18 | + |
| 19 | +/** GitHub REST adapter limited to creating result issues and reading their receipt comments. */ |
| 20 | +final class GitHubIssueOpsTransport implements IssueOpsTransport { |
| 21 | + private static final URI API_ROOT = URI.create("https://api.github.com/"); |
| 22 | + private static final String API_VERSION = "2026-03-10"; |
| 23 | + |
| 24 | + private final HttpClient client; |
| 25 | + private final String token; |
| 26 | + |
| 27 | + GitHubIssueOpsTransport(final String token) { |
| 28 | + this(HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20)).build(), token); |
| 29 | + } |
| 30 | + |
| 31 | + GitHubIssueOpsTransport(final HttpClient client, final String token) { |
| 32 | + if (token == null || token.isBlank()) { |
| 33 | + throw new IllegalArgumentException("RUMBLE_CLIENT_TOKEN must contain an Issues-only GitHub token"); |
| 34 | + } |
| 35 | + this.client = client; |
| 36 | + this.token = token; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public SubmittedBatch createIssue(final URI repository, final String body, final String title) throws IOException { |
| 41 | + final JsonObject request = new JsonObject(); |
| 42 | + request.addProperty("title", title); |
| 43 | + request.addProperty("body", body); |
| 44 | + final JsonArray labels = new JsonArray(); |
| 45 | + labels.add("result-submission"); |
| 46 | + request.add("labels", labels); |
| 47 | + final JsonObject response = request("POST", endpoint(repository, "issues"), request.toString()); |
| 48 | + final int issueNumber = response.get("number").getAsInt(); |
| 49 | + final String issueUrl = response.get("html_url").getAsString(); |
| 50 | + return new SubmittedBatch(issueNumber, issueUrl, battleIds(body)); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<SubmissionReceipt> receipts(final URI repository, final SubmittedBatch batch) throws IOException { |
| 55 | + final JsonArray comments = request("GET", endpoint(repository, "issues/" + batch.issueNumber() |
| 56 | + + "/comments?per_page=100"), null).getAsJsonArray("comments"); |
| 57 | + final List<SubmissionReceipt> receipts = new ArrayList<>(); |
| 58 | + for (final JsonElement comment : comments) { |
| 59 | + final JsonElement body = comment.getAsJsonObject().get("body"); |
| 60 | + if (body != null && body.isJsonPrimitive()) { |
| 61 | + receipts.addAll(receipts(body.getAsString(), batch.issueUrl())); |
| 62 | + } |
| 63 | + } |
| 64 | + return receipts; |
| 65 | + } |
| 66 | + |
| 67 | + private JsonObject request(final String method, final URI endpoint, final String body) throws IOException { |
| 68 | + final HttpRequest.Builder request = HttpRequest.newBuilder(endpoint).timeout(Duration.ofSeconds(30)) |
| 69 | + .header("Accept", "application/vnd.github+json") |
| 70 | + .header("Authorization", "Bearer " + token) |
| 71 | + .header("X-GitHub-Api-Version", API_VERSION); |
| 72 | + if (body == null) { |
| 73 | + request.GET(); |
| 74 | + } else { |
| 75 | + request.header("Content-Type", "application/json") |
| 76 | + .method(method, HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)); |
| 77 | + } |
| 78 | + try { |
| 79 | + final HttpResponse<String> response = client.send(request.build(), HttpResponse.BodyHandlers.ofString()); |
| 80 | + if (response.statusCode() < 200 || response.statusCode() >= 300) { |
| 81 | + throw new IOException("GitHub Issues API returned HTTP " + response.statusCode()); |
| 82 | + } |
| 83 | + final JsonElement parsed = JsonParser.parseString(response.body()); |
| 84 | + if (parsed.isJsonObject()) { |
| 85 | + return parsed.getAsJsonObject(); |
| 86 | + } |
| 87 | + final JsonObject wrapper = new JsonObject(); |
| 88 | + wrapper.add("comments", parsed.getAsJsonArray()); |
| 89 | + return wrapper; |
| 90 | + } catch (InterruptedException exception) { |
| 91 | + Thread.currentThread().interrupt(); |
| 92 | + throw new IOException("Interrupted while calling the GitHub Issues API", exception); |
| 93 | + } catch (RuntimeException exception) { |
| 94 | + throw new IOException("GitHub Issues API returned invalid JSON", exception); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static URI endpoint(final URI repository, final String suffix) { |
| 99 | + if (!"github.com".equalsIgnoreCase(repository.getHost())) { |
| 100 | + throw new IllegalArgumentException("Issues-only submission requires a github.com canonical repository"); |
| 101 | + } |
| 102 | + final String[] segments = repository.getPath().replaceFirst("/$", "").replaceFirst("\\.git$", "") |
| 103 | + .split("/"); |
| 104 | + if (segments.length != 3 || segments[1].isBlank() || segments[2].isBlank()) { |
| 105 | + throw new IllegalArgumentException("Canonical repository must identify a GitHub owner and repository"); |
| 106 | + } |
| 107 | + return API_ROOT.resolve("repos/" + segments[1] + "/" + segments[2] + "/" + suffix); |
| 108 | + } |
| 109 | + |
| 110 | + private static List<UUID> battleIds(final String body) { |
| 111 | + final int begin = body.indexOf('{'); |
| 112 | + final int end = body.lastIndexOf('}'); |
| 113 | + final JsonArray results = JsonParser.parseString(body.substring(begin, end + 1)).getAsJsonObject() |
| 114 | + .getAsJsonArray("results"); |
| 115 | + return results.asList().stream().map(result -> UUID.fromString(result.getAsJsonObject() |
| 116 | + .get("battleId").getAsString())).toList(); |
| 117 | + } |
| 118 | + |
| 119 | + private static List<SubmissionReceipt> receipts(final String comment, final String issueUrl) { |
| 120 | + return comment.lines().map(String::trim).filter(line -> line.endsWith(": accepted")) |
| 121 | + .map(line -> line.substring(0, line.length() - ": accepted".length())) |
| 122 | + .flatMap(value -> { |
| 123 | + try { |
| 124 | + return java.util.stream.Stream.of(new SubmissionReceipt(UUID.fromString(value), issueUrl)); |
| 125 | + } catch (IllegalArgumentException exception) { |
| 126 | + return java.util.stream.Stream.empty(); |
| 127 | + } |
| 128 | + }).toList(); |
| 129 | + } |
| 130 | +} |
0 commit comments