Chromium Code Reviews| Index: scripts/master/gerrit_poller.py |
| diff --git a/scripts/master/gerrit_poller.py b/scripts/master/gerrit_poller.py |
| index 57425fd740afd0da300c1fee9e55b9569cbcf26d..dea568c9819b74061140baf784b3acfdc28ad657 100644 |
| --- a/scripts/master/gerrit_poller.py |
| +++ b/scripts/master/gerrit_poller.py |
| @@ -15,6 +15,8 @@ from common.gerrit_agent import GerritAgent |
| class GerritPoller(base.PollingChangeSource): |
| """A poller which queries a gerrit server for new changes and patchsets.""" |
| + change_category = 'patchset-created' |
| + |
| def __init__(self, gerrit_host, gerrit_projects=None, pollInterval=None): |
| if isinstance(gerrit_projects, basestring): |
| gerrit_projects = [gerrit_projects] |
| @@ -34,10 +36,13 @@ class GerritPoller(base.PollingChangeSource): |
| self.initLastTimeStamp() |
| base.PollingChangeSource.startService(self) |
| + def getChangeQuery(self): # pylint: disable=R0201 |
| + return 'status:open' |
| + |
| @deferredLocked('initLock') |
| def initLastTimeStamp(self): |
| log.msg('GerritPoller: Getting latest timestamp from gerrit server.') |
| - path = '/changes/?q=status:open&n=1' |
| + path = '/changes/?q=%s&n=1' % self.getChangeQuery() |
|
Vadim Sh.
2014/04/29 01:22:45
So if try server was offline and brought back onli
nodir
2014/04/29 04:01:00
I've intentionally didn't improve this part much b
|
| d = self.agent.request('GET', path) |
| def _get_timestamp(j): |
| if len(j) == 0: |
| @@ -48,11 +53,14 @@ class GerritPoller(base.PollingChangeSource): |
| return d |
| def getChanges(self, sortkey=None): |
| - path = '/changes/?q=status:open&n=10' |
| + path = '/changes/?q=%s&n=10' % self.getChangeQuery() |
| if sortkey: |
| path += '&N=%s' % sortkey |
| return self.agent.request('GET', path) |
| + def _is_interesting_comment(self, comment): # pylint: disable=R0201 |
|
Vadim Sh.
2014/04/29 01:22:45
Let's follow Gerrit REST API and call that entity
nodir
2014/04/29 04:01:00
Done.
|
| + return comment['message'].startswith('Uploaded patch set ') |
| + |
| def checkForNewPatchset(self, change, since): |
| o_params = '&'.join('o=%s' % x for x in ( |
| 'MESSAGES', 'CURRENT_REVISION', 'CURRENT_COMMIT', 'ALL_FILES')) |
| @@ -64,12 +72,12 @@ class GerritPoller(base.PollingChangeSource): |
| for m in reversed(j['messages']): |
| if self._parse_timestamp(m['date']) <= since: |
| break |
| - if m['message'].startswith('Uploaded patch set '): |
| - return j |
| + if self._is_interesting_comment(m): |
| + return j, m |
| d.addCallback(_parse_messages) |
| return d |
| - def createBuildbotChange(self, change): |
| + def addBuildbotChange(self, change, comment): |
| revision = change['revisions'].values()[0] |
| commit = revision['commit'] |
| properties = {'event.change.number': change['_number']} |
| @@ -87,7 +95,7 @@ class GerritPoller(base.PollingChangeSource): |
| 'revision': change['current_revision'], |
| 'comments': commit['subject'], |
| 'files': commit['files'].keys() if 'files' in commit else ['UNKNOWN'], |
| - 'category': 'patchset-created', |
| + 'category': self.change_category, |
| 'when_timestamp': self._parse_timestamp(commit['committer']['date']), |
| 'revlink': '%s://%s/#/c/%s' % ( |
| self.agent.gerrit_protocol, self.agent.gerrit_host, |
| @@ -95,12 +103,16 @@ class GerritPoller(base.PollingChangeSource): |
| 'repository': '%s://%s/%s' % ( |
| self.agent.gerrit_protocol, self.agent.gerrit_host, |
| change['project']), |
| - 'properties': properties} |
| + 'properties': properties, |
| + } |
| d = self.master.addChange(**chdict) |
| d.addErrback(log.err, 'GerritPoller: Could not add buildbot change for ' |
| 'gerrit change %s.' % revision['_number']) |
| return d |
| + def addChange(self, (change, comment)): |
|
Vadim Sh.
2014/04/29 01:22:45
I'd prefer we not use structured args. That's unus
nodir
2014/04/29 04:01:00
Done
|
| + return self.addBuildbotChange(change, comment) |
| + |
| def processChanges(self, j, since): |
|
Vadim Sh.
2014/04/29 01:22:45
Somehow I can't shake a feeling this code has subt
nodir
2014/04/29 04:01:00
This will be also change with the new GerritAgent
|
| need_more = bool(j) |
| for change in j: |
| @@ -111,7 +123,7 @@ class GerritPoller(base.PollingChangeSource): |
| if self.gerrit_projects and change['project'] not in self.gerrit_projects: |
| continue |
| d = self.checkForNewPatchset(change, since) |
| - d.addCallback(lambda x: self.createBuildbotChange(x) if x else None) |
| + d.addCallback(lambda x: self.addChange(x) if x else None) |
| if need_more and j[-1].get('_more_changes'): |
| d = self.getChanges(sortkey=j[-1]['_sortkey']) |
| d.addCallback(self.processChanges, since=since) |