From 8f014bc9703e7335ae8d22fed98dbec8c66188b7 Mon Sep 17 00:00:00 2001 From: Daniel Gut Date: Sat, 6 Apr 2024 13:48:57 +0200 Subject: [PATCH] #785: Only minimise Github summary comments with same project key The Github decorator was not checking the project key listed against a summary comment so was minimising all summary comments when performing multiple decorations against a mono-repository. To overcome this, the contents of the summary comments are being retrieved and only the ones with a project key matching the current project are being minimised during decoration. --- .../almclient/github/model/CheckRunDetails.java | 12 ++++++++++++ .../plugin/almclient/github/v4/Comments.java | 8 +++++++- .../almclient/github/v4/GraphqlGithubClient.java | 5 +++-- .../github/GithubPullRequestDecorator.java | 1 + .../almclient/github/v4/GraphqlGithubClientTest.java | 4 +++- .../github/GithubPullRequestDecoratorTest.java | 1 + 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/model/CheckRunDetails.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/model/CheckRunDetails.java index 47a4974a6..502598bdb 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/model/CheckRunDetails.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/model/CheckRunDetails.java @@ -36,6 +36,7 @@ public class CheckRunDetails { private final List annotations; private final CheckConclusionState checkConclusionState; private final int pullRequestId; + private final String projectKey; private CheckRunDetails(Builder builder) { summary = builder.summary; @@ -49,6 +50,7 @@ private CheckRunDetails(Builder builder) { annotations = builder.annotations; checkConclusionState = builder.checkConclusionState; pullRequestId = builder.pullRequestId; + projectKey = builder.projectKey; } public String getSummary() { @@ -95,6 +97,10 @@ public int getPullRequestId() { return pullRequestId; } + public String getProjectKey() { + return projectKey; + } + public static Builder builder() { return new Builder(); } @@ -111,6 +117,7 @@ public static final class Builder { private List annotations; private CheckConclusionState checkConclusionState; private int pullRequestId; + private String projectKey; private Builder() { super(); @@ -171,6 +178,11 @@ public Builder withPullRequestId(int pullRequestId) { return this; } + public Builder withProjectKey(String projectKey) { + this.projectKey = projectKey; + return this; + } + public CheckRunDetails build() { return new CheckRunDetails(this); } diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/Comments.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/Comments.java index 5bb595f38..7331a7b31 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/Comments.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/Comments.java @@ -49,12 +49,14 @@ public static class CommentNode { private final Actor author; @GraphQLProperty(name = "isMinimized") private final boolean minimized; + private final String body; @JsonCreator - public CommentNode(@JsonProperty("id") String id, @JsonProperty("author") Actor author, @JsonProperty("isMinimized") boolean minimized) { + public CommentNode(@JsonProperty("id") String id, @JsonProperty("author") Actor author, @JsonProperty("isMinimized") boolean minimized, @JsonProperty("body") String body) { this.id = id; this.author = author; this.minimized = minimized; + this.body = body; } public String getId() { @@ -68,5 +70,9 @@ public Actor getAuthor() { public boolean isMinimized() { return minimized; } + + public String getBody() { + return body; + } } } diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClient.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClient.java index abcbae80b..4b40d7008 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClient.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClient.java @@ -116,7 +116,7 @@ public String createCheckRun(CheckRunDetails checkRunDetails, boolean postSummar if (postSummaryComment) { - postSummaryComment(graphqlUrl, headers, checkRunDetails.getPullRequestId(), checkRunDetails.getSummary()); + postSummaryComment(graphqlUrl, headers, checkRunDetails.getPullRequestId(), checkRunDetails.getSummary(), checkRunDetails.getProjectKey()); } return graphQLResponseEntity.getResponse().getCheckRun().getId(); @@ -128,7 +128,7 @@ public String getRepositoryUrl() { return repositoryAuthenticationToken.getRepositoryUrl(); } - private void postSummaryComment(String graphqlUrl, Map headers, int pullRequestKey, String summary) throws IOException { + private void postSummaryComment(String graphqlUrl, Map headers, int pullRequestKey, String summary, String projectId) throws IOException { String login = getLogin(graphqlUrl, headers); GetRepository.PullRequest pullRequest = getPullRequest(graphqlUrl, headers, pullRequestKey); @@ -137,6 +137,7 @@ private void postSummaryComment(String graphqlUrl, Map headers, getComments(pullRequest, graphqlUrl, headers, pullRequestKey).stream() .filter(c -> "Bot".equalsIgnoreCase(c.getAuthor().getType()) && login.equalsIgnoreCase(c.getAuthor().getLogin())) .filter(c -> !c.isMinimized()) + .filter(c -> c.getBody().contains(String.format("**Project ID:** %s\r\n", projectId))) .map(Comments.CommentNode::getId) .forEach(commentId -> this.minimizeComment(graphqlUrl, headers, commentId)); diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecorator.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecorator.java index 197702e65..bacf7dc3b 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecorator.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecorator.java @@ -79,6 +79,7 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysisDetail .withExternalId(analysisDetails.getAnalysisId()) .withName(String.format("%s Sonarqube Results", analysisDetails.getAnalysisProjectName())) .withTitle("Quality Gate " + (analysisDetails.getQualityGateStatus() == QualityGate.Status.OK ? "success" : "failed")) + .withProjectKey(analysisDetails.getAnalysisProjectKey()) .build(); try { diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClientTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClientTest.java index 8efd476c9..5699ccaa0 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClientTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/almclient/github/v4/GraphqlGithubClientTest.java @@ -159,6 +159,7 @@ void verifyCheckRunSubmitsCorrectAnnotations() throws IOException { " {" + " \"id\": \"MDEyOklzc3VlQ29tbWVudDE1MDE3\"," + " \"isMinimized\": false," + + " \"body\": \"**Project ID:** project-key-test\\r\\n\"," + " \"author\": {" + " \"__typename\": \"Bot\"," + " \"login\": \"test-sonar\"" + @@ -207,6 +208,7 @@ void verifyCheckRunSubmitsCorrectAnnotations() throws IOException { .withName("Name") .withTitle("Title") .withPullRequestId(999) + .withProjectKey("project-key-test") .build(); @@ -299,7 +301,7 @@ void verifyCheckRunSubmitsCorrectAnnotations() throws IOException { assertEquals(requestEntities.get(2), getPullRequestRequestEntityArgumentCaptor.getValue()); assertEquals( "query { repository (owner:\"owner\",name:\"repository\") { url pullRequest : pullRequest (number:999) { comments : comments (first:100) { nodes" + - " { author { type : __typename login } id minimized : isMinimized } pageInfo { hasNextPage endCursor } } id } } } ", + " { author { type : __typename login } id minimized : isMinimized body } pageInfo { hasNextPage endCursor } } id } } } ", getPullRequestRequestEntityArgumentCaptor.getValue().getRequest() ); diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecoratorTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecoratorTest.java index 10f595b90..b60b40171 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecoratorTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/github/GithubPullRequestDecoratorTest.java @@ -147,6 +147,7 @@ void verifyCorrectArgumentsAndReturnValuesUsed() throws IOException { .withSeverity(i % 5 < 1 ? CheckAnnotationLevel.NOTICE : i % 5 > 2 ? CheckAnnotationLevel.FAILURE : CheckAnnotationLevel.WARNING) .build()).collect(Collectors.toList())) .withCheckConclusionState(CheckConclusionState.SUCCESS) + .withProjectKey(analysisDetails.getAnalysisProjectKey()) .build()); assertThat(decorationResult).usingRecursiveComparison().isEqualTo(expectedResult); }