Skip to content

Commit

Permalink
reporters/bitbucket: cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Sobolev committed Jul 22, 2016
1 parent e22dbcb commit 5ee286b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 42 deletions.
51 changes: 36 additions & 15 deletions master/buildbot/reporters/bitbucket.py
Expand Up @@ -26,42 +26,62 @@
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='https://api.bitbucket.org/2.0/repositories',
oauth_url='https://bitbucket.org/site/oauth2/access_token', **kwargs):
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)

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']
}
body = {
'state': status,
'key': build['builder']['name'],
'name': build['builder']['name'],
'url': build['url']
}

owner, repo = self.get_owner_and_repo(sourcestamp['repository'])
token = ''
oauth_request = yield self.session.post(self.oauth_url, auth=self.auth,
data={'grant_type': 'client_credentials'})

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'])

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" %
Expand All @@ -70,7 +90,8 @@ def send(self, build):
log.msg("Unable to determine owner or repository name: (owner: %s, repo: %s)" %
(str(owner), str(repo)))

def get_owner_and_repo(self, repourl):
@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
Expand Down
70 changes: 43 additions & 27 deletions master/buildbot/test/unit/test_reporter_bitbucket.py
Expand Up @@ -36,48 +36,64 @@ def setUp(self):
self.master = fakemaster.make_master(testcase=self,
wantData=True, wantDb=True, wantMq=True)

self.sp = sp = BitbucketStatusPush("key", "secret")
sp.sessionFactory = Mock(return_value=Mock())
yield sp.setServiceParent(self.master)
yield sp.startService()
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.sp.stopService()
self.assertEqual(self.sp.session.close.call_count, 1)
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))
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.sp.buildStarted(("build", 20, "started"), build)
self.bsp.buildStarted(('build', 20, 'started'), build)

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

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

# we make sure proper calls to txrequests have been made
self.assertEqual(
self.sp.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/repo/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/repo/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/repo/repo/commit/d34db33fd43db33f/statuses/build',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'FAILED', 'key': u'Builder0', 'name': u'Builder0'})
])
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'})
])

0 comments on commit 5ee286b

Please sign in to comment.