import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
/**
* Not an API, please do not directly use this class.
// only static methods
}
+ public static Map<String, Object> toMap(String... array) {
+ Map<String, Object> map = new LinkedHashMap<String, Object>();
+ for (int i = 0; i < array.length; i += 2) {
+ Object value = array[i + 1];
+ if (value != null) {
+ map.put(array[i], array[i + 1]);
+ }
+ }
+ return map;
+ }
+
public static String toQueryParam(String[] strings) {
StringBuilder sb = new StringBuilder();
boolean first = true;
package org.sonar.wsclient.issue;
import com.github.kevinsawicki.http.HttpRequest;
+import org.sonar.wsclient.internal.EncodingUtils;
import org.sonar.wsclient.internal.HttpRequestFactory;
-import java.util.LinkedHashMap;
+import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
*/
public class DefaultIssueClient implements IssueClient {
- private static final String TRANSITIONS_BASE_URL = "/api/issues/transitions";
- private static final String DO_TRANSITION_BASE_URL = "/api/issues/do_transition";
-
private final HttpRequestFactory requestFactory;
private final IssueParser parser;
}
@Override
- public void change(String issueKey, IssueChange change) {
- if (!change.urlParams().isEmpty()) {
- Map<String, Object> queryParams = new LinkedHashMap<String, Object>(change.urlParams());
- queryParams.put("issue", issueKey);
- HttpRequest request = requestFactory.post(IssueChange.BASE_URL, queryParams);
- if (!request.ok()) {
- throw new IllegalStateException("Fail to change issue " + issueKey + ".Bad HTTP response status: " + request.code());
- }
+ public void create(NewIssue newIssue) {
+ HttpRequest request = requestFactory.post(NewIssue.BASE_URL, newIssue.urlParams());
+ if (!request.ok()) {
+ throw new IllegalStateException("Fail to create issue. Bad HTTP response status: " + request.code());
}
}
@Override
- public void create(NewIssue newIssue) {
- HttpRequest request = requestFactory.post(NewIssue.BASE_URL, newIssue.urlParams());
+ public void setSeverity(String issueKey, String severity) {
+ Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "severity", severity);
+ HttpRequest request = requestFactory.post("/api/issues/set_severity", params);
if (!request.ok()) {
- throw new IllegalStateException("Fail to create issue. Bad HTTP response status: " + request.code());
+ throw new IllegalStateException("Fail to set severity. Bad HTTP response status: " + request.code());
}
}
@Override
- public void comment(String issueKey, String comment) {
- change(issueKey, IssueChange.create().comment(comment));
+ public void assign(String issueKey, @Nullable String assignee) {
+ Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "assignee", assignee);
+ HttpRequest request = requestFactory.post("/api/issues/assign", params);
+ if (!request.ok()) {
+ throw new IllegalStateException("Fail to assign issue to user. Bad HTTP response status: " + request.code());
+ }
}
@Override
public List<String> transitions(String issueKey) {
- Map<String, Object> queryParams = new LinkedHashMap<String, Object>();
- queryParams.put("issue", issueKey);
- HttpRequest request = requestFactory.get(TRANSITIONS_BASE_URL, queryParams);
+ Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
+ HttpRequest request = requestFactory.get("/api/issues/transitions", queryParams);
if (!request.ok()) {
throw new IllegalStateException("Fail to return transition for issue. Bad HTTP response status: " + request.code());
}
@Override
public void doTransition(String issueKey, String transition) {
- Map<String, Object> queryParams = new LinkedHashMap<String, Object>();
- queryParams.put("issue", issueKey);
- queryParams.put("transition", transition);
- HttpRequest request = requestFactory.post(DO_TRANSITION_BASE_URL, queryParams);
+ Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "transition", transition);
+ HttpRequest request = requestFactory.post("/api/issues/do_transition", params);
if (!request.ok()) {
throw new IllegalStateException("Fail to execute transition on issue " + issueKey + ".Bad HTTP response status: " + request.code());
}
}
+
+
}
+++ /dev/null
-/*
- * 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 javax.annotation.Nullable;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * @since 3.6
- */
-public class IssueChange {
- static final String BASE_URL = "/api/issues/change";
- private final Map<String, Object> params = new LinkedHashMap<String, Object>();
-
- private IssueChange() {
- }
-
- public static IssueChange create() {
- return new IssueChange();
- }
-
- Map<String, Object> urlParams() {
- return params;
- }
-
- public IssueChange severity(String s) {
- params.put("newSeverity", s);
- return this;
- }
-
- /**
- * Add a comment
- */
- public IssueChange comment(String s) {
- params.put("comment", s);
- return this;
- }
-
- public IssueChange cost(Double d) {
- params.put("newCost", d);
- return this;
- }
-
- public IssueChange line(Integer i) {
- params.put("newLine", i);
- return this;
- }
-
- public IssueChange transition(String s) {
- params.put("transition", s);
- return this;
- }
-
- public IssueChange assignee(String login) {
- params.put("newAssignee", login);
- return this;
- }
-
- public IssueChange description(String s) {
- params.put("newDesc", s);
- return this;
- }
-
- public IssueChange attribute(String key, @Nullable String value) {
- if (value == null) {
- params.put("newAttr[" + key + "]", "");
- } else {
- params.put("newAttr[" + key + "]", value);
- }
- return this;
- }
-}
*/
package org.sonar.wsclient.issue;
+import javax.annotation.Nullable;
import java.util.List;
/**
Issues find(IssueQuery query);
- void change(String issueKey, IssueChange change);
+ void assign(String issueKey, @Nullable String assignee);
+
+ void setSeverity(String issueKey, String severity);
void create(NewIssue issue);
void doTransition(String issueKey, String transition);
- /**
- * Shortcut for {@code #change(issueKey, IssueChange.create().comment(comment)}
- */
- void comment(String issueKey, String comment);
}
import java.util.Date;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
public class EncodingUtilsTest {
@Test
assertThat(EncodingUtils.toQueryParam(new String[]{"foo"})).isEqualTo("foo");
assertThat(EncodingUtils.toQueryParam(new String[]{""})).isEqualTo("");
}
+
+ @Test
+ public void test_toMap() {
+ assertThat(EncodingUtils.toMap()).isEmpty();
+ assertThat(EncodingUtils.toMap("foo", "bar")).hasSize(1).includes(entry("foo", "bar"));
+ assertThat(EncodingUtils.toMap("1", "one", "2", "two")).hasSize(2).includes(entry("1", "one"), entry("2", "two"));
+ assertThat(EncodingUtils.toMap("foo", null)).isEmpty();
+ }
}
}
@Test
- public void should_apply_change() {
+ public void should_set_severity() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
IssueClient client = new DefaultIssueClient(requestFactory);
- client.change("ABCDE", IssueChange.create().severity("BLOCKER").comment("because!"));
+ client.setSeverity("ABCDE", "BLOCKER");
- assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/change?newSeverity=BLOCKER&comment=because!&issue=ABCDE");
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/set_severity?issue=ABCDE&severity=BLOCKER");
}
@Test
- public void should_not_apply_empty_change() {
+ public void should_assign() {
HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
IssueClient client = new DefaultIssueClient(requestFactory);
- client.change("ABCDE", IssueChange.create());
+ client.assign("ABCDE", "emmerik");
- assertThat(httpServer.requestedPath()).isNull();
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign?issue=ABCDE&assignee=emmerik");
+ }
+
+ @Test
+ public void should_unassign() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+
+ IssueClient client = new DefaultIssueClient(requestFactory);
+ client.assign("ABCDE", null);
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign?issue=ABCDE");
}
@Test
+++ /dev/null
-/*
- * 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.junit.Test;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.MapAssert.entry;
-
-public class IssueChangeTest {
- @Test
- public void should_create_empty_change() {
- IssueChange change = IssueChange.create();
- assertThat(change.urlParams()).isEmpty();
- }
-
- @Test
- public void should_create_change() {
- IssueChange change = IssueChange.create()
- .comment("this is a comment")
- .assignee("lancelot")
- .attribute("JIRA", "FOO-1234")
- .attribute("LINK", null)
- .cost(4.2)
- .transition("resolve")
- .severity("BLOCKER");
- assertThat(change.urlParams()).hasSize(7).includes(
- entry("comment", "this is a comment"),
- entry("newAssignee", "lancelot"),
- entry("newAttr[JIRA]", "FOO-1234"),
- entry("newAttr[LINK]", ""),
- entry("newCost", 4.2),
- entry("transition", "resolve"),
- entry("newSeverity", "BLOCKER")
- );
- }
-}