Skip to content

Commit

Permalink
Merge branch 'feature/submit-summary-note-first' into releases
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
#	src/main/java/com/github/mc1arke/sonarqube/plugin/CommunityBranchPlugin.java
#	src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/DiscussionAwarePullRequestDecorator.java
#	src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/PullRequestBuildStatusDecorator.java
#	src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/ScannerPullRequestPropertySensor.java
#	src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabMergeRequestDecoratorTest.java
  • Loading branch information
alexintech committed Aug 4, 2023
2 parents c5b102c + 199c67d commit 6d43d36
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class CommunityBranchPlugin implements Plugin, CoreExtension {

public static final String IMAGE_URL_BASE = "com.github.mc1arke.sonarqube.plugin.branch.image-url-base";
public static final String PR_SUMMARY_NOTE_EDIT = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.summary.edit";
public static final String PR_SUMMARY_NOTE_FIRST = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.summary.first";

@Override
public String getName() {
Expand Down Expand Up @@ -167,6 +168,15 @@ public void load(CoreExtension.Context context) {
.type(PropertyType.BOOLEAN)
.defaultValue(String.valueOf(false))
.build());
context.addExtensions(PropertyDefinition.builder(PR_SUMMARY_NOTE_FIRST)
.category(getName())
.subCategory("Merge Request Decoration")
.onQualifiers(Qualifiers.PROJECT)
.name("Submit summary note first")
.description("Submit summary discussion thread before issue comments.")
.type(PropertyType.BOOLEAN)
.defaultValue(String.valueOf(false))
.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysis, AlmS
.filter(issue -> isIssueFromCommitInCurrentRequest(issue.getLeft(), commitIds, scmInfoRepository))
.collect(Collectors.toList());

AnalysisSummary analysisSummary = reportGenerator.createAnalysisSummary(analysis);
boolean isSummaryNoteFirst = isSummaryNoteFirstEnabled(analysis);
if (isSummaryNoteFirst) {
editExistingOrSubmitSummaryNote(client, pullRequest, currentProjectSonarqubeComments, analysis, analysisSummary);
}

uncommentedIssues.forEach(issue -> submitCommitNoteForIssue(client,
pullRequest,
issue.getLeft(),
issue.getRight(),
analysis,
reportGenerator.createAnalysisIssueSummary(issue.getLeft(), analysis)));

AnalysisSummary analysisSummary = reportGenerator.createAnalysisSummary(analysis);
editExistingOrSubmitSummaryNote(client, pullRequest, currentProjectSonarqubeComments, analysis, analysisSummary);
if (!isSummaryNoteFirst) {
editExistingOrSubmitSummaryNote(client, pullRequest, currentProjectSonarqubeComments, analysis, analysisSummary);
}
submitPipelineStatus(client, pullRequest, analysis, analysisSummary);

DecorationResult.Builder builder = DecorationResult.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest;

import com.github.mc1arke.sonarqube.plugin.CommunityBranchPlugin;
import org.sonar.db.alm.setting.ALM;
import org.sonar.db.alm.setting.AlmSettingDto;
import org.sonar.db.alm.setting.ProjectAlmSettingDto;
Expand All @@ -34,4 +35,10 @@ DecorationResult decorateQualityGateStatus(AnalysisDetails analysisDetails, AlmS
default boolean isEditSummaryNoteEnabled(AnalysisDetails analysisDetails) {
return false;
}

default boolean isSummaryNoteFirstEnabled(AnalysisDetails analysisDetails) {
return analysisDetails.getScannerProperty(CommunityBranchPlugin.PR_SUMMARY_NOTE_FIRST)
.map(Boolean::parseBoolean)
.orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public void execute(SensorContext sensorContext) {

sensorContext.config().get(CommunityBranchPlugin.PR_SUMMARY_NOTE_EDIT)
.ifPresent(p -> sensorContext.addContextProperty(CommunityBranchPlugin.PR_SUMMARY_NOTE_EDIT, p));
sensorContext.config().get(CommunityBranchPlugin.PR_SUMMARY_NOTE_FIRST)
.ifPresent(p -> sensorContext.addContextProperty(CommunityBranchPlugin.PR_SUMMARY_NOTE_FIRST, p));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,26 @@ private void reportIssue() {
when(scmInfoRepository.getScmInfo(component)).thenReturn(Optional.of(scmInfo));
}

@Test
public void shouldSubmitSummaryNoteBeforeIssueCommentWhenSummaryNoteFirstEnabled() throws IOException {
enableScannerProperty(CommunityBranchPlugin.PR_SUMMARY_NOTE_FIRST);
reportIssue();
noDiscussionsExist();

underTest.decorateQualityGateStatus(analysisDetails, almSettingDto, projectAlmSettingDto);

verify(gitlabClient, never()).resolveMergeRequestDiscussion(anyLong(), anyLong(), any());
verify(gitlabClient, never()).addMergeRequestDiscussionNote(anyLong(), anyLong(), any(), any());

ArgumentCaptor<MergeRequestNote> mergeRequestNoteArgumentCaptor = ArgumentCaptor.forClass(MergeRequestNote.class);
verify(gitlabClient, times(2)).addMergeRequestDiscussion(eq(PROJECT_ID), eq(MERGE_REQUEST_IID), mergeRequestNoteArgumentCaptor.capture());

assertThat(mergeRequestNoteArgumentCaptor.getAllValues().get(0)).isInstanceOf(MergeRequestNote.class);
assertThat(mergeRequestNoteArgumentCaptor.getAllValues().get(1))
.usingRecursiveComparison()
.isEqualTo(new CommitNote("Issue Summary", BASE_SHA, START_SHA, HEAD_SHA, "path-to-file", "path-to-file", 999));
}

@Test
public void shouldNotCreateCommentsForIssuesWithNoLineNumbers() throws IOException {
PostAnalysisIssueVisitor.LightIssue lightIssue = mock(PostAnalysisIssueVisitor.LightIssue.class);
Expand Down

0 comments on commit 6d43d36

Please sign in to comment.