mirror of
https://github.com/gryf/ferrit.git
synced 2026-02-14 14:15:45 +01:00
Added more event to be supported from REST server.
Also, now we closing channel in case of "gerrit stream-events" command, since it is a command like any other - there is no special case for keeping it open.
This commit is contained in:
123
ferrit/rest.py
123
ferrit/rest.py
@@ -14,44 +14,105 @@ LOG_PATH = './'
|
|||||||
LOG = logging.getLogger('bottle')
|
LOG = logging.getLogger('bottle')
|
||||||
LOG.setLevel(logging.DEBUG)
|
LOG.setLevel(logging.DEBUG)
|
||||||
handler = logging.FileHandler(os.path.join(LOG_PATH, 'ferrit-http.log'))
|
handler = logging.FileHandler(os.path.join(LOG_PATH, 'ferrit-http.log'))
|
||||||
handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
|
handler.setFormatter(logging.Formatter('%(message)s'))
|
||||||
LOG.addHandler(handler)
|
LOG.addHandler(handler)
|
||||||
|
|
||||||
|
JDOE = {"email": "j.doe@nonexistent.com",
|
||||||
|
"name": "John Doe",
|
||||||
|
"username": "jdoe"}
|
||||||
PATCHSET_CREATED = {"change": {"branch": "master",
|
PATCHSET_CREATED = {"change": {"branch": "master",
|
||||||
"commitMessage": "commit msg",
|
"commitMessage": "commit msg",
|
||||||
"id": "I1",
|
"id": "I1",
|
||||||
"number": "691277",
|
"number": "1",
|
||||||
"owner": {"email": "j.doe@nonexistent.com",
|
"owner": JDOE,
|
||||||
"name": "John Doe",
|
|
||||||
"username": "jdoe"},
|
|
||||||
"project": "foo",
|
"project": "foo",
|
||||||
"status": "NEW",
|
"status": "NEW",
|
||||||
"subject": "create new patch",
|
"subject": "create new patch",
|
||||||
"url": "http://localhost:8181/1"},
|
"url": "http://localhost:8181/1"},
|
||||||
"changeKey": {"id": "I1"},
|
"changeKey": {"id": "I1"},
|
||||||
"eventCreatedOn": int(time.time()),
|
"eventCreatedOn": int(time.time()),
|
||||||
"patchSet": {"author": {"email": "j.doe@nonexistent.com",
|
"patchSet": {"author": JDOE,
|
||||||
"name": "John Doe",
|
|
||||||
"username": "jdoe"},
|
|
||||||
"createdOn": int(time.time()) - 1000,
|
"createdOn": int(time.time()) - 1000,
|
||||||
"isDraft": False,
|
"isDraft": False,
|
||||||
"kind": "REWORK",
|
"kind": "REWORK",
|
||||||
"number": "1",
|
"number": "1",
|
||||||
"parents": ["559721d9"],
|
"parents": ["2"],
|
||||||
"ref": "refs/changes/77/691277/1",
|
"ref": "refs/changes/77/1/1",
|
||||||
"revision": "e3c8ac50",
|
"revision": "e3c8ac50",
|
||||||
"sizeDeletions": -15,
|
"sizeDeletions": -15,
|
||||||
"sizeInsertions": 29,
|
"sizeInsertions": 29,
|
||||||
"uploader": {"email": "j.doe@nonexistent.com",
|
"uploader": JDOE},
|
||||||
"name": "John Doe",
|
|
||||||
"username": "jdoe"}},
|
|
||||||
"project": "foo",
|
"project": "foo",
|
||||||
"refName": "refs/heads/master",
|
"refName": "refs/heads/master",
|
||||||
"type": "patchset-created",
|
"type": "patchset-created",
|
||||||
"uploader": {"email": "j.doe@nonexistent.com",
|
"uploader": JDOE}
|
||||||
"name": "John Doe",
|
RECHECK = {"approvals": [{"description": "Verified",
|
||||||
"username": "jdoe"}}
|
"type": "Verified",
|
||||||
|
"value": "0"},
|
||||||
|
{"description": "Code-Review",
|
||||||
|
"type": "Code-Review",
|
||||||
|
"value": "0"},
|
||||||
|
{"description": "Workflow",
|
||||||
|
"type": "Workflow",
|
||||||
|
"value": "0"}],
|
||||||
|
"author": JDOE,
|
||||||
|
"change": {"branch": "master",
|
||||||
|
"commitMessage": "commit msg",
|
||||||
|
"id": "I1",
|
||||||
|
"number": "690077",
|
||||||
|
"owner": JDOE,
|
||||||
|
"project": "foo",
|
||||||
|
"status": "NEW",
|
||||||
|
"subject": "create new patch",
|
||||||
|
"topic": "test",
|
||||||
|
"url": "http://localhost:8181/1"},
|
||||||
|
"changeKey": {"id": "I1"},
|
||||||
|
"comment": "Patch Set 1:\n\nrecheck",
|
||||||
|
"eventCreatedOn": int(time.time()),
|
||||||
|
"patchSet": {"author": JDOE,
|
||||||
|
"createdOn": int(time.time()) - 1000,
|
||||||
|
"isDraft": False,
|
||||||
|
"kind": "NO_CODE_CHANGE",
|
||||||
|
"number": "1",
|
||||||
|
"parents": ["2"],
|
||||||
|
"ref": "refs/changes/77/1/1",
|
||||||
|
"revision": "e3c8ac50",
|
||||||
|
"sizeDeletions": -15,
|
||||||
|
"sizeInsertions": 29,
|
||||||
|
"uploader": JDOE},
|
||||||
|
"project": "foo",
|
||||||
|
"refName": "refs/heads/master",
|
||||||
|
"type": "comment-added"}
|
||||||
|
MERGED = {"change": {"branch": "master",
|
||||||
|
"commitMessage": "commit msg",
|
||||||
|
"id": "I1",
|
||||||
|
"number": "1",
|
||||||
|
"owner": JDOE,
|
||||||
|
"project": "foo",
|
||||||
|
"status": "MERGED",
|
||||||
|
"subject": "create new patch",
|
||||||
|
"topic": "test",
|
||||||
|
"url": "http://localhost:8181/3"},
|
||||||
|
"changeKey": {"id": "I1"},
|
||||||
|
"eventCreatedOn": int(time.time()),
|
||||||
|
"newRev": "0ce5beac",
|
||||||
|
"patchSet": {
|
||||||
|
"author": JDOE,
|
||||||
|
"createdOn": int(time.time()) - 1000,
|
||||||
|
"isDraft": False,
|
||||||
|
"kind": "REWORK",
|
||||||
|
"number": "3",
|
||||||
|
"parents": ["1"],
|
||||||
|
"ref": "refs/changes/77/1/1",
|
||||||
|
"revision": "e3c8ac50",
|
||||||
|
"sizeDeletions": -8,
|
||||||
|
"sizeInsertions": 83,
|
||||||
|
"uploader": JDOE},
|
||||||
|
"project": "foo",
|
||||||
|
"refName": "refs/heads/master",
|
||||||
|
"submitter": {"name": "CI",
|
||||||
|
"username": "ci"},
|
||||||
|
"type": "change-merged"}
|
||||||
|
|
||||||
|
|
||||||
class App(bottle.Bottle):
|
class App(bottle.Bottle):
|
||||||
@@ -66,19 +127,24 @@ class App(bottle.Bottle):
|
|||||||
self.post('/make/event', callback=self._mk_event)
|
self.post('/make/event', callback=self._mk_event)
|
||||||
|
|
||||||
def _mk_event(self):
|
def _mk_event(self):
|
||||||
if bottle.request.forms.get('type') == 'patchset-created':
|
events = {PATCHSET_CREATED['type']: PATCHSET_CREATED,
|
||||||
data = dict(PATCHSET_CREATED)
|
RECHECK['type']: RECHECK,
|
||||||
|
MERGED['type']: MERGED}
|
||||||
|
|
||||||
if 'project' in bottle.request.forms:
|
if bottle.request.forms.get('type') not in events.keys():
|
||||||
data['change']['project'] = bottle.request.forms['project']
|
return
|
||||||
data['project'] = bottle.request.forms['project']
|
|
||||||
|
|
||||||
if 'branch' in bottle.request.forms:
|
data = dict(events[bottle.request.forms.get('type')])
|
||||||
data['change']['branch'] = bottle.request.forms['branch']
|
if 'project' in bottle.request.forms:
|
||||||
|
data['change']['project'] = bottle.request.forms['project']
|
||||||
|
data['project'] = bottle.request.forms['project']
|
||||||
|
|
||||||
with open(FIFO, 'w') as fobj:
|
if 'branch' in bottle.request.forms:
|
||||||
fobj.write(json.dumps(data))
|
data['change']['branch'] = bottle.request.forms['branch']
|
||||||
fobj.write('\n')
|
|
||||||
|
with open(FIFO, 'w') as fobj:
|
||||||
|
fobj.write(json.dumps(data))
|
||||||
|
fobj.write('\n')
|
||||||
|
|
||||||
def _documentation(self, whatever, params=None):
|
def _documentation(self, whatever, params=None):
|
||||||
return
|
return
|
||||||
@@ -122,7 +188,6 @@ class App(bottle.Bottle):
|
|||||||
return
|
return
|
||||||
|
|
||||||
LOG.info(json.dumps(bottle.request.json))
|
LOG.info(json.dumps(bottle.request.json))
|
||||||
LOG.info('Verified: %s', labels.get('Verified'))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -132,7 +197,7 @@ def main():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# development version, meant to be run as stand alone module, like
|
# development version, meant to be run as stand alone module, like
|
||||||
# python -m ferrit.http
|
# python -m ferrit.rest
|
||||||
handler = logging.StreamHandler(sys.stdout)
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
|
handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
|
||||||
LOG.addHandler(handler)
|
LOG.addHandler(handler)
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ class SSHHandler(socketserver.StreamRequestHandler):
|
|||||||
LOG.debug("Writing %s to channel", data)
|
LOG.debug("Writing %s to channel", data)
|
||||||
channel.send(data)
|
channel.send(data)
|
||||||
data = None
|
data = None
|
||||||
|
channel.close()
|
||||||
|
|
||||||
def _version(self, channel):
|
def _version(self, channel):
|
||||||
LOG.debug('sending version string')
|
LOG.debug('sending version string')
|
||||||
@@ -181,13 +182,6 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import argparse
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('-k', '--key', default=KEY,
|
|
||||||
help='Path to the private server key')
|
|
||||||
args = parser.parse_args()
|
|
||||||
KEY = args.key
|
|
||||||
os.mkfifo(FIFO)
|
|
||||||
handler = logging.StreamHandler(sys.stdout)
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] '
|
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] '
|
||||||
'%(funcName)s:%(lineno)s - '
|
'%(funcName)s:%(lineno)s - '
|
||||||
@@ -196,7 +190,17 @@ if __name__ == "__main__":
|
|||||||
LOG.setLevel(logging.DEBUG)
|
LOG.setLevel(logging.DEBUG)
|
||||||
LOG.addHandler(handler)
|
LOG.addHandler(handler)
|
||||||
LOG.debug('Start up development server')
|
LOG.debug('Start up development server')
|
||||||
|
import argparse
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('-k', '--key', default=KEY,
|
||||||
|
help='Path to the private server key')
|
||||||
|
args = parser.parse_args()
|
||||||
|
KEY = args.key
|
||||||
|
LOG.debug('Creating FIFO: %s', FIFO)
|
||||||
|
os.mkfifo(FIFO)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
main()
|
main()
|
||||||
finally:
|
finally:
|
||||||
|
LOG.debug('Removing FIFO: %s', FIFO)
|
||||||
os.unlink(FIFO)
|
os.unlink(FIFO)
|
||||||
|
|||||||
Reference in New Issue
Block a user