Skip to content

Commit 016a8c5

Browse files
authored
Merge pull request #31 from mirango-kft/feature/configurable-status-name
Make the GitLab status name configurable
2 parents f6c4218 + a834510 commit 016a8c5

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ https://docs.gitlab.com/ce/ci/variables/#9-0-renaming
350350
| sonar.gitlab.merge_request_discussion | Allows to post the comments as discussions (default false) | Project, Variable | >= 4.0.0 |
351351
| sonar.gitlab.ci_merge_request_iid | The IID of the merge request if it’s pipelines for merge requests | Project, Variable | >= 4.0.0 |
352352
| 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 |
353+
| sonar.gitlab.status_name | The name of the commit status created by the plugin (default `sonarqube`) | Project, Variable | >= 5.2.2 |
353354

354355
- Administration : **Settings** globals in SonarQube
355356
- Project : **Settings** of project in SonarQube

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public class GitLabApiV4Wrapper implements IGitLabApiWrapper {
4545

4646
private static final Logger LOG = Loggers.get(GitLabApiV4Wrapper.class);
4747

48-
private static final String COMMIT_CONTEXT = "sonarqube";
49-
5048
private final GitLabPluginConfiguration config;
5149
private GitLabAPI gitLabAPIV4;
5250
private GitLabProject gitLabProject;
@@ -220,7 +218,7 @@ private boolean verifyProjectName(GitLabProject project) {
220218
public void createOrUpdateSonarQubeStatus(String status, String statusDescription) {
221219
try {
222220
gitLabAPIV4.getGitLabAPICommits()
223-
.postCommitStatus(gitLabProject.getId(), getFirstCommitSHA(), status, config.refName(), COMMIT_CONTEXT, null, statusDescription);
221+
.postCommitStatus(gitLabProject.getId(), getFirstCommitSHA(), status, config.refName(), config.statusName(), null, statusDescription);
224222
} catch (IOException e) {
225223
// Workaround for https://gitlab.com/gitlab-org/gitlab-ce/issues/25807
226224
if (e.getMessage() != null && e.getMessage().contains("Cannot transition 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
@@ -70,6 +70,7 @@ public class GitLabPlugin implements Plugin {
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";
7272
public static final String GITLAB_FAIL_ON_QUALITY_GATE = "sonar.gitlab.fail_on_qualitygate";
73+
public static final String GITLAB_STATUS_NAME = "sonar.gitlab.status_name";
7374

7475
public static final String CATEGORY = "gitlab";
7576
public static final String SUBCATEGORY = "reporting";
@@ -171,7 +172,11 @@ public static List<PropertyDefinition> definitions() {
171172
PropertyDefinition.builder(GITLAB_FAIL_ON_QUALITY_GATE).name("Quality Gate fail").description("Fail the scan process based on quality gate error status")
172173
.category(CATEGORY).subCategory(SUBCATEGORY).type(PropertyType.BOOLEAN)
173174
.defaultValue(String.valueOf(false))
174-
.index(36).build()
175+
.index(36).build(),
176+
PropertyDefinition.builder(GITLAB_STATUS_NAME).name("GitLab status name").description("The name of the commit status created by the plugin.")
177+
.category(CATEGORY).subCategory(SUBCATEGORY)
178+
.defaultValue("sonarqube")
179+
.index(37).hidden().build()
175180

176181
);
177182
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,8 @@ public boolean failOnQualityGate() {
284284
return configuration.getBoolean(GitLabPlugin.GITLAB_FAIL_ON_QUALITY_GATE).orElse(false);
285285
}
286286

287+
public String statusName() {
288+
return configuration.get(GitLabPlugin.GITLAB_STATUS_NAME).orElse("sonarqube");
289+
}
290+
287291
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public void testStatusSuccess() throws IOException {
6161
GitLabPluginConfiguration gitLabPluginConfiguration = mock(GitLabPluginConfiguration.class);
6262
when(gitLabPluginConfiguration.commitSHA()).thenReturn(Collections.singletonList("1"));
6363
when(gitLabPluginConfiguration.refName()).thenReturn("master");
64+
when(gitLabPluginConfiguration.statusName()).thenReturn("sonarqube-1");
6465

6566
GitLabApiV4Wrapper facade = new GitLabApiV4Wrapper(gitLabPluginConfiguration);
6667

6768
GitLabAPI gitLabAPI = mock(GitLabAPI.class);
6869
facade.setGitLabAPI(gitLabAPI);
6970

7071
GitLabAPICommits gitLabAPICommits = mock(GitLabAPICommits.class);
71-
when(gitLabAPICommits.postCommitStatus(1, "1", "pending", "master", "sonarqube", "server", "")).thenReturn(null);
72+
when(gitLabAPICommits.postCommitStatus(1, "1", "pending", "master", "sonarqube-1", "server", "")).thenReturn(null);
7273

7374
when(gitLabAPI.getGitLabAPICommits()).thenReturn(gitLabAPICommits);
7475

@@ -78,22 +79,23 @@ public void testStatusSuccess() throws IOException {
7879

7980
facade.createOrUpdateSonarQubeStatus("pending", "nothing");
8081

81-
verify(gitLabAPICommits).postCommitStatus(1, "1", "pending", "master", "sonarqube", null, "nothing");
82+
verify(gitLabAPICommits).postCommitStatus(1, "1", "pending", "master", "sonarqube-1", null, "nothing");
8283
}
8384

8485
@Test
8586
public void testStatusFailed() throws IOException {
8687
GitLabPluginConfiguration gitLabPluginConfiguration = mock(GitLabPluginConfiguration.class);
8788
when(gitLabPluginConfiguration.commitSHA()).thenReturn(Collections.singletonList("1"));
8889
when(gitLabPluginConfiguration.refName()).thenReturn("master");
90+
when(gitLabPluginConfiguration.statusName()).thenReturn("sonarqube-2");
8991

9092
GitLabApiV4Wrapper facade = new GitLabApiV4Wrapper(gitLabPluginConfiguration);
9193

9294
GitLabAPI gitLabAPI = mock(GitLabAPI.class);
9395
facade.setGitLabAPI(gitLabAPI);
9496

9597
GitLabAPICommits gitLabAPICommits = mock(GitLabAPICommits.class);
96-
when(gitLabAPICommits.postCommitStatus(1, "1", "pending", "master", "sonarqube", null, "nothing")).thenThrow(new IOException());
98+
when(gitLabAPICommits.postCommitStatus(1, "1", "pending", "master", "sonarqube-2", null, "nothing")).thenThrow(new IOException());
9799

98100
when(gitLabAPI.getGitLabAPICommits()).thenReturn(gitLabAPICommits);
99101

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public void testProject() {
188188
Assertions.assertThat(config.isMergeRequestDiscussionEnabled()).isFalse();
189189
settings.setProperty(GitLabPlugin.GITLAB_MERGE_REQUEST_DISCUSSION, "true");
190190
Assertions.assertThat(config.isMergeRequestDiscussionEnabled()).isTrue();
191+
192+
Assertions.assertThat(config.statusName()).isEqualTo("sonarqube");
193+
settings.setProperty(GitLabPlugin.GITLAB_STATUS_NAME, "sonar-analysis-1");
194+
Assertions.assertThat(config.statusName()).isEqualTo("sonar-analysis-1");
191195
}
192196

193197
@Test

0 commit comments

Comments
 (0)