Skip to content

Commit ffa433b

Browse files
authored
Merge pull request #24 from softreigns/github-master
Throw exception on quality gate fail/error status at plugin level instead of sonar scanner
2 parents 06abea4 + 265519b commit ffa433b

File tree

8 files changed

+142
-16
lines changed

8 files changed

+142
-16
lines changed

.gitlab-ci.yml

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,67 @@
11
image: maven:3.3.3-jdk-8
2+
3+
workflow:
4+
rules:
5+
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
6+
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
7+
when: never
8+
- if: $CI_COMMIT_BRANCH
9+
210
stages:
311
- build
12+
- publish
413
- test
514
- deploy
15+
616
build_job:
717
stage: build
8-
only:
9-
- master
1018
script:
11-
- mvn --batch-mode compile -Dmaven.test.skip=true -Djacoco.skip=true
19+
- mvn --quiet clean package
20+
cache:
21+
key: maven
22+
paths:
23+
- .m2/repository
24+
artifacts:
25+
paths:
26+
- target/*.jar
1227
tags:
1328
- docker
14-
build_merge_job:
15-
stage: build
16-
except:
17-
- master
18-
- tags
29+
30+
publish_job:
31+
stage: publish
32+
dependencies:
33+
- build_job
34+
artifacts:
35+
paths:
36+
- ci_settings.xml
1937
script:
20-
- git merge origin master --no-commit --no-ff
21-
- mvn --batch-mode compile -Dmaven.test.skip=true -Djacoco.skip=true
22-
tags:
23-
- docker
38+
- |
39+
jarfile=$(ls target/sonar-gitlab-plugin*.jar)
40+
mvn deploy:deploy-file -s gitlab_settings.xml -DpomFile=pom.xml \
41+
-Dfile=${jarfile} \
42+
-DrepositoryId=gitlab-maven \
43+
-Durl=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/maven
44+
2445
test_sonar_preview_job:
2546
stage: test
2647
except:
2748
- master
2849
- tags
2950
script:
30-
- git merge origin master --no-commit --no-ff
3151
- mvn --batch-mode verify org.sonarsource.scanner.maven:sonar-maven-plugin:3.4.0.905:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.analysis.mode=preview -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME -Dsonar.gitlab.project_id=$CI_PROJECT_ID
3252
tags:
3353
- docker
54+
3455
test_sonar_feature_job:
3556
stage: test
3657
except:
3758
- master
3859
- tags
3960
script:
40-
- git merge origin master --no-commit --no-ff
4161
- mvn --batch-mode verify org.sonarsource.scanner.maven:sonar-maven-plugin:3.4.0.905:sonar -Dsonar.host.url=$SONAR_OFF_URL -Dsonar.login=$SONAR_OFF_LOGIN -Dsonar.branch.name=$CI_COMMIT_REF_NAME
4262
tags:
4363
- docker
64+
4465
test_sonar_job:
4566
stage: test
4667
only:

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Inspired by https://github.com/SonarCommunity/sonar-github
77

88
# Current version
99

10+
## Version 5.1.3
11+
12+
* merged [Throw exception on quality gate fail/error](https://github.com/javamachr/sonar-gitlab-plugin/pull/24)
1013

1114
## Version 5.1.2
1215

@@ -338,6 +341,7 @@ https://docs.gitlab.com/ce/ci/variables/#9-0-renaming
338341
| sonar.gitlab.disable_proxy | Disable proxy if system contains proxy config (default false) | Administration, Variable | >= 4.0.0 |
339342
| sonar.gitlab.merge_request_discussion | Allows to post the comments as discussions (default false) | Project, Variable | >= 4.0.0 |
340343
| sonar.gitlab.ci_merge_request_iid | The IID of the merge request if it’s pipelines for merge requests | Project, Variable | >= 4.0.0 |
344+
| sonar.gitlab.fail_on_qualitygate | Fail scan if the quality gate fails (default false), this is required to fail the scanner since the plugin requires the `sonar.qualitygate.wait=false` to run | Project, Variable | >= 5.0.2 |
341345

342346
- Administration : **Settings** globals in SonarQube
343347
- Project : **Settings** of project in SonarQube

gitlab_settings.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
3+
<servers>
4+
<server>
5+
<id>gitlab-maven</id>
6+
<configuration>
7+
<httpHeaders>
8+
<property>
9+
<name>Job-Token</name>
10+
<value>${CI_JOB_TOKEN}</value>
11+
</property>
12+
</httpHeaders>
13+
</configuration>
14+
</server>
15+
</servers>
16+
</settings>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.talanlabs</groupId>
1212
<artifactId>sonar-gitlab-plugin</artifactId>
13-
<version>5.1.2</version>
13+
<version>5.1.3</version>
1414
<name>SonarQube :: GitLab Plugin</name>
1515
<description>GitLab Plugin for Reporting</description>
1616
<packaging>sonar-plugin</packaging>

src/main/java/com/talanlabs/sonar/plugins/gitlab/CommitPublishPostJob.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public void execute(@NotNull PostJobContext context) {
9898

9999
Reporter report = reporterBuilder.build(qualityGate, issues);
100100
notification(report);
101+
102+
if(gitLabPluginConfiguration.failOnQualityGate() && QualityGate.Status.ERROR.equals(qualityGate.getStatus()))
103+
{
104+
throw MessageException.of("Quality Gate failed. Exiting scan with failure.");
105+
}
106+
101107
} catch (MessageException e) {
102108
StatusNotificationsMode i = gitLabPluginConfiguration.statusNotificationsMode();
103109
if (i == StatusNotificationsMode.COMMIT_STATUS) {

src/main/java/com/talanlabs/sonar/plugins/gitlab/GitLabPlugin.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class GitLabPlugin implements Plugin {
6969
public static final String GITLAB_MERGE_REQUEST_DISCUSSION = "sonar.gitlab.merge_request_discussion";
7070
public static final String GITLAB_CI_MERGE_REQUEST_IID = "sonar.gitlab.ci_merge_request_iid";
7171
public static final String SONAR_PULL_REQUEST_KEY = "sonar.pullrequest.key";
72+
public static final String GITLAB_FAIL_ON_QUALITY_GATE = "sonar.gitlab.fail_on_qualitygate";
7273

7374
public static final String CATEGORY = "gitlab";
7475
public static final String SUBCATEGORY = "reporting";
@@ -166,7 +167,11 @@ public static List<PropertyDefinition> definitions() {
166167
PropertyDefinition.builder(GITLAB_CI_MERGE_REQUEST_IID).name("Merge Request IID").description("The IID of the merge request if it’s pipelines for merge requests")
167168
.category(CATEGORY).subCategory(SUBCATEGORY).type(PropertyType.INTEGER)
168169
.defaultValue(String.valueOf(-1))
169-
.index(35).build()
170+
.index(35).build(),
171+
PropertyDefinition.builder(GITLAB_FAIL_ON_QUALITY_GATE).name("Quality Gate fail").description("Fail the scan process based on quality gate error status")
172+
.category(CATEGORY).subCategory(SUBCATEGORY).type(PropertyType.BOOLEAN)
173+
.defaultValue(String.valueOf(false))
174+
.index(36).build()
170175

171176
);
172177
}

src/main/java/com/talanlabs/sonar/plugins/gitlab/GitLabPluginConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,8 @@ public int pullRequestKey() {
280280
return configuration.getInt(GitLabPlugin.SONAR_PULL_REQUEST_KEY).orElse(-1);
281281
}
282282

283+
public boolean failOnQualityGate() {
284+
return configuration.getBoolean(GitLabPlugin.GITLAB_FAIL_ON_QUALITY_GATE).orElse(false);
285+
}
283286

284287
}

src/test/java/com/talanlabs/sonar/plugins/gitlab/CommitPublishPostJobTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.sonar.api.utils.System2;
4141

4242
import java.io.File;
43+
import java.util.ArrayList;
4344
import java.util.Arrays;
4445
import java.util.Collections;
4546
import java.util.List;
@@ -626,4 +627,74 @@ public void testFaileReporterNotificationExit() {
626627
Mockito.verify(commitFacade, never()).createOrUpdateSonarQubeStatus("failed", "SonarQube Condition Error:0 Warning:2 Ok:3 SonarQube reported no issues");
627628
}
628629

630+
@Test
631+
public void testFailedWithExceptionWithQualityGageFailSetting() {
632+
settings.setProperty(GitLabPlugin.GITLAB_STATUS_NOTIFICATION_MODE, StatusNotificationsMode.NOTHING.getMeaning());
633+
settings.setProperty(GitLabPlugin.GITLAB_FAIL_ON_QUALITY_GATE, "true");
634+
635+
PostJobIssue issue1 = Utils.newMockedPostJobIssue("foo:src", Severity.BLOCKER, true, "msg4");
636+
PostJobIssue issue2 = Utils.newMockedPostJobIssue("foo", Severity.BLOCKER, true, "msg");
637+
Iterable<PostJobIssue> issues = Arrays.asList(issue1, issue2);
638+
639+
List<Issue> issuelist = new ArrayList<Issue>();
640+
641+
QualityGate qualityGate = Mockito.mock(QualityGate.class);
642+
when(qualityGate.getStatus()).thenReturn(QualityGate.Status.ERROR);
643+
when(sonarFacade.loadQualityGate()).thenReturn(qualityGate);
644+
when(sonarFacade.getNewIssues()).thenReturn(issuelist);
645+
646+
647+
Reporter reporter = Mockito.mock(Reporter.class);
648+
when(reporter.getStatus()).thenReturn("failed");
649+
when(reporter.getStatusDescription()).thenReturn("SonarQube reported 2 issues");
650+
when(reporterBuilder.build(qualityGate, issuelist)).thenReturn(reporter);
651+
652+
Assertions.assertThatThrownBy(() -> commitPublishPostJob.execute(context)).isInstanceOf(MessageException.class).hasMessage("Quality Gate failed. Exiting scan with failure.");
653+
654+
Mockito.verify(commitFacade, never()).createOrUpdateSonarQubeStatus("failed", "SonarQube reported 2 issues");
655+
656+
}
657+
658+
@Test
659+
public void testFailedWithoutExceptionWithQualityGageFailSettingAsFalse() {
660+
settings.setProperty(GitLabPlugin.GITLAB_STATUS_NOTIFICATION_MODE, StatusNotificationsMode.NOTHING.getMeaning());
661+
settings.setProperty(GitLabPlugin.GITLAB_FAIL_ON_QUALITY_GATE, "false");
662+
663+
PostJobIssue issue1 = Utils.newMockedPostJobIssue("foo:src", Severity.BLOCKER, true, "msg4");
664+
PostJobIssue issue2 = Utils.newMockedPostJobIssue("foo", Severity.BLOCKER, true, "msg");
665+
Iterable<PostJobIssue> issues = Arrays.asList(issue1, issue2);
666+
667+
Reporter reporter = Mockito.mock(Reporter.class);
668+
when(reporter.getStatus()).thenReturn("failed");
669+
when(reporter.getStatusDescription()).thenReturn("SonarQube reported 2 issues");
670+
671+
when(reporterBuilder.build(eq(null), any())).thenReturn(reporter);
672+
673+
commitPublishPostJob.execute(context);
674+
675+
Mockito.verify(reporterBuilder).build(eq(null), any());
676+
Mockito.verify(commitFacade, never()).createOrUpdateSonarQubeStatus("failed", "SonarQube reported 2 issues");
677+
678+
}
679+
680+
@Test
681+
public void testFailedWithoutExceptionWithNoQualityGageFailSetting() {
682+
settings.setProperty(GitLabPlugin.GITLAB_STATUS_NOTIFICATION_MODE, StatusNotificationsMode.NOTHING.getMeaning());
683+
684+
PostJobIssue issue1 = Utils.newMockedPostJobIssue("foo:src", Severity.BLOCKER, true, "msg4");
685+
PostJobIssue issue2 = Utils.newMockedPostJobIssue("foo", Severity.BLOCKER, true, "msg");
686+
Iterable<PostJobIssue> issues = Arrays.asList(issue1, issue2);
687+
688+
Reporter reporter = Mockito.mock(Reporter.class);
689+
when(reporter.getStatus()).thenReturn("failed");
690+
when(reporter.getStatusDescription()).thenReturn("SonarQube reported 2 issues");
691+
692+
when(reporterBuilder.build(eq(null), any())).thenReturn(reporter);
693+
694+
commitPublishPostJob.execute(context);
695+
696+
Mockito.verify(reporterBuilder).build(eq(null), any());
697+
Mockito.verify(commitFacade, never()).createOrUpdateSonarQubeStatus("failed", "SonarQube reported 2 issues");
698+
699+
}
629700
}

0 commit comments

Comments
 (0)