Skip to content

Commit

Permalink
reporters/bitbucket: change the way repourl is parsed
Browse files Browse the repository at this point in the history
* add unit tests for the parser
  • Loading branch information
Mikhail Sobolev committed Jul 22, 2016
1 parent 5ee286b commit 23938af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
29 changes: 17 additions & 12 deletions master/buildbot/reporters/bitbucket.py
Expand Up @@ -14,6 +14,7 @@
# Copyright Buildbot Team Members

import json
from urlparse import urlparse

from twisted.internet import defer
from twisted.python import log
Expand Down Expand Up @@ -100,15 +101,19 @@ def get_owner_and_repo(repourl):
ssh://git@bitbucket.com/OWNER/REPONAME.git
:return: owner, repo: The owner of the repository and the repository name
"""
owner = 'repo'
repo = 'repo'
if repourl.startswith('git@'):
tail = repourl.split(':')[-1]
owner = tail.split('/')[-2]
repo = tail.split('/')[-1]
elif repourl.startswith('http') or repourl.startswith('ssh://git@'):
repo = repourl.split('/')[-1]
owner = repourl.split('/')[-2]
if repo is not None and repo.endswith('.git'):
repo = repo[:-4]
return owner, repo
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
16 changes: 16 additions & 0 deletions master/buildbot/test/unit/test_reporter_bitbucket.py
Expand Up @@ -97,3 +97,19 @@ def test_basic(self):
'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'))

0 comments on commit 23938af

Please sign in to comment.