Fishbone of patchset-created hook
authorAlexander Kanevskiy <alexander.kanevskiy@intel.com>
Mon, 12 Aug 2013 13:01:07 +0000 (16:01 +0300)
committerAlexander Kanevskiy <alexander.kanevskiy@intel.com>
Mon, 12 Aug 2013 13:01:07 +0000 (16:01 +0300)
gerrithooks/__init__.py [new file with mode: 0644]
gerrithooks/patchset_created.py [new file with mode: 0644]

diff --git a/gerrithooks/__init__.py b/gerrithooks/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/gerrithooks/patchset_created.py b/gerrithooks/patchset_created.py
new file mode 100644 (file)
index 0000000..8d03bcc
--- /dev/null
@@ -0,0 +1,82 @@
+#!/usr/bin/env
+# -*- coding: UTF-8 -*-
+# vim: sw=4 ts=4 expandtab ai
+#
+# Copyright (c) 2013 Intel, Inc.
+# License: GPLv2
+# Author: Alexander Kanevskiy <alexander.kanevskiy@intel.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License, version 2,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+
+import os
+import sys
+import argparse
+
+def get_logger(appname, debug=False):
+    """Set up syslog logger."""
+    logger = logging.getLogger(appname)
+    logger.setLevel(logging.DEBUG)
+    formatter = logging.Formatter("%(name)s: %(levelname)s %(message)s")
+    if debug:
+        handler = logging.StreamHandler()
+    else:
+        if sys.platform == 'darwin':
+            syslog_socket = "/var/run/syslog"
+        else:
+            syslog_socket = "/dev/log"
+        handler = handlers.SysLogHandler(syslog_socket)
+    handler.setFormatter(formatter)
+    logger.addHandler(handler)
+    return logger
+
+
+def parse_cmdline(argv):
+    """Parse hook command line parameters."""
+    parser = argparse.ArgumentParser(
+        description="Gerrit patchset-created hook")
+    args = (
+                       "--change",     # <change id> 
+                       "--is-draft",   # <boolean>
+                       "--change-url", # <change url>
+                       "--project",    # <project name>
+                       "--branch",     # <branch>
+                       "--topic",      # <topic> 
+                       "--uploader",   # <uploader>
+                       "--commit",     # <sha1> 
+                       "--patchset"    #<patchset id>
+       )
+    for arg in args:
+        parser.add_argument(arg)
+    parser.add_argument("-d", "--debug", action="store_true")
+    return parser.parse_args(argv)
+
+
+def main():
+
+    # get parameters from command line
+    params = parse_cmdline(sys.argv[1:])
+
+    # set up logger
+    logger = get_logger(os.path.basename(argv[0]), params.debug)
+
+    logger.warning("processing project %s : branch %s, commit %s, change: %s (draft: %s url: %s) topic: %s uploader: %s patchset: %s",
+                params.project, params.branch, params.commit, params.change, 
+                params.is_draft, params.change_url, params.topic, 
+                params.uploader, params.patchset)
+
+    if not params.debug:
+        daemonize()
+
+
+    logger.info("done")
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv))