001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2013 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 */ 020package org.sonar.wsclient.issue.internal; 021 022import org.json.simple.JSONValue; 023import org.sonar.wsclient.internal.EncodingUtils; 024import org.sonar.wsclient.internal.HttpRequestFactory; 025import org.sonar.wsclient.issue.*; 026 027import javax.annotation.Nullable; 028 029import java.util.List; 030import java.util.Map; 031 032/** 033 * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#issueClient()}. 034 */ 035public class DefaultIssueClient implements IssueClient { 036 037 private static final String SEARCH_URL = "/api/issues/search"; 038 039 private final HttpRequestFactory requestFactory; 040 private final IssueJsonParser parser; 041 042 public DefaultIssueClient(HttpRequestFactory requestFactory) { 043 this.requestFactory = requestFactory; 044 this.parser = new IssueJsonParser(); 045 } 046 047 public Issues find(IssueQuery query) { 048 String json = requestFactory.get(SEARCH_URL, query.urlParams()); 049 return parser.parseIssues(json); 050 } 051 052 @Override 053 public Issue create(NewIssue newIssue) { 054 String json = requestFactory.post("/api/issues/create", newIssue.urlParams()); 055 return jsonToIssue(json); 056 } 057 058 @Override 059 public Issue setSeverity(String issueKey, String severity) { 060 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "severity", severity); 061 String json = requestFactory.post("/api/issues/set_severity", params); 062 return jsonToIssue(json); 063 } 064 065 @Override 066 public Issue assign(String issueKey, @Nullable String assignee) { 067 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "assignee", assignee); 068 String json = requestFactory.post("/api/issues/assign", params); 069 return jsonToIssue(json); 070 } 071 072 @Override 073 public Issue plan(String issueKey, @Nullable String actionPlanKey) { 074 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "plan", actionPlanKey); 075 String json = requestFactory.post("/api/issues/plan", params); 076 return jsonToIssue(json); 077 } 078 079 @Override 080 public IssueComment addComment(String issueKey, String markdownText) { 081 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "text", markdownText); 082 String json = requestFactory.post("/api/issues/add_comment", params); 083 Map rootJson = (Map) JSONValue.parse(json); 084 return new DefaultIssueComment((Map) rootJson.get("comment")); 085 } 086 087 @Override 088 public List<String> transitions(String issueKey) { 089 Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey); 090 String json = requestFactory.get("/api/issues/transitions", queryParams); 091 return parser.parseTransitions(json); 092 } 093 094 @Override 095 public Issue doTransition(String issueKey, String transition) { 096 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "transition", transition); 097 String json = requestFactory.post("/api/issues/do_transition", params); 098 return jsonToIssue(json); 099 } 100 101 @Override 102 public List<String> actions(String issueKey) { 103 Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey); 104 String json = requestFactory.get("/api/issues/actions", queryParams); 105 return parser.parseActions(json); 106 } 107 108 @Override 109 public Issue doAction(String issueKey, String action) { 110 Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "actionKey", action); 111 String json = requestFactory.post("/api/issues/do_action", params); 112 return jsonToIssue(json); 113 } 114 115 @Override 116 public BulkChange bulkChange(BulkChangeQuery query) { 117 String json = requestFactory.post("/api/issues/bulk_change", query.urlParams()); 118 return parser.parseBulkChange(json); 119 } 120 121 @Override 122 public List<IssueChange> changes(String issueKey) { 123 Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey); 124 String json = requestFactory.post("/api/issues/changelog", queryParams); 125 return parser.parseChangelog(json); 126 } 127 128 private Issue jsonToIssue(String json) { 129 Map jsonRoot = (Map) JSONValue.parse(json); 130 return new DefaultIssue((Map) jsonRoot.get("issue")); 131 } 132 133}