diff rhodecode/lib/middleware/simplegit.py @ 2203:d9972f76322e beta

added emulation of pull hook for git-backend, and dummy git-push hook
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 19 Apr 2012 01:27:37 +0200
parents b14d8bd96144
children 17ff5693566b
line wrap: on
line diff
--- a/rhodecode/lib/middleware/simplegit.py	Wed Apr 18 02:07:38 2012 +0200
+++ b/rhodecode/lib/middleware/simplegit.py	Thu Apr 19 01:27:37 2012 +0200
@@ -74,7 +74,7 @@
 from rhodecode.lib.utils2 import safe_str
 from rhodecode.lib.base import BaseVCSController
 from rhodecode.lib.auth import get_container_username
-from rhodecode.lib.utils import is_valid_repo
+from rhodecode.lib.utils import is_valid_repo, make_ui
 from rhodecode.model.db import User
 
 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError
@@ -103,6 +103,7 @@
 
         ipaddr = self._get_ip_addr(environ)
         username = None
+        self._git_first_op = False
         # skip passing error to error controller
         environ['pylons.status_code_redirect'] = True
 
@@ -178,6 +179,13 @@
                     perm = self._check_permission(action, user, repo_name)
                     if perm is not True:
                         return HTTPForbidden()(environ, start_response)
+        extras = {
+            'ip': ipaddr,
+            'username': username,
+            'action': action,
+            'repository': repo_name,
+            'scm': 'git',
+        }
 
         #===================================================================
         # GIT REQUEST HANDLING
@@ -185,10 +193,16 @@
         repo_path = os.path.join(safe_str(self.basepath), safe_str(repo_name))
         log.debug('Repository path is %s' % repo_path)
 
+        baseui = make_ui('db')
+        for k, v in extras.items():
+            baseui.setconfig('rhodecode_extras', k, v)
+
         try:
-            #invalidate cache on push
+            # invalidate cache on push
             if action == 'push':
                 self._invalidate_cache(repo_name)
+            self._handle_githooks(action, baseui, environ)
+
             log.info('%s action on GIT repo "%s"' % (action, repo_name))
             app = self.__make_app(repo_name, repo_path)
             return app(environ, start_response)
@@ -249,3 +263,25 @@
             # operation is pull/push
             op = getattr(self, '_git_stored_op', 'pull')
         return op
+
+    def _handle_githooks(self, action, baseui, environ):
+
+        from rhodecode.lib.hooks import log_pull_action, log_push_action
+        service = environ['QUERY_STRING'].split('=')
+        if len(service) < 2:
+            return
+
+        class cont(object):
+            pass
+
+        repo = cont()
+        setattr(repo, 'ui', baseui)
+
+        push_hook = 'pretxnchangegroup.push_logger'
+        pull_hook = 'preoutgoing.pull_logger'
+        _hooks = dict(baseui.configitems('hooks')) or {}
+        if action == 'push' and _hooks.get(push_hook):
+            log_push_action(ui=baseui, repo=repo)
+        elif action == 'pull' and _hooks.get(pull_hook):
+            log_pull_action(ui=baseui, repo=repo)
+