Skip to content

Commit

Permalink
Adds support for pull_request endpoint
Browse files Browse the repository at this point in the history
wip POST request

Remove binding.pry

Add endpoint to get a particular pull request

Finish implement create endpoint

Implement updating pull request endpoint

Implement pull requests endpoints

Fix encoding

Remove wip rspec tags
Alfred Oliver Willder authored and dmgarland committed Mar 29, 2016
1 parent 26b21db commit 24f6ce6
Showing 3 changed files with 368 additions and 12 deletions.
118 changes: 118 additions & 0 deletions lib/bitbucket_rest_api/repos/pull_request.rb
Original file line number Diff line number Diff line change
@@ -38,5 +38,123 @@ def participants(user_name, repo_name, pull_request_id, params={})
response.each { |el| yield el }
end

def get(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}", params)
return response unless block_given?
end

def create(user_name, repo_name, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:post, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests", params)
return response unless block_given?
end

def update(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:put, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}", params)
return response unless block_given?
end

def get_commits(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/commits", params)
return response unless block_given?
end

def approve(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:post, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/approve", params)
return response unless block_given?
end

def delete_approval(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:delete, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/approve", params)
return response unless block_given?
end

def diff(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/diff", params)
return response unless block_given?
end


def all_activity(user_name, repo_name, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/activity", params)
return response unless block_given?
end


def activity(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/activity", params)
return response unless block_given?
end

def merge(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:post, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/merge", params)
return response unless block_given?
end

def decline(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:post, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/decline", params)
return response unless block_given?
end

def comments(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/comments", params)
return response unless block_given?
end

def comment(user_name, repo_name, pull_request_id, comment_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/comments/#{comment_id}", params)
return response unless block_given?
end
end # Repos::Keys
end # BitBucket
2 changes: 1 addition & 1 deletion lib/bitbucket_rest_api/request.rb
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ def delete_request(path, params={}, options={})
request(:delete, path, params, options)
end

def request(method, path, params, options)
def request(method, path, params, options={})
if !METHODS.include?(method)
raise ArgumentError, "unkown http method: #{method}"
end
260 changes: 249 additions & 11 deletions spec/bitbucket_rest_api/repos/pull_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,272 @@
require 'spec_helper'

describe BitBucket::Repos::PullRequest do
let(:pull_request) { described_class.new }
describe '.list' do
subject { described_class.new }
describe '#list' do
before do
expect(pull_request).to receive(:request).with(
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_username/mock_repo/pullrequests',
'/2.0/repositories/mock_user/mock_repo/pullrequests',
{},
{}
)
end

it 'should make a GET request for the list of pull requests' do
pull_request.list('mock_username', 'mock_repo')
it 'makes a GET request for all pull requests belonging to the repo' do
subject.list('mock_user', 'mock_repo')
end
end

describe '.participants' do
describe '#participants' do
before do
expect(pull_request).to receive(:request).with(
expect(subject).to receive(:request).with(
:get,
'/1.0/repositories/mock_username/mock_repo/pullrequests/mock_pull_request_id/participants',
"/1.0/repositories/mock_user/mock_repo/pullrequests/mock_pull_request_id/participants",
{},
{}
)
end

it 'should make a GET request for the list of participants' do
pull_request.participants('mock_username', 'mock_repo', 'mock_pull_request_id')
it 'makes a GET request for all participants belonging to the repo' do
subject.participants('mock_user', 'mock_repo', 'mock_pull_request_id')
end
end

describe '#get' do
before do
expect(subject).to receive(:request).with(
:get,
"/2.0/repositories/mock_user/mock_repo/pullrequests/mock_pull_request_id",
{}
)
end

it 'makes a GET request for the pull request belonging to the repo' do
subject.get('mock_user', 'mock_repo', 'mock_pull_request_id')
end
end

describe '#create' do
before do
@params = {
title: "mock_pr_title",
description: "mock_pull_request_description",
source: {
branch: {
name: "mock_source_branch_name"
},
repository: {
full_name: "mock_owner/mock_repo"
}
},
destination: {
branch: {
name: "mock_destination_branch_name"
},
commit: {
hash: "mock_uuid"
}
},
close_source_branch: true
}

expect(subject).to receive(:request).with(
:post,
'/2.0/repositories/mock_user/mock_repo/pullrequests',
@params
)
end

it 'makes a POST request to create a new pull request' do
subject.create('mock_user', 'mock_repo', @params)
end

xit 'validates presence of required params' do
# expect do
subject.create(
'mock_user',
'mock_repo',
{
title: "",
description: "mock_pull_request_description",
source: {
branch: {
name: "mock_source_branch_name"
},
repository: {
full_name: "mock_owner/mock_repo"
}
},
destination: {
branch: {
name: "mock_destination_branch_name"
},
commit: {
hash: "mock_uuid"
}
},
close_source_branch: true
}
)
# end.to(raise_error())
end
end

describe '.put' do
before do
expect(subject).to receive(:request).with(
:put,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id',
{}
)
end

it 'makes a PUT request for the given pull request' do
subject.update('mock_user', 'mock_repo', 'mock_id')
end
end

describe '.get_commits' do
before do
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/commits',
{}
)
end

it 'makes a GET request for the commits' do
subject.get_commits('mock_user', 'mock_repo', 'mock_id')
end
end

describe '.get_commits' do
before do
expect(subject).to receive(:request).with(
:post,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/approve',
{}
)
end

it 'makes a POST request' do
subject.approve('mock_user', 'mock_repo', 'mock_id')
end
end

describe '.delete_approval' do
before do
expect(subject).to receive(:request).with(
:delete,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/approve',
{}
)
end

it 'makes a DELTE request' do
subject.delete_approval('mock_user', 'mock_repo', 'mock_id')
end
end


describe '.diff' do
before do
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/diff',
{}
)
end

it 'makes a GET request for the diff of the pull request' do
subject.diff('mock_user', 'mock_repo', 'mock_id')
end
end

describe '.all_activity' do
before do
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_user/mock_repo/pullrequests/activity',
{}
)
end

it 'makes a GET request' do
subject.all_activity('mock_user', 'mock_repo')
end
end


describe '.activity' do
before do
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/activity',
{}
)
end

it 'makes a GET request' do
subject.activity('mock_user', 'mock_repo', 'mock_id')
end
end


describe '.accept_and_merge' do
before do
expect(subject).to receive(:request).with(
:post,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/merge',
{}
)
end

it 'makes a POST request' do
subject.merge('mock_user', 'mock_repo', 'mock_id')
end
end

describe '.decline' do
before do
expect(subject).to receive(:request).with(
:post,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/decline',
{}
)
end

it 'makes a POST request' do
subject.decline('mock_user', 'mock_repo', 'mock_id')
end
end


describe '.comments' do
before do
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/comments',
{}
)
end

it 'makes a GET request' do
subject.comments('mock_user', 'mock_repo', 'mock_id')
end
end

describe '.comment' do
before do
expect(subject).to receive(:request).with(
:get,
'/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/comments/comment_id',
{}
)
end

it 'makes a GET request' do
subject.comment('mock_user', 'mock_repo', 'mock_id', 'comment_id')
end
end
end

1 comment on commit 24f6ce6

@iconnor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tagging issue #44 and PR #55

Please sign in to comment.