diff options
Diffstat (limited to 'sonar-ws-client')
4 files changed, 66 insertions, 1 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java index 727a2e4c409..4dd1972e2ba 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java @@ -109,7 +109,7 @@ public class DefaultIssueClient implements IssueClient { 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()); + throw new IllegalStateException("Fail to return transitions for issue. Bad HTTP response status: " + request.code()); } String json = request.body("UTF-8"); return parser.parseTransitions(json); @@ -125,6 +125,27 @@ public class DefaultIssueClient implements IssueClient { return createIssueResult(request); } + @Override + public List<String> actions(String issueKey) { + Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey); + HttpRequest request = requestFactory.get("/api/issues/actions", queryParams); + if (!request.ok()) { + throw new IllegalStateException("Fail to return actions for issue. Bad HTTP response status: " + request.code()); + } + String json = request.body("UTF-8"); + return parser.parseActions(json); + } + + @Override + public Issue doAction(String issueKey, String action) { + Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "actionKey", action); + HttpRequest request = requestFactory.post("/api/issues/do_action", params); + if (!request.ok()) { + throw new IllegalStateException("Fail to execute action on issue " + issueKey + ".Bad HTTP response status: " + request.code()); + } + return createIssueResult(request); + } + private Issue createIssueResult(HttpRequest request){ String json = request.body("UTF-8"); Map jsonRoot = (Map) JSONValue.parse(json); diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java index 2130d3c92ae..3527938710a 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java @@ -44,4 +44,8 @@ public interface IssueClient { Issue doTransition(String issueKey, String transition); + List<String> actions(String issueKey); + + Issue doAction(String issueKey, String action); + } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueJsonParser.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueJsonParser.java index 635a624758b..906929ce308 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueJsonParser.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueJsonParser.java @@ -112,4 +112,14 @@ class IssueJsonParser { } return transitions; } + + List<String> parseActions(String json) { + List<String> actions = new ArrayList<String>(); + Map jRoot = (Map) JSONValue.parse(json); + List<String> jActions = (List) jRoot.get("actions"); + for (String jAction : jActions) { + actions.add(jAction); + } + return actions; + } } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java index 41118a61ff9..7ad6f6df3c8 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java @@ -179,4 +179,34 @@ public class DefaultIssueClientTest { assertThat(comment.login()).isEqualTo("admin"); assertThat(comment.createdAt().getDate()).isEqualTo(18); } + + @Test + public void should_get_actions() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + httpServer.doReturnBody("{\n" + + " \"actions\": [\n" + + " \"link-to-jira\",\n" + + " \"tweet\"\n" + + " ]\n" + + "}"); + + IssueClient client = new DefaultIssueClient(requestFactory); + List<String> actions = client.actions("ABCDE"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/actions?issue=ABCDE"); + assertThat(actions).hasSize(2); + assertThat(actions).containsOnly("link-to-jira", "tweet"); + } + + @Test + public void should_apply_action() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + + IssueClient client = new DefaultIssueClient(requestFactory); + Issue result = client.doAction("ABCDE", "tweet"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/do_action?issue=ABCDE&actionKey=tweet"); + assertThat(result).isNotNull(); + } } |