diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-06-25 14:12:27 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-06-25 14:12:27 +0200 |
commit | 9ff8a62374c6fe47c0acf8a443bb1f8312229693 (patch) | |
tree | 21b8700ddcb2bcaf83b163fcb1bffa3c6a8170e6 /sonar-ws-client | |
parent | 8f43c6a54d4ac59bd11548fdd3488823b21835a1 (diff) | |
download | sonarqube-9ff8a62374c6fe47c0acf8a443bb1f8312229693.tar.gz sonarqube-9ff8a62374c6fe47c0acf8a443bb1f8312229693.zip |
SONAR-4420 New web service to bulk update issues
Diffstat (limited to 'sonar-ws-client')
10 files changed, 292 insertions, 6 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Bulkchange.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Bulkchange.java new file mode 100644 index 00000000000..d20dd220315 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/Bulkchange.java @@ -0,0 +1,35 @@ +/* + * 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 java.util.List; + +/** + * @since 3.7 + */ +public interface BulkChange { + + List<String> issuesNotChangedKeys(); + + int totalIssuesChanged(); + + int totalIssuesNotChanged(); + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/BulkchangeQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/BulkchangeQuery.java new file mode 100644 index 00000000000..b6980dfef68 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/BulkchangeQuery.java @@ -0,0 +1,73 @@ +/* + * 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.sonar.wsclient.internal.EncodingUtils; + +import java.util.HashMap; +import java.util.Map; + +/** + * @since 3.7 + */ +public class BulkChangeQuery { + + private final Map<String, Object> params = new HashMap<String, Object>(); + + private BulkChangeQuery() { + } + + public static BulkChangeQuery create() { + return new BulkChangeQuery(); + } + + /** + * URL query string, for internal use + */ + public Map<String, Object> urlParams() { + return params; + } + + public BulkChangeQuery issues(String... keys) { + return addParam("issues", keys); + } + + public BulkChangeQuery actions(String... actions) { + return addParam("actions", actions); + } + + public BulkChangeQuery actionParameter(String action, String parameter, Object value) { + params.put(action + "." + parameter, value); + return this; + } + + public BulkChangeQuery comment(String comment) { + params.put("comment", comment); + return this; + } + + private BulkChangeQuery addParam(String key, String[] values) { + if (values != null) { + params.put(key, EncodingUtils.toQueryParam(values)); + } + return this; + } + +} 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 1e1fd52df4f..b5f26595474 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 @@ -67,4 +67,9 @@ public interface IssueClient { Issue doAction(String issueKey, String action); + /** + * Execute bulk change on a list of issues + */ + BulkChange bulkChange(BulkChangeQuery query); + } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultBulkChange.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultBulkChange.java new file mode 100644 index 00000000000..4671d89a9dc --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultBulkChange.java @@ -0,0 +1,68 @@ +/* + * 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.internal; + +import org.sonar.wsclient.issue.BulkChange; + +import java.util.ArrayList; +import java.util.List; + +/** + * @since 3.7 + */ +public class DefaultBulkChange implements BulkChange { + + private int totalIssuesChanged; + private int totalIssuesNotChanged; + private final List<String> issuesNotChangedKeys = new ArrayList<String>(); + + public List<String> issuesNotChangedKeys() { + return issuesNotChangedKeys; + } + + public int totalIssuesChanged() { + return totalIssuesChanged; + } + + public int totalIssuesNotChanged() { + return totalIssuesNotChanged; + } + + DefaultBulkChange setTotalIssuesChanged(int totalIssuesChanged) { + this.totalIssuesChanged = totalIssuesChanged; + return this; + } + + DefaultBulkChange setTotalIssuesNotChanged(int totalIssuesNotChanged) { + this.totalIssuesNotChanged = totalIssuesNotChanged; + return this; + } + + DefaultBulkChange setIssuesNotChanged(List<String> issueKeys) { + issuesNotChangedKeys.addAll(issueKeys); + return this; + } + + DefaultBulkChange addIssueNotchanged(String issueKey) { + issuesNotChangedKeys.add(issueKey); + return this; + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java index c4b2f326555..6e8c60c8c14 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java @@ -25,6 +25,7 @@ import org.sonar.wsclient.internal.HttpRequestFactory; import org.sonar.wsclient.issue.*; import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -111,6 +112,12 @@ public class DefaultIssueClient implements IssueClient { return jsonToIssue(json); } + @Override + public BulkChange bulkChange(BulkChangeQuery query) { + String json = requestFactory.post("/api/issues/bulk_change", query.urlParams()); + return parser.parseBulkChange(json); + } + private Issue jsonToIssue(String json) { Map jsonRoot = (Map) JSONValue.parse(json); return new DefaultIssue((Map) jsonRoot.get("issue")); diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/IssueJsonParser.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/IssueJsonParser.java index d123e9779cb..706267021d4 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/IssueJsonParser.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/IssueJsonParser.java @@ -20,9 +20,10 @@ package org.sonar.wsclient.issue.internal; import org.json.simple.JSONValue; +import org.sonar.wsclient.base.Paging; import org.sonar.wsclient.component.Component; +import org.sonar.wsclient.issue.BulkChange; import org.sonar.wsclient.issue.Issues; -import org.sonar.wsclient.base.Paging; import org.sonar.wsclient.rule.Rule; import org.sonar.wsclient.unmarshallers.JsonUtils; import org.sonar.wsclient.user.User; @@ -124,4 +125,18 @@ public class IssueJsonParser { } return actions; } + + BulkChange parseBulkChange(String json) { + DefaultBulkChange result = new DefaultBulkChange(); + + Map jsonRoot = (Map) JSONValue.parse(json); + Map issuesChanged = (Map) jsonRoot.get("issuesChanged"); + result.setTotalIssuesChanged(JsonUtils.getInteger(issuesChanged, "total")); + + Map issuesNotChanged = (Map) jsonRoot.get("issuesNotChanged"); + result.setTotalIssuesNotChanged(JsonUtils.getInteger(issuesNotChanged, "total")); + result.setIssuesNotChanged(JsonUtils.getArray(issuesNotChanged, "issues")); + + return result; + } } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/BulkChangeQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/BulkChangeQueryTest.java new file mode 100644 index 00000000000..cd756e7001e --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/BulkChangeQueryTest.java @@ -0,0 +1,51 @@ +/* + * 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 BulkChangeQueryTest { + + @Test + public void get_all_issues() { + BulkChangeQuery query = BulkChangeQuery.create(); + assertThat(query.urlParams()).isEmpty(); + } + + @Test + public void test_create() { + BulkChangeQuery query = BulkChangeQuery.create() + .issues("ABCD", "EFGH") + .actions("assign") + .actionParameter("assign", "assignee", "geoffrey") + ; + + assertThat(query.urlParams()).hasSize(3).includes( + entry("issues", "ABCD,EFGH"), + entry("actions", "assign"), + entry("assign.assignee", "geoffrey") + ); + } + +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java index 9ad9f130e53..9befff8294f 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java @@ -213,4 +213,21 @@ public class DefaultIssueClientTest { assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/do_action?issue=ABCDE&actionKey=tweet"); assertThat(result).isNotNull(); } + + @Test + public void should_do_bulk_change() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + httpServer.stubResponseBody("{\"issuesChanged\": {\"total\": 2}, \"issuesNotChanged\": {\"total\": 1, \"issues\": [\"06ed4db6-fd96-450a-bcb0-e0184db50105\"]} }"); + + BulkChangeQuery query = BulkChangeQuery.create() + .issues("ABCD", "EFGH") + .actions("assign") + .actionParameter("assign", "assignee", "geoffrey"); + + IssueClient client = new DefaultIssueClient(requestFactory); + BulkChange result = client.bulkChange(query); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/bulk_change?assign.assignee=geoffrey&issues=ABCD,EFGH&actions=assign"); + assertThat(result).isNotNull(); + } } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java index c84bfe8c001..211bfe0723d 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java @@ -23,11 +23,7 @@ import org.apache.commons.io.IOUtils; import org.junit.Test; import org.sonar.wsclient.base.Paging; import org.sonar.wsclient.component.Component; -import org.sonar.wsclient.issue.ActionPlan; -import org.sonar.wsclient.issue.Issue; -import org.sonar.wsclient.issue.IssueComment; -import org.sonar.wsclient.issue.Issues; -import org.sonar.wsclient.issue.internal.IssueJsonParser; +import org.sonar.wsclient.issue.*; import org.sonar.wsclient.user.User; import java.util.List; @@ -196,4 +192,14 @@ public class IssueJsonParserTest { assertThat(actionPlan.createdAt().getTime()).isEqualTo(1369828520000l); assertThat(actionPlan.updatedAt().getTime()).isEqualTo(1369828520000l); } + + @Test + public void should_parse_bulk_change() throws Exception { + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/bulk-change.json")); + BulkChange bulkChange = new IssueJsonParser().parseBulkChange(json); + + assertThat(bulkChange.totalIssuesChanged()).isEqualTo(3); + assertThat(bulkChange.totalIssuesNotChanged()).isEqualTo(2); + assertThat(bulkChange.issuesNotChangedKeys()).containsOnly("06ed4db6-fd96-450a-bcb0-e0184db50105", "06ed4db6-fd96-450a-bcb0-e0184db50654"); + } } diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/internal/IssueJsonParserTest/bulk-change.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/internal/IssueJsonParserTest/bulk-change.json new file mode 100644 index 00000000000..9180ebf5d34 --- /dev/null +++ b/sonar-ws-client/src/test/resources/org/sonar/wsclient/issue/internal/IssueJsonParserTest/bulk-change.json @@ -0,0 +1,9 @@ +{ + "issuesChanged": { + "total": 3 + }, + "issuesNotChanged": { + "total": 2, + "issues": ["06ed4db6-fd96-450a-bcb0-e0184db50105", "06ed4db6-fd96-450a-bcb0-e0184db50654"] + } +}
\ No newline at end of file |