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