Skip to content

Commit

Permalink
handle missing account properties for GerritChangeSource
Browse files Browse the repository at this point in the history
* create a separate function to convert account properties
* make use of the function in all cases where the situation might take place
* update test cases

Fixes ticket:2918
  • Loading branch information
Mikhail Sobolev committed Sep 30, 2014
1 parent a25b439 commit 2748c9b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
25 changes: 18 additions & 7 deletions master/buildbot/changes/gerritchangesource.py
Expand Up @@ -42,6 +42,21 @@ def __init__(self,
del self.checks["branch"]


def _gerrit_user_to_author(props, username=u"unknown"):
"""
Convert Gerrit account properties to Buildbot format
Take into account missing values
"""
username = props.get("username", username)

result = [props.get("name", username)]
if "email" in props:
result.append(u"<%s>" % props["email"])

return u" ".join(result)


class GerritChangeSource(base.ChangeSource):

"""This source will maintain a connection to gerrit ssh server
Expand Down Expand Up @@ -187,11 +202,8 @@ def addChangeFromEvent(self, properties, event):

if "change" in event and "patchSet" in event:
event_change = event["change"]
username = event_change["owner"].get("username", u"unknown")
return self.addChange({
'author': "%s <%s>" % (
event_change["owner"].get("name", username),
event_change["owner"].get("email", u'unknown@example.com')),
'author': _gerrit_user_to_author(event_change["owner"]),
'project': util.ascii2unicode(event_change["project"]),
'repository': u"ssh://%s@%s:%s/%s" % (
self.username, self.gerritserver,
Expand All @@ -206,11 +218,10 @@ def addChangeFromEvent(self, properties, event):

def eventReceived_ref_updated(self, properties, event):
ref = event["refUpdate"]
author = "gerrit"
author = u"gerrit"

if "submitter" in event:
author = "%s <%s>" % (
event["submitter"]["name"], event["submitter"]["email"])
author = _gerrit_user_to_author(event["submitter"], author)

return self.addChange(dict(
author=author,
Expand Down
53 changes: 53 additions & 0 deletions master/buildbot/test/unit/test_changes_gerritchangesource.py
Expand Up @@ -21,6 +21,59 @@
from twisted.trial import unittest


class TestGerritHelpers(unittest.TestCase):
def test_proper_json(self):
self.assertEqual(u"Justin Case <justin.case@example.com>",
gerritchangesource._gerrit_user_to_author({
"username": "justincase",
"name": "Justin Case",
"email": "justin.case@example.com"
}))

def test_missing_username(self):
self.assertEqual(u"Justin Case <justin.case@example.com>",
gerritchangesource._gerrit_user_to_author({
"name": "Justin Case",
"email": "justin.case@example.com"
}))

def test_missing_name(self):
self.assertEqual(u"unknown <justin.case@example.com>",
gerritchangesource._gerrit_user_to_author({
"email": "justin.case@example.com"
}))
self.assertEqual(u"gerrit <justin.case@example.com>",
gerritchangesource._gerrit_user_to_author({
"email": "justin.case@example.com"
}, u"gerrit"))
self.assertEqual(u"justincase <justin.case@example.com>",
gerritchangesource._gerrit_user_to_author({
"username": "justincase",
"email": "justin.case@example.com"
}, u"gerrit"))

def test_missing_email(self):
self.assertEqual(u"Justin Case",
gerritchangesource._gerrit_user_to_author({
"username": "justincase",
"name": "Justin Case"
}))
self.assertEqual(u"Justin Case",
gerritchangesource._gerrit_user_to_author({
"name": "Justin Case"
}))
self.assertEqual(u"justincase",
gerritchangesource._gerrit_user_to_author({
"username": "justincase"
}))
self.assertEqual(u"unknown",
gerritchangesource._gerrit_user_to_author({
}))
self.assertEqual(u"gerrit",
gerritchangesource._gerrit_user_to_author({
}, u"gerrit"))


class TestGerritChangeSource(changesource.ChangeSourceMixin,
unittest.TestCase):

Expand Down

0 comments on commit 2748c9b

Please sign in to comment.