summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-07-12 12:13:53 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-07-24 14:52:38 +0900
commit8022ecb188b5675644e6cfaa913d6b39eaa4b9af (patch)
tree92e9ab365777387f7efab566313cec7d6fbffc08
parente419c9e14e23c255518d9565ed96ac34228bb3fb (diff)
downloadpygerrit-8022ecb188b5675644e6cfaa913d6b39eaa4b9af.tar.gz
`comment-added` event can have multiple approvals
The `comment-added` event can include multiple approvals, but the current implementation does not support this. Refactor it to allow multiple approvals to be handled. Also add the description field in the approval object. Refactor a bit to make the handling of supported approval categories easier to extend later if necessary.
-rwxr-xr-xgerrit_stream.py40
1 files changed, 24 insertions, 16 deletions
diff --git a/gerrit_stream.py b/gerrit_stream.py
index 0f07c97..67c7dfb 100755
--- a/gerrit_stream.py
+++ b/gerrit_stream.py
@@ -16,6 +16,12 @@ CHANGE_ABANDONED = "change-abandoned"
CHANGE_RESTORED = "change-restored"
REF_UPDATED = "ref-updated"
+# Approval categories
+APPROVAL_CATEGORY_CODE_REVIEW = "CRVW"
+APPROVAL_CATEGORY_VERIFY = "VRIF"
+SUPPORTED_APPROVAL_CATEGORIES = [APPROVAL_CATEGORY_CODE_REVIEW,
+ APPROVAL_CATEGORY_VERIFY]
+
class GerritStreamError(Exception):
''' GerritStreamError is raised when an error occurs while
@@ -73,25 +79,27 @@ class GerritPatchset(object):
raise GerritStreamError("GerritPatchset: %s" % e)
-class GerritApprovals(object):
- ''' Representation of the Gerrit approvals (verification and code review)
+class GerritApproval(object):
+ ''' Representation of a Gerrit approval (verification, code review, etc)
described in `json_data`.
Raise GerritStreamError if a required field is missing or has an
unexpected value.
'''
def __init__(self, json_data):
- try:
- for approval in json_data:
- if approval["type"] == "VRIF":
- self.verified = approval["value"]
- elif approval["type"] == "CRVW":
- self.code_review = approval["value"]
- else:
- raise GerritStreamError("GerritApprovals: Bad type %s" %
- (approval["type"]))
- except KeyError, e:
- raise GerritStreamError("GerritApprovals: %s" % e)
+ if "type" not in json_data:
+ raise GerritStreamError("GerritApproval: Missing type")
+ if "value" not in json_data:
+ raise GerritStreamError("GerritApproval: Missing value")
+ if json_data["type"] not in SUPPORTED_APPROVAL_CATEGORIES:
+ raise GerritStreamError("GerritApproval: Type %s not supported" %
+ json_data["type"])
+ self.category = json_data["type"]
+ self.value = json_data["value"]
+ if "description" in json_data:
+ self.description = json_data["description"]
+ else:
+ self.description = None
class GerritRefUpdate(object):
@@ -161,10 +169,10 @@ class GerritCommentAddedEvent(GerritEvent):
self.change = GerritChange(json_data["change"])
self.patchset = GerritPatchset(json_data["patchSet"])
self.author = GerritAccount(json_data["author"])
+ self.approvals = []
if "approvals" in json_data:
- self.approvals = GerritApprovals(json_data["approvals"])
- else:
- self.approvals = None
+ for approval in json_data["approvals"]:
+ self.approvals.append(GerritApproval(approval))
self.comment = json_data["comment"]
except ValueError, e:
raise GerritStreamError("GerritCommentAddedEvent: %s" % e)