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 */
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  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  @Override
049  public Issues find(IssueQuery query) {
050    String json = requestFactory.get(SEARCH_URL, query.urlParams());
051    return parser.parseIssues(json);
052  }
053
054  @Override
055  public Issue create(NewIssue newIssue) {
056    String json = requestFactory.post("/api/issues/create", newIssue.urlParams());
057    return jsonToIssue(json);
058  }
059
060  @Override
061  public Issue setSeverity(String issueKey, String severity) {
062    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "severity", severity);
063    String json = requestFactory.post("/api/issues/set_severity", params);
064    return jsonToIssue(json);
065  }
066
067  @Override
068  public Issue assign(String issueKey, @Nullable String assignee) {
069    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "assignee", assignee);
070    String json = requestFactory.post(ASSIGN_URL, params);
071    return jsonToIssue(json);
072  }
073
074  @Override
075  public Issue assignToMe(String issueKey) {
076    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "me", "true");
077    String json = requestFactory.post(ASSIGN_URL, params);
078    return jsonToIssue(json);
079  }
080
081  @Override
082  public Issue plan(String issueKey, @Nullable String actionPlanKey) {
083    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "plan", actionPlanKey);
084    String json = requestFactory.post("/api/issues/plan", params);
085    return jsonToIssue(json);
086  }
087
088  @Override
089  public IssueComment addComment(String issueKey, String markdownText) {
090    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "text", markdownText);
091    String json = requestFactory.post("/api/issues/add_comment", params);
092    Map rootJson = (Map) JSONValue.parse(json);
093    return new DefaultIssueComment((Map) rootJson.get("comment"));
094  }
095
096  @Override
097  public List<String> transitions(String issueKey) {
098    Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
099    String json = requestFactory.get("/api/issues/transitions", queryParams);
100    return parser.parseTransitions(json);
101  }
102
103  @Override
104  public Issue doTransition(String issueKey, String transition) {
105    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "transition", transition);
106    String json = requestFactory.post("/api/issues/do_transition", params);
107    return jsonToIssue(json);
108  }
109
110  @Override
111  public List<String> actions(String issueKey) {
112    Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
113    String json = requestFactory.get("/api/issues/actions", queryParams);
114    return parser.parseActions(json);
115  }
116
117  @Override
118  public Issue doAction(String issueKey, String action) {
119    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "actionKey", action);
120    String json = requestFactory.post("/api/issues/do_action", params);
121    return jsonToIssue(json);
122  }
123
124  @Override
125  public BulkChange bulkChange(BulkChangeQuery query) {
126    String json = requestFactory.post("/api/issues/bulk_change", query.urlParams());
127    return parser.parseBulkChange(json);
128  }
129
130  @Override
131  public List<IssueChange> changes(String issueKey) {
132    Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
133    String json = requestFactory.post("/api/issues/changelog", queryParams);
134    return parser.parseChangelog(json);
135  }
136
137  private Issue jsonToIssue(String json) {
138    Map jsonRoot = (Map) JSONValue.parse(json);
139    return new DefaultIssue((Map) jsonRoot.get("issue"));
140  }
141
142}