From: Jean-Baptiste Lievremont Date: Wed, 13 May 2015 07:55:03 +0000 (+0200) Subject: SONAR-6523 Replace Rails api/users/search with Java implementation X-Git-Tag: 5.2-RC1~1934 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9bf718ee9ea0a4f8d0605305ec1df65d83ac872b;p=sonarqube.git SONAR-6523 Replace Rails api/users/search with Java implementation --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java index 4b4dee32d4d..7f4cf2439ce 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/SearchAction.java @@ -52,10 +52,11 @@ public class SearchAction implements UsersWsAction { @Override public void define(WebService.NewController controller) { - WebService.NewAction action = controller.createAction("search2") + WebService.NewAction action = controller.createAction("search") .setDescription("Get a list of active users.") .setSince("3.6") - .setHandler(this); + .setHandler(this) + .setResponseExample(getClass().getResource("example-search.json")); action.addFieldsParam(FIELDS); action.addPagingParams(50); diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/UsersWs.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/UsersWs.java index 476461d2c27..d8bc63bc696 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/UsersWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/UsersWs.java @@ -20,8 +20,6 @@ package org.sonar.server.user.ws; -import com.google.common.io.Resources; -import org.sonar.api.server.ws.RailsHandler; import org.sonar.api.server.ws.WebService; public class UsersWs implements WebService { @@ -38,34 +36,10 @@ public class UsersWs implements WebService { .setSince("3.6") .setDescription("Users management"); - defineSearchAction(controller); for (UsersWsAction action : actions) { action.define(controller); } controller.done(); } - - private void defineSearchAction(NewController controller) { - NewAction action = controller.createAction("search") - .setDescription("Get a list of users") - .setSince("3.6") - .setHandler(RailsHandler.INSTANCE) - .setResponseExample(Resources.getResource(this.getClass(), "example-search.json")); - - action.createParam("includeDeactivated") - .setDescription("Include deactivated users") - .setDefaultValue("false") - .setPossibleValues("true", "false"); - - action.createParam("logins") - .setDescription("Comma-separated list of user logins") - .setExampleValue("admin,sbrandhof"); - - action.createParam("s") - .setDescription("UTF-8 search query on login or name") - .setExampleValue("bran"); - - RailsHandler.addFormatParam(action); - } } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/example-search.json b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/example-search.json index 3cbb0005bcf..7b959b2c594 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/example-search.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/example-search.json @@ -9,7 +9,8 @@ { "login": "sbrandhof", "name": "Simon", - "active": true + "active": true, + "scmAccounts": ["simon.brandhof", "s.brandhof@company.tld"] } ] } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java index 722f1eaaddf..ab3c60cb8f2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java @@ -58,54 +58,54 @@ public class SearchActionTest { @Test public void search_empty() throws Exception { - tester.newGetRequest("api/users", "search2").execute().assertJson(getClass(), "empty.json"); + tester.newGetRequest("api/users", "search").execute().assertJson(getClass(), "empty.json"); } @Test public void search_without_parameters() throws Exception { injectUsers(5); - tester.newGetRequest("api/users", "search2").execute().assertJson(getClass(), "five_users.json"); + tester.newGetRequest("api/users", "search").execute().assertJson(getClass(), "five_users.json"); } @Test public void search_with_query() throws Exception { injectUsers(5); - tester.newGetRequest("api/users", "search2").setParam("q", "user-1").execute().assertJson(getClass(), "user_one.json"); + tester.newGetRequest("api/users", "search").setParam("q", "user-1").execute().assertJson(getClass(), "user_one.json"); } @Test public void search_with_paging() throws Exception { injectUsers(10); - tester.newGetRequest("api/users", "search2").setParam("ps", "5").execute().assertJson(getClass(), "page_one.json"); - tester.newGetRequest("api/users", "search2").setParam("ps", "5").setParam("p", "2").execute().assertJson(getClass(), "page_two.json"); + tester.newGetRequest("api/users", "search").setParam("ps", "5").execute().assertJson(getClass(), "page_one.json"); + tester.newGetRequest("api/users", "search").setParam("ps", "5").setParam("p", "2").execute().assertJson(getClass(), "page_two.json"); } @Test public void search_with_fields() throws Exception { injectUsers(1); - assertThat(tester.newGetRequest("api/users", "search2").execute().outputAsString()) + assertThat(tester.newGetRequest("api/users", "search").execute().outputAsString()) .contains("login") .contains("name") .contains("email") .contains("scmAccounts"); - assertThat(tester.newGetRequest("api/users", "search2").setParam("f", "").execute().outputAsString()) + assertThat(tester.newGetRequest("api/users", "search").setParam("f", "").execute().outputAsString()) .contains("login") .contains("name") .contains("email") .contains("scmAccounts"); - assertThat(tester.newGetRequest("api/users", "search2").setParam("f", "login").execute().outputAsString()) + assertThat(tester.newGetRequest("api/users", "search").setParam("f", "login").execute().outputAsString()) .contains("login") .doesNotContain("name") .doesNotContain("email") .doesNotContain("scmAccounts"); - assertThat(tester.newGetRequest("api/users", "search2").setParam("f", "scmAccounts").execute().outputAsString()) + assertThat(tester.newGetRequest("api/users", "search").setParam("f", "scmAccounts").execute().outputAsString()) .doesNotContain("login") .doesNotContain("name") .doesNotContain("email") diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java index 62dd223058e..1ea55a41462 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java @@ -24,7 +24,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.i18n.I18n; -import org.sonar.api.server.ws.RailsHandler; import org.sonar.api.server.ws.WebService; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.UserUpdater; @@ -46,7 +45,8 @@ public class UsersWsTest { new UpdateAction(mock(UserIndex.class), mock(UserUpdater.class), userSessionRule), new CurrentUserAction(userSessionRule), new DeactivateAction(mock(UserIndex.class), mock(UserUpdater.class), userSessionRule), - new ChangePasswordAction(mock(UserUpdater.class), userSessionRule))); + new ChangePasswordAction(mock(UserUpdater.class), userSessionRule), + new SearchAction(mock(UserIndex.class)))); controller = tester.controller("api/users"); } @@ -63,7 +63,6 @@ public class UsersWsTest { WebService.Action action = controller.action("search"); assertThat(action).isNotNull(); assertThat(action.isPost()).isFalse(); - assertThat(action.handler()).isInstanceOf(RailsHandler.class); assertThat(action.responseExampleAsString()).isNotEmpty(); assertThat(action.params()).hasSize(4); } diff --git a/server/sonar-web/src/main/coffee/issue/views/assign-form-view.coffee b/server/sonar-web/src/main/coffee/issue/views/assign-form-view.coffee index f32d5879224..b92d7a475fd 100644 --- a/server/sonar-web/src/main/coffee/issue/views/assign-form-view.coffee +++ b/server/sonar-web/src/main/coffee/issue/views/assign-form-view.coffee @@ -119,7 +119,7 @@ define [ search: (query) -> if query.length > 1 - $.get "#{baseUrl}/api/users/search2", q: query + $.get "#{baseUrl}/api/users/search", q: query .done (data) => @resetAssignees data.users else diff --git a/server/sonar-web/src/main/coffee/issues/facets/assignee-facet.coffee b/server/sonar-web/src/main/coffee/issues/facets/assignee-facet.coffee index b1b91c7e207..366b13bb032 100644 --- a/server/sonar-web/src/main/coffee/issues/facets/assignee-facet.coffee +++ b/server/sonar-web/src/main/coffee/issues/facets/assignee-facet.coffee @@ -33,7 +33,7 @@ define [ getUrl: -> - "#{baseUrl}/api/users/search2" + "#{baseUrl}/api/users/search" prepareAjaxSearch: -> quietMillis: 300 diff --git a/server/sonar-web/src/main/coffee/issues/facets/reporter-facet.coffee b/server/sonar-web/src/main/coffee/issues/facets/reporter-facet.coffee index aa2c9dcd659..2feacfc2184 100644 --- a/server/sonar-web/src/main/coffee/issues/facets/reporter-facet.coffee +++ b/server/sonar-web/src/main/coffee/issues/facets/reporter-facet.coffee @@ -28,7 +28,7 @@ define [ class extends CustomValuesFacet getUrl: -> - "#{baseUrl}/api/users/search2" + "#{baseUrl}/api/users/search" prepareAjaxSearch: -> quietMillis: 300 diff --git a/server/sonar-web/src/main/js/navigator/filters/ajax-select-filters.js b/server/sonar-web/src/main/js/navigator/filters/ajax-select-filters.js index fecdcada306..c354b73b48b 100644 --- a/server/sonar-web/src/main/js/navigator/filters/ajax-select-filters.js +++ b/server/sonar-web/src/main/js/navigator/filters/ajax-select-filters.js @@ -68,7 +68,7 @@ define([ var UserSuggestions = Suggestions.extend({ url: function() { - return baseUrl + '/api/users/search2'; + return baseUrl + '/api/users/search'; }, parse: function(response) { @@ -440,7 +440,7 @@ define([ var that = this; return $j .ajax({ - url: baseUrl + '/api/users/search2', + url: baseUrl + '/api/users/search', type: 'GET', data: { q: v } }) @@ -473,7 +473,7 @@ define([ var that = this; return $j .ajax({ - url: baseUrl + '/api/users/search2', + url: baseUrl + '/api/users/search', type: 'GET', data: { q: v } }) diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/users_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/users_controller.rb deleted file mode 100644 index 11353ded147..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/users_controller.rb +++ /dev/null @@ -1,73 +0,0 @@ -# -# SonarQube, open source software quality management tool. -# Copyright (C) 2008-2014 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. -# - -# since 3.6 -class Api::UsersController < Api::ApiController - - # - # GET /api/users/search? - # - # -- Example - # curl -v 'http://localhost:9000/api/users/search?includeDeactivated=true&logins=simon,julien' - # - def search - users = Api.users.find(params) - - select2_format=(params[:f]=='s2') - if select2_format - hash = { - :more => false, - :results => users.map { |user| {:id => user.login, :text => "#{user.name} (#{user.login})"} } - } - else - hash = {:users => users.map { |user| User.to_hash(user) }} - end - - respond_to do |format| - format.json { render :json => jsonp(hash) } - format.xml { render :xml => hash.to_xml(:skip_types => true, :root => 'users') } - end - end - - # - # POST /api/users/deactivate - # - # -- Mandatory parameters - # 'login' is the user identifier - # - # -- Example - # curl -X POST -v -u admin:admin 'http://localhost:9000/api/users/deactivate?login=' - # - # since 3.7 - # - def deactivate - verify_post_request - require_parameters :login - - Api.users.deactivate(params[:login]) - - hash={} - respond_to do |format| - format.json { render :json => jsonp(hash) } - format.xml { render :xml => hash.to_xml(:skip_types => true, :root => 'users') } - end - end - -end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 780779064ce..7565b805190 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -725,7 +725,7 @@ module ApplicationHelper # * :select2_options - hash of select2 options # def user_select_tag(name, options={}) - ws_url="#{ApplicationController::root_context}/api/users/search2" + ws_url="#{ApplicationController::root_context}/api/users/search" options[:min_length]=2 options[:select2_ajax_options]={ 'data' => 'function (term, page) { return { q: term, p: page } }',