Skip to content

Commit

Permalink
Merge pull request #4 from codeship/create_status
Browse files Browse the repository at this point in the history
Adding create_status call
  • Loading branch information
Kyle Rames authored Jun 28, 2016
2 parents 7ba7830 + 4daec3d commit 3da75f0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/bitbucket_rest_api/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Repos < API
:Services => 'services',
:Following => 'following',
:Sources => 'sources',
:Statuses => 'statuses',
:Forks => 'forks',
:Commits => 'commits',
:Download => 'download',
Expand Down Expand Up @@ -69,6 +70,10 @@ def services
@services ||= ApiFactory.new 'Repos::Services'
end

def statuses
@statuses ||= ApiFactory.new 'Repos::Statuses'
end

def forks
@forks ||= ApiFactory.new 'Repos::Forks'
end
Expand Down
46 changes: 46 additions & 0 deletions lib/bitbucket_rest_api/repos/statuses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# encoding: utf-8

module BitBucket
class Repos::Statuses < API

# Add build status to commit
#
# = Parameters
# <tt>:user_name</tt> - Required string. The account of the repository owner.
# <tt>:repo_name</tt> - Required string. The repository name.
# <tt>:sha</tt> - Requrired string. The SHA1 value for the commit where you want to add the build status.
# <tt>:state</tt> - Required String. An indication of the status of the commit: (INPROGRESS, SUCCESSFUL, or FAILED)
# <tt>:key</tt> - Required String. A key that the vendor or build system supplies to identify the submitted build status.
# <tt>:url</tt> - Required String. The URL for the vendor or system that produces the build.
# <tt>:options</tt> Optional hash. Containing the following optional parameters:
# * <tt>description</tt> - A user-defined description of the build. For example, 4 out of 128 tests passed.
# * <tt>name<tt> - The name of the build. Your build system may provide the name, which will also appear in Bitbucket. For example, Unit Tests.
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.create_status(client.repos.statuses.create('homer',
# 'monorail',
# 'e14caad7c501e0ae52daef24b26aa2625b7534e6',
# 'SUCCESSFUL',
# 'BAMBOO-PROJECT-X',
# 'https://example.com/path/to/build/info',
# 'name' => 'this is covered by build_concurrency_spec',
# 'description' => 'Changes by John Doe')
#
def create(user_name, repo_name, sha, state, key, url, options = {})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_validate_presence_of(sha, state, key, url)

build_options = options.merge({
"state" => state,
"key" => key,
"url" => url
})

post_request("/2.0/repositories/#{user}/#{sanitize_repository_name(repo)}/commit/#{sha}/statuses/build",
build_options,
json: true)
end
end # Repos::Statuses
end # BitBucket
48 changes: 48 additions & 0 deletions spec/bitbucket_rest_api/repos/statuses_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'bitbucket_rest_api'

describe BitBucket::Repos::Statuses do
let(:repo_name) { 'wrath' }
let(:sha) { '123456' }
let(:state) { 'FAILED' }
let(:key) { 'my-build-key' }
let(:url) { 'http://somewhere/build' }

let(:statuses) { described_class.new }

before { allow(statuses).to receive(:post_request) }

subject(:status_create) do
statuses.create('khan', repo_name, sha, state, key, url, { 'extra-stuff' => 'hello'})
end

describe 'create' do
it 'should send request if parameters are valid' do
expect(statuses).to receive(:post_request).with("/2.0/repositories/khan/wrath/commit/123456/statuses/build",
{
"extra-stuff"=>"hello",
"state"=> state,
"key"=> key,
"url"=> url
},
{json: true})

status_create
end

it 'should sanitize_repository_name' do
expect(statuses).to receive(:sanitize_repository_name).with(repo_name) { 'radio-edit' }
expect(statuses).to receive(:post_request).with("/2.0/repositories/khan/radio-edit/commit/123456/statuses/build",
anything,
anything)

status_create
end

it 'should validate presence of sha, state, key, url' do
expect(statuses).to receive(:_validate_presence_of).with(sha, state, key, url)

status_create
end

end
end

0 comments on commit 3da75f0

Please sign in to comment.