From 8a1b257bb0f3ad7da9b7182543f8030316177fd2 Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Tue, 11 Feb 2020 17:22:07 +0100 Subject: [PATCH] [GITLAB] URL needs to point to the GitLab API Fixes #84 --- .../gitlab/GitlabServerPullRequestDecorator.java | 11 +++++------ .../scanner/ScannerPullRequestPropertySensor.java | 6 +++--- .../gitlab/GitlabServerPullRequestDecoratorTest.java | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecorator.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecorator.java index 2645505ee..ca2258550 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecorator.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecorator.java @@ -68,8 +68,8 @@ public class GitlabServerPullRequestDecorator implements PullRequestBuildStatusDecorator { - public static final String PULLREQUEST_GITLAB_URL = - "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.url"; + public static final String PULLREQUEST_GITLAB_API_URL = + "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.api.url"; public static final String PULLREQUEST_GITLAB_REPOSITORY_SLUG = "com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.repositorySlug"; @@ -94,10 +94,10 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysis, AlmS String revision = analysis.getCommitSha(); try { - final String hostURL = analysis.getScannerProperty(PULLREQUEST_GITLAB_URL).orElseThrow( + final String apiURL = analysis.getScannerProperty(PULLREQUEST_GITLAB_API_URL).orElseThrow( () -> new IllegalStateException(String.format( "Could not decorate Gitlab merge request. '%s' has not been set in scanner properties", - PULLREQUEST_GITLAB_URL))); + PULLREQUEST_GITLAB_API_URL))); final String apiToken = almSettingDto.getPersonalAccessToken(); final String repositorySlug = analysis.getScannerProperty(PULLREQUEST_GITLAB_REPOSITORY_SLUG).orElseThrow( () -> new IllegalStateException(String.format( @@ -105,8 +105,7 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysis, AlmS PULLREQUEST_GITLAB_REPOSITORY_SLUG))); final String pullRequestId = analysis.getBranchName(); - final String restURL = String.format("%s/api/v4", hostURL); - final String projectURL = restURL + String.format("/projects/%s", URLEncoder + final String projectURL = apiURL + String.format("/projects/%s", URLEncoder .encode(repositorySlug, StandardCharsets.UTF_8.name())); final String statusUrl = projectURL + String.format("/statuses/%s", revision); final String mergeRequestURl = projectURL + String.format("/merge_requests/%s", pullRequestId); diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/ScannerPullRequestPropertySensor.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/ScannerPullRequestPropertySensor.java index 660c5a61d..6b1880977 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/ScannerPullRequestPropertySensor.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/ScannerPullRequestPropertySensor.java @@ -44,12 +44,12 @@ public void describe(SensorDescriptor sensorDescriptor) { public void execute(SensorContext sensorContext) { if (Boolean.parseBoolean(system2.envVariable("GITLAB_CI"))) { Optional.ofNullable(system2.envVariable("CI_API_V4_URL")).ifPresent( - v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_URL, v)); + v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_API_URL, v)); Optional.ofNullable(system2.envVariable("CI_PROJECT_PATH")).ifPresent(v -> sensorContext .addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_REPOSITORY_SLUG, v)); } else { - Optional.ofNullable(system2.property(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_URL)).ifPresent( - v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_URL, v)); + Optional.ofNullable(system2.property(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_API_URL)).ifPresent( + v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_API_URL, v)); Optional.ofNullable(system2.property(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_REPOSITORY_SLUG)) .ifPresent(v -> sensorContext .addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_REPOSITORY_SLUG, diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecoratorTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecoratorTest.java index ff50a9252..a70735322 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecoratorTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/ce/pullrequest/gitlab/GitlabServerPullRequestDecoratorTest.java @@ -78,8 +78,8 @@ public void decorateQualityGateStatus() { when(almSettingDto.getPersonalAccessToken()).thenReturn("token"); AnalysisDetails analysisDetails = mock(AnalysisDetails.class); - when(analysisDetails.getScannerProperty(eq(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_URL))) - .thenReturn(Optional.of(wireMockRule.baseUrl())); + when(analysisDetails.getScannerProperty(eq(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_API_URL))) + .thenReturn(Optional.of(wireMockRule.baseUrl()+"/api/v4")); when(analysisDetails .getScannerProperty(eq(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_REPOSITORY_SLUG))) .thenReturn(Optional.of(repositorySlug));