From ca91d2e56e4b4b7bd5c4fbd1138eac5f520f55c4 Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Tue, 2 Jul 2024 14:16:17 -0400 Subject: [PATCH 1/7] Debug logging for transitions --- .../oicr/gsi/shesmu/jira/JiraConnection.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java index cf4f5debf..54c58af90 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java @@ -381,6 +381,15 @@ boolean transition( Issue issue, BiFunction, Predicate, Boolean> matcher, String comment) throws URISyntaxException, IOException, InterruptedException { IssueAction.issueUpdates.labels(url, projectKey).inc(); + ((Definer) definer) + .log( + new StringBuilder("Attempting to transition issue ") + .append(issue.getKey()) + .append(" with comment ") + .append(comment) + .toString(), + LogLevel.DEBUG, + null); final var builder = HttpRequest.newBuilder( new URI( @@ -395,8 +404,28 @@ boolean transition( .body() .get() .transitions(); + ((Definer) definer) + .log( + new StringBuilder("Transitions available to ") + .append(issue.getKey()) + .append(" are ") + .append(transitions) + .toString(), + LogLevel.DEBUG, + null); for (final var transition : transitions) { + ((Definer) definer) + .log( + new StringBuilder("Attempting to apply transition ") + .append(transition) + .append(" to issue ") + .append(issue.getKey()) + .append(" by matching against ") + .append(closedStatuses()) + .toString(), + LogLevel.DEBUG, + null); if (matcher.apply(closedStatuses(), transition.to().name()::equalsIgnoreCase)) { final var request = new TransitionRequest(); /** "fields": { "assignee": { "name": "Will" }, "resolution": { "name": "Fixed" } } */ @@ -410,7 +439,18 @@ boolean transition( } } request.setTransition(transition); - + ((Definer) definer) + .log( + new StringBuilder("Sending transition request ") + .append(request) + .append(" to ") + .append( + String.format( + "%s/rest/api/%s/issue/%s/transitions", + url, version.slug(), issue.getId())) + .toString(), + LogLevel.DEBUG, + null); final var requestBuilder = HttpRequest.newBuilder( new URI( @@ -426,6 +466,11 @@ boolean transition( .POST(BodyPublishers.ofString(MAPPER.writeValueAsString(request))) .build(), BodyHandlers.ofString()); + ((Definer) definer) + .log( + new StringBuilder("Got response ").append(transitionResult).toString(), + LogLevel.DEBUG, + null); if (transitionResult.statusCode() / 100 != 2) { StringBuilder errorBuilder = new StringBuilder(); errorBuilder @@ -456,6 +501,17 @@ boolean transition( authenticationHeader.ifPresent( header -> commentRequestBuilder.header("Authorization", header)); + ((Definer) definer) + .log( + new StringBuilder("Sending comment request ") + .append(BodyPublishers.ofString(MAPPER.writeValueAsString(updateComment))) + .append(" to ") + .append( + String.format( + "%s/rest/api/%s/issue/%s/comment", url, version.slug(), issue.getId())) + .toString(), + LogLevel.DEBUG, + null); var commentResult = CLIENT.send( commentRequestBuilder @@ -463,6 +519,11 @@ boolean transition( .POST(BodyPublishers.ofString(MAPPER.writeValueAsString(updateComment))) .build(), BodyHandlers.ofString()); + ((Definer) definer) + .log( + new StringBuilder("Got response ").append(commentResult).toString(), + LogLevel.DEBUG, + null); boolean isGood = commentResult.statusCode() / 100 == 2; if (!isGood) { StringBuilder errorBuilder = new StringBuilder(); From 59e7b846e7e0aa5af7e46d5e9d3486d962016572 Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Tue, 2 Jul 2024 15:05:21 -0400 Subject: [PATCH 2/7] Logging for close verb --- .../ca/on/oicr/gsi/shesmu/jira/IssueVerb.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java index cd4de5fff..d5316551f 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java @@ -46,7 +46,25 @@ public ActionState perform( Consumer bestMatch) throws URISyntaxException, IOException, InterruptedException { JiraConnection connection = definer.get(); + ((Definer) definer) + .log( + new StringBuilder("Trying to close ") + .append(issues) + .append(" to one of ") + .append(connection.closedStatuses()) + .toString(), + LogLevel.DEBUG, + null); for (final var issue : issues) { + ((Definer) definer) + .log( + new StringBuilder("Attempting to close ") + .append(issue) + .append(" whose status is ") + .append(issue.extract(Issue.STATUS)) + .toString(), + LogLevel.DEBUG, + null); if (issue .extract(Issue.STATUS) .map(s -> connection.closedStatuses().noneMatch(s.name()::equalsIgnoreCase)) From eb4e51a3ceb63011a30fc65993bc0e88c8a4a467 Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Tue, 2 Jul 2024 15:05:47 -0400 Subject: [PATCH 3/7] Debug logging for IssueAction --- .../java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java index 6bae39226..86d0038b7 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java @@ -3,6 +3,8 @@ import ca.on.oicr.gsi.shesmu.jira.IssueVerb.Close; import ca.on.oicr.gsi.shesmu.jira.IssueVerb.Open; import ca.on.oicr.gsi.shesmu.plugin.AlgebraicValue; +import ca.on.oicr.gsi.shesmu.plugin.Definer; +import ca.on.oicr.gsi.shesmu.plugin.LogLevel; import ca.on.oicr.gsi.shesmu.plugin.action.Action; import ca.on.oicr.gsi.shesmu.plugin.action.ActionParameter; import ca.on.oicr.gsi.shesmu.plugin.action.ActionServices; @@ -122,10 +124,16 @@ public ActionState perform( if (connection == null) { // 'connection' is a bit of a misnomer - it's the Definer supplied by the PluginManager. // It should never be null. Very bad things have happened if it's null + // We also can't log through the Definer if we have no Definer :( System.err.println("JIRA Connection Definer for " + issueUrl + " is null."); return ActionState.FAILED; } final var current = connection.get(); + ((Definer) connection) + .log( + new StringBuilder("Performing jira updates with ").append(connection).toString(), + LogLevel.DEBUG, + null); requests.labels(current.url(), current.projectKey()).inc(); try { final var issues = @@ -141,6 +149,8 @@ public ActionState perform( Issue.TYPE.name(), Issue.UPDATED.name())); this.issues = issues.stream().map(Issue::getKey).collect(Collectors.toSet()); + ((Definer) connection) + .log(new StringBuilder("Got ").append(issues).toString(), LogLevel.DEBUG, null); final var missingLabels = new TreeSet(); final var result = verb.perform( @@ -165,7 +175,7 @@ public ActionState perform( return result; } catch (final Exception e) { failure.labels(connection.get().url(), connection.get().projectKey()).inc(); - e.printStackTrace(); + ((Definer) connection).log(e.toString(), LogLevel.ERROR, null); return ActionState.UNKNOWN; } } From 98fe8645801e775ad6d4aa85ee0d315b602d4bad Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Wed, 3 Jul 2024 11:48:19 -0400 Subject: [PATCH 4/7] Logging for opening and creating issues --- .../ca/on/oicr/gsi/shesmu/jira/IssueVerb.java | 37 +++++++++++++++++++ .../oicr/gsi/shesmu/jira/JiraConnection.java | 25 +++++++++++++ 2 files changed, 62 insertions(+) diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java index d5316551f..0fa33e09c 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java @@ -144,6 +144,15 @@ public ActionState perform( Consumer bestMatch) throws URISyntaxException, IOException, InterruptedException { JiraConnection connection = definer.get(); + ((Definer) definer) + .log( + new StringBuilder("Trying to open ") + .append(issues) + .append(" to something other than ") + .append(connection.closedStatuses()) + .toString(), + LogLevel.DEBUG, + null); if (issues.stream() .anyMatch( issue -> @@ -151,10 +160,27 @@ public ActionState perform( .extract(Issue.STATUS) .map( status -> { + ((Definer) definer) + .log( + new StringBuilder(issue.getKey()) + .append(" is of status ") + .append(issue.extract(Issue.STATUS)) + .toString(), + LogLevel.DEBUG, + null); final var isOpen = connection .closedStatuses() .noneMatch(status.name()::equalsIgnoreCase); + ((Definer) definer) + .log( + new StringBuilder("Issue ") + .append(issue.getKey()) + .append(" is already open: ") + .append(isOpen) + .toString(), + LogLevel.DEBUG, + null); if (isOpen) { bestMatch.accept(issue); } @@ -165,11 +191,22 @@ public ActionState perform( } for (final var issue : issues) { + ((Definer) definer) + .log( + new StringBuilder("Attempting to open ") + .append(issue) + .append(" whose status is ") + .append(issue.extract(Issue.STATUS)) + .toString(), + LogLevel.DEBUG, + null); if (connection.transition(issue, Stream::noneMatch, comment)) { bestMatch.accept(issue); return ActionState.SUCCEEDED; } } + ((Definer) definer) + .log("No other attempts worked, creating an issue...", LogLevel.DEBUG, null); bestMatch.accept(connection.createIssue(summary, description, assignee, labels, type)); return ActionState.SUCCEEDED; diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java index 54c58af90..c6abbd803 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java @@ -194,6 +194,17 @@ Issue createIssue( throws URISyntaxException, IOException, InterruptedException { final var request = new Issue(); final var project = new Project(); + + StringBuilder logmsg = + new StringBuilder("Attempting to create a new issue in ") + .append(projectKey) + .append(" with ") + .append(summary) + .append(", ") + .append(description); + assignee.ifPresent(s -> logmsg.append(", ").append(s)); + ((Definer) definer).log(logmsg.toString(), LogLevel.DEBUG, null); + project.setId(projectId); request.put(Issue.PROJECT, project); request.put(Issue.SUMMARY, summary); @@ -214,6 +225,16 @@ Issue createIssue( issueType.setName(type); request.put(Issue.TYPE, issueType); + ((Definer) definer) + .log( + new StringBuilder("Sending request ") + .append(MAPPER.writeValueAsString(request)) + .append(" to ") + .append(String.format("%s/rest/api/%s/issue", url, version.slug())) + .toString(), + LogLevel.DEBUG, + null); + IssueAction.issueCreates.labels(url, projectKey).inc(); final var builder = HttpRequest.newBuilder(new URI(String.format("%s/rest/api/%s/issue", url, version.slug()))); @@ -225,6 +246,10 @@ Issue createIssue( .POST(BodyPublishers.ofString(MAPPER.writeValueAsString(request))) .build(), BodyHandlers.ofString()); + + ((Definer) definer) + .log(new StringBuilder("Got result ").append(result).toString(), LogLevel.DEBUG, null); + if (result.statusCode() / 100 != 2) { throw new RuntimeException( String.format( From 5c5c39872f719c17d17bedd0e5c86f29119e26d4 Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Wed, 3 Jul 2024 14:07:39 -0400 Subject: [PATCH 5/7] labels can't be null --- .../on/oicr/gsi/shesmu/jira/IssueAction.java | 7 +++-- .../ca/on/oicr/gsi/shesmu/jira/IssueVerb.java | 28 +++++++++++------- .../oicr/gsi/shesmu/jira/JiraConnection.java | 29 ++++++++++--------- .../shesmu/server/plugins/PluginManager.java | 3 +- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java index 86d0038b7..2ee47f52d 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java @@ -133,7 +133,7 @@ public ActionState perform( .log( new StringBuilder("Performing jira updates with ").append(connection).toString(), LogLevel.DEBUG, - null); + new TreeMap<>()); requests.labels(current.url(), current.projectKey()).inc(); try { final var issues = @@ -150,7 +150,8 @@ public ActionState perform( Issue.UPDATED.name())); this.issues = issues.stream().map(Issue::getKey).collect(Collectors.toSet()); ((Definer) connection) - .log(new StringBuilder("Got ").append(issues).toString(), LogLevel.DEBUG, null); + .log( + new StringBuilder("Got ").append(issues).toString(), LogLevel.DEBUG, new TreeMap<>()); final var missingLabels = new TreeSet(); final var result = verb.perform( @@ -175,7 +176,7 @@ public ActionState perform( return result; } catch (final Exception e) { failure.labels(connection.get().url(), connection.get().projectKey()).inc(); - ((Definer) connection).log(e.toString(), LogLevel.ERROR, null); + ((Definer) connection).log(e.toString(), LogLevel.ERROR, new TreeMap<>()); return ActionState.UNKNOWN; } } diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java index 0fa33e09c..1ae933065 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java @@ -54,8 +54,11 @@ public ActionState perform( .append(connection.closedStatuses()) .toString(), LogLevel.DEBUG, - null); + new TreeMap<>()); for (final var issue : issues) { + Map lokiLabels = new HashMap<>(); + lokiLabels.put("issue", issue.getKey()); + lokiLabels.put("verb", "close"); ((Definer) definer) .log( new StringBuilder("Attempting to close ") @@ -64,7 +67,7 @@ public ActionState perform( .append(issue.extract(Issue.STATUS)) .toString(), LogLevel.DEBUG, - null); + lokiLabels); if (issue .extract(Issue.STATUS) .map(s -> connection.closedStatuses().noneMatch(s.name()::equalsIgnoreCase)) @@ -77,9 +80,6 @@ public ActionState perform( .append(issue.getKey()) .append("\nConnection: ") .append(connection); - Map lokiLabels = new HashMap<>(); - lokiLabels.put("issue", issue.getKey()); - lokiLabels.put("verb", "close"); ((Definer) definer) .log(errorBuilder.toString(), LogLevel.ERROR, lokiLabels); return ActionState.FAILED; @@ -152,7 +152,7 @@ public ActionState perform( .append(connection.closedStatuses()) .toString(), LogLevel.DEBUG, - null); + new TreeMap<>()); if (issues.stream() .anyMatch( issue -> @@ -160,6 +160,9 @@ public ActionState perform( .extract(Issue.STATUS) .map( status -> { + Map lokiLabels = new HashMap<>(); + lokiLabels.put("issue", issue.getKey()); + lokiLabels.put("verb", "open"); ((Definer) definer) .log( new StringBuilder(issue.getKey()) @@ -167,7 +170,7 @@ public ActionState perform( .append(issue.extract(Issue.STATUS)) .toString(), LogLevel.DEBUG, - null); + lokiLabels); final var isOpen = connection .closedStatuses() @@ -180,7 +183,7 @@ public ActionState perform( .append(isOpen) .toString(), LogLevel.DEBUG, - null); + lokiLabels); if (isOpen) { bestMatch.accept(issue); } @@ -191,6 +194,9 @@ public ActionState perform( } for (final var issue : issues) { + Map lokiLabels = new HashMap<>(); + lokiLabels.put("issue", issue.getKey()); + lokiLabels.put("verb", "open"); ((Definer) definer) .log( new StringBuilder("Attempting to open ") @@ -199,14 +205,16 @@ public ActionState perform( .append(issue.extract(Issue.STATUS)) .toString(), LogLevel.DEBUG, - null); + lokiLabels); if (connection.transition(issue, Stream::noneMatch, comment)) { bestMatch.accept(issue); return ActionState.SUCCEEDED; } } + Map lokiLabels = new HashMap<>(); + lokiLabels.put("verb", "close"); ((Definer) definer) - .log("No other attempts worked, creating an issue...", LogLevel.DEBUG, null); + .log("No other attempts worked, creating an issue...", LogLevel.DEBUG, lokiLabels); bestMatch.accept(connection.createIssue(summary, description, assignee, labels, type)); return ActionState.SUCCEEDED; diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java index c6abbd803..3292218eb 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java @@ -203,7 +203,7 @@ Issue createIssue( .append(", ") .append(description); assignee.ifPresent(s -> logmsg.append(", ").append(s)); - ((Definer) definer).log(logmsg.toString(), LogLevel.DEBUG, null); + ((Definer) definer).log(logmsg.toString(), LogLevel.DEBUG, new TreeMap<>()); project.setId(projectId); request.put(Issue.PROJECT, project); @@ -233,7 +233,7 @@ Issue createIssue( .append(String.format("%s/rest/api/%s/issue", url, version.slug())) .toString(), LogLevel.DEBUG, - null); + new TreeMap<>()); IssueAction.issueCreates.labels(url, projectKey).inc(); final var builder = @@ -248,7 +248,10 @@ Issue createIssue( BodyHandlers.ofString()); ((Definer) definer) - .log(new StringBuilder("Got result ").append(result).toString(), LogLevel.DEBUG, null); + .log( + new StringBuilder("Got result ").append(result).toString(), + LogLevel.DEBUG, + new TreeMap<>()); if (result.statusCode() / 100 != 2) { throw new RuntimeException( @@ -406,6 +409,8 @@ boolean transition( Issue issue, BiFunction, Predicate, Boolean> matcher, String comment) throws URISyntaxException, IOException, InterruptedException { IssueAction.issueUpdates.labels(url, projectKey).inc(); + Map lokiLabels = new HashMap<>(); + lokiLabels.put("issue", issue.getKey()); ((Definer) definer) .log( new StringBuilder("Attempting to transition issue ") @@ -414,7 +419,7 @@ boolean transition( .append(comment) .toString(), LogLevel.DEBUG, - null); + lokiLabels); final var builder = HttpRequest.newBuilder( new URI( @@ -437,7 +442,7 @@ boolean transition( .append(transitions) .toString(), LogLevel.DEBUG, - null); + lokiLabels); for (final var transition : transitions) { ((Definer) definer) @@ -450,7 +455,7 @@ boolean transition( .append(closedStatuses()) .toString(), LogLevel.DEBUG, - null); + lokiLabels); if (matcher.apply(closedStatuses(), transition.to().name()::equalsIgnoreCase)) { final var request = new TransitionRequest(); /** "fields": { "assignee": { "name": "Will" }, "resolution": { "name": "Fixed" } } */ @@ -475,7 +480,7 @@ boolean transition( url, version.slug(), issue.getId())) .toString(), LogLevel.DEBUG, - null); + lokiLabels); final var requestBuilder = HttpRequest.newBuilder( new URI( @@ -495,7 +500,7 @@ boolean transition( .log( new StringBuilder("Got response ").append(transitionResult).toString(), LogLevel.DEBUG, - null); + lokiLabels); if (transitionResult.statusCode() / 100 != 2) { StringBuilder errorBuilder = new StringBuilder(); errorBuilder @@ -509,8 +514,6 @@ boolean transition( .append(MAPPER.writeValueAsString(request)) .append(", received: ") .append(transitionResult.body()); - Map lokiLabels = new HashMap<>(); - lokiLabels.put("issue", issue.getKey()); ((Definer) definer) .log(errorBuilder.toString(), LogLevel.ERROR, lokiLabels); return false; @@ -536,7 +539,7 @@ boolean transition( "%s/rest/api/%s/issue/%s/comment", url, version.slug(), issue.getId())) .toString(), LogLevel.DEBUG, - null); + lokiLabels); var commentResult = CLIENT.send( commentRequestBuilder @@ -548,7 +551,7 @@ boolean transition( .log( new StringBuilder("Got response ").append(commentResult).toString(), LogLevel.DEBUG, - null); + lokiLabels); boolean isGood = commentResult.statusCode() / 100 == 2; if (!isGood) { StringBuilder errorBuilder = new StringBuilder(); @@ -559,8 +562,6 @@ boolean transition( .append(comment) .append("\nGot ") .append(commentResult.body()); - Map lokiLabels = new HashMap<>(); - lokiLabels.put("issue", issue.getKey()); ((Definer) definer) .log(errorBuilder.toString(), LogLevel.ERROR, lokiLabels); } diff --git a/shesmu-server/src/main/java/ca/on/oicr/gsi/shesmu/server/plugins/PluginManager.java b/shesmu-server/src/main/java/ca/on/oicr/gsi/shesmu/server/plugins/PluginManager.java index 1b125a434..fb7fc8022 100644 --- a/shesmu-server/src/main/java/ca/on/oicr/gsi/shesmu/server/plugins/PluginManager.java +++ b/shesmu-server/src/main/java/ca/on/oicr/gsi/shesmu/server/plugins/PluginManager.java @@ -845,7 +845,8 @@ public Stream isOverloaded(Set services) { @Override public void log(String message, LogLevel level, Map labels) { - final Map amendedLabels = new TreeMap<>(labels); + final Map amendedLabels = + labels == null ? new TreeMap<>() : new TreeMap<>(labels); amendedLabels.put("plugin", instance.fileName().toString()); amendedLabels.put("plugin_type", FormatTypeWrapper.this.fileFormat.getClass().toString()); PluginManager.this.log(message, level, amendedLabels); From 0947c5e2dfd060fae9c07edcc62ae514d62eec47 Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Thu, 4 Jul 2024 09:30:39 -0400 Subject: [PATCH 6/7] Logging improvements --- .../ca/on/oicr/gsi/shesmu/jira/Issue.java | 12 ++++++++ .../on/oicr/gsi/shesmu/jira/IssueAction.java | 6 ++-- .../ca/on/oicr/gsi/shesmu/jira/IssueVerb.java | 30 ++++++++----------- .../oicr/gsi/shesmu/jira/JiraConnection.java | 22 ++++++++++---- .../oicr/gsi/shesmu/jira/SearchRequest.java | 12 ++++++++ .../oicr/gsi/shesmu/jira/SearchResponse.java | 8 +++++ 6 files changed, 65 insertions(+), 25 deletions(-) diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/Issue.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/Issue.java index 7a6f1cdb4..82f370b73 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/Issue.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/Issue.java @@ -88,4 +88,16 @@ public void setKey(String key) { public void setSelf(String self) { this.self = self; } + + public String toString() { + return new StringBuilder("Issue with fields = ") + .append(fields) + .append(", id = ") + .append(id) + .append(", key = ") + .append(key) + .append(", self = ") + .append(self) + .toString(); + } } diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java index 2ee47f52d..9f5f38729 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueAction.java @@ -131,7 +131,7 @@ public ActionState perform( final var current = connection.get(); ((Definer) connection) .log( - new StringBuilder("Performing jira updates with ").append(connection).toString(), + new StringBuilder("Performing jira updates with ").append(current).toString(), LogLevel.DEBUG, new TreeMap<>()); requests.labels(current.url(), current.projectKey()).inc(); @@ -151,7 +151,9 @@ public ActionState perform( this.issues = issues.stream().map(Issue::getKey).collect(Collectors.toSet()); ((Definer) connection) .log( - new StringBuilder("Got ").append(issues).toString(), LogLevel.DEBUG, new TreeMap<>()); + new StringBuilder("Got ").append(issues.isEmpty() ? "nothing" : issues).toString(), + LogLevel.DEBUG, + new TreeMap<>()); final var missingLabels = new TreeSet(); final var result = verb.perform( diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java index 1ae933065..cfe8afdfe 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/IssueVerb.java @@ -46,23 +46,23 @@ public ActionState perform( Consumer bestMatch) throws URISyntaxException, IOException, InterruptedException { JiraConnection connection = definer.get(); + Map lokiLabels = new HashMap<>(); + lokiLabels.put("verb", "close"); ((Definer) definer) .log( new StringBuilder("Trying to close ") - .append(issues) + .append(issues.isEmpty() ? "nothing" : issues) .append(" to one of ") - .append(connection.closedStatuses()) + .append(connection.closedStatuses().toList()) .toString(), LogLevel.DEBUG, - new TreeMap<>()); + lokiLabels); for (final var issue : issues) { - Map lokiLabels = new HashMap<>(); lokiLabels.put("issue", issue.getKey()); - lokiLabels.put("verb", "close"); ((Definer) definer) .log( new StringBuilder("Attempting to close ") - .append(issue) + .append(issue.getKey()) .append(" whose status is ") .append(issue.extract(Issue.STATUS)) .toString(), @@ -144,12 +144,14 @@ public ActionState perform( Consumer bestMatch) throws URISyntaxException, IOException, InterruptedException { JiraConnection connection = definer.get(); + Map lokiLabels = new HashMap<>(); + lokiLabels.put("verb", "open"); ((Definer) definer) .log( new StringBuilder("Trying to open ") - .append(issues) + .append(issues.isEmpty() ? "nothing" : issues) .append(" to something other than ") - .append(connection.closedStatuses()) + .append(connection.closedStatuses().toList()) .toString(), LogLevel.DEBUG, new TreeMap<>()); @@ -160,9 +162,7 @@ public ActionState perform( .extract(Issue.STATUS) .map( status -> { - Map lokiLabels = new HashMap<>(); lokiLabels.put("issue", issue.getKey()); - lokiLabels.put("verb", "open"); ((Definer) definer) .log( new StringBuilder(issue.getKey()) @@ -177,9 +177,9 @@ public ActionState perform( .noneMatch(status.name()::equalsIgnoreCase); ((Definer) definer) .log( - new StringBuilder("Issue ") + new StringBuilder("Is issue ") .append(issue.getKey()) - .append(" is already open: ") + .append(" already open?: ") .append(isOpen) .toString(), LogLevel.DEBUG, @@ -194,13 +194,11 @@ public ActionState perform( } for (final var issue : issues) { - Map lokiLabels = new HashMap<>(); lokiLabels.put("issue", issue.getKey()); - lokiLabels.put("verb", "open"); ((Definer) definer) .log( new StringBuilder("Attempting to open ") - .append(issue) + .append(issue.getKey()) .append(" whose status is ") .append(issue.extract(Issue.STATUS)) .toString(), @@ -211,8 +209,6 @@ public ActionState perform( return ActionState.SUCCEEDED; } } - Map lokiLabels = new HashMap<>(); - lokiLabels.put("verb", "close"); ((Definer) definer) .log("No other attempts worked, creating an issue...", LogLevel.DEBUG, lokiLabels); bestMatch.accept(connection.createIssue(summary, description, assignee, labels, type)); diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java index 3292218eb..a703b2ba9 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/JiraConnection.java @@ -194,6 +194,8 @@ Issue createIssue( throws URISyntaxException, IOException, InterruptedException { final var request = new Issue(); final var project = new Project(); + Map lokiLabels = new HashMap<>(); + lokiLabels.put("verb", "open"); StringBuilder logmsg = new StringBuilder("Attempting to create a new issue in ") @@ -203,7 +205,7 @@ Issue createIssue( .append(", ") .append(description); assignee.ifPresent(s -> logmsg.append(", ").append(s)); - ((Definer) definer).log(logmsg.toString(), LogLevel.DEBUG, new TreeMap<>()); + ((Definer) definer).log(logmsg.toString(), LogLevel.DEBUG, lokiLabels); project.setId(projectId); request.put(Issue.PROJECT, project); @@ -233,7 +235,7 @@ Issue createIssue( .append(String.format("%s/rest/api/%s/issue", url, version.slug())) .toString(), LogLevel.DEBUG, - new TreeMap<>()); + lokiLabels); IssueAction.issueCreates.labels(url, projectKey).inc(); final var builder = @@ -249,9 +251,7 @@ Issue createIssue( ((Definer) definer) .log( - new StringBuilder("Got result ").append(result).toString(), - LogLevel.DEBUG, - new TreeMap<>()); + new StringBuilder("Got result ").append(result).toString(), LogLevel.DEBUG, lokiLabels); if (result.statusCode() / 100 != 2) { throw new RuntimeException( @@ -329,6 +329,11 @@ List search(String jql, Set fields) request.setFields(fields); request.setJql(jql); + ((Definer) definer) + .log( + new StringBuilder("Searching for ").append(request).toString(), + LogLevel.DEBUG, + new TreeMap<>()); for (var page = 0; true; page++) { request.setStartAt(500 * page); final var builder = @@ -346,6 +351,11 @@ List search(String jql, Set fields) .body() .get(); buffer.addAll(results.getIssues()); + ((Definer) definer) + .log( + new StringBuilder("Search returned ").append(results).toString(), + LogLevel.DEBUG, + new TreeMap<>()); if (results.getIssues().isEmpty()) { break; } @@ -560,7 +570,7 @@ boolean transition( .append(issue.getKey()) .append(" using comment ") .append(comment) - .append("\nGot ") + .append(" and got ") .append(commentResult.body()); ((Definer) definer) .log(errorBuilder.toString(), LogLevel.ERROR, lokiLabels); diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchRequest.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchRequest.java index 32e0f4d93..e854d991c 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchRequest.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchRequest.java @@ -41,4 +41,16 @@ public void setMaxResults(int maxResults) { public void setStartAt(int startAt) { this.startAt = startAt; } + + public String toString() { + return new StringBuilder("SearchRequest with fields = ") + .append(fields) + .append(", jql = ") + .append(jql) + .append(", maxResults = ") + .append(maxResults) + .append(", startAt = ") + .append(startAt) + .toString(); + } } diff --git a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchResponse.java b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchResponse.java index 1b8ceb106..2641bdea5 100644 --- a/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchResponse.java +++ b/plugin-jira/src/main/java/ca/on/oicr/gsi/shesmu/jira/SearchResponse.java @@ -23,4 +23,12 @@ public void setIssues(List issues) { public void setTotal(int total) { this.total = total; } + + public String toString() { + return new StringBuilder("SearchResponse with issues = ") + .append(issues) + .append(", total = ") + .append(total) + .toString(); + } } From cd0b50b95005f86aafeb99ccf93563b3fa73bada Mon Sep 17 00:00:00 2001 From: Alexis Varsava Date: Thu, 4 Jul 2024 14:19:24 -0400 Subject: [PATCH 7/7] changelog --- changes/add_jira-debug-log.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changes/add_jira-debug-log.md diff --git a/changes/add_jira-debug-log.md b/changes/add_jira-debug-log.md new file mode 100644 index 000000000..cf023b4cb --- /dev/null +++ b/changes/add_jira-debug-log.md @@ -0,0 +1,2 @@ +Debug logging for jira issues +