Skip to content

Commit

Permalink
Add support for decorating merge requests on Gitlab
Browse files Browse the repository at this point in the history
Uses Gitlab's rest API to create conversations/threads on a merge request containing annotations for each issue discovered by SonarQube, and provides a message on the merge request containing the scan summary.
  • Loading branch information
tisoft authored and mc1arke committed Jan 8, 2020
1 parent 7a582ea commit 9a12d6d
Show file tree
Hide file tree
Showing 14 changed files with 780 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Either build the project or [download a compatible release version of the plugin

# Features
The plugin is intended to support the [features and parameters specified in the SonarQube documentation](https://docs.sonarqube.org/latest/branches/overview/), with the following caveats
* __Pull Requests:__ Analysis of Pull Requests is fully supported, but the decoration of pull requests is only currently available for Github, and Bitbucket Server, and only as an experimental feature
* __Pull Requests:__ Analysis of Pull Requests is fully supported, but the decoration of pull requests is only currently available for Github, Gitlab and Bitbucket Server, and only as an experimental feature

# Properties
Property key | Description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.mc1arke.sonarqube.plugin.ce.CommunityReportAnalysisComponentProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PullRequestBuildStatusDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.server.BitbucketServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab.GitlabServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchConfigurationLoader;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityBranchParamsValidator;
import com.github.mc1arke.sonarqube.plugin.scanner.CommunityProjectBranchesLoader;
Expand All @@ -46,6 +47,7 @@ public class CommunityBranchPlugin implements Plugin, CoreExtension {
private static final String GITHUB_INTEGRATION_SUBCATEGORY_LABEL = "Integration With Github";
private static final String GENERAL = "General";
private static final String BITBUCKET_INTEGRATION_SUBCATEGORY_LABEL = "Integration With Bitbucket";
private static final String GITLAB_INTEGRATION_SUBCATEGORY_LABEL = "Integration With Gitlab";

@Override
public String getName() {
Expand Down Expand Up @@ -86,7 +88,7 @@ public void load(CoreExtension.Context context) {
context.addExtensions(
PropertyDefinition.builder("sonar.pullrequest.provider").category(PULL_REQUEST_CATEGORY_LABEL)
.subCategory("General").onlyOnQualifiers(Qualifiers.PROJECT).name("Provider")
.type(PropertyType.SINGLE_SELECT_LIST).options("Github", "BitbucketServer").build(),
.type(PropertyType.SINGLE_SELECT_LIST).options("Github", "BitbucketServer", "GitlabServer").build(),

PropertyDefinition.builder("sonar.alm.github.app.privateKey.secured")
.category(PULL_REQUEST_CATEGORY_LABEL).subCategory(GITHUB_INTEGRATION_SUBCATEGORY_LABEL)
Expand Down Expand Up @@ -146,7 +148,36 @@ public void load(CoreExtension.Context context) {

PropertyDefinition.builder(BitbucketServerPullRequestDecorator.PULL_REQUEST_BITBUCKET_PROJECT_KEY).category(PULL_REQUEST_CATEGORY_LABEL).subCategory(BITBUCKET_INTEGRATION_SUBCATEGORY_LABEL)
.onlyOnQualifiers(Qualifiers.PROJECT).name("ProjectKey").description("This is used for '/projects' repos. Only set one User Slug or ProjectKey!")
.type(PropertyType.STRING).index(1).build());
.type(PropertyType.STRING).index(1).build(),

PropertyDefinition.builder(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_URL)
.category(PULL_REQUEST_CATEGORY_LABEL)
.subCategory(GITLAB_INTEGRATION_SUBCATEGORY_LABEL)
.onQualifiers(Qualifiers.PROJECT)
.name("URL for Gitlab (Server or Cloud) instance")
.description("Example: https://ci-server.local/gitlab")
.type(PropertyType.STRING)
.defaultValue("https://gitlab.com")
.build(),

PropertyDefinition.builder(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_TOKEN)
.category(PULL_REQUEST_CATEGORY_LABEL)
.subCategory(GITLAB_INTEGRATION_SUBCATEGORY_LABEL)
.onQualifiers(Qualifiers.PROJECT)
.name("The token for the user to comment to the PR on Gitlab (Server or Cloud) instance")
.description("Token used for authentication and commenting to your Gitlab instance")
.type(PropertyType.STRING)
.build(),

PropertyDefinition.builder(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_REPOSITORY_SLUG)
.category(PULL_REQUEST_CATEGORY_LABEL)
.subCategory(GITLAB_INTEGRATION_SUBCATEGORY_LABEL)
.onQualifiers(Qualifiers.PROJECT)
.name("Repository Slug for the Gitlab (Server or Cloud) instance")
.description("The repository slug can be eiter in the form of user/repo or it can be the Project ID")
.type(PropertyType.STRING)
.build()
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.GithubPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v3.RestApplicationAuthenticationProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.github.v4.GraphqlCheckRunProvider;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab.GitlabServerPullRequestDecorator;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.bitbucket.server.BitbucketServerPullRequestDecorator;
import org.sonar.ce.task.projectanalysis.container.ReportAnalysisComponentProvider;

Expand All @@ -39,7 +40,7 @@ public List<Object> getComponents() {
return Arrays.asList(CommunityBranchLoaderDelegate.class, PullRequestPostAnalysisTask.class,
PostAnalysisIssueVisitor.class, GithubPullRequestDecorator.class,
GraphqlCheckRunProvider.class, RestApplicationAuthenticationProvider.class,
BitbucketServerPullRequestDecorator.class);
BitbucketServerPullRequestDecorator.class, GitlabServerPullRequestDecorator.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private Optional<MeasureWrapper> findMeasure(String metricKey) {
.map(MeasureWrapper::new);
}

private Optional<QualityGate.Condition> findQualityGateCondition(String metricKey) {
public Optional<QualityGate.Condition> findQualityGateCondition(String metricKey) {
return qualityGate.getConditions().stream().filter(c -> metricKey.equals(c.getMetricKey())).findFirst();
}

Expand Down
Loading

0 comments on commit 9a12d6d

Please sign in to comment.