summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2016-11-25 12:17:23 +0000
committerYorick Peterse <yorickpeterse@gmail.com>2016-11-25 12:17:23 +0000
commit838c1aad68e8c7f81a0b8778f46f0ce4e7c5439d (patch)
treeb8ca9fabfb7e7dfedccb75896bb87ba6e5a57177 /lib/api
parentafe90d529c82566886d1f2513dd6bee4fa73ff94 (diff)
parent81ba3f9177fcfd76f6b3b715c572ce4920398345 (diff)
downloadgitlab-ce-838c1aad68e8c7f81a0b8778f46f0ce4e7c5439d.tar.gz
Merge branch '22373-reduce-queries-in-api-helpers-find_project' into 'master'
Resolve "Reduce queries in API::Helpers#find_project" See merge request !7714
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/groups.rb8
-rw-r--r--lib/api/helpers.rb22
-rw-r--r--lib/api/helpers/members_helpers.rb2
-rw-r--r--lib/api/issues.rb2
-rw-r--r--lib/api/projects.rb2
-rw-r--r--lib/api/services.rb2
-rw-r--r--lib/api/triggers.rb2
7 files changed, 28 insertions, 12 deletions
diff --git a/lib/api/groups.rb b/lib/api/groups.rb
index fc39fdf4b67..5315c22e1e4 100644
--- a/lib/api/groups.rb
+++ b/lib/api/groups.rb
@@ -82,7 +82,7 @@ module API
:lfs_enabled, :request_access_enabled
end
put ':id' do
- group = find_group(params[:id])
+ group = find_group!(params[:id])
authorize! :admin_group, group
if ::Groups::UpdateService.new(group, current_user, declared_params(include_missing: false)).execute
@@ -96,13 +96,13 @@ module API
success Entities::GroupDetail
end
get ":id" do
- group = find_group(params[:id])
+ group = find_group!(params[:id])
present group, with: Entities::GroupDetail
end
desc 'Remove a group.'
delete ":id" do
- group = find_group(params[:id])
+ group = find_group!(params[:id])
authorize! :admin_group, group
DestroyGroupService.new(group, current_user).execute
end
@@ -111,7 +111,7 @@ module API
success Entities::Project
end
get ":id/projects" do
- group = find_group(params[:id])
+ group = find_group!(params[:id])
projects = GroupProjectsFinder.new(group).execute(current_user)
projects = paginate projects
present projects, with: Entities::Project, user: current_user
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index 2c593dbb4ea..0d3ddb89dc3 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -68,7 +68,7 @@ module API
end
def user_project
- @project ||= find_project(params[:id])
+ @project ||= find_project!(params[:id])
end
def available_labels
@@ -76,7 +76,15 @@ module API
end
def find_project(id)
- project = Project.find_with_namespace(id) || Project.find_by(id: id)
+ if id =~ /^\d+$/
+ Project.find_by(id: id)
+ else
+ Project.find_with_namespace(id)
+ end
+ end
+
+ def find_project!(id)
+ project = find_project(id)
if can?(current_user, :read_project, project)
project
@@ -97,7 +105,15 @@ module API
end
def find_group(id)
- group = Group.find_by(path: id) || Group.find_by(id: id)
+ if id =~ /^\d+$/
+ Group.find_by(id: id)
+ else
+ Group.find_by(path: id)
+ end
+ end
+
+ def find_group!(id)
+ group = find_group(id)
if can?(current_user, :read_group, group)
group
diff --git a/lib/api/helpers/members_helpers.rb b/lib/api/helpers/members_helpers.rb
index 90114f6f667..d9cae1501f8 100644
--- a/lib/api/helpers/members_helpers.rb
+++ b/lib/api/helpers/members_helpers.rb
@@ -2,7 +2,7 @@ module API
module Helpers
module MembersHelpers
def find_source(source_type, id)
- public_send("find_#{source_type}", id)
+ public_send("find_#{source_type}!", id)
end
def authorize_admin_source!(source_type, source)
diff --git a/lib/api/issues.rb b/lib/api/issues.rb
index eea5b91d4f9..2fea71870b8 100644
--- a/lib/api/issues.rb
+++ b/lib/api/issues.rb
@@ -68,7 +68,7 @@ module API
# GET /groups/:id/issues?milestone=1.0.0
# GET /groups/:id/issues?milestone=1.0.0&state=closed
get ":id/issues" do
- group = find_group(params[:id])
+ group = find_group!(params[:id])
params[:state] ||= 'opened'
params[:group_id] = group.id
diff --git a/lib/api/projects.rb b/lib/api/projects.rb
index ddfde178d30..2ea3c433ae2 100644
--- a/lib/api/projects.rb
+++ b/lib/api/projects.rb
@@ -379,7 +379,7 @@ module API
# POST /projects/:id/fork/:forked_from_id
post ":id/fork/:forked_from_id" do
authenticated_as_admin!
- forked_from_project = find_project(params[:forked_from_id])
+ forked_from_project = find_project!(params[:forked_from_id])
unless forked_from_project.nil?
if user_project.forked_from_project.nil?
user_project.create_forked_project_link(forked_to_project_id: user_project.id, forked_from_project_id: forked_from_project.id)
diff --git a/lib/api/services.rb b/lib/api/services.rb
index 4d23499aa39..bc427705777 100644
--- a/lib/api/services.rb
+++ b/lib/api/services.rb
@@ -65,7 +65,7 @@ module API
detail 'Added in GitLab 8.13'
end
post ':id/services/:service_slug/trigger' do
- project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
+ project = find_project(params[:id])
# This is not accurate, but done to prevent leakage of the project names
not_found!('Service') unless project
diff --git a/lib/api/triggers.rb b/lib/api/triggers.rb
index 569598fbd2c..bb4de39def1 100644
--- a/lib/api/triggers.rb
+++ b/lib/api/triggers.rb
@@ -13,7 +13,7 @@ module API
optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
end
post ":id/(ref/:ref/)trigger/builds" do
- project = Project.find_with_namespace(params[:id]) || Project.find_by(id: params[:id])
+ project = find_project(params[:id])
trigger = Ci::Trigger.find_by_token(params[:token].to_s)
not_found! unless project && trigger
unauthorized! unless trigger.project == project