diff --git a/pom.xml b/pom.xml index 57543a0..299b1d0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.tulis TuliErrorer - 1.0-SNAPSHOT + 1.1 21 @@ -31,4 +31,18 @@ https://git.tulisiowice.top/api/packages/Tulis/maven + + + + com.fasterxml.jackson.core + jackson-core + 2.19.2 + + + + com.fasterxml.jackson.core + jackson-databind + 2.19.2 + + \ No newline at end of file diff --git a/src/main/java/dev/tulis/errorer/Errorer.java b/src/main/java/dev/tulis/errorer/Errorer.java index f70e251..284cd2c 100644 --- a/src/main/java/dev/tulis/errorer/Errorer.java +++ b/src/main/java/dev/tulis/errorer/Errorer.java @@ -1,60 +1,80 @@ package dev.tulis.errorer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URI; +import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.Map; public class Errorer { String repoPath; String token; int labelId = 1; + int duplicateId = 2; + String assignee = "Tulis"; public Errorer(String repoPath, String token) { this.repoPath = repoPath; this.token = token; } - public Errorer(String repoPath, String token, int labelId) { - this.repoPath = repoPath; - this.token = token; - this.labelId = labelId; - } - public void reportException(Exception e, Object o, String... additionalData) { String stackTrace = getErrorStackTrace(e); String className = o.getClass().getName(); + String errorText = e.getClass().getName() + " in " + className; ZonedDateTime warsawTime = ZonedDateTime.now(ZoneId.of("Europe/Warsaw")); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); String formattedTime = warsawTime.format(formatter); - StringBuilder description = new StringBuilder("### Error stackTrace: \n```\n"); - description.append(stackTrace); - description.append("```\n\n"); - for(String str : additionalData) { - description.append(str); - if(!additionalData[additionalData.length - 1].equals(str)) description.append(" "); - } - if(additionalData.length != 0) description.append("\n"); - description.append("Automatically reported by TuliErrorer"); + StringBuilder description = getDescription(e, additionalData, stackTrace); try(HttpClient client = HttpClient.newHttpClient()) { + boolean isDuplicate = false; + + { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(String.format("https://git.tulisiowice.top/api/v1/repos/%s/issues?q=%s", repoPath, URLEncoder.encode(errorText, StandardCharsets.UTF_8)))) + .header("Content-Type", "application/json") + .header("Authorization", "token " + token) + .GET() + .build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + String str = response.body(); + + ObjectMapper mapper = new ObjectMapper(); + + JsonNode rootNode = mapper.readTree(str); + JsonNode isDuplicateNode = rootNode.at("/0"); + if (!isDuplicateNode.isMissingNode()) isDuplicate = true; + } + String json = String.format(""" { "title": "%s", "body": "%s", - "assignees": ["Tulis"], - "labels": [1] + "assignees": ["%s"], + "labels": [%s] } - """, e.getMessage() + " in " + className + " at " + formattedTime, description); + """, + String.format("%s %s", errorText, formattedTime), + description, + assignee, + (isDuplicate) ? labelId + ", " + duplicateId : labelId); + HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://git.tulisiowice.top/api/v1/repos/" + repoPath + "/issues")) .header("Content-Type", "application/json") @@ -68,6 +88,21 @@ public class Errorer { } } + private static StringBuilder getDescription(Exception e, String[] additionalData, String stackTrace) { + StringBuilder description = new StringBuilder(String.format("###%s Error stackTrace: \n```\n", (e.getMessage() == null) ? "" : String.format(" [%s]", e.getMessage()))); + description.append(stackTrace); + description.append("```\n\n"); + + for(String str : additionalData) { + description.append(str); + if(!additionalData[additionalData.length - 1].equals(str)) description.append(" "); + } + + if(additionalData.length != 0) description.append("\n"); + description.append("Automatically reported by TuliErrorer"); + return description; + } + private String getErrorStackTrace(Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); @@ -76,6 +111,22 @@ public class Errorer { return sw.toString(); } + public String getAssignee() { + return assignee; + } + + public void setAssignee(String assignee) { + this.assignee = assignee; + } + + public int getDuplicateId() { + return duplicateId; + } + + public void setDuplicateId(int duplicateId) { + this.duplicateId = duplicateId; + } + public String getRepoPath() { return repoPath; }