Skip to content

Commit

Permalink
Merge branch 'bitbucket-reporter' of https://github.com/sa2ajj/buildbot
Browse files Browse the repository at this point in the history
… into sa2ajj-bitbucket-reporter
  • Loading branch information
tardyp committed Jul 26, 2016
2 parents c886a4e + 6463043 commit 126779f
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 3 deletions.
116 changes: 116 additions & 0 deletions master/buildbot/reporters/bitbucket.py
@@ -0,0 +1,116 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import json
from urlparse import urlparse

from twisted.internet import defer
from twisted.python import log

from buildbot.process.results import SUCCESS
from buildbot.reporters import http

# Magic words understood by Butbucket REST API
BITBUCKET_INPROGRESS = 'INPROGRESS'
BITBUCKET_SUCCESSFUL = 'SUCCESSFUL'
BITBUCKET_FAILED = 'FAILED'

_BASE_URL = 'https://api.bitbucket.org/2.0/repositories'
_OAUTH_URL = 'https://bitbucket.org/site/oauth2/access_token'
_GET_TOKEN_DATA = {
'grant_type': 'client_credentials'
}


class BitbucketStatusPush(http.HttpStatusPushBase):
name = "BitbucketStatusPush"

@defer.inlineCallbacks
def reconfigService(self, oauth_key, oauth_secret,
base_url=_BASE_URL,
oauth_url=_OAUTH_URL,
**kwargs):
yield http.HttpStatusPushBase.reconfigService(self, **kwargs)

if base_url.endswith('/'):
base_url = base_url[:-1]

self._base_url = base_url
self._oauth_url = oauth_url
self._auth = (oauth_key, oauth_secret)

@defer.inlineCallbacks
def send(self, build):
results = build['results']

if build['complete']:
status = BITBUCKET_SUCCESSFUL if results == SUCCESS else BITBUCKET_FAILED
else:
status = BITBUCKET_INPROGRESS

for sourcestamp in build['buildset']['sourcestamps']:
sha = sourcestamp['revision']
body = {
'state': status,
'key': build['builder']['name'],
'name': build['builder']['name'],
'url': build['url']
}

owner, repo = self.get_owner_and_repo(sourcestamp['repository'])

oauth_request = yield self.session.post(self._oauth_url,
auth=self._auth,
data=_GET_TOKEN_DATA)
if oauth_request.status_code == 200:
token = json.loads(oauth_request.content)['access_token']
else:
token = ''

self.session.headers.update({'Authorization': 'Bearer ' + token})

bitbucket_uri = '/'.join([self._base_url, owner, repo, 'commit', sha, 'statuses', 'build'])

response = yield self.session.post(bitbucket_uri, json=body)
if response.status_code != 201:
log.msg("%s: unable to upload Bitbucket status: %s" %
(response.status_code, response.content))

@staticmethod
def get_owner_and_repo(repourl):
"""
Takes a git repository URL from Bitbucket and tries to determine the owner and repository name
:param repourl: Bitbucket git repo in the form of
git@bitbucket.com:OWNER/REPONAME.git
https://bitbucket.com/OWNER/REPONAME.git
ssh://git@bitbucket.com/OWNER/REPONAME.git
:return: owner, repo: The owner of the repository and the repository name
"""
parsed = urlparse(repourl)

if parsed.scheme:
path = parsed.path[1:]
else:
# we assume git@host:owner/repo.git here
path = parsed.path.split(':', 1)[-1]

if path.endswith('.git'):
path = path[:-4]

parts = path.split('/')

assert len(parts) == 2, 'OWNER/REPONAME is expected'

return parts
115 changes: 115 additions & 0 deletions master/buildbot/test/unit/test_reporter_bitbucket.py
@@ -0,0 +1,115 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from mock import Mock
from mock import call

from twisted.internet import defer
from twisted.trial import unittest

from buildbot import config
from buildbot.process.results import FAILURE
from buildbot.process.results import SUCCESS
from buildbot.reporters.bitbucket import BitbucketStatusPush
from buildbot.test.fake import fakemaster
from buildbot.test.util.reporter import ReporterTestMixin


class TestBitbucketStatusPush(unittest.TestCase, ReporterTestMixin):
TEST_REPO = u'https://example.org/user/repo'

@defer.inlineCallbacks
def setUp(self):
# ignore config error if txrequests is not installed
config._errors = Mock()
self.master = fakemaster.make_master(testcase=self,
wantData=True, wantDb=True, wantMq=True)

self.bsp = bsp = BitbucketStatusPush('key', 'secret')
bsp.sessionFactory = Mock(return_value=Mock())
yield bsp.setServiceParent(self.master)
yield bsp.startService()

@defer.inlineCallbacks
def tearDown(self):
yield self.bsp.stopService()
self.assertEqual(self.bsp.session.close.call_count, 1)
config._errors = None

@defer.inlineCallbacks
def setupBuildResults(self, buildResults):
self.insertTestData([buildResults], buildResults)
build = yield self.master.data.get(('builds', 20))
defer.returnValue(build)

@defer.inlineCallbacks
def test_basic(self):
build = yield self.setupBuildResults(SUCCESS)

build['complete'] = False
self.bsp.buildStarted(('build', 20, 'started'), build)

build['complete'] = True
self.bsp.buildFinished(('build', 20, 'finished'), build)

build['results'] = FAILURE
self.bsp.buildFinished(('build', 20, 'finished'), build)

# we make sure proper calls to txrequests have been made
self.assertEqual(
self.bsp.session.post.mock_calls, [
call('https://bitbucket.org/site/oauth2/access_token',
auth=('key', 'secret'),
data={'grant_type': 'client_credentials'}),
call(u'https://api.bitbucket.org/2.0/repositories/user/repo/commit/d34db33fd43db33f/statuses/build',
json={
'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'INPROGRESS',
'key': u'Builder0',
'name': u'Builder0'}),
call('https://bitbucket.org/site/oauth2/access_token',
auth=('key', 'secret'),
data={'grant_type': 'client_credentials'}),
call(u'https://api.bitbucket.org/2.0/repositories/user/repo/commit/d34db33fd43db33f/statuses/build',
json={
'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'SUCCESSFUL',
'key': u'Builder0',
'name': u'Builder0'}),
call('https://bitbucket.org/site/oauth2/access_token',
auth=('key', 'secret'),
data={'grant_type': 'client_credentials'}),
call(u'https://api.bitbucket.org/2.0/repositories/user/repo/commit/d34db33fd43db33f/statuses/build',
json={
'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'FAILED',
'key': u'Builder0',
'name': u'Builder0'})
])


class TestBitbucketStatusPushRepoParsing(unittest.TestCase):
def parse(self, repourl):
return tuple(BitbucketStatusPush.get_owner_and_repo(repourl))

def test_parse_no_scheme(self):
self.assertEqual(('user', 'repo'), self.parse('git@bitbucket.com:user/repo.git'))
self.assertEqual(('user', 'repo'), self.parse('git@bitbucket.com:user/repo'))

def test_parse_with_scheme(self):
self.assertEqual(('user', 'repo'), self.parse('https://bitbucket.com/user/repo.git'))
self.assertEqual(('user', 'repo'), self.parse('https://bitbucket.com/user/repo'))

self.assertEqual(('user', 'repo'), self.parse('ssh://git@bitbucket.com/user/repo.git'))
self.assertEqual(('user', 'repo'), self.parse('ssh://git@bitbucket.com/user/repo'))
8 changes: 6 additions & 2 deletions master/buildbot/test/util/reporter.py
Expand Up @@ -23,6 +23,7 @@
class ReporterTestMixin(object):

TEST_PROJECT = u'testProject'
TEST_REPO = u'https://example.org/repo'
TEST_REVISION = u'd34db33fd43db33f'
TEST_CHANGE_ID = u'I5bdc2e500d00607af53f0fa4df661aada17f81fc'
TEST_BUILDER_NAME = u'Builder0'
Expand All @@ -45,9 +46,12 @@ def insertTestData(self, buildResults, finalResult):
fakedb.Builder(id=80, name='Builder1'),
fakedb.Buildset(id=98, results=finalResult, reason="testReason1"),
fakedb.BuildsetSourceStamp(buildsetid=98, sourcestampid=234),
fakedb.SourceStamp(id=234, project=self.TEST_PROJECT, revision=self.TEST_REVISION),
fakedb.SourceStamp(id=234,
project=self.TEST_PROJECT,
revision=self.TEST_REVISION,
repository=self.TEST_REPO),
fakedb.Change(changeid=13, branch=u'master', revision=u'9283', author='me@foo',
repository=u'https://...', codebase=u'cbgerrit',
repository=self.TEST_REPO, codebase=u'cbgerrit',
project=u'world-domination', sourcestampid=234),
])
for i, results in enumerate(buildResults):
Expand Down
34 changes: 33 additions & 1 deletion master/docs/manual/cfg-reporters.rst
Expand Up @@ -879,6 +879,39 @@ As a result, we recommend you use https in your base_url rather than http.
:param string password: the stash user's password
:param list builders: only send update for specified builders

.. bb:reporter:: BitbucketStatusPush
BitbucketStatusPush
~~~~~~~~~~~~~~~~~~~

.. py:class:: buildbot.reporters.bitbucket.BitbucketStatusPush
::

from buildbot.plugins import reporters
bs = reporters.BitbucketStatusPush('oauth_key', 'oauth_secret')
c['services'].append(bs)

:class:`BitbucketStatusPush` publishes build status using `Bitbucket Build Status API <https://confluence.atlassian.com/bitbucket/buildstatus-resource-779295267.html>`_.
The build status is published to a specific commit SHA in Bitbucket.
It tracks the last build for each builderName for each commit built.

It requires `txrequests`_ package to allow interaction with the Bitbucket REST and OAuth APIs.

It uses OAuth 2.x to authenticate with Bitbucket.
To enable this, you need to go to your Bitbucket Settings -> OAuth page.
Click "Add consumer".
Give the new consumer a name, eg 'buildbot', and put in any URL as the callback (this is needed for Oauth 2.x but is not used by this reporter, eg 'https://localhost:8010/callback').
Give the consumer Repositories:Write access.
After creating the consumer, you will then be able to see the OAuth key and secret.

.. py:class:: BitbucketStatusPush(oauth_key, oauth_secret, base_url='https://api.bitbucket.org/2.0/repositories', oauth_url='https://bitbucket.org/site/oauth2/access_token', builders=None)
:param string oauth_key: The OAuth consumer key
:param string oauth_secret: The OAuth consumer secret
:param string base_url: Bitbucket's Build Status API URL
:param string oauth_url: Bitbucket's OAuth API URL
:param list builders: only send update for specified builders

.. bb:reporter:: GitLabStatusPush
Expand Down Expand Up @@ -1033,4 +1066,3 @@ Here's a complete example:
result['color'] = 'green' if build['results'] == 0 else 'red'
result['notify'] = (build['results'] != 0)
return result
2 changes: 2 additions & 0 deletions master/docs/relnotes/0.9.0rc1.rst
Expand Up @@ -23,6 +23,8 @@ Features

* The ``dist`` parameter in :bb:step:`RpmBuild` is now renderable.

* new :bb:reporter:`BitbucketStatusPush` to report build results to a Bitbucket Cloud repository.

Fixes
~~~~~

Expand Down
1 change: 1 addition & 0 deletions master/docs/spelling_wordlist.txt
Expand Up @@ -538,6 +538,7 @@ npm
nullability
oauth
oAuth
OAuth
objectid
objtained
occured
Expand Down
1 change: 1 addition & 0 deletions master/setup.py
Expand Up @@ -302,6 +302,7 @@ def define_plugin_entries(groups):
('buildbot.reporters.http', ['HttpStatusPush']),
('buildbot.reporters.github', ['GitHubStatusPush']),
('buildbot.reporters.stash', ['StashStatusPush']),
('buildbot.reporters.bitbucket', ['BitbucketStatusPush']),
('buildbot.reporters.irc', ['IRC']),

]),
Expand Down

0 comments on commit 126779f

Please sign in to comment.