Add release-notes-ai module: JGit history, Conventional Commit grouping, guarded Spring AI highlights, PR descriptions and idempotent Gitea publishing on Spring AI 2.1.0
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7h4ZNF4BDUpJk3Zou
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
package com.ankurm.releasenotes;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* NOT Gitea. A small HTTP server that answers the seven calls GiteaClient makes, using the status codes from
|
||||
* Gitea 1.27.3's swagger document, and records every request. It lets the client's real HTTP code run
|
||||
* without writing to a real repository.
|
||||
*/
|
||||
final class FakeGitea implements AutoCloseable {
|
||||
|
||||
final List<String> log = new ArrayList<>();
|
||||
final Map<String, JsonNode> releases = new HashMap<>();
|
||||
final Map<String, JsonNode> pulls = new HashMap<>();
|
||||
private final JsonMapper json = JsonMapper.builder().build();
|
||||
private final HttpServer server;
|
||||
private int nextId = 100;
|
||||
|
||||
FakeGitea(String expectedToken) throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/api/v1/", ex -> {
|
||||
try {
|
||||
handle(ex, expectedToken);
|
||||
} catch (Exception e) {
|
||||
reply(ex, 500, "{\"message\":\"" + e + "\"}");
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
}
|
||||
|
||||
String apiBase() {
|
||||
return "http://127.0.0.1:" + server.getAddress().getPort() + "/api/v1";
|
||||
}
|
||||
|
||||
private void handle(HttpExchange ex, String token) throws IOException {
|
||||
String path = ex.getRequestURI().getRawPath().substring("/api/v1".length());
|
||||
String method = ex.getRequestMethod();
|
||||
String body = new String(ex.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
||||
String auth = ex.getRequestHeaders().getFirst("Authorization");
|
||||
if (!("token " + token).equals(auth)) {
|
||||
record(method, path, 401, null);
|
||||
reply(ex, 401, "{\"message\":\"token is required\"}");
|
||||
return;
|
||||
}
|
||||
String[] p = path.split("/"); // "", repos, owner, repo, kind, ...
|
||||
String kind = p.length > 4 ? p[4] : "";
|
||||
if (kind.equals("releases") && p.length == 7 && p[5].equals("tags") && method.equals("GET")) {
|
||||
JsonNode r = releases.get(p[6]);
|
||||
record(method, path, r == null ? 404 : 200, null);
|
||||
reply(ex, r == null ? 404 : 200, r == null ? "{\"message\":\"release does not exist\"}" : r.toString());
|
||||
} else if (kind.equals("releases") && p.length == 5 && method.equals("POST")) {
|
||||
JsonNode in = json.readTree(body);
|
||||
String tag = in.path("tag_name").asString();
|
||||
var out = json.createObjectNode().put("id", nextId++).put("tag_name", tag)
|
||||
.put("html_url", "http://fake/releases/tag/" + tag).put("body", in.path("body").asString())
|
||||
.put("draft", in.path("draft").asBoolean());
|
||||
releases.put(tag, out);
|
||||
record(method, path, 201, in);
|
||||
reply(ex, 201, out.toString());
|
||||
} else if (kind.equals("releases") && p.length == 6 && method.equals("PATCH")) {
|
||||
JsonNode in = json.readTree(body);
|
||||
JsonNode old = releases.values().stream().filter(r -> r.path("id").asString().equals(p[5])).findFirst().orElseThrow();
|
||||
var out = (tools.jackson.databind.node.ObjectNode) old;
|
||||
out.put("body", in.path("body").asString());
|
||||
record(method, path, 200, in);
|
||||
reply(ex, 200, out.toString());
|
||||
} else if (kind.equals("pulls") && p.length == 5 && method.equals("GET")) {
|
||||
String query = ex.getRequestURI().getRawQuery();
|
||||
boolean firstPage = query != null && query.contains("page=1");
|
||||
var list = json.createArrayNode();
|
||||
if (firstPage) pulls.values().forEach(list::add);
|
||||
record(method, path + "?" + query, 200, null);
|
||||
reply(ex, 200, list.toString());
|
||||
} else if (kind.equals("pulls") && p.length == 5 && method.equals("POST")) {
|
||||
JsonNode in = json.readTree(body);
|
||||
var out = json.createObjectNode().put("number", nextId++ - 90).put("title", in.path("title").asString())
|
||||
.put("body", in.path("body").asString()).put("html_url", "http://fake/pulls/" + (nextId - 91));
|
||||
out.putObject("base").put("ref", in.path("base").asString());
|
||||
out.putObject("head").put("ref", in.path("head").asString());
|
||||
pulls.put(in.path("base").asString() + "..." + in.path("head").asString(), out);
|
||||
record(method, path, 201, in);
|
||||
reply(ex, 201, out.toString());
|
||||
} else if (kind.equals("pulls") && p.length == 6 && method.equals("PATCH")) {
|
||||
JsonNode in = json.readTree(body);
|
||||
var out = (tools.jackson.databind.node.ObjectNode) pulls.values().stream()
|
||||
.filter(r -> r.path("number").asString().equals(p[5])).findFirst().orElseThrow();
|
||||
out.put("title", in.path("title").asString()).put("body", in.path("body").asString());
|
||||
record(method, path, 201, in);
|
||||
reply(ex, 201, out.toString());
|
||||
} else {
|
||||
record(method, path, 404, null);
|
||||
reply(ex, 404, "{\"message\":\"not found\"}");
|
||||
}
|
||||
}
|
||||
|
||||
private void record(String method, String path, int status, JsonNode in) {
|
||||
String fields = in == null ? "" : " fields=" + fieldNames(in);
|
||||
log.add(method + " " + path + " -> " + status + fields);
|
||||
}
|
||||
|
||||
private static List<String> fieldNames(JsonNode n) {
|
||||
List<String> names = new ArrayList<>();
|
||||
n.propertyNames().forEach(names::add);
|
||||
return names;
|
||||
}
|
||||
|
||||
private void reply(HttpExchange ex, int status, String body) throws IOException {
|
||||
byte[] b = body.getBytes(StandardCharsets.UTF_8);
|
||||
ex.getResponseHeaders().add("Content-Type", "application/json");
|
||||
ex.sendResponseHeaders(status, b.length);
|
||||
ex.getResponseBody().write(b);
|
||||
ex.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user