diff --git a/lib/bitbucket_rest_api.rb b/lib/bitbucket_rest_api.rb index de13095..0941ef5 100644 --- a/lib/bitbucket_rest_api.rb +++ b/lib/bitbucket_rest_api.rb @@ -81,6 +81,7 @@ def lookup_constant(const_name) #:Teams => 'teams', #:PullRequests => 'pull_requests', + #:Status => 'statuses', #:Users => 'users', #:Events => 'events', #:Search => 'search', diff --git a/lib/bitbucket_rest_api/client.rb b/lib/bitbucket_rest_api/client.rb index bd594e0..451beb7 100644 --- a/lib/bitbucket_rest_api/client.rb +++ b/lib/bitbucket_rest_api/client.rb @@ -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 diff --git a/lib/bitbucket_rest_api/repos.rb b/lib/bitbucket_rest_api/repos.rb index e9a62a1..2039858 100644 --- a/lib/bitbucket_rest_api/repos.rb +++ b/lib/bitbucket_rest_api/repos.rb @@ -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" => "", @@ -73,6 +76,9 @@ 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 @@ -80,6 +86,11 @@ 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' diff --git a/lib/bitbucket_rest_api/repos/commit.rb b/lib/bitbucket_rest_api/repos/commit.rb new file mode 100644 index 0000000..0b612f8 --- /dev/null +++ b/lib/bitbucket_rest_api/repos/commit.rb @@ -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 diff --git a/lib/bitbucket_rest_api/repos/pull_request.rb b/lib/bitbucket_rest_api/repos/pull_request.rb index 48fef84..776b69e 100644 --- a/lib/bitbucket_rest_api/repos/pull_request.rb +++ b/lib/bitbucket_rest_api/repos/pull_request.rb @@ -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) @@ -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={}) @@ -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? @@ -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 diff --git a/lib/bitbucket_rest_api/repos/statuses.rb b/lib/bitbucket_rest_api/repos/statuses.rb new file mode 100644 index 0000000..edeb648 --- /dev/null +++ b/lib/bitbucket_rest_api/repos/statuses.rb @@ -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 \ No newline at end of file diff --git a/lib/bitbucket_rest_api/request.rb b/lib/bitbucket_rest_api/request.rb index 3ab68ab..5afdbb9 100644 --- a/lib/bitbucket_rest_api/request.rb +++ b/lib/bitbucket_rest_api/request.rb @@ -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 diff --git a/spec/bitbucket_rest_api/repos/commits_spec.rb b/spec/bitbucket_rest_api/repos/commits_spec.rb index 605c62c..3f3ad1f 100644 --- a/spec/bitbucket_rest_api/repos/commits_spec.rb +++ b/spec/bitbucket_rest_api/repos/commits_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 27cfe60..1436900 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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|