Skip to content

Commit

Permalink
Resolve a discussion when successfully passing a quality gate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Zhoholiev authored and Yurii Zhoholiev committed Dec 13, 2020
1 parent d5232b4 commit bf56780
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
Expand Down Expand Up @@ -173,7 +174,14 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysis, AlmS

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

postCommitComment(mergeRequestDiscussionURL, headers, summaryContentParams);
Discussion discussion = postCommitComment(mergeRequestDiscussionURL, headers, summaryContentParams);

if (analysis.getQualityGateStatus() == QualityGate.Status.OK) {
List<NameValuePair> resolveContentParams = Collections.singletonList(
new BasicNameValuePair("resolved", "true"));

putCommitComment(mergeRequestDiscussionURL + String.format("/%s", discussion.getId()), headers, resolveContentParams);
}

for (PostAnalysisIssueVisitor.ComponentIssue issue : openIssues) {
String path = analysis.getSCMPathForIssue(issue).orElse(null);
Expand Down Expand Up @@ -298,7 +306,7 @@ private void deleteCommitDiscussionNote(String commitDiscussionNoteURL, Map<Stri
}
}

private void postCommitComment(String commitCommentUrl, Map<String, String> headers, List<NameValuePair> params) throws IOException {
private Discussion postCommitComment(String commitCommentUrl, Map<String, String> headers, List<NameValuePair> params) throws IOException {
//https://docs.gitlab.com/ee/api/commits.html#post-comment-to-commit
HttpPost httpPost = new HttpPost(commitCommentUrl);
for (Map.Entry<String, String> entry : headers.entrySet()) {
Expand All @@ -311,6 +319,33 @@ private void postCommitComment(String commitCommentUrl, Map<String, String> head
try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
HttpResponse httpResponse = httpClient.execute(httpPost);
validateGitlabResponse(httpResponse, 201, "Comment posted");

HttpEntity entity = httpResponse.getEntity();
Discussion discussion = new ObjectMapper()
.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.readValue(IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8), Discussion.class);

LOGGER.info("Discussion received");

return discussion;
}
}

private void putCommitComment(String commitCommentUrl, Map<String, String> headers, List<NameValuePair> params) throws IOException {
// https://docs.gitlab.com/ee/api/commits.html#post-comment-to-commit
HttpPut httpPut = new HttpPut(commitCommentUrl);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.addHeader(entry.getKey(), entry.getValue());
}
httpPut.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

LOGGER.info("Putting {} with headers {} to {}", params, headers, commitCommentUrl);

try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
HttpResponse httpResponse = httpClient.execute(httpPut);
validateGitlabResponse(httpResponse, 200, "Comment updated");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.AnalysisDetails;
import com.github.mc1arke.sonarqube.plugin.ce.pullrequest.PostAnalysisIssueVisitor;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.net.HttpHeaders;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -149,7 +151,9 @@ public void decorateQualityGateStatus() {

wireMockRule.stubFor(post(urlPathEqualTo("/api/v4/projects/" + urlEncode(repositorySlug) + "/merge_requests/" + branchName + "/discussions"))
.withRequestBody(equalTo("body=summary"))
.willReturn(created()));
.willReturn(created()
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("{\"id\": \"6a9c1750b37d513a43987b574953fceb50b03ce7\"}")));

wireMockRule.stubFor(post(urlPathEqualTo("/api/v4/projects/" + urlEncode(repositorySlug) + "/merge_requests/" + branchName + "/discussions"))
.withRequestBody(equalTo("body=issue&" +
Expand All @@ -160,7 +164,9 @@ public void decorateQualityGateStatus() {
urlEncode("position[new_path]") + "=" + urlEncode(filePath) + "&" +
urlEncode("position[new_line]") + "=" + lineNumber + "&" +
urlEncode("position[position_type]") + "=text"))
.willReturn(created()));
.willReturn(created()
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody("{\"id\": \"6a9c1750b37d513a43987b574953fceb50b03ce7\"}")));

Server server = mock(Server.class);
when(server.getPublicRootUrl()).thenReturn(sonarRootUrl);
Expand Down

0 comments on commit bf56780

Please sign in to comment.