From b4d325c80c63ee9ee2676a57a42fac472b5b20d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 21 Jun 2017 16:49:51 +0200 Subject: Allow the feature flags to be enabled/disabled with more granularity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows to enable/disable a feature flag for a given user, or a given Flipper group (must be declared statically in the `flipper.rb` initializer beforehand). Signed-off-by: Rémy Coutable --- lib/api/features.rb | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'lib/api') diff --git a/lib/api/features.rb b/lib/api/features.rb index cff0ba2ddff..4e10e36fce9 100644 --- a/lib/api/features.rb +++ b/lib/api/features.rb @@ -2,6 +2,29 @@ module API class Features < Grape::API before { authenticated_as_admin! } + helpers do + def gate_value(params) + case params[:value] + when 'true' + true + when '0', 'false' + false + else + params[:value].to_i + end + end + + def gate_target(params) + if params[:flipper_group] + Feature.group(params[:flipper_group]) + elsif params[:user] + User.find_by_username(params[:user]) + else + gate_value(params) + end + end + end + resource :features do desc 'Get a list of all features' do success Entities::Feature @@ -17,16 +40,21 @@ module API end params do requires :value, type: String, desc: '`true` or `false` to enable/disable, an integer for percentage of time' + optional :flipper_group, type: String, desc: 'A Flipper group name' + optional :user, type: String, desc: 'A GitLab username' end post ':name' do feature = Feature.get(params[:name]) + target = gate_target(params) + value = gate_value(params) - if %w(0 false).include?(params[:value]) - feature.disable - elsif params[:value] == 'true' - feature.enable + case value + when true + feature.enable(target) + when false + feature.disable(target) else - feature.enable_percentage_of_time(params[:value].to_i) + feature.enable_percentage_of_time(value) end present feature, with: Entities::Feature, current_user: current_user -- cgit v1.2.1 From 5fa9d6a17dac86e9976946ded7857e1392403136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 27 Jun 2017 18:58:56 +0200 Subject: Rename FLippable to FeatureGate and make `flipper_group` and `user` mutually exclusive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- lib/api/features.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/api') diff --git a/lib/api/features.rb b/lib/api/features.rb index 4e10e36fce9..e426bc050eb 100644 --- a/lib/api/features.rb +++ b/lib/api/features.rb @@ -42,6 +42,7 @@ module API requires :value, type: String, desc: '`true` or `false` to enable/disable, an integer for percentage of time' optional :flipper_group, type: String, desc: 'A Flipper group name' optional :user, type: String, desc: 'A GitLab username' + mutually_exclusive :flipper_group, :user end post ':name' do feature = Feature.get(params[:name]) -- cgit v1.2.1 From 59e7c39f4ceb054d3803e3012107a3d0d6d2d2f4 Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Tue, 11 Apr 2017 14:07:46 -0700 Subject: use subject scope in :id/users since we're loading all the members anyways --- lib/api/projects.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index c5df45b7902..886e97a2638 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -1,3 +1,5 @@ +require 'declarative_policy' + module API # Projects API class Projects < Grape::API @@ -396,7 +398,7 @@ module API use :pagination end get ':id/users' do - users = user_project.team.users + users = DeclarativePolicy.subject_scope { user_project.team.users } users = users.search(params[:search]) if params[:search].present? present paginate(users), with: Entities::UserBasic -- cgit v1.2.1 From 34f57b462bc14ad194cf890a4585d403bdbde55c Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Thu, 15 Jun 2017 13:06:49 +0100 Subject: Fix current feature related specs --- lib/api/helpers/runner.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/helpers/runner.rb b/lib/api/helpers/runner.rb index 1369b021ea4..f8645e364ce 100644 --- a/lib/api/helpers/runner.rb +++ b/lib/api/helpers/runner.rb @@ -46,7 +46,8 @@ module API yield if block_given? - forbidden!('Project has been deleted!') unless job.project + project = job.project + forbidden!('Project has been deleted!') if project.nil? || project.pending_delete? forbidden!('Job has been erased!') if job.erased? end -- cgit v1.2.1 From 289fae78e971e117e69fb87602f5f6284419b863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 28 Jun 2017 19:29:56 +0200 Subject: Rename flipper_group to feature_group MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- lib/api/features.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/api') diff --git a/lib/api/features.rb b/lib/api/features.rb index e426bc050eb..21745916463 100644 --- a/lib/api/features.rb +++ b/lib/api/features.rb @@ -15,8 +15,8 @@ module API end def gate_target(params) - if params[:flipper_group] - Feature.group(params[:flipper_group]) + if params[:feature_group] + Feature.group(params[:feature_group]) elsif params[:user] User.find_by_username(params[:user]) else @@ -40,9 +40,9 @@ module API end params do requires :value, type: String, desc: '`true` or `false` to enable/disable, an integer for percentage of time' - optional :flipper_group, type: String, desc: 'A Flipper group name' + optional :feature_group, type: String, desc: 'A Feature group name' optional :user, type: String, desc: 'A GitLab username' - mutually_exclusive :flipper_group, :user + mutually_exclusive :feature_group, :user end post ':name' do feature = Feature.get(params[:name]) -- cgit v1.2.1 From da3e4f412846b754d31439da0d884181653bced0 Mon Sep 17 00:00:00 2001 From: Oswaldo Ferreira Date: Tue, 27 Jun 2017 17:35:35 -0300 Subject: Add "members_count" and "parent_id" data on namespaces API --- lib/api/entities.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index aa91451c9f4..2fe5280bc1c 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -444,7 +444,11 @@ module API end class Namespace < Grape::Entity - expose :id, :name, :path, :kind, :full_path + expose :id, :name, :path, :kind, :full_path, :parent_id + + expose :members_count do |namespace, _| + namespace.users_with_descendants.count if namespace.kind == 'group' + end end class MemberAccess < Grape::Entity -- cgit v1.2.1 From bd4c2847f4a60b392902aa1866c1ccc87cfacbf6 Mon Sep 17 00:00:00 2001 From: Oswaldo Ferreira Date: Wed, 28 Jun 2017 17:27:01 -0300 Subject: Rename members_count to members_count_with_descendants and expose only to group admins --- lib/api/entities.rb | 8 ++++++-- lib/api/namespaces.rb | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 2fe5280bc1c..cef5a0abe12 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -446,8 +446,12 @@ module API class Namespace < Grape::Entity expose :id, :name, :path, :kind, :full_path, :parent_id - expose :members_count do |namespace, _| - namespace.users_with_descendants.count if namespace.kind == 'group' + expose :members_count_with_descendants, if: -> (namespace, opts) { expose_members_count_with_descendants?(namespace, opts) } do |namespace, _| + namespace.users_with_descendants.count + end + + def expose_members_count_with_descendants?(namespace, opts) + namespace.kind == 'group' && Ability.allowed?(opts[:current_user], :admin_group, namespace) end end diff --git a/lib/api/namespaces.rb b/lib/api/namespaces.rb index 30761cb9b55..f1eaff6b0eb 100644 --- a/lib/api/namespaces.rb +++ b/lib/api/namespaces.rb @@ -17,7 +17,7 @@ module API namespaces = namespaces.search(params[:search]) if params[:search].present? - present paginate(namespaces), with: Entities::Namespace + present paginate(namespaces), with: Entities::Namespace, current_user: current_user end end end -- cgit v1.2.1 From 7765dd6a1d04189da49b8a36ffc5bb8d22e5184f Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Thu, 29 Jun 2017 11:57:59 -0700 Subject: bugfix: use `require_dependency` to bring in DeclarativePolicy --- lib/api/projects.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 886e97a2638..d0bd64b2972 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -1,4 +1,4 @@ -require 'declarative_policy' +require_dependency 'declarative_policy' module API # Projects API -- cgit v1.2.1