comparison 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
comparison
equal deleted inserted replaced
2202:48d9a62c9b75 2203:d9972f76322e
72 from paste.httpheaders import REMOTE_USER, AUTH_TYPE 72 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
73 73
74 from rhodecode.lib.utils2 import safe_str 74 from rhodecode.lib.utils2 import safe_str
75 from rhodecode.lib.base import BaseVCSController 75 from rhodecode.lib.base import BaseVCSController
76 from rhodecode.lib.auth import get_container_username 76 from rhodecode.lib.auth import get_container_username
77 from rhodecode.lib.utils import is_valid_repo 77 from rhodecode.lib.utils import is_valid_repo, make_ui
78 from rhodecode.model.db import User 78 from rhodecode.model.db import User
79 79
80 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError 80 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError
81 81
82 log = logging.getLogger(__name__) 82 log = logging.getLogger(__name__)
101 if not is_git(environ): 101 if not is_git(environ):
102 return self.application(environ, start_response) 102 return self.application(environ, start_response)
103 103
104 ipaddr = self._get_ip_addr(environ) 104 ipaddr = self._get_ip_addr(environ)
105 username = None 105 username = None
106 self._git_first_op = False
106 # skip passing error to error controller 107 # skip passing error to error controller
107 environ['pylons.status_code_redirect'] = True 108 environ['pylons.status_code_redirect'] = True
108 109
109 #====================================================================== 110 #======================================================================
110 # EXTRACT REPOSITORY NAME FROM ENV 111 # EXTRACT REPOSITORY NAME FROM ENV
176 177
177 #check permissions for this repository 178 #check permissions for this repository
178 perm = self._check_permission(action, user, repo_name) 179 perm = self._check_permission(action, user, repo_name)
179 if perm is not True: 180 if perm is not True:
180 return HTTPForbidden()(environ, start_response) 181 return HTTPForbidden()(environ, start_response)
182 extras = {
183 'ip': ipaddr,
184 'username': username,
185 'action': action,
186 'repository': repo_name,
187 'scm': 'git',
188 }
181 189
182 #=================================================================== 190 #===================================================================
183 # GIT REQUEST HANDLING 191 # GIT REQUEST HANDLING
184 #=================================================================== 192 #===================================================================
185 repo_path = os.path.join(safe_str(self.basepath), safe_str(repo_name)) 193 repo_path = os.path.join(safe_str(self.basepath), safe_str(repo_name))
186 log.debug('Repository path is %s' % repo_path) 194 log.debug('Repository path is %s' % repo_path)
187 195
196 baseui = make_ui('db')
197 for k, v in extras.items():
198 baseui.setconfig('rhodecode_extras', k, v)
199
188 try: 200 try:
189 #invalidate cache on push 201 # invalidate cache on push
190 if action == 'push': 202 if action == 'push':
191 self._invalidate_cache(repo_name) 203 self._invalidate_cache(repo_name)
204 self._handle_githooks(action, baseui, environ)
205
192 log.info('%s action on GIT repo "%s"' % (action, repo_name)) 206 log.info('%s action on GIT repo "%s"' % (action, repo_name))
193 app = self.__make_app(repo_name, repo_path) 207 app = self.__make_app(repo_name, repo_path)
194 return app(environ, start_response) 208 return app(environ, start_response)
195 except Exception: 209 except Exception:
196 log.error(traceback.format_exc()) 210 log.error(traceback.format_exc())
247 else: 261 else:
248 # try to fallback to stored variable as we don't know if the last 262 # try to fallback to stored variable as we don't know if the last
249 # operation is pull/push 263 # operation is pull/push
250 op = getattr(self, '_git_stored_op', 'pull') 264 op = getattr(self, '_git_stored_op', 'pull')
251 return op 265 return op
266
267 def _handle_githooks(self, action, baseui, environ):
268
269 from rhodecode.lib.hooks import log_pull_action, log_push_action
270 service = environ['QUERY_STRING'].split('=')
271 if len(service) < 2:
272 return
273
274 class cont(object):
275 pass
276
277 repo = cont()
278 setattr(repo, 'ui', baseui)
279
280 push_hook = 'pretxnchangegroup.push_logger'
281 pull_hook = 'preoutgoing.pull_logger'
282 _hooks = dict(baseui.configitems('hooks')) or {}
283 if action == 'push' and _hooks.get(push_hook):
284 log_push_action(ui=baseui, repo=repo)
285 elif action == 'pull' and _hooks.get(pull_hook):
286 log_pull_action(ui=baseui, repo=repo)
287