001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.wsclient.issue.internal;
021
022 import org.json.simple.JSONValue;
023 import org.sonar.wsclient.internal.EncodingUtils;
024 import org.sonar.wsclient.internal.HttpRequestFactory;
025 import org.sonar.wsclient.issue.*;
026
027 import javax.annotation.Nullable;
028
029 import java.util.List;
030 import java.util.Map;
031
032 /**
033 * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#issueClient()}.
034 */
035 public class DefaultIssueClient implements IssueClient {
036
037 private static final String SEARCH_URL = "/api/issues/search";
038 private static final String ASSIGN_URL = "/api/issues/assign";
039
040 private final HttpRequestFactory requestFactory;
041 private final IssueJsonParser parser;
042
043 public DefaultIssueClient(HttpRequestFactory requestFactory) {
044 this.requestFactory = requestFactory;
045 this.parser = new IssueJsonParser();
046 }
047
048 public Issues find(IssueQuery query) {
049 String json = requestFactory.get(SEARCH_URL, query.urlParams());
050 return parser.parseIssues(json);
051 }
052
053 @Override
054 public Issue create(NewIssue newIssue) {
055 String json = requestFactory.post("/api/issues/create", newIssue.urlParams());
056 return jsonToIssue(json);
057 }
058
059 @Override
060 public Issue setSeverity(String issueKey, String severity) {
061 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "severity", severity);
062 String json = requestFactory.post("/api/issues/set_severity", params);
063 return jsonToIssue(json);
064 }
065
066 @Override
067 public Issue assign(String issueKey, @Nullable String assignee) {
068 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "assignee", assignee);
069 String json = requestFactory.post(ASSIGN_URL, params);
070 return jsonToIssue(json);
071 }
072
073 @Override
074 public Issue assignToMe(String issueKey) {
075 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "me", "true");
076 String json = requestFactory.post(ASSIGN_URL, params);
077 return jsonToIssue(json);
078 }
079
080 @Override
081 public Issue plan(String issueKey, @Nullable String actionPlanKey) {
082 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "plan", actionPlanKey);
083 String json = requestFactory.post("/api/issues/plan", params);
084 return jsonToIssue(json);
085 }
086
087 @Override
088 public IssueComment addComment(String issueKey, String markdownText) {
089 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "text", markdownText);
090 String json = requestFactory.post("/api/issues/add_comment", params);
091 Map rootJson = (Map) JSONValue.parse(json);
092 return new DefaultIssueComment((Map) rootJson.get("comment"));
093 }
094
095 @Override
096 public List<String> transitions(String issueKey) {
097 Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
098 String json = requestFactory.get("/api/issues/transitions", queryParams);
099 return parser.parseTransitions(json);
100 }
101
102 @Override
103 public Issue doTransition(String issueKey, String transition) {
104 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "transition", transition);
105 String json = requestFactory.post("/api/issues/do_transition", params);
106 return jsonToIssue(json);
107 }
108
109 @Override
110 public List<String> actions(String issueKey) {
111 Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
112 String json = requestFactory.get("/api/issues/actions", queryParams);
113 return parser.parseActions(json);
114 }
115
116 @Override
117 public Issue doAction(String issueKey, String action) {
118 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "actionKey", action);
119 String json = requestFactory.post("/api/issues/do_action", params);
120 return jsonToIssue(json);
121 }
122
123 @Override
124 public BulkChange bulkChange(BulkChangeQuery query) {
125 String json = requestFactory.post("/api/issues/bulk_change", query.urlParams());
126 return parser.parseBulkChange(json);
127 }
128
129 @Override
130 public List<IssueChange> changes(String issueKey) {
131 Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
132 String json = requestFactory.post("/api/issues/changelog", queryParams);
133 return parser.parseChangelog(json);
134 }
135
136 private Issue jsonToIssue(String json) {
137 Map jsonRoot = (Map) JSONValue.parse(json);
138 return new DefaultIssue((Map) jsonRoot.get("issue"));
139 }
140
141 }