Skip to content

Commit

Permalink
[GITLAB] URL needs to point to the GitLab API
Browse files Browse the repository at this point in the history
Fixes #84
  • Loading branch information
tisoft authored and mc1arke committed Jun 7, 2020
1 parent af9c21b commit 8a1b257
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -94,19 +94,18 @@ 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(
"Could not decorate Gitlab merge request. '%s' has not been set in scanner properties",
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 8a1b257

Please sign in to comment.