summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorSebastian Ziebell <sebastian.ziebell@asquera.de>2013-02-27 17:50:30 +0100
committerSebastian Ziebell <sebastian.ziebell@asquera.de>2013-02-27 17:50:30 +0100
commit7499f65014257989510da50505fa7c0f5a4fae88 (patch)
treeb8c9355b73ec48c12bfa9d6416fa91ba4c4444e6 /lib/api
parent43d75960303be538fb79804ca6e2a90241aee0df (diff)
downloadgitlab-ce-7499f65014257989510da50505fa7c0f5a4fae88.tar.gz
API: extracted helper method to validate required parameters, code clean up
Added a helper method to check if required parameters are given in an API call. Can be used to return a `400 Bad Request` return code if a required attribute is missing. Code clean up and fixed tests.
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/groups.rb4
-rw-r--r--lib/api/helpers.rb11
-rw-r--r--lib/api/issues.rb2
-rw-r--r--lib/api/merge_requests.rb7
-rw-r--r--lib/api/milestones.rb3
-rw-r--r--lib/api/notes.rb5
-rw-r--r--lib/api/projects.rb26
-rw-r--r--lib/api/users.rb9
8 files changed, 29 insertions, 38 deletions
diff --git a/lib/api/groups.rb b/lib/api/groups.rb
index 3f213073f80..5aaa5eb4f54 100644
--- a/lib/api/groups.rb
+++ b/lib/api/groups.rb
@@ -29,9 +29,7 @@ module Gitlab
# POST /groups
post do
authenticated_as_admin!
-
- bad_request!(:name) unless params[:name].present?
- bad_request!(:path) unless params[:path].present?
+ required_attributes! [:name, :path]
attrs = attributes_for_keys [:name, :path]
@group = Group.new(attrs)
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index becb3bce5b0..f12fb5fdbd0 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -41,6 +41,17 @@ module Gitlab
abilities.allowed?(object, action, subject)
end
+ # Checks the occurrences of required attributes, each attribute must be present in the params hash
+ # or a Bad Request error is invoked.
+ #
+ # Parameters:
+ # keys (required) - A hash consisting of keys that must be present
+ def required_attributes!(keys)
+ keys.each do |key|
+ bad_request!(key) unless params[key].present?
+ end
+ end
+
def attributes_for_keys(keys)
attrs = {}
keys.each do |key|
diff --git a/lib/api/issues.rb b/lib/api/issues.rb
index da966fc0399..500a8551f35 100644
--- a/lib/api/issues.rb
+++ b/lib/api/issues.rb
@@ -48,7 +48,7 @@ module Gitlab
# Example Request:
# POST /projects/:id/issues
post ":id/issues" do
- bad_request!(:title) unless params[:title].present?
+ required_attributes! [:title]
attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id]
attrs[:label_list] = params[:labels] if params[:labels].present?
@issue = user_project.issues.new attrs
diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb
index 76cf8154bf8..7e4ec7e803c 100644
--- a/lib/api/merge_requests.rb
+++ b/lib/api/merge_requests.rb
@@ -68,10 +68,7 @@ module Gitlab
#
post ":id/merge_requests" do
authorize! :write_merge_request, user_project
-
- bad_request!(:source_branch) unless params[:source_branch].present?
- bad_request!(:target_branch) unless params[:target_branch].present?
- bad_request!(:title) unless params[:title].present?
+ required_attributes! [:source_branch, :target_branch, :title]
attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title]
merge_request = user_project.merge_requests.new(attrs)
@@ -125,7 +122,7 @@ module Gitlab
# POST /projects/:id/merge_request/:merge_request_id/comments
#
post ":id/merge_request/:merge_request_id/comments" do
- bad_request!(:note) unless params[:note].present?
+ required_attributes! [:note]
merge_request = user_project.merge_requests.find(params[:merge_request_id])
note = merge_request.notes.new(note: params[:note], project_id: user_project.id)
diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb
index ff98f005180..1adeefece1f 100644
--- a/lib/api/milestones.rb
+++ b/lib/api/milestones.rb
@@ -41,8 +41,7 @@ module Gitlab
# POST /projects/:id/milestones
post ":id/milestones" do
authorize! :admin_milestone, user_project
-
- bad_request!(:title) unless params[:title].present?
+ required_attributes! [:title]
attrs = attributes_for_keys [:title, :description, :due_date]
@milestone = user_project.milestones.new attrs
diff --git a/lib/api/notes.rb b/lib/api/notes.rb
index 953514b6f04..759fd3a9819 100644
--- a/lib/api/notes.rb
+++ b/lib/api/notes.rb
@@ -37,7 +37,7 @@ module Gitlab
# Example Request:
# POST /projects/:id/notes
post ":id/notes" do
- bad_request!(:body) unless params[:body].present?
+ required_attributes! [:body]
@note = user_project.notes.new(note: params[:body])
@note.author = current_user
@@ -93,8 +93,7 @@ module Gitlab
# POST /projects/:id/issues/:noteable_id/notes
# POST /projects/:id/snippets/:noteable_id/notes
post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
- bad_request!(:"#{noteable_id_str}") unless params[:"#{noteable_id_str}"].present?
- bad_request!(:body) unless params[:body].present?
+ required_attributes! [:"#{noteable_id_str}"]
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
@note = @noteable.notes.new(note: params[:body])
diff --git a/lib/api/projects.rb b/lib/api/projects.rb
index a1a7cee4032..a65d65840b0 100644
--- a/lib/api/projects.rb
+++ b/lib/api/projects.rb
@@ -45,7 +45,7 @@ module Gitlab
# Example Request
# POST /projects
post do
- bad_request!(:name) if !params.has_key? :name
+ required_attributes! [:name]
attrs = attributes_for_keys [:name,
:description,
:default_branch,
@@ -103,9 +103,7 @@ module Gitlab
# POST /projects/:id/members
post ":id/members" do
authorize! :admin_project, user_project
-
- bad_request!(:user_id) if !params.has_key? :user_id
- bad_request!(:access_level) if !params.has_key? :access_level
+ required_attributes! [:user_id, :access_level]
# either the user is already a team member or a new one
team_member = user_project.team_member_by_id(params[:user_id])
@@ -134,9 +132,9 @@ module Gitlab
# PUT /projects/:id/members/:user_id
put ":id/members/:user_id" do
authorize! :admin_project, user_project
+ required_attributes! [:access_level]
team_member = user_project.users_projects.find_by_user_id(params[:user_id])
- bad_request!(:access_level) if !params.has_key? :access_level
not_found!("User can not be found") if team_member.nil?
if team_member.update_attributes(project_access: params[:access_level])
@@ -199,8 +197,7 @@ module Gitlab
# POST /projects/:id/hooks
post ":id/hooks" do
authorize! :admin_project, user_project
-
- bad_request!(:url) unless params.has_key? :url
+ required_attributes! [:url]
@hook = user_project.hooks.new({"url" => params[:url]})
if @hook.save
@@ -224,8 +221,7 @@ module Gitlab
put ":id/hooks/:hook_id" do
@hook = user_project.hooks.find(params[:hook_id])
authorize! :admin_project, user_project
-
- bad_request!(:url) unless params.has_key? :url
+ required_attributes! [:url]
attrs = attributes_for_keys [:url]
if @hook.update_attributes attrs
@@ -245,9 +241,9 @@ module Gitlab
# hook_id (required) - The ID of hook to delete
# Example Request:
# DELETE /projects/:id/hooks/:hook_id
- delete ":id/hooks/:hook_id" do
+ delete ":id/hooks" do
authorize! :admin_project, user_project
- bad_request!(:hook_id) unless params.has_key? :hook_id
+ required_attributes! [:hook_id]
begin
@hook = ProjectHook.find(params[:hook_id])
@@ -381,10 +377,7 @@ module Gitlab
# POST /projects/:id/snippets
post ":id/snippets" do
authorize! :write_snippet, user_project
-
- bad_request!(:title) if !params[:title].present?
- bad_request!(:file_name) if !params[:file_name].present?
- bad_request!(:code) if !params[:code].present?
+ required_attributes! [:title, :file_name, :code]
attrs = attributes_for_keys [:title, :file_name]
attrs[:expires_at] = params[:lifetime] if params[:lifetime].present?
@@ -464,8 +457,7 @@ module Gitlab
# GET /projects/:id/repository/commits/:sha/blob
get ":id/repository/commits/:sha/blob" do
authorize! :download_code, user_project
-
- bad_request!(:filepath) if !params.has_key? :filepath
+ required_attributes! [:filepath]
ref = params[:sha]
diff --git a/lib/api/users.rb b/lib/api/users.rb
index b9dce58a13d..5e0680de71a 100644
--- a/lib/api/users.rb
+++ b/lib/api/users.rb
@@ -41,11 +41,7 @@ module Gitlab
# POST /users
post do
authenticated_as_admin!
-
- bad_request!(:email) if !params.has_key? :email
- bad_request!(:password) if !params.has_key? :password
- bad_request!(:name) if !params.has_key? :name
- bad_request!(:username) if !params.has_key? :username
+ required_attributes! [:email, :password, :name, :username]
attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio]
user = User.new attrs, as: :admin
@@ -135,8 +131,7 @@ module Gitlab
# Example Request:
# POST /user/keys
post "keys" do
- bad_request!(:title) unless params[:title].present?
- bad_request!(:key) unless params[:key].present?
+ required_attributes! [:title, :key]
attrs = attributes_for_keys [:title, :key]
key = current_user.keys.new attrs