Skip to content

Commit

Permalink
Enable discussion support see issue 186 for more details.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezienecker committed Apr 13, 2019
1 parent 0535ffe commit 93adab4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ https://docs.gitlab.com/ce/ci/variables/#9-0-renaming
| sonar.gitlab.issue_filter | Filter on issue, if MAJOR then show only MAJOR, CRITICAL and BLOCKER (default INFO) | Administration, Variable | >= 3.0.0 |
| sonar.gitlab.load_rules | Load rules for all issues (default false) | Administration, Variable | >= 3.0.0 |
| sonar.gitlab.disable_proxy | Disable proxy if system contains proxy config (default false) | Administration, Variable | >= 4.0.0 |
| sonar.gitlab.merge_request_discussion | Allows to post the comments as discussions (default false) | Project, Variable | >= 4.0.0 |

- Administration : **Settings** globals in SonarQube
- Project : **Settings** of project in SonarQube
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@

import com.talanlabs.gitlab.api.Paged;
import com.talanlabs.gitlab.api.v4.GitLabAPI;
import com.talanlabs.gitlab.api.v4.GitlabMergeRequestDiff;
import com.talanlabs.gitlab.api.v4.models.GitlabMergeRequest;
import com.talanlabs.gitlab.api.v4.models.GitlabPosition;
import com.talanlabs.gitlab.api.v4.models.commits.GitLabCommit;
import com.talanlabs.gitlab.api.v4.models.commits.GitLabCommitComments;
import com.talanlabs.gitlab.api.v4.models.commits.GitLabCommitDiff;
import com.talanlabs.gitlab.api.v4.models.discussion.GitlabDiscussion;
import com.talanlabs.gitlab.api.v4.models.projects.GitLabProject;
import com.talanlabs.gitlab.api.v4.models.users.GitLabUser;
import org.apache.commons.lang3.StringUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

Expand Down Expand Up @@ -267,12 +272,59 @@ public String getGitLabUrl(@Nullable String revision, String path, @Nullable Int
@Override
public void createOrUpdateReviewComment(String revision, String fullPath, Integer line, String body) {
try {
gitLabAPIV4.getGitLabAPICommits().postCommitComments(gitLabProject.getId(), revision != null ? revision : getFirstCommitSHA(), body, fullPath, line, "new");
if (config.isMergeRequestDiscussion()) {
createReviewDiscussion(revision, fullPath, line, body);
} else {
gitLabAPIV4.getGitLabAPICommits().postCommitComments(gitLabProject.getId(), revision != null ? revision : getFirstCommitSHA(), body, fullPath, line, "new");
}
} catch (IOException e) {
throw new IllegalStateException("Unable to create or update review comment in file " + fullPath + " at line " + line, e);
}
}

private void createReviewDiscussion(String revision, String fullPath, Integer line, String body) throws IOException {
Integer projectId = gitLabProject.getId();
String branch = config.refName();
String commitSha = revision != null ? revision : getFirstCommitSHA();

checkArgument(StringUtils.isNotBlank(branch), "The branch name (ref name) can not be empty.");

LOG.trace("Request Merge Request for project with id {} on the {} branch including the commit sha {}.", projectId, branch, commitSha);

Paged<GitlabMergeRequest> mergeRequests = gitLabAPIV4.getGitLabAPIMergeRequest()
.getAllProjectMergeRequestsBySourceBranchAndCommitShaWithStateOpen(projectId, branch, commitSha, null);

checkArgument(mergeRequests.getResults() != null && !mergeRequests.getResults().isEmpty(), "There are no merge requests.");

GitlabMergeRequest mergeRequest = mergeRequests.getResults().get(0);
Paged<GitlabMergeRequestDiff> mergeRequestDiffs = gitLabAPIV4
.getGitLabAPIMergeRequestDiff().getMergeRequestDiff(projectId, mergeRequest.getIid());

checkArgument(mergeRequestDiffs.getResults() != null && !mergeRequestDiffs.getResults().isEmpty(), "There are no merge request diffs.");

GitlabMergeRequestDiff mergeRequestDiff = mergeRequestDiffs.getResults().get(0);

GitlabDiscussion discussion = createMergeRequestDiscussion(mergeRequestDiff, fullPath, line, body);

gitLabAPIV4.getGitLabAPIMergeRequestDiscussion().createDiscussion(projectId, mergeRequest.getIid(), discussion);
}

private GitlabDiscussion createMergeRequestDiscussion(GitlabMergeRequestDiff mergeRequestDiff, String fullPath, Integer line, String body) {
GitlabPosition position = new GitlabPosition();
position.setBaseSha(mergeRequestDiff.getBaseCommitSha());
position.setStartSha(mergeRequestDiff.getStartCommitSha());
position.setHeadSha(mergeRequestDiff.getHeadCommitSha());
position.setPositionType(GitlabPosition.PositionType.TEXT);
position.setOldPath(fullPath);
position.setNewPath(fullPath);
position.setNewLine(line);

GitlabDiscussion discussion = new GitlabDiscussion();
discussion.setBody(body);
discussion.setPosition(position);
return discussion;
}

@Override
public void addGlobalComment(String comment) {
try {
Expand All @@ -286,4 +338,10 @@ private String getFirstCommitSHA() {
return config.commitSHA() != null && !config.commitSHA().isEmpty() ? config.commitSHA().get(0) : null;
}

public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class GitLabPlugin implements Plugin {
public static final String GITLAB_ISSUE_FILTER = "sonar.gitlab.issue_filter";
public static final String GITLAB_LOAD_RULES = "sonar.gitlab.load_rules";
public static final String GITLAB_DISABLE_PROXY = "sonar.gitlab.disable_proxy";
public static final String GITLAB_MERGE_REQUEST_DISCUSSION = "sonar.gitlab.merge_request_discussion";

public static final String CATEGORY = "gitlab";
public static final String SUBCATEGORY = "reporting";
Expand Down Expand Up @@ -156,7 +157,11 @@ public static List<PropertyDefinition> definitions() {
PropertyDefinition.builder(GITLAB_DISABLE_PROXY).name("Disable proxy").description("Disable proxy if system contains proxy config")
.category(CATEGORY).subCategory(SUBCATEGORY).type(PropertyType.BOOLEAN)
.defaultValue(String.valueOf(false))
.index(33).build()
.index(33).build(),
PropertyDefinition.builder(GITLAB_MERGE_REQUEST_DISCUSSION).name("Enable merge request discussion").description("Allows to post discussions instead of comments on merge request")
.category(CATEGORY).subCategory(SUBCATEGORY).type(PropertyType.BOOLEAN)
.defaultValue(String.valueOf(false))
.index(34).build()

);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,8 @@ public PasswordAuthentication getPasswordAuthentication() {
public String baseUrl() {
return baseUrl;
}

public boolean isMergeRequestDiscussion() {
return configuration.getBoolean(GitLabPlugin.GITLAB_MERGE_REQUEST_DISCUSSION).orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public void testProject() {
Assertions.assertThat(config.loadRule()).isFalse();
settings.setProperty(GitLabPlugin.GITLAB_LOAD_RULES, "true");
Assertions.assertThat(config.loadRule()).isTrue();

Assertions.assertThat(config.isMergeRequestDiscussion()).isFalse();
settings.setProperty(GitLabPlugin.GITLAB_MERGE_REQUEST_DISCUSSION, "true");
Assertions.assertThat(config.isMergeRequestDiscussion()).isTrue();
}

@Test
Expand Down

0 comments on commit 93adab4

Please sign in to comment.