Skip to content

Make the GitLab status name configurable #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ https://docs.gitlab.com/ce/ci/variables/#9-0-renaming
| sonar.gitlab.merge_request_discussion | Allows to post the comments as discussions (default false) | Project, Variable | >= 4.0.0 |
| sonar.gitlab.ci_merge_request_iid | The IID of the merge request if it’s pipelines for merge requests | Project, Variable | >= 4.0.0 |
| 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 |
| sonar.gitlab.status_name | The name of the commit status created by the plugin (default `sonarqube`) | Project, Variable | >= 5.2.2 |

- Administration : **Settings** globals in SonarQube
- Project : **Settings** of project in SonarQube
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public class GitLabApiV4Wrapper implements IGitLabApiWrapper {

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

private static final String COMMIT_CONTEXT = "sonarqube";

private final GitLabPluginConfiguration config;
private GitLabAPI gitLabAPIV4;
private GitLabProject gitLabProject;
Expand Down Expand Up @@ -220,7 +218,7 @@ private boolean verifyProjectName(GitLabProject project) {
public void createOrUpdateSonarQubeStatus(String status, String statusDescription) {
try {
gitLabAPIV4.getGitLabAPICommits()
.postCommitStatus(gitLabProject.getId(), getFirstCommitSHA(), status, config.refName(), COMMIT_CONTEXT, null, statusDescription);
.postCommitStatus(gitLabProject.getId(), getFirstCommitSHA(), status, config.refName(), config.statusName(), null, statusDescription);
} catch (IOException e) {
// Workaround for https://gitlab.com/gitlab-org/gitlab-ce/issues/25807
if (e.getMessage() != null && e.getMessage().contains("Cannot transition status")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class GitLabPlugin implements Plugin {
public static final String GITLAB_CI_MERGE_REQUEST_IID = "sonar.gitlab.ci_merge_request_iid";
public static final String SONAR_PULL_REQUEST_KEY = "sonar.pullrequest.key";
public static final String GITLAB_FAIL_ON_QUALITY_GATE = "sonar.gitlab.fail_on_qualitygate";
public static final String GITLAB_STATUS_NAME = "sonar.gitlab.status_name";

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

);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,8 @@ public boolean failOnQualityGate() {
return configuration.getBoolean(GitLabPlugin.GITLAB_FAIL_ON_QUALITY_GATE).orElse(false);
}

public String statusName() {
return configuration.get(GitLabPlugin.GITLAB_STATUS_NAME).orElse("sonarqube");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public void testStatusSuccess() throws IOException {
GitLabPluginConfiguration gitLabPluginConfiguration = mock(GitLabPluginConfiguration.class);
when(gitLabPluginConfiguration.commitSHA()).thenReturn(Collections.singletonList("1"));
when(gitLabPluginConfiguration.refName()).thenReturn("master");
when(gitLabPluginConfiguration.statusName()).thenReturn("sonarqube-1");

GitLabApiV4Wrapper facade = new GitLabApiV4Wrapper(gitLabPluginConfiguration);

GitLabAPI gitLabAPI = mock(GitLabAPI.class);
facade.setGitLabAPI(gitLabAPI);

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

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

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

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

verify(gitLabAPICommits).postCommitStatus(1, "1", "pending", "master", "sonarqube", null, "nothing");
verify(gitLabAPICommits).postCommitStatus(1, "1", "pending", "master", "sonarqube-1", null, "nothing");
}

@Test
public void testStatusFailed() throws IOException {
GitLabPluginConfiguration gitLabPluginConfiguration = mock(GitLabPluginConfiguration.class);
when(gitLabPluginConfiguration.commitSHA()).thenReturn(Collections.singletonList("1"));
when(gitLabPluginConfiguration.refName()).thenReturn("master");
when(gitLabPluginConfiguration.statusName()).thenReturn("sonarqube-2");

GitLabApiV4Wrapper facade = new GitLabApiV4Wrapper(gitLabPluginConfiguration);

GitLabAPI gitLabAPI = mock(GitLabAPI.class);
facade.setGitLabAPI(gitLabAPI);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public void testProject() {
Assertions.assertThat(config.isMergeRequestDiscussionEnabled()).isFalse();
settings.setProperty(GitLabPlugin.GITLAB_MERGE_REQUEST_DISCUSSION, "true");
Assertions.assertThat(config.isMergeRequestDiscussionEnabled()).isTrue();

Assertions.assertThat(config.statusName()).isEqualTo("sonarqube");
settings.setProperty(GitLabPlugin.GITLAB_STATUS_NAME, "sonar-analysis-1");
Assertions.assertThat(config.statusName()).isEqualTo("sonar-analysis-1");
}

@Test
Expand Down