Skip to content

Commit

Permalink
#785: Only minimise Github summary comments with same project key
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
OneCyrus authored Apr 6, 2024
1 parent e039e72 commit 8f014bc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class CheckRunDetails {
private final List<Annotation> annotations;
private final CheckConclusionState checkConclusionState;
private final int pullRequestId;
private final String projectKey;

private CheckRunDetails(Builder builder) {
summary = builder.summary;
Expand All @@ -49,6 +50,7 @@ private CheckRunDetails(Builder builder) {
annotations = builder.annotations;
checkConclusionState = builder.checkConclusionState;
pullRequestId = builder.pullRequestId;
projectKey = builder.projectKey;
}

public String getSummary() {
Expand Down Expand Up @@ -95,6 +97,10 @@ public int getPullRequestId() {
return pullRequestId;
}

public String getProjectKey() {
return projectKey;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -111,6 +117,7 @@ public static final class Builder {
private List<Annotation> annotations;
private CheckConclusionState checkConclusionState;
private int pullRequestId;
private String projectKey;

private Builder() {
super();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -68,5 +70,9 @@ public Actor getAuthor() {
public boolean isMinimized() {
return minimized;
}

public String getBody() {
return body;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -128,7 +128,7 @@ public String getRepositoryUrl() {
return repositoryAuthenticationToken.getRepositoryUrl();
}

private void postSummaryComment(String graphqlUrl, Map<String, String> headers, int pullRequestKey, String summary) throws IOException {
private void postSummaryComment(String graphqlUrl, Map<String, String> headers, int pullRequestKey, String summary, String projectId) throws IOException {
String login = getLogin(graphqlUrl, headers);

GetRepository.PullRequest pullRequest = getPullRequest(graphqlUrl, headers, pullRequestKey);
Expand All @@ -137,6 +137,7 @@ private void postSummaryComment(String graphqlUrl, Map<String, String> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"" +
Expand Down Expand Up @@ -207,6 +208,7 @@ void verifyCheckRunSubmitsCorrectAnnotations() throws IOException {
.withName("Name")
.withTitle("Title")
.withPullRequestId(999)
.withProjectKey("project-key-test")
.build();


Expand Down Expand Up @@ -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()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8f014bc

Please sign in to comment.