diff options
author | Rémy Coutable <remy@rymai.me> | 2016-10-31 20:37:13 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2016-10-31 20:47:30 +0300 |
commit | e5c6f943fe6e33510813f05a6f7e6d56af47461a (patch) | |
tree | d93062daf78ef0179cf96c071cdb999e0ccca74e | |
parent | a60cc42b262cb63ce5d2284f1f3f41d6521daa14 (diff) |
Backport Group API code that was added in EE only
Signed-off-by: Rémy Coutable <remy@rymai.me>
-rw-r--r-- | app/assets/javascripts/api.js | 12 | ||||
-rw-r--r-- | app/assets/javascripts/groups_select.js | 7 | ||||
-rw-r--r-- | app/assets/javascripts/project_select.js | 4 | ||||
-rw-r--r-- | app/assets/javascripts/search.js | 2 | ||||
-rw-r--r-- | doc/api/groups.md | 8 | ||||
-rw-r--r-- | lib/api/groups.rb | 3 | ||||
-rw-r--r-- | spec/requests/api/groups_spec.rb | 13 |
7 files changed, 33 insertions, 16 deletions
diff --git a/app/assets/javascripts/api.js b/app/assets/javascripts/api.js index 7ebe1599fca..1cab66e109e 100644 --- a/app/assets/javascripts/api.js +++ b/app/assets/javascripts/api.js @@ -22,16 +22,14 @@ }); }, // Return groups list. Filtered by query - // Only active groups retrieved - groups: function(query, skip_ldap, skip_groups, callback) { + groups: function(query, options, callback) { var url = Api.buildUrl(Api.groupsPath); return $.ajax({ url: url, - data: { - search: query, - skip_groups: skip_groups, - per_page: 20 - }, + data: $.extend({ + search: query, + per_page: 20 + }, options), dataType: "json" }).done(function(groups) { return callback(groups); diff --git a/app/assets/javascripts/groups_select.js b/app/assets/javascripts/groups_select.js index b275620c799..e3c39c895ba 100644 --- a/app/assets/javascripts/groups_select.js +++ b/app/assets/javascripts/groups_select.js @@ -6,15 +6,16 @@ function GroupsSelect() { $('.ajax-groups-select').each((function(_this) { return function(i, select) { - var skip_ldap, skip_groups; - skip_ldap = $(select).hasClass('skip_ldap'); + var all_available, skip_groups; + all_available = $(select).data('all-available'); skip_groups = $(select).data('skip-groups') || []; return $(select).select2({ placeholder: "Search for a group", multiple: $(select).hasClass('multiselect'), minimumInputLength: 0, query: function(query) { - return Api.groups(query.term, skip_ldap, skip_groups, function(groups) { + options = { all_available: all_available, skip_groups: skip_groups }; + return Api.groups(query.term, options, function(groups) { var data; data = { results: groups diff --git a/app/assets/javascripts/project_select.js b/app/assets/javascripts/project_select.js index b74b4ae68ff..e1acf3c8232 100644 --- a/app/assets/javascripts/project_select.js +++ b/app/assets/javascripts/project_select.js @@ -24,7 +24,7 @@ data = groups.concat(projects); return finalCallback(data); }; - return Api.groups(term, false, false, groupsCallback); + return Api.groups(term, {}, groupsCallback); }; } else { projectsCallback = finalCallback; @@ -73,7 +73,7 @@ data = groups.concat(projects); return finalCallback(data); }; - return Api.groups(query.term, false, false, groupsCallback); + return Api.groups(query.term, {}, groupsCallback); }; } else { projectsCallback = finalCallback; diff --git a/app/assets/javascripts/search.js b/app/assets/javascripts/search.js index 6c2389f202f..d79e6f014f6 100644 --- a/app/assets/javascripts/search.js +++ b/app/assets/javascripts/search.js @@ -11,7 +11,7 @@ filterable: true, fieldName: 'group_id', data: function(term, callback) { - return Api.groups(term, false, false, function(data) { + return Api.groups(term, {}, function(data) { data.unshift({ name: 'Any' }); diff --git a/doc/api/groups.md b/doc/api/groups.md index e81d6f9de4b..b56d74d25e0 100644 --- a/doc/api/groups.md +++ b/doc/api/groups.md @@ -2,7 +2,12 @@ ## List groups -Get a list of groups. (As user: my groups, as admin: all groups) +Get a list of groups. (As user: my groups or all available, as admin: all groups). + +Parameters: + +- `all_available` (optional) - if passed, show all groups you have access to +- `skip_groups` (optional)(array of group IDs) - if passed, skip groups ``` GET /groups @@ -21,7 +26,6 @@ GET /groups You can search for groups by name or path, see below. - ## List a group's projects Get a list of projects in this group. diff --git a/lib/api/groups.rb b/lib/api/groups.rb index bfb89475025..a13e353b7f5 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -8,11 +8,14 @@ module API # # Parameters: # skip_groups (optional) - Array of group ids to exclude from list + # all_available (optional, boolean) - Show all group that you have access to # Example Request: # GET /groups get do @groups = if current_user.admin Group.all + elsif params[:all_available] + GroupsFinder.new.execute(current_user) else current_user.groups end diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index 3ba257256a0..7b47bf5afc1 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -37,7 +37,7 @@ describe API::API, api: true do end end - context "when authenticated as admin" do + context "when authenticated as admin" do it "admin: returns an array of all groups" do get api("/groups", admin) expect(response).to have_http_status(200) @@ -55,6 +55,17 @@ describe API::API, api: true do expect(json_response.length).to eq(1) end end + + context "when using all_available in request" do + it "returns all groups you have access to" do + public_group = create :group, :public + get api("/groups", user1), all_available: true + + expect(response).to have_http_status(200) + expect(json_response).to be_an Array + expect(json_response.first['name']).to eq(public_group.name) + end + end end describe "GET /groups/:id" do |