From a76adc7ef61d9b35a1350800dd8373cf42f28c0e Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Apr 28 2016 18:39:17 +0000 Subject: Move install/remove logic to BaseHook, works on fedmsg --- diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py index 400bdd2..8a3cc2f 100644 --- a/pagure/hooks/__init__.py +++ b/pagure/hooks/__init__.py @@ -15,6 +15,14 @@ import wtforms from pagure import APP, get_repo_path +def _get_hook_name(filename): + hook_file_words = [word for word in filename.replace('_hook.py', '').split('_')] + hook_file_name = '' + for word in hook_file_words: + hook_file_name += word + return hook_file_name + + class RequiredIf(wtforms.validators.Required): """ Wtforms validator setting a field as required if another field has a value. @@ -75,7 +83,7 @@ class BaseHook(object): os.chmod(postreceive, 0755) @classmethod - def install(cls, project, dbobj): # pragma: no cover + def install(cls, project, dbobj, filein): # pragma: no cover ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook @@ -84,14 +92,42 @@ class BaseHook(object): information. ''' - pass + repopath = get_repo_path(project) + if not os.path.exists(repopath): + flask.abort(404, 'No git repo found') + + hook_files = os.path.join( + os.path.dirname(os.path.realpath(__file__)), 'files') + + # Make sure the hooks folder exists + hookfolder = os.path.join(repopath, 'hooks') + if not os.path.exists(hookfolder): + os.makedirs(hookfolder) + + # Install the hook itself + + hook_file_name = _get_hook_name(filein) + hook_file = os.path.join(repopath, 'hooks', 'post-receive.' + + hook_file_name) + + if not os.path.exists(hook_file): + os.symlink( + os.path.join(hook_files, filein), + hook_file + ) @classmethod - def remove(cls, project): # pragma: no cover + def remove(cls, project, fileout): # pragma: no cover ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' - pass + repopath = get_repo_path(project) + + hook_file_name = _get_hook_name(fileout) + hook_path = os.path.join(repopath, 'hooks', 'post-receive.' + + hook_file_name) + if os.path.exists(hook_path): + os.unlink(hook_path) diff --git a/pagure/hooks/fedmsg.py b/pagure/hooks/fedmsg.py index 5589df2..fb77798 100644 --- a/pagure/hooks/fedmsg.py +++ b/pagure/hooks/fedmsg.py @@ -74,25 +74,7 @@ class Fedmsg(BaseHook): should be installed ''' - repopath = get_repo_path(project) - if not os.path.exists(repopath): - flask.abort(404, 'No git repo found') - - hook_files = os.path.join( - os.path.dirname(os.path.realpath(__file__)), 'files') - - # Make sure the hooks folder exists - hookfolder = os.path.join(repopath, 'hooks') - if not os.path.exists(hookfolder): - os.makedirs(hookfolder) - - # Install the hook itself - hook_file = os.path.join(repopath, 'hooks', 'post-receive.fedmsg') - if not os.path.exists(hook_file): - os.symlink( - os.path.join(hook_files, 'fedmsg_hook.py'), - hook_file - ) + BaseHook.install(project, dbobj, 'fedmsg_hook.py') @classmethod def remove(cls, project): @@ -102,8 +84,4 @@ class Fedmsg(BaseHook): should be installed ''' - repopath = get_repo_path(project) - - hook_path = os.path.join(repopath, 'hooks', 'post-receive.fedmsg') - if os.path.exists(hook_path): - os.unlink(hook_path) + BaseHook.remove(project, 'fedmsg_hook.py')