Extracted helper method to avoid code duplication

This commit is contained in:
Sebastian Ziebell 2013-02-05 18:36:36 +01:00
parent 413952ff94
commit 3f4e215c80

View file

@ -5,6 +5,23 @@ module Gitlab
resource :projects do
helpers do
# If an error occurred this helper method provides an appropriate status code
#
# Parameters:
# merge_request_errors (required) - The errors collection of MR
#
def handle_merge_request_error(merge_request_errors)
if merge_request_errors[:target_branch].any?
error!(merge_request_errors[:target_branch], 400)
elsif merge_request_errors[:source_branch].any?
error!(merge_request_errors[:source_branch], 400)
elsif merge_request_errors[:base].any?
error!(merge_request_errors[:base], 422)
end
end
end
# List merge requests
#
# Parameters:
@ -60,13 +77,7 @@ module Gitlab
merge_request.reload_code
present merge_request, with: Entities::MergeRequest
else
if merge_request.errors[:target_branch].any?
error!(merge_request.errors[:target_branch], 400)
elsif merge_request.errors[:source_branch].any?
error!(merge_request.errors[:source_branch], 400)
elsif merge_request.errors[:base].any?
error!(merge_request.errors[:base], 422)
end
handle_merge_request_error(merge_request.errors)
not_found!
end
end
@ -95,13 +106,7 @@ module Gitlab
merge_request.mark_as_unchecked
present merge_request, with: Entities::MergeRequest
else
if merge_request.errors[:target_branch].any?
error!(merge_request.errors[:target_branch], 400)
elsif merge_request.errors[:source_branch].any?
error!(merge_request.errors[:source_branch], 400)
elsif merge_request.errors[:base].any?
error!(merge_request.errors[:base], 422)
end
handle_merge_request_error(merge_request.errors)
not_found!
end
end