diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-05-12 21:29:40 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-05-12 21:29:40 +0200 |
commit | 5765e0955d1516e035e7a970be9b992d9a90b270 (patch) | |
tree | 984a2cb5dada83778de77d36a57b798647bfd3e8 /sonar-ws-client | |
parent | 2b27e74cff921c1fa9a96fa4b5e5e5c11aad2352 (diff) | |
download | sonarqube-5765e0955d1516e035e7a970be9b992d9a90b270.tar.gz sonarqube-5765e0955d1516e035e7a970be9b992d9a90b270.zip |
SONAR-3755 refactor comments
Diffstat (limited to 'sonar-ws-client')
8 files changed, 154 insertions, 9 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java index cf82f66e74e..68c9a926e4f 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java @@ -20,11 +20,11 @@ package org.sonar.wsclient.issue; import com.github.kevinsawicki.http.HttpRequest; +import org.json.simple.JSONValue; import org.sonar.wsclient.internal.EncodingUtils; import org.sonar.wsclient.internal.HttpRequestFactory; import javax.annotation.Nullable; - import java.util.List; import java.util.Map; @@ -89,6 +89,17 @@ public class DefaultIssueClient implements IssueClient { } @Override + public IssueComment addComment(String issueKey, String markdownText) { + Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "text", markdownText); + HttpRequest request = requestFactory.post("/api/issues/add_comment", params); + if (!request.ok()) { + throw new IllegalStateException("Fail to add issue comment. Bad HTTP response status: " + request.code()); + } + String json = request.body(); + return new IssueComment((Map) JSONValue.parse(json)); + } + + @Override public List<String> transitions(String issueKey) { Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey); HttpRequest request = requestFactory.get("/api/issues/transitions", queryParams); diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issue.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issue.java index d22ed22afbb..ad922698f50 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issue.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issue.java @@ -22,9 +22,7 @@ package org.sonar.wsclient.issue; import org.sonar.wsclient.unmarshallers.JsonUtils; import javax.annotation.CheckForNull; -import java.util.Collections; -import java.util.Date; -import java.util.Map; +import java.util.*; /** * @since 3.6 @@ -66,11 +64,6 @@ public class Issue { return JsonUtils.getInteger(json, "line"); } - // TODO to be removed - public Double cost() { - return JsonUtils.getDouble(json, "effortToFix"); - } - @CheckForNull public Double effortToFix() { return JsonUtils.getDouble(json, "effortToFix"); @@ -129,4 +122,18 @@ public class Issue { } return attr; } + + /** + * Non-null list of comments + */ + public List<IssueComment> comments() { + List<IssueComment> comments = new ArrayList(); + List<Map> jsonComments = (List<Map>) json.get("comments"); + if (jsonComments != null) { + for (Map jsonComment : jsonComments) { + comments.add(new IssueComment(jsonComment)); + } + } + return comments; + } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java index 805ba49fbef..324c9e79132 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java @@ -36,6 +36,8 @@ public interface IssueClient { void plan(String issueKey, @Nullable String actionPlan); + IssueComment addComment(String issueKey, String markdownText); + void create(NewIssue issue); List<String> transitions(String issueKey); diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueComment.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueComment.java new file mode 100644 index 00000000000..f649d573b8f --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueComment.java @@ -0,0 +1,52 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.wsclient.issue; + +import org.sonar.wsclient.unmarshallers.JsonUtils; + +import java.util.Date; +import java.util.Map; + +/** + * @since 3.6 + */ +public class IssueComment { + private final Map json; + + IssueComment(Map json) { + this.json = json; + } + + public String key() { + return JsonUtils.getString(json, "key"); + } + + public String htmlText() { + return JsonUtils.getString(json, "htmlText"); + } + + public String login() { + return JsonUtils.getString(json, "login"); + } + + public Date createdAt() { + return JsonUtils.getDateTime(json, "createdAt"); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java index 4fa2517c528..5f0a9a2e16a 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java @@ -27,6 +27,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +/** + * @since 3.6 + */ class IssueParser { Issues parseIssues(String json) { diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java index b5db438fa63..8282d39b723 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Issues.java @@ -42,6 +42,10 @@ public class Issues { return list; } + public int size() { + return list.size(); + } + Issues add(Rule rule) { rulesByKey.put(rule.key(), rule); return this; diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java index 276266f5c1d..c7e05d26332 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java @@ -20,8 +20,10 @@ package org.sonar.wsclient.issue; import org.apache.commons.io.IOUtils; +import org.apache.http.impl.cookie.DateUtils; import org.junit.Test; +import java.util.Date; import java.util.List; import static org.fest.assertions.Assertions.assertThat; @@ -52,6 +54,7 @@ public class IssueParserTest { assertThat(first.attribute("JIRA")).isEqualTo("FOO-1234"); assertThat(first.attribute("OTHER")).isNull(); assertThat(first.attributes()).hasSize(1); + assertThat(first.comments()).isEmpty(); Issue second = list.get(1); assertThat(second.key()).isEqualTo("FGHIJ"); @@ -60,6 +63,7 @@ public class IssueParserTest { assertThat(second.description()).isNull(); assertThat(second.attribute("JIRA")).isNull(); assertThat(second.attributes()).isEmpty(); + assertThat(second.comments()).isEmpty(); assertThat(issues.rules()).hasSize(2); assertThat(issues.rule(first).key()).isEqualTo("squid:CycleBetweenPackages"); @@ -96,4 +100,25 @@ public class IssueParserTest { assertThat(transitions).containsOnly("resolve", "falsepositive"); } + @Test + public void should_parse_comments() throws Exception { + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-comments.json")); + Issues issues = new IssueParser().parseIssues(json); + assertThat(issues.size()).isEqualTo(1); + + Issue issue = issues.list().get(0); + assertThat(issue.comments()).hasSize(2); + + IssueComment firstComment = issue.comments().get(0); + assertThat(firstComment.key()).isEqualTo("COMMENT-1"); + assertThat(firstComment.login()).isEqualTo("morgan"); + assertThat(firstComment.htmlText()).isEqualTo("the first comment"); + assertThat(firstComment.createdAt().getDate()).isEqualTo(18); + + IssueComment secondComment = issue.comments().get(1); + assertThat(secondComment.key()).isEqualTo("COMMENT-2"); + assertThat(secondComment.login()).isEqualTo("arthur"); + assertThat(secondComment.htmlText()).isEqualTo("the second comment"); + assertThat(secondComment.createdAt().getDate()).isEqualTo(19); + } } diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/issue-with-comments.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/issue-with-comments.json new file mode 100644 index 00000000000..914e7627e7c --- /dev/null +++ b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/IssueParserTest/issue-with-comments.json @@ -0,0 +1,41 @@ +{ + "issues": [ + { + "key": "ABCDE", + "component": "Action.java", + "rule": "squid:CycleBetweenPackages", + "severity": "CRITICAL", + "status": "OPEN", + "comments": [ + { + "key": "COMMENT-1", + "login": "morgan", + "htmlText": "the first comment", + "createdAt": "2013-05-18T13:45:34+0200" + }, + { + "key": "COMMENT-2", + "login": "arthur", + "htmlText": "the second comment", + "createdAt": "2013-06-19T00:02:03+0100" + } + ] + } + ], + "rules": [ + { + + "key": "squid:CycleBetweenPackages", + "name": "Avoid cycle between java packages", + "desc": "<p>\nWhen several packages are involved in a cycle (package A > package B > package C > package A where \">\" means \"depends upon\"),\nthat means that those packages are highly coupled and that there is no way to reuse/extract one of those packages without importing all the other packages.\nSuch cycle could quickly increase the effort required to maintain an application and to embrace business change.\nSonar not only detect cycles between packages but also determines what is the minimum effort to break those cycles.\nThis rule log a violation on each source file having an outgoing dependency to be but in order to break a cycle.\n</p>\n" + + } + ], + "paging": { + "pageIndex": 1, + "pageSize": 100, + "total": 2, + "pages": 1 + }, + "securityExclusions": true +}
\ No newline at end of file |