Skip to content

Commit

Permalink
Adding a check_attributes method in order to verify that the correct …
Browse files Browse the repository at this point in the history
…parameters have been passed into create_merge_request and comment_merge_request. Updating tests.
  • Loading branch information
TJ Biddle committed Jul 30, 2013
1 parent 6f4a170 commit dd33a2f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
17 changes: 12 additions & 5 deletions lib/gitlab/client/merge_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions lib/gitlab/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 33 additions & 6 deletions spec/gitlab/client/merge_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit dd33a2f

Please sign in to comment.