Skip to content

Commit

Permalink
[mc1arke#137][GitLab] Extra property to disable pipeline failing on f…
Browse files Browse the repository at this point in the history
…ailed analysis

Could be useful in the interim period, especially for the analysis using
"-Dsonar.qualitygate.wait=true" with "allow_failure: true" in GitLab.
  • Loading branch information
szpak committed Jul 1, 2022
1 parent d1ebf81 commit 82828a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class GitlabMergeRequestDecorator extends DiscussionAwarePullRequestDecor
public static final String PULLREQUEST_GITLAB_PROJECT_URL = "sonar.pullrequest.gitlab.projectUrl";
public static final String PULLREQUEST_GITLAB_PIPELINE_ID =
"com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.pipelineId";
public static final String PULLREQUEST_GITLAB_DONT_FAIL_PIPELINE =
"com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.dontFailPipeline";

private final GitlabClientFactory gitlabClientFactory;
private final MarkdownFormatterFactory formatterFactory;
Expand Down Expand Up @@ -121,11 +123,15 @@ protected void submitPipelineStatus(GitlabClient gitlabClient, MergeRequest merg
Long pipelineId = analysis.getScannerProperty(PULLREQUEST_GITLAB_PIPELINE_ID)
.map(Long::parseLong)
.orElse(null);
boolean dontFailPipeline = analysis.getScannerProperty(PULLREQUEST_GITLAB_DONT_FAIL_PIPELINE)
.map(Boolean::parseBoolean)
.orElse(false);

try {
PipelineStatus pipelineStatus = new PipelineStatus("SonarQube",
"SonarQube Status",
analysis.getQualityGateStatus() == QualityGate.Status.OK ? PipelineStatus.State.SUCCESS : PipelineStatus.State.FAILED,
analysis.getQualityGateStatus() == QualityGate.Status.OK || dontFailPipeline ?
PipelineStatus.State.SUCCESS : PipelineStatus.State.FAILED,
analysisSummary.getDashboardUrl(),
analysisSummary.getNewCoverage(),
pipelineId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,39 @@ public void shouldSubmitFailedPipelineStatusAndUnresolvedSummaryCommentOnFailedA
PipelineStatus.State.FAILED, "https://sonarqube2.dummy/dashboard?id=" + PROJECT_KEY + "&pullRequest=" + MERGE_REQUEST_IID, BigDecimal.TEN, 11L));
}

//TODO: Masive duplication - use junit-jupiter-params
@Test //https://github.com/mc1arke/sonarqube-community-branch-plugin/issues/137
public void shouldSubmitPassedPipelineStatusIfPipelineFailingIsDisabledAndUnresolvedSummaryCommentOnFailedAnalysis() throws IOException {
when(analysisDetails.getQualityGateStatus()).thenReturn(QualityGate.Status.ERROR);
when(analysisDetails.getCommitSha()).thenReturn("other sha");
when(analysisDetails.getScannerProperty("com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.pipelineId")).thenReturn(Optional.of("11"));
when(analysisDetails.getScannerProperty("com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.dontFailPipeline")).thenReturn(Optional.of("true"));

when(analysisSummary.format(any())).thenReturn("Different Summary comment");
when(analysisSummary.getDashboardUrl()).thenReturn("https://sonarqube2.dummy/dashboard?id=projectKey&pullRequest=123");
when(analysisSummary.getNewCoverage()).thenReturn(BigDecimal.TEN);

Discussion discussion = mock(Discussion.class);
when(discussion.getId()).thenReturn("dicussion id 2");
when(gitlabClient.addMergeRequestDiscussion(anyLong(), anyLong(), any())).thenReturn(discussion);

underTest.decorateQualityGateStatus(analysisDetails, almSettingDto, projectAlmSettingDto);

ArgumentCaptor<MergeRequestNote> mergeRequestNoteArgumentCaptor = ArgumentCaptor.forClass(MergeRequestNote.class);
verify(gitlabClient).addMergeRequestDiscussion(eq(PROJECT_ID), eq(MERGE_REQUEST_IID), mergeRequestNoteArgumentCaptor.capture());
verify(gitlabClient, never()).resolveMergeRequestDiscussion(PROJECT_ID, MERGE_REQUEST_IID, discussion.getId());
ArgumentCaptor<PipelineStatus> pipelineStatusArgumentCaptor = ArgumentCaptor.forClass(PipelineStatus.class);
verify(gitlabClient).setMergeRequestPipelineStatus(eq(PROJECT_ID), eq("other sha"), pipelineStatusArgumentCaptor.capture());

assertThat(mergeRequestNoteArgumentCaptor.getValue())
.usingRecursiveComparison()
.isEqualTo(new MergeRequestNote("Different Summary comment"));
assertThat(pipelineStatusArgumentCaptor.getValue())
.usingRecursiveComparison()
.isEqualTo(new PipelineStatus("SonarQube", "SonarQube Status",
PipelineStatus.State.SUCCESS, "https://sonarqube2.dummy/dashboard?id=" + PROJECT_KEY + "&pullRequest=" + MERGE_REQUEST_IID, BigDecimal.TEN, 11L));
}

@Test
public void shouldThrowErrorWhenSubmitPipelineStatusToGitlabFails() throws IOException {
when(analysisDetails.getQualityGateStatus()).thenReturn(QualityGate.Status.ERROR);
Expand Down

0 comments on commit 82828a5

Please sign in to comment.