Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pull request comments #44 #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/bitbucket_rest_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def lookup_constant(const_name)

#:Teams => 'teams',
#:PullRequests => 'pull_requests',
#:Status => 'statuses',
#:Users => 'users',
#:Events => 'events',
#:Search => 'search',
Expand Down
4 changes: 4 additions & 0 deletions lib/bitbucket_rest_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def pull_requests(options = {})
@pull_requests ||= ApiFactory.new 'Repos::PullRequest', options
end

def status(options = {})
@status ||= ApiFactory.new 'Repos::Status', options
end

def repos(options = {})
@repos ||= ApiFactory.new 'Repos', options
end
Expand Down
13 changes: 12 additions & 1 deletion lib/bitbucket_rest_api/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class Repos < API
:Following => 'following',
:Sources => 'sources',
:Forks => 'forks',
:Commit => 'commit',
:Commits => 'commits',
:Download => 'download',
:Webhooks => 'webhooks',
:PullRequest => 'pull_request',
:DefaultReviewers => 'default_reviewers',
:Components => 'components'
:Components => 'components',
:Status => 'statuses',
:DefaultReviewers => 'default_reviewers'

DEFAULT_REPO_OPTIONS = {
"website" => "",
Expand Down Expand Up @@ -73,13 +76,21 @@ def services
def forks
@forks ||= ApiFactory.new 'Repos::Forks'
end
def commit
@commit ||=ApiFactory.new 'Repos::Commit'
end
def commits
@commits ||=ApiFactory.new 'Repos::Commits'
end
def download
@download ||=ApiFactory.new "Repos::Download"
end

# Access to Repos::Status API
def status
@status ||= ApiFactory.new 'Repos::Status'
end

# Access to Repos::PullRequests API
def pull_request
@pull_request ||= ApiFactory.new 'Repos::PullRequest'
Expand Down
21 changes: 21 additions & 0 deletions lib/bitbucket_rest_api/repos/commit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# encoding: utf-8
module BitBucket
class Repos::Commit < API
# Gets the commit information associated with a repository.
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.commit.list 'user-name', 'repo-name', hash,
#
def list(user_name, repo_name, hash)
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?

path = "/2.0/repositories/#{user}/#{repo.downcase}/commit/#{hash}"
response = get_request(path)
return response unless block_given?
response.each { |el| yield el }
end
alias :all :list
end # Repos::Commit
end # BitBucket
30 changes: 25 additions & 5 deletions lib/bitbucket_rest_api/repos/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def list(user_name, repo_name, params={})
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.pull_request.list 'user-name', 'repo-name'
# bitbucket.repos.pull_request.list 'user-name', 'repo-name' { |status| ... }
# bitbucket.repos.pull_request.participants 'user-name', 'repo-name', 'number'
#
def participants(user_name, repo_name, pull_request_id, params={})
_update_user_repo_params(user_name, repo_name)
Expand Down Expand Up @@ -71,7 +70,11 @@ def commits(user_name, repo_name, pull_request_id, params={})
normalize! params

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

def approve(user_name, repo_name, pull_request_id, params={})
Expand All @@ -92,16 +95,32 @@ def delete_approval(user_name, repo_name, pull_request_id, params={})
return response unless block_given?
end

# Stack that is raw and will follow redirects needed by diffs
#
def raw_follow_middleware()
Proc.new do |builder|
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
builder.use FaradayMiddleware::OAuth, {:consumer_key => client_id, :consumer_secret => client_secret, :token => oauth_token, :token_secret => oauth_secret} if client_id? and client_secret?
builder.use BitBucket::Request::BasicAuth, authentication if basic_authed?
builder.use BitBucket::Response::Helpers
builder.use BitBucket::Response::RaiseError
builder.use FaradayMiddleware::FollowRedirects
builder.adapter adapter
end
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

clear_cache
@connection = Faraday.new(default_options({}).merge(builder: Faraday::RackBuilder.new(&raw_follow_middleware)))
response = request(:get, "/2.0/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/diff", params)
clear_cache
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?
Expand Down Expand Up @@ -156,5 +175,6 @@ def comment(user_name, repo_name, pull_request_id, comment_id, 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
61 changes: 61 additions & 0 deletions lib/bitbucket_rest_api/repos/statuses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# encoding: utf-8

module BitBucket
class Repos::Status < API

# List pull requests
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.status.get 'user-name', 'repo-name', 'sha', 'yourapp'
# params state Yes
# An indication of the status of the commit:
# INPROGRESS indicates that a build for the commit is in progress but not yet complete.
# SUCCESSFUL indicates that a build for the commit completed successfully.
# FAILED indicates that a build for the commit failed.
# key Yes
# A key that the vendor or build system supplies to identify the submitted build status. Because a single commit can involve multiple builds, the key needs to be unique compared to other builds associated with the commit.
# For example, BAMBOO-PROJECT-X or JENKINS-BUILD-5.
# name No The name of the build. Your build system may provide the name, which will also appear in Bitbucket. For example, Unit Tests.
# url Yes The URL for the vendor or system that produces the build.
# description No A user-defined description of the build. For example, 4 out of 128 tests passed.
#
def get(user_name, repo_name, sha, key, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params

response = get_request("/2.0/repositories/#{user}/#{repo.downcase}/commit/#{sha}/statuses/build/#{key}", params)
return response unless block_given?
response.each { |el| yield el }
end

def post(user_name, repo_name, sha, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params
assert_required_keys(%w(description url state key), params)
assert_required_values_present(
params,
'description',
'url',
'state',
'key'
)

response = post_request("/2.0/repositories/#{user}/#{repo.downcase}/commit/#{sha}/statuses/build", params)
return response unless block_given?
response.each { |el| yield el }
end

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

response = put_request("/2.0/repositories/#{user}/#{repo.downcase}/commit/#{sha}/statuses/build/#{key}", params)
return response unless block_given?
response.each { |el| yield el }
end
end
end
6 changes: 5 additions & 1 deletion lib/bitbucket_rest_api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def request(method, path, params, options={})
unless params.empty?
# data = extract_data_from_params(params)
# request.body = MultiJson.dump(data)
request.body = MultiJson.dump(params)
if path =~ /statuses\/build$/
request.body = params
else
request.body = MultiJson.dump(params)
end
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions spec/bitbucket_rest_api/repos/commits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
'/2.0/repositories/mock_username/mock_repo/commits',
{},
{}
)
).and_return(["Commit1","Commit2","Commit3"])
end

it 'should send a GET request for the commits belonging to the given repo' do
commits.list('mock_username', 'mock_repo')
context 'without a block' do
it 'should send a GET request for the commits belonging to the given repo' do
commits.list('mock_username', 'mock_repo')
end
end

context 'with a block' do
it 'should send a GET request for the commits belonging to the given repo' do
commits.list('mock_username', 'mock_repo') {|commits| commits}
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'vcr'

require 'bitbucket_rest_api'
WebMock.disable_net_connect!(:allow_localhost => true)

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
Expand Down