diff --git a/lib/gitlab/client/merge_requests.rb b/lib/gitlab/client/merge_requests.rb index bdfdb1270..7f33427c7 100644 --- a/lib/gitlab/client/merge_requests.rb +++ b/lib/gitlab/client/merge_requests.rb @@ -50,10 +50,7 @@ def merge_request(project, id) # # Accepts source_branch, target_branch, assignee_id, & title in params. def create_merge_request(project, params={}) - - raise("Attribute source_branch is required.") unless params.has_key?(:source_branch) - raise("Attribute target_branch is required.") unless params.has_key?(:target_branch) - raise("Attribute title is required.") unless params.has_key?(:title) + check_attributes!(params, [:source_branch, :target_branch, :title]) post("/projects/#{project}/merge_requests", :body => params @@ -99,12 +96,22 @@ def update_merge_request(project, merge_id, params={}) # Accepts note (And alias, comment) def comment_merge_request(project, merge_id, params={}) params[:note] = params[:comment] if params.has_key?(:comment) - raise("Attribute note is required.") unless params.has_key?(:note) + check_attributes!(params, [:note]) post("/projects/#{project}/merge_request/#{merge_id}/comments", :body => params ) end + private + + def check_attributes!(options, attrs) + attrs.each do |attr| + unless options.has_key?(attr) || options.has_key?(attr.to_s) + raise Gitlab::Error::MissingAttributes.new("Missing '#{attr}' parameter") + end + end + end + end end diff --git a/lib/gitlab/error.rb b/lib/gitlab/error.rb index daf48b919..2e0d4415e 100644 --- a/lib/gitlab/error.rb +++ b/lib/gitlab/error.rb @@ -3,6 +3,9 @@ module Error # Custom error class for rescuing from all Gitlab errors. class Error < StandardError; end + # Raise when attributes are missing. + class MissingAttributes < Error; end + # Raised when API endpoint credentials not configured. class MissingCredentials < Error; end diff --git a/spec/gitlab/client/merge_requests_spec.rb b/spec/gitlab/client/merge_requests_spec.rb index 093e26bd6..64936074a 100644 --- a/spec/gitlab/client/merge_requests_spec.rb +++ b/spec/gitlab/client/merge_requests_spec.rb @@ -36,14 +36,35 @@ describe ".create_merge_request" do before do stub_post("/projects/3/merge_requests", "create_merge_request") - @merge_request = Gitlab.create_merge_request(3, - :source_branch => 'api', + end + + it "should fail if it doens't have a source_branch" do + expect { Gitlab.create_merge_request(3, :target_branch => 'master', :title => 'New feature' - ) + ) }.to raise_error Gitlab::Error::MissingAttributes + end + + it "should fail if it doens't have a target_branch" do + expect { Gitlab.create_merge_request(3, + :source_branch => 'master', + :title => 'New feature' + ) }.to raise_error Gitlab::Error::MissingAttributes + end + + it "should fail if it doens't have a title" do + expect { Gitlab.create_merge_request(3, + :target_branch => 'master', + :source_branch => 'master' + ) }.to raise_error Gitlab::Error::MissingAttributes end it "should return information about a merge request" do + @merge_request = Gitlab.create_merge_request(3, + :source_branch => 'api', + :target_branch => 'master', + :title => 'New feature' + ) @merge_request.project_id.should == 3 @merge_request.assignee.name.should == "Jack Smith" @merge_request.title.should == 'New feature' @@ -70,12 +91,18 @@ describe ".comment_merge_request" do before do stub_post("/projects/3/merge_request/2/comments", "comment_merge_request") - @merge_request = Gitlab.comment_merge_request(3, 2, - :note => 'Cool Merge Request!' - ) + end + + it "should fail if it doens't have a note" do + expect { + Gitlab.create_merge_request(3) + }.to raise_error Gitlab::Error::MissingAttributes end it "should return information about a merge request" do + @merge_request = Gitlab.comment_merge_request(3, 2, + :note => 'Cool Merge Request!' + ) @merge_request.note.should == 'Cool Merge Request!' @merge_request.author.id == 1 end