Skip to content

Commit

Permalink
[GITLAB] support multiple pipelines on a single commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tisoft authored and mc1arke committed Jun 7, 2020
1 parent 35c40fe commit 928c766
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class GitlabServerPullRequestDecorator implements PullRequestBuildStatusD
"sonar.pullrequest.gitlab.projectId";
public static final String PULLREQUEST_GITLAB_PROJECT_URL =
"sonar.pullrequest.gitlab.projectUrl";
public static final String PULLREQUEST_GITLAB_PIPELINE_ID =
"com.github.mc1arke.sonarqube.plugin.branch.pullrequest.gitlab.pipelineId";

private static final Logger LOGGER = Loggers.get(GitlabServerPullRequestDecorator.class);
private static final List<String> OPEN_ISSUE_STATUSES =
Expand Down Expand Up @@ -166,7 +168,7 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysis, AlmS

String coverageValue = analysis.getNewCoverage().orElse(BigDecimal.ZERO).toString();

postStatus(statusUrl, headers, analysis, coverageValue);
postStatus(new StringBuilder(statusUrl), headers, analysis, coverageValue);

postCommitComment(mergeRequestDiscussionURL, headers, summaryContentParams);

Expand Down Expand Up @@ -309,22 +311,22 @@ private void postCommitComment(String commitCommentUrl, Map<String, String> head
}
}

private void postStatus(String statusPostUrl, Map<String, String> headers, AnalysisDetails analysis,
private void postStatus(StringBuilder statusPostUrl, Map<String, String> headers, AnalysisDetails analysis,
String coverage) throws IOException {
//See https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit
statusPostUrl += "?name=SonarQube";
statusPostUrl.append("?name=SonarQube");
String status = (analysis.getQualityGateStatus() == QualityGate.Status.OK ? "success" : "failed");
statusPostUrl += "&state=" + status;
statusPostUrl += "&target_url=" + URLEncoder.encode(String.format("%s/dashboard?id=%s&pullRequest=%s", server.getPublicRootUrl(),
statusPostUrl.append("&state=").append(status);
statusPostUrl.append("&target_url=").append(URLEncoder.encode(String.format("%s/dashboard?id=%s&pullRequest=%s", server.getPublicRootUrl(),
URLEncoder.encode(analysis.getAnalysisProjectKey(),
StandardCharsets.UTF_8.name()), URLEncoder
.encode(analysis.getBranchName(),
StandardCharsets.UTF_8.name())), StandardCharsets.UTF_8.name());
statusPostUrl+="&description="+URLEncoder.encode("SonarQube Status", StandardCharsets.UTF_8.name());
statusPostUrl+="&coverage="+coverage;
//TODO: add pipelineId if we have it
StandardCharsets.UTF_8.name())), StandardCharsets.UTF_8.name()));
statusPostUrl.append("&description=").append(URLEncoder.encode("SonarQube Status", StandardCharsets.UTF_8.name()));
statusPostUrl.append("&coverage=").append(coverage);
analysis.getScannerProperty(PULLREQUEST_GITLAB_PIPELINE_ID).ifPresent(pipelineId -> statusPostUrl.append("&pipeline_id=").append(pipelineId));

HttpPost httpPost = new HttpPost(statusPostUrl);
HttpPost httpPost = new HttpPost(statusPostUrl.toString());
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public void execute(SensorContext sensorContext) {
.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PROJECT_ID, v));
Optional.ofNullable(system2.envVariable("CI_MERGE_REQUEST_PROJECT_URL")).ifPresent(v -> sensorContext
.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PROJECT_URL, v));
Optional.ofNullable(system2.envVariable("CI_PIPELINE_ID")).ifPresent(v -> sensorContext
.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PIPELINE_ID, v));
}

Optional.ofNullable(system2.property(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_INSTANCE_URL)).ifPresent(
Expand All @@ -57,6 +59,8 @@ public void execute(SensorContext sensorContext) {
v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PROJECT_ID, v));
Optional.ofNullable(system2.property(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PROJECT_URL)).ifPresent(
v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PROJECT_URL, v));
Optional.ofNullable(system2.property(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PIPELINE_ID)).ifPresent(
v -> sensorContext.addContextProperty(GitlabServerPullRequestDecorator.PULLREQUEST_GITLAB_PIPELINE_ID, v));
}

}

0 comments on commit 928c766

Please sign in to comment.