From a76adc7ef61d9b35a1350800dd8373cf42f28c0e Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:17 +0000
Subject: [PATCH 1/12] 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')

From fd7659eff019ba9847a5b8436168993d2930f847 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:17 +0000
Subject: [PATCH 2/12] Reworked Basehook install/remove methods and updated all hooks to call these methods


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index 8a3cc2f..55059c3 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -15,14 +15,6 @@ 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.
@@ -83,7 +75,7 @@ class BaseHook(object):
                 os.chmod(postreceive, 0755)
 
     @classmethod
-    def install(cls, project, dbobj, filein):  # pragma: no cover
+    def install(cls, repopaths, dbobj, hook_name, filein):  # pragma: no cover
         ''' Method called to install the hook for a project.
 
         :arg project: a ``pagure.model.Project`` object to which the hook
@@ -92,42 +84,36 @@ class BaseHook(object):
             information.
 
         '''
-        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')
+        for repopath in repopaths:
+            if not os.path.exists(repopath):
+                flask.abort(404, 'No git repo found')
 
-        # Make sure the hooks folder exists
-        hookfolder = os.path.join(repopath, 'hooks')
-        if not os.path.exists(hookfolder):
-            os.makedirs(hookfolder)
+            hook_files = os.path.join(
+                os.path.dirname(os.path.realpath(__file__)), 'files')
 
-        # Install the hook itself
+            # Make sure the hooks folder exists
+            hookfolder = os.path.join(repopath, 'hooks')
+            if not os.path.exists(hookfolder):
+                os.makedirs(hookfolder)
 
-        hook_file_name = _get_hook_name(filein)
-        hook_file = os.path.join(repopath, 'hooks', 'post-receive.' +
-                                 hook_file_name)
+            # Install the hook itself
+            hook_file = os.path.join(repopath, 'hooks', 'post-receive.' + hook_name)
 
-        if not os.path.exists(hook_file):
-            os.symlink(
-                os.path.join(hook_files, filein),
-                hook_file
-            )
+            if not os.path.exists(hook_file):
+                os.symlink(
+                    os.path.join(hook_files, filein),
+                    hook_file
+                )
 
     @classmethod
-    def remove(cls, project, fileout):  # pragma: no cover
+    def remove(cls, repopaths, hook_name):  # 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
 
         '''
-        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)
+        for repopath in repopaths:
+            hook_path = os.path.join(repopath, 'hooks', 'post-receive.' + hook_name)
+            if os.path.exists(hook_path):
+                os.unlink(hook_path)
diff --git a/pagure/hooks/fedmsg.py b/pagure/hooks/fedmsg.py
index fb77798..c47e973 100644
--- a/pagure/hooks/fedmsg.py
+++ b/pagure/hooks/fedmsg.py
@@ -74,7 +74,8 @@ class Fedmsg(BaseHook):
             should be installed
 
         '''
-        BaseHook.install(project, dbobj, 'fedmsg_hook.py')
+        repopaths = [get_repo_path(project)]
+        BaseHook.install(repopaths, dbobj, 'fedmsg', 'fedmsg_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -84,4 +85,5 @@ class Fedmsg(BaseHook):
             should be installed
 
         '''
-        BaseHook.remove(project, 'fedmsg_hook.py')
+        repopaths = [get_repo_path(project)]
+        BaseHook.remove(repopaths, 'fedmsg')
diff --git a/pagure/hooks/irc.py b/pagure/hooks/irc.py
index 3fd6187..ee6c25c 100644
--- a/pagure/hooks/irc.py
+++ b/pagure/hooks/irc.py
@@ -114,22 +114,15 @@ class Hook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
+        repopaths = [get_repo_path(project)]
 
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        repo_obj = pygit2.Repository(repopath)
+        repo_obj = pygit2.Repository(repopaths[0])
 
         # Configure the hook
         # repo_obj.config.set_multivar()
 
         # Install the hook itself
-        #hook_file = os.path.join(hook_files, 'git_irc.py')
-        #if not os.path.exists(hook_file):
-            #os.symlink(
-                #hook_file,
-                #os.path.join(repopath, 'hooks', 'post-receive.irc')
-            #)
+        # BaseHook.install(repopaths, dbobj, 'irc', 'git_irc.py')
 
     @classmethod
     def remove(cls, project):
@@ -139,8 +132,6 @@ class Hook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
+        repopaths = [get_repo_path(project)]
 
-        #hook_path = os.path.join(repopath, 'hooks', 'post-receive.irc')
-        #if os.path.exists(hook_path):
-            #os.unlink(hook_path)
+        # BaseHook.remove(repopaths, 'irc')
diff --git a/pagure/hooks/mail.py b/pagure/hooks/mail.py
index 9a17cbd..86bbece 100644
--- a/pagure/hooks/mail.py
+++ b/pagure/hooks/mail.py
@@ -80,11 +80,8 @@ class Mail(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
-
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        repo_obj = pygit2.Repository(repopath)
+        repopaths = [get_repo_path(project)]
+        repo_obj = pygit2.Repository(repopaths[0])
 
         # Configure the hook
         repo_obj.config.set_multivar(
@@ -96,12 +93,7 @@ class Mail(BaseHook):
             'multimailhook.environment', '', 'gitolite')
 
         # Install the hook itself
-        hook_file = os.path.join(repopath, 'hooks', 'post-receive.mail')
-        if not os.path.exists(hook_file):
-            os.symlink(
-                os.path.join(hook_files, 'git_multimail.py'),
-                hook_file
-            )
+        BaseHook.install(repopaths, dbobj, 'mail', 'git_multimail.py')
 
     @classmethod
     def remove(cls, project):
@@ -111,8 +103,5 @@ class Mail(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
-
-        hook_path = os.path.join(repopath, 'hooks', 'post-receive.mail')
-        if os.path.exists(hook_path):
-            os.unlink(hook_path)
+        repopaths = [get_repo_path(project)]
+        BaseHook.remove(repopaths, 'mail')
diff --git a/pagure/hooks/pagure_force_commit.py b/pagure/hooks/pagure_force_commit.py
index 3f6725c..38fe39b 100644
--- a/pagure/hooks/pagure_force_commit.py
+++ b/pagure/hooks/pagure_force_commit.py
@@ -83,20 +83,12 @@ class PagureForceCommitHook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
-
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        hook_file = os.path.join(hook_files, 'pagure_force_commit_hook.py')
-
         # Init the git repo in case
-        pygit2.Repository(repopath)
+        repopaths = [get_repo_path(project)]
+        pygit2.Repository(repopaths[0])
 
-        # Install the hook itself
-        hook_path = os.path.join(
-            repopath, 'hooks', 'pre-receive.pagureforcecommit')
-        if not os.path.exists(hook_path):
-            os.symlink(hook_file, hook_path)
+        BaseHook.install(repopaths, dbobj, 'pagureforcecommit',
+                         'pagure_force_commit_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -106,8 +98,5 @@ class PagureForceCommitHook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
-        hook_path = os.path.join(
-            repopath, 'hooks', 'pre-receive.pagureforcecommit')
-        if os.path.exists(hook_path):
-            os.unlink(hook_path)
+        repopaths = [get_repo_path(project)]
+        BaseHook.remove(repopaths, 'pagureforcecommit')
diff --git a/pagure/hooks/pagure_hook.py b/pagure/hooks/pagure_hook.py
index 8ba51f9..8a7a1e4 100644
--- a/pagure/hooks/pagure_hook.py
+++ b/pagure/hooks/pagure_hook.py
@@ -103,20 +103,7 @@ class PagureHook(BaseHook):
                 os.path.join(folder, project.path)
             )
 
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        hook_file = os.path.join(hook_files, 'pagure_hook.py')
-
-        for repopath in repopaths:
-            # Init the git repo in case
-            pygit2.Repository(repopath)
-
-            # Install the hook itself
-            hook_path = os.path.join(
-                repopath, 'hooks', 'post-receive.pagure')
-            hook_file = os.path.join(hook_files, 'pagure_hook.py')
-            if not os.path.exists(hook_path):
-                os.symlink(hook_file, hook_path)
+        BaseHook.install(repopaths, dbobj, 'pagure', 'pagure_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -134,8 +121,4 @@ class PagureHook(BaseHook):
                 os.path.join(folder, project.path)
             )
 
-        for repopath in repopaths:
-            hook_path = os.path.join(
-                repopath, 'hooks', 'post-receive.pagure')
-            if os.path.exists(hook_path):
-                os.unlink(hook_path)
+        BaseHook.remove(repopaths, 'pagure')
diff --git a/pagure/hooks/pagure_request_hook.py b/pagure/hooks/pagure_request_hook.py
index b79a725..6f89019 100644
--- a/pagure/hooks/pagure_request_hook.py
+++ b/pagure/hooks/pagure_request_hook.py
@@ -100,24 +100,10 @@ class PagureRequestHook(BaseHook):
             should be installed
 
         '''
-        repopath = os.path.join(APP.config['REQUESTS_FOLDER'], project.path)
-        if not os.path.exists(repopath):  # pragma: no cover
-            # We cannot test this as un-existing repo should be catched
-            # at the set_up() stage
-            flask.abort(404, 'No git repo found')
-
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        pygit2.Repository(repopath)
+        repopaths = [os.path.join(APP.config['REQUESTS_FOLDER'], project.path)]
 
-        # Install the hook itself
-        hook_path = os.path.join(
-            repopath, 'hooks', 'post-receive.pagure-requests')
-        if not os.path.exists(hook_path):
-            os.symlink(
-                os.path.join(hook_files, 'pagure_hook_requests.py'),
-                hook_path
-            )
+        BaseHook.install(repopaths, dbobj, 'pagure-requests',
+                         'pagure_hook_requests.py')
 
     @classmethod
     def remove(cls, project):
@@ -127,11 +113,6 @@ class PagureRequestHook(BaseHook):
             should be installed
 
         '''
-        repopath = os.path.join(APP.config['REQUESTS_FOLDER'], project.path)
-        if not os.path.exists(repopath):
-            flask.abort(404, 'No git repo found')
+        repopaths = [os.path.join(APP.config['REQUESTS_FOLDER'], project.path)]
 
-        hook_path = os.path.join(
-            repopath, 'hooks', 'post-receive.pagure-requests')
-        if os.path.exists(hook_path):
-            os.unlink(hook_path)
+        BaseHook.remove(repopaths, 'pagure-requests')
diff --git a/pagure/hooks/pagure_ticket_hook.py b/pagure/hooks/pagure_ticket_hook.py
index 39203ed..363408e 100644
--- a/pagure/hooks/pagure_ticket_hook.py
+++ b/pagure/hooks/pagure_ticket_hook.py
@@ -99,24 +99,10 @@ class PagureTicketHook(BaseHook):
             should be installed
 
         '''
-        repopath = os.path.join(APP.config['TICKETS_FOLDER'], project.path)
-        if not os.path.exists(repopath):  # pragma: no cover
-            # We cannot test this as un-existing repo should be catched
-            # at the set_up() stage
-            flask.abort(404, 'No git repo found')
-
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        pygit2.Repository(repopath)
+        repopaths = [os.path.join(APP.config['TICKETS_FOLDER'], project.path)]
 
-        # Install the hook itself
-        hook_path = os.path.join(
-            repopath, 'hooks', 'post-receive.pagure-ticket')
-        if not os.path.exists(hook_path):
-            os.symlink(
-                os.path.join(hook_files, 'pagure_hook_tickets.py'),
-                hook_path
-            )
+        BaseHook.install(repopaths, dbobj, 'pagure-ticket',
+                         'pagure_hook_tickets.py')
 
     @classmethod
     def remove(cls, project):
@@ -126,11 +112,6 @@ class PagureTicketHook(BaseHook):
             should be installed
 
         '''
-        repopath = os.path.join(APP.config['TICKETS_FOLDER'], project.path)
-        if not os.path.exists(repopath):
-            flask.abort(404, 'No git repo found')
+        repopaths = [os.path.join(APP.config['TICKETS_FOLDER'], project.path)]
 
-        hook_path = os.path.join(
-            repopath, 'hooks', 'post-receive.pagure-ticket')
-        if os.path.exists(hook_path):
-            os.unlink(hook_path)
+        BaseHook.remove(repopaths, 'pagure-ticket')
diff --git a/pagure/hooks/pagure_unsigned_commits.py b/pagure/hooks/pagure_unsigned_commits.py
index 00fb76a..b57c8c3 100644
--- a/pagure/hooks/pagure_unsigned_commits.py
+++ b/pagure/hooks/pagure_unsigned_commits.py
@@ -77,20 +77,10 @@ class PagureUnsignedCommitHook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
+        repopaths = [get_repo_path(project)]
 
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        hook_file = os.path.join(hook_files, 'pagure_block_unsigned.py')
-
-        # Init the git repo in case
-        pygit2.Repository(repopath)
-
-        # Install the hook itself
-        hook_path = os.path.join(
-            repopath, 'hooks', 'pre-receive.pagureunsignedcommit')
-        if not os.path.exists(hook_path):
-            os.symlink(hook_file, hook_path)
+        BaseHook.install(repopaths, dbobj, 'pagureunsignedcommit',
+                         'pagure_block_unsigned.py')
 
     @classmethod
     def remove(cls, project):
@@ -100,8 +90,6 @@ class PagureUnsignedCommitHook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
-        hook_path = os.path.join(
-            repopath, 'hooks', 'pre-receive.pagureunsignedcommit')
-        if os.path.exists(hook_path):
-            os.unlink(hook_path)
+        repopaths = [get_repo_path(project)]
+
+        BaseHook.remove(repopaths, 'pagureunsignedcommit')
diff --git a/pagure/hooks/rtd.py b/pagure/hooks/rtd.py
index 788fcbc..08211ce 100644
--- a/pagure/hooks/rtd.py
+++ b/pagure/hooks/rtd.py
@@ -86,21 +86,9 @@ class RtdHook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
+        repopaths = [get_repo_path(project)]
 
-        hook_files = os.path.join(
-            os.path.dirname(os.path.realpath(__file__)), 'files')
-        hook_file = os.path.join(hook_files, 'rtd_hook.py')
-
-        # Init the git repo in case
-        pygit2.Repository(repopath)
-
-        # Install the hook itself
-        hook_path = os.path.join(
-            repopath, 'hooks', 'post-receive.rtd')
-        hook_file = os.path.join(hook_files, 'rtd_hook.py')
-        if not os.path.exists(hook_path):
-            os.symlink(hook_file, hook_path)
+        BaseHook.install(repopaths, dbobj, 'rtd', 'rtd_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -110,8 +98,6 @@ class RtdHook(BaseHook):
             should be installed
 
         '''
-        repopath = get_repo_path(project)
-        hook_path = os.path.join(
-            repopath, 'hooks', 'post-receive.rtd')
-        if os.path.exists(hook_path):
-            os.unlink(hook_path)
+        repopaths = [get_repo_path(project)]
+
+        BaseHook.remove(repopaths, 'rtd')

From d86c69c137c76e29c2ff846cc33ec7b972935d8d Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:17 +0000
Subject: [PATCH 3/12] Fixed post-receive pre-receive hook name


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index 55059c3..ed2e2a2 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -97,7 +97,12 @@ class BaseHook(object):
                 os.makedirs(hookfolder)
 
             # Install the hook itself
-            hook_file = os.path.join(repopath, 'hooks', 'post-receive.' + hook_name)
+            if hook_name in ['pagureforcecommit', 'pagureunsignedcommit']:
+                hook_file = os.path.join(repopath, 'hooks', 'pre-receive.'
+                                         + hook_name)
+            else:
+                hook_file = os.path.join(repopath, 'hooks', 'post-receive.'
+                                         + hook_name)
 
             if not os.path.exists(hook_file):
                 os.symlink(
@@ -114,6 +119,11 @@ class BaseHook(object):
 
         '''
         for repopath in repopaths:
-            hook_path = os.path.join(repopath, 'hooks', 'post-receive.' + hook_name)
+            if hook_name in ['pagureforcecommit', 'pagureunsignedcommit']:
+                hook_path = os.path.join(repopath, 'hooks', 'pre-receive.'
+                                         + hook_name)
+            else:
+                hook_path = os.path.join(repopath, 'hooks', 'post-receive.'
+                                         + hook_name)
             if os.path.exists(hook_path):
                 os.unlink(hook_path)

From 4123d546b4b3e7c8335b54847874e71ec4875caf Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:18 +0000
Subject: [PATCH 4/12] Added 404 error on hook remove method


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index ed2e2a2..d6e461f 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -11,6 +11,7 @@
 import os
 import shutil
 import wtforms
+import flask
 
 from pagure import APP, get_repo_path
 
@@ -119,6 +120,9 @@ class BaseHook(object):
 
         '''
         for repopath in repopaths:
+            if not os.path.exists(repopath):
+                flask.abort(404, 'No git repo found')
+
             if hook_name in ['pagureforcecommit', 'pagureunsignedcommit']:
                 hook_path = os.path.join(repopath, 'hooks', 'pre-receive.'
                                          + hook_name)

From 66e9bc5f7ed7771e264e10d4991d70685aec165a Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:18 +0000
Subject: [PATCH 5/12] Use symlink instead of copy for hook setup.

Changed post/pre-receive perm to add x rights

---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index d6e461f..f5afbbc 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -70,10 +70,8 @@ class BaseHook(object):
             # Install the main post-receive file
             postreceive = os.path.join(hookfolder, cls.hook_type)
             if not os.path.exists(postreceive):
-                shutil.copyfile(
-                    os.path.join(hook_files, cls.hook_type),
-                    postreceive)
-                os.chmod(postreceive, 0755)
+                os.symlink(os.path.join(hook_files, cls.hook_type),
+                           postreceive)
 
     @classmethod
     def install(cls, repopaths, dbobj, hook_name, filein):  # pragma: no cover
diff --git a/pagure/hooks/files/post-receive b/pagure/hooks/files/post-receive
old mode 100644
new mode 100755
diff --git a/pagure/hooks/files/pre-receive b/pagure/hooks/files/pre-receive
old mode 100644
new mode 100755

From 799be6bf4f20b98510a60f015fbdadfedf3552cf Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:18 +0000
Subject: [PATCH 6/12] Pass full hookname to BaseHook to simply logic.

Added logger to have debug info in case of not found repo

---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index f5afbbc..c06eb09 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -12,9 +12,13 @@ import os
 import shutil
 import wtforms
 import flask
+import logging
+
 
 from pagure import APP, get_repo_path
 
+log = logging.getLogger(__name__)
+
 
 class RequiredIf(wtforms.validators.Required):
     """ Wtforms validator setting a field as required if another field
@@ -86,6 +90,7 @@ class BaseHook(object):
         for repopath in repopaths:
             if not os.path.exists(repopath):
                 flask.abort(404, 'No git repo found')
+                log.debug('Hook install repo %s not found' % repopath)
 
             hook_files = os.path.join(
                 os.path.dirname(os.path.realpath(__file__)), 'files')
@@ -96,12 +101,7 @@ class BaseHook(object):
                 os.makedirs(hookfolder)
 
             # Install the hook itself
-            if hook_name in ['pagureforcecommit', 'pagureunsignedcommit']:
-                hook_file = os.path.join(repopath, 'hooks', 'pre-receive.'
-                                         + hook_name)
-            else:
-                hook_file = os.path.join(repopath, 'hooks', 'post-receive.'
-                                         + hook_name)
+            hook_file = os.path.join(repopath, 'hooks', hook_name)
 
             if not os.path.exists(hook_file):
                 os.symlink(
@@ -120,12 +120,8 @@ class BaseHook(object):
         for repopath in repopaths:
             if not os.path.exists(repopath):
                 flask.abort(404, 'No git repo found')
+                log.debug('Hook remove repo %s not found' % repopath)
 
-            if hook_name in ['pagureforcecommit', 'pagureunsignedcommit']:
-                hook_path = os.path.join(repopath, 'hooks', 'pre-receive.'
-                                         + hook_name)
-            else:
-                hook_path = os.path.join(repopath, 'hooks', 'post-receive.'
-                                         + hook_name)
+            hook_path = os.path.join(repopath, 'hooks', hook_name)
             if os.path.exists(hook_path):
                 os.unlink(hook_path)
diff --git a/pagure/hooks/fedmsg.py b/pagure/hooks/fedmsg.py
index c47e973..a6522b8 100644
--- a/pagure/hooks/fedmsg.py
+++ b/pagure/hooks/fedmsg.py
@@ -75,7 +75,7 @@ class Fedmsg(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.install(repopaths, dbobj, 'fedmsg', 'fedmsg_hook.py')
+        BaseHook.install(repopaths, dbobj, 'post-receive.fedmsg', 'fedmsg_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -86,4 +86,4 @@ class Fedmsg(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.remove(repopaths, 'fedmsg')
+        BaseHook.remove(repopaths, 'post-receive.fedmsg')
diff --git a/pagure/hooks/irc.py b/pagure/hooks/irc.py
index ee6c25c..af36311 100644
--- a/pagure/hooks/irc.py
+++ b/pagure/hooks/irc.py
@@ -122,7 +122,7 @@ class Hook(BaseHook):
         # repo_obj.config.set_multivar()
 
         # Install the hook itself
-        # BaseHook.install(repopaths, dbobj, 'irc', 'git_irc.py')
+        # BaseHook.install(repopaths, dbobj, 'post-receive.irc', 'git_irc.py')
 
     @classmethod
     def remove(cls, project):
@@ -134,4 +134,4 @@ class Hook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        # BaseHook.remove(repopaths, 'irc')
+        # BaseHook.remove(repopaths, 'post-receive.irc')
diff --git a/pagure/hooks/mail.py b/pagure/hooks/mail.py
index 86bbece..6e688a5 100644
--- a/pagure/hooks/mail.py
+++ b/pagure/hooks/mail.py
@@ -93,7 +93,7 @@ class Mail(BaseHook):
             'multimailhook.environment', '', 'gitolite')
 
         # Install the hook itself
-        BaseHook.install(repopaths, dbobj, 'mail', 'git_multimail.py')
+        BaseHook.install(repopaths, dbobj, 'post-receive.mail', 'git_multimail.py')
 
     @classmethod
     def remove(cls, project):
@@ -104,4 +104,4 @@ class Mail(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.remove(repopaths, 'mail')
+        BaseHook.remove(repopaths, 'post-receive.mail')
diff --git a/pagure/hooks/pagure_force_commit.py b/pagure/hooks/pagure_force_commit.py
index 38fe39b..3119f9f 100644
--- a/pagure/hooks/pagure_force_commit.py
+++ b/pagure/hooks/pagure_force_commit.py
@@ -87,7 +87,7 @@ class PagureForceCommitHook(BaseHook):
         repopaths = [get_repo_path(project)]
         pygit2.Repository(repopaths[0])
 
-        BaseHook.install(repopaths, dbobj, 'pagureforcecommit',
+        BaseHook.install(repopaths, dbobj, 'pre-receive.pagureforcecommit',
                          'pagure_force_commit_hook.py')
 
     @classmethod
@@ -99,4 +99,4 @@ class PagureForceCommitHook(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.remove(repopaths, 'pagureforcecommit')
+        BaseHook.remove(repopaths, 'pre-receive.pagureforcecommit')
diff --git a/pagure/hooks/pagure_hook.py b/pagure/hooks/pagure_hook.py
index 8a7a1e4..bfadcc6 100644
--- a/pagure/hooks/pagure_hook.py
+++ b/pagure/hooks/pagure_hook.py
@@ -103,7 +103,8 @@ class PagureHook(BaseHook):
                 os.path.join(folder, project.path)
             )
 
-        BaseHook.install(repopaths, dbobj, 'pagure', 'pagure_hook.py')
+        BaseHook.install(repopaths, dbobj, 'post-receive.pagure',
+                         'pagure_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -121,4 +122,4 @@ class PagureHook(BaseHook):
                 os.path.join(folder, project.path)
             )
 
-        BaseHook.remove(repopaths, 'pagure')
+        BaseHook.remove(repopaths, 'post-receive.pagure')
diff --git a/pagure/hooks/pagure_request_hook.py b/pagure/hooks/pagure_request_hook.py
index 6f89019..20f0710 100644
--- a/pagure/hooks/pagure_request_hook.py
+++ b/pagure/hooks/pagure_request_hook.py
@@ -102,7 +102,7 @@ class PagureRequestHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['REQUESTS_FOLDER'], project.path)]
 
-        BaseHook.install(repopaths, dbobj, 'pagure-requests',
+        BaseHook.install(repopaths, dbobj, 'post-receive.pagure-requests',
                          'pagure_hook_requests.py')
 
     @classmethod
@@ -115,4 +115,4 @@ class PagureRequestHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['REQUESTS_FOLDER'], project.path)]
 
-        BaseHook.remove(repopaths, 'pagure-requests')
+        BaseHook.remove(repopaths, 'post-receive.pagure-requests')
diff --git a/pagure/hooks/pagure_ticket_hook.py b/pagure/hooks/pagure_ticket_hook.py
index 363408e..71fe780 100644
--- a/pagure/hooks/pagure_ticket_hook.py
+++ b/pagure/hooks/pagure_ticket_hook.py
@@ -101,7 +101,7 @@ class PagureTicketHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['TICKETS_FOLDER'], project.path)]
 
-        BaseHook.install(repopaths, dbobj, 'pagure-ticket',
+        BaseHook.install(repopaths, dbobj, 'post-receive.pagure-ticket',
                          'pagure_hook_tickets.py')
 
     @classmethod
@@ -114,4 +114,4 @@ class PagureTicketHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['TICKETS_FOLDER'], project.path)]
 
-        BaseHook.remove(repopaths, 'pagure-ticket')
+        BaseHook.remove(repopaths, 'post-receive.pagure-ticket')
diff --git a/pagure/hooks/pagure_unsigned_commits.py b/pagure/hooks/pagure_unsigned_commits.py
index b57c8c3..4097edc 100644
--- a/pagure/hooks/pagure_unsigned_commits.py
+++ b/pagure/hooks/pagure_unsigned_commits.py
@@ -79,7 +79,7 @@ class PagureUnsignedCommitHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.install(repopaths, dbobj, 'pagureunsignedcommit',
+        BaseHook.install(repopaths, dbobj, 'pre-receive.pagureunsignedcommit',
                          'pagure_block_unsigned.py')
 
     @classmethod
@@ -92,4 +92,4 @@ class PagureUnsignedCommitHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.remove(repopaths, 'pagureunsignedcommit')
+        BaseHook.remove(repopaths, 'pre-receive.pagureunsignedcommit')
diff --git a/pagure/hooks/rtd.py b/pagure/hooks/rtd.py
index 08211ce..27c2dfc 100644
--- a/pagure/hooks/rtd.py
+++ b/pagure/hooks/rtd.py
@@ -88,7 +88,7 @@ class RtdHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.install(repopaths, dbobj, 'rtd', 'rtd_hook.py')
+        BaseHook.install(repopaths, dbobj, 'post-receive.rtd', 'rtd_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -100,4 +100,4 @@ class RtdHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.remove(repopaths, 'rtd')
+        BaseHook.remove(repopaths, 'post-receive.rtd')

From da506d65ac0bbd18039581710b87c24e0ad61b3e Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:18 +0000
Subject: [PATCH 7/12] Use APP.logger to log repo not found


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index c06eb09..9f6b8d5 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -17,8 +17,6 @@ import logging
 
 from pagure import APP, get_repo_path
 
-log = logging.getLogger(__name__)
-
 
 class RequiredIf(wtforms.validators.Required):
     """ Wtforms validator setting a field as required if another field
@@ -89,8 +87,8 @@ class BaseHook(object):
         '''
         for repopath in repopaths:
             if not os.path.exists(repopath):
+                APP.logger.debug('Hook install repo %s not found', repopath)
                 flask.abort(404, 'No git repo found')
-                log.debug('Hook install repo %s not found' % repopath)
 
             hook_files = os.path.join(
                 os.path.dirname(os.path.realpath(__file__)), 'files')
@@ -119,8 +117,8 @@ class BaseHook(object):
         '''
         for repopath in repopaths:
             if not os.path.exists(repopath):
+                APP.logger.debug('Hook remove repo %s not found', repopath)
                 flask.abort(404, 'No git repo found')
-                log.debug('Hook remove repo %s not found' % repopath)
 
             hook_path = os.path.join(repopath, 'hooks', hook_name)
             if os.path.exists(hook_path):

From a66db316603eb91431c816f2d74a2a0a40580236 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:18 +0000
Subject: [PATCH 8/12] Remove unused import (logging)


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index 9f6b8d5..7146448 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -12,8 +12,6 @@ import os
 import shutil
 import wtforms
 import flask
-import logging
-
 
 from pagure import APP, get_repo_path
 

From 85047cef89ba4f08d1354cfb806866a2ad8d577d Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:18 +0000
Subject: [PATCH 9/12] Renamed BaseHook install/remove method

so we can use the inheritance to build the hook_name

---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index 7146448..b3aeb54 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -74,7 +74,7 @@ class BaseHook(object):
                            postreceive)
 
     @classmethod
-    def install(cls, repopaths, dbobj, hook_name, filein):  # pragma: no cover
+    def base_install(cls, repopaths, dbobj, hook_name, filein):  # pragma: no cover
         ''' Method called to install the hook for a project.
 
         :arg project: a ``pagure.model.Project`` object to which the hook
@@ -97,7 +97,8 @@ class BaseHook(object):
                 os.makedirs(hookfolder)
 
             # Install the hook itself
-            hook_file = os.path.join(repopath, 'hooks', hook_name)
+            hook_file = os.path.join(repopath, 'hooks', cls.hook_type + '.'
+                                     + hook_name)
 
             if not os.path.exists(hook_file):
                 os.symlink(
@@ -106,7 +107,7 @@ class BaseHook(object):
                 )
 
     @classmethod
-    def remove(cls, repopaths, hook_name):  # pragma: no cover
+    def base_remove(cls, repopaths, hook_name):  # pragma: no cover
         ''' Method called to remove the hook of a project.
 
         :arg project: a ``pagure.model.Project`` object to which the hook
@@ -118,6 +119,7 @@ class BaseHook(object):
                 APP.logger.debug('Hook remove repo %s not found', repopath)
                 flask.abort(404, 'No git repo found')
 
-            hook_path = os.path.join(repopath, 'hooks', hook_name)
+            hook_path = os.path.join(repopath, 'hooks', cls.hook_type + '.'
+                                     + hook_name)
             if os.path.exists(hook_path):
                 os.unlink(hook_path)
diff --git a/pagure/hooks/fedmsg.py b/pagure/hooks/fedmsg.py
index a6522b8..aa21493 100644
--- a/pagure/hooks/fedmsg.py
+++ b/pagure/hooks/fedmsg.py
@@ -75,7 +75,7 @@ class Fedmsg(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.install(repopaths, dbobj, 'post-receive.fedmsg', 'fedmsg_hook.py')
+        cls.base_install(repopaths, dbobj, 'fedmsg', 'fedmsg_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -86,4 +86,4 @@ class Fedmsg(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.remove(repopaths, 'post-receive.fedmsg')
+        cls.base_remove(repopaths, 'fedmsg')
diff --git a/pagure/hooks/irc.py b/pagure/hooks/irc.py
index af36311..48f0a47 100644
--- a/pagure/hooks/irc.py
+++ b/pagure/hooks/irc.py
@@ -122,7 +122,7 @@ class Hook(BaseHook):
         # repo_obj.config.set_multivar()
 
         # Install the hook itself
-        # BaseHook.install(repopaths, dbobj, 'post-receive.irc', 'git_irc.py')
+        # cls.base_install(repopaths, dbobj, 'irc', 'git_irc.py')
 
     @classmethod
     def remove(cls, project):
@@ -134,4 +134,4 @@ class Hook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        # BaseHook.remove(repopaths, 'post-receive.irc')
+        # cls.base_remove(repopaths, 'irc')
diff --git a/pagure/hooks/mail.py b/pagure/hooks/mail.py
index 6e688a5..a4cc2cb 100644
--- a/pagure/hooks/mail.py
+++ b/pagure/hooks/mail.py
@@ -93,7 +93,7 @@ class Mail(BaseHook):
             'multimailhook.environment', '', 'gitolite')
 
         # Install the hook itself
-        BaseHook.install(repopaths, dbobj, 'post-receive.mail', 'git_multimail.py')
+        cls.base_install(repopaths, dbobj, 'mail', 'git_multimail.py')
 
     @classmethod
     def remove(cls, project):
@@ -104,4 +104,4 @@ class Mail(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.remove(repopaths, 'post-receive.mail')
+        cls.base_remove(repopaths, 'mail')
diff --git a/pagure/hooks/pagure_force_commit.py b/pagure/hooks/pagure_force_commit.py
index 3119f9f..ff58b19 100644
--- a/pagure/hooks/pagure_force_commit.py
+++ b/pagure/hooks/pagure_force_commit.py
@@ -87,7 +87,7 @@ class PagureForceCommitHook(BaseHook):
         repopaths = [get_repo_path(project)]
         pygit2.Repository(repopaths[0])
 
-        BaseHook.install(repopaths, dbobj, 'pre-receive.pagureforcecommit',
+        cls.base_install(repopaths, dbobj, 'pagureforcecommit',
                          'pagure_force_commit_hook.py')
 
     @classmethod
@@ -99,4 +99,4 @@ class PagureForceCommitHook(BaseHook):
 
         '''
         repopaths = [get_repo_path(project)]
-        BaseHook.remove(repopaths, 'pre-receive.pagureforcecommit')
+        cls.base_remove(repopaths, 'pagureforcecommit')
diff --git a/pagure/hooks/pagure_hook.py b/pagure/hooks/pagure_hook.py
index bfadcc6..f3ee149 100644
--- a/pagure/hooks/pagure_hook.py
+++ b/pagure/hooks/pagure_hook.py
@@ -103,8 +103,7 @@ class PagureHook(BaseHook):
                 os.path.join(folder, project.path)
             )
 
-        BaseHook.install(repopaths, dbobj, 'post-receive.pagure',
-                         'pagure_hook.py')
+        cls.base_install(repopaths, dbobj, 'pagure', 'pagure_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -122,4 +121,4 @@ class PagureHook(BaseHook):
                 os.path.join(folder, project.path)
             )
 
-        BaseHook.remove(repopaths, 'post-receive.pagure')
+        cls.base_remove(repopaths, 'pagure')
diff --git a/pagure/hooks/pagure_request_hook.py b/pagure/hooks/pagure_request_hook.py
index 20f0710..af08004 100644
--- a/pagure/hooks/pagure_request_hook.py
+++ b/pagure/hooks/pagure_request_hook.py
@@ -102,7 +102,7 @@ class PagureRequestHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['REQUESTS_FOLDER'], project.path)]
 
-        BaseHook.install(repopaths, dbobj, 'post-receive.pagure-requests',
+        cls.base_install(repopaths, dbobj, 'pagure-requests',
                          'pagure_hook_requests.py')
 
     @classmethod
@@ -115,4 +115,4 @@ class PagureRequestHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['REQUESTS_FOLDER'], project.path)]
 
-        BaseHook.remove(repopaths, 'post-receive.pagure-requests')
+        cls.base_remove(repopaths, 'pagure-requests')
diff --git a/pagure/hooks/pagure_ticket_hook.py b/pagure/hooks/pagure_ticket_hook.py
index 71fe780..97e3822 100644
--- a/pagure/hooks/pagure_ticket_hook.py
+++ b/pagure/hooks/pagure_ticket_hook.py
@@ -101,7 +101,7 @@ class PagureTicketHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['TICKETS_FOLDER'], project.path)]
 
-        BaseHook.install(repopaths, dbobj, 'post-receive.pagure-ticket',
+        cls.base_install(repopaths, dbobj, 'pagure-ticket',
                          'pagure_hook_tickets.py')
 
     @classmethod
@@ -114,4 +114,4 @@ class PagureTicketHook(BaseHook):
         '''
         repopaths = [os.path.join(APP.config['TICKETS_FOLDER'], project.path)]
 
-        BaseHook.remove(repopaths, 'post-receive.pagure-ticket')
+        cls.base_remove(repopaths, 'pagure-ticket')
diff --git a/pagure/hooks/pagure_unsigned_commits.py b/pagure/hooks/pagure_unsigned_commits.py
index 4097edc..9885b4f 100644
--- a/pagure/hooks/pagure_unsigned_commits.py
+++ b/pagure/hooks/pagure_unsigned_commits.py
@@ -79,7 +79,7 @@ class PagureUnsignedCommitHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.install(repopaths, dbobj, 'pre-receive.pagureunsignedcommit',
+        cls.base_install(repopaths, dbobj, 'pagureunsignedcommit',
                          'pagure_block_unsigned.py')
 
     @classmethod
@@ -92,4 +92,4 @@ class PagureUnsignedCommitHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.remove(repopaths, 'pre-receive.pagureunsignedcommit')
+        cls.base_remove(repopaths, 'pagureunsignedcommit')
diff --git a/pagure/hooks/rtd.py b/pagure/hooks/rtd.py
index 27c2dfc..b1cb0f3 100644
--- a/pagure/hooks/rtd.py
+++ b/pagure/hooks/rtd.py
@@ -88,7 +88,7 @@ class RtdHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.install(repopaths, dbobj, 'post-receive.rtd', 'rtd_hook.py')
+        cls.base_install(repopaths, dbobj, 'rtd', 'rtd_hook.py')
 
     @classmethod
     def remove(cls, project):
@@ -100,4 +100,4 @@ class RtdHook(BaseHook):
         '''
         repopaths = [get_repo_path(project)]
 
-        BaseHook.remove(repopaths, 'post-receive.rtd')
+        cls.base_remove(repopaths, 'rtd')

From 45cdc3433e37009ec16274e4c5a9b636b3e3a127 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:19 +0000
Subject: [PATCH 10/12] Added exception for repo not found


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index b3aeb54..fc446f5 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -13,6 +13,7 @@ import shutil
 import wtforms
 import flask
 
+from pagure.exceptions import FileNotFoundException
 from pagure import APP, get_repo_path
 
 
@@ -85,8 +86,7 @@ class BaseHook(object):
         '''
         for repopath in repopaths:
             if not os.path.exists(repopath):
-                APP.logger.debug('Hook install repo %s not found', repopath)
-                flask.abort(404, 'No git repo found')
+                raise FileNotFoundException('Repo %s not found' % repopath)
 
             hook_files = os.path.join(
                 os.path.dirname(os.path.realpath(__file__)), 'files')
@@ -116,8 +116,7 @@ class BaseHook(object):
         '''
         for repopath in repopaths:
             if not os.path.exists(repopath):
-                APP.logger.debug('Hook remove repo %s not found', repopath)
-                flask.abort(404, 'No git repo found')
+                raise FileNotFoundException('Repo %s not found' % repopath)
 
             hook_path = os.path.join(repopath, 'hooks', cls.hook_type + '.'
                                      + hook_name)
diff --git a/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py b/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
index a821bae..26931f9 100644
--- a/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
+++ b/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
@@ -17,8 +17,10 @@ import shutil
 import sys
 import os
 
+
 import pygit2
 from mock import patch
+from pagure.exceptions import FileNotFoundException
 
 sys.path.insert(0, os.path.join(os.path.dirname(
     os.path.abspath(__file__)), '..'))
@@ -85,8 +87,7 @@ class PagureFlaskPluginPagureRequestHooktests(tests.Modeltests):
 
             data['csrf_token'] = csrf_token
             # No git found
-            output = self.app.post('/test/settings/Pagure requests', data=data)
-            self.assertEqual(output.status_code, 404)
+            self.assertRaises(pagure.exceptions.FileNotFoundException)
 
             # Create both the requests repo
             tests.create_projects_git(os.path.join(tests.HERE, 'requests'))
diff --git a/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py b/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
index fcd8baa..125f076 100644
--- a/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
+++ b/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
@@ -19,6 +19,7 @@ import os
 
 import pygit2
 from mock import patch
+from pagure.exceptions import FileNotFoundException
 
 sys.path.insert(0, os.path.join(os.path.dirname(
     os.path.abspath(__file__)), '..'))
@@ -85,8 +86,7 @@ class PagureFlaskPluginPagureTicketHooktests(tests.Modeltests):
 
             data['csrf_token'] = csrf_token
             # No git found
-            output = self.app.post('/test/settings/Pagure tickets', data=data)
-            self.assertEqual(output.status_code, 404)
+            self.assertRaises(pagure.exceptions.FileNotFoundException)
 
             # Create both the tickets repo
             tests.create_projects_git(os.path.join(tests.HERE, 'tickets'))

From 380ecece4e2781fb960d90d7acc16a8617528113 Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 28 2016 18:39:19 +0000
Subject: [PATCH 11/12] Fixed exception handling and test


---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index fc446f5..b44cbed 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -11,7 +11,6 @@
 import os
 import shutil
 import wtforms
-import flask
 
 from pagure.exceptions import FileNotFoundException
 from pagure import APP, get_repo_path
diff --git a/pagure/ui/plugins.py b/pagure/ui/plugins.py
index 6102acd..88c9c8a 100644
--- a/pagure/ui/plugins.py
+++ b/pagure/ui/plugins.py
@@ -19,7 +19,7 @@ import pagure.lib
 import pagure.forms
 from pagure import APP, SESSION, login_required, is_repo_admin
 from pagure.lib.model import BASE
-
+from pagure.exceptions import FileNotFoundException
 # pylint: disable=E1101
 
 
@@ -127,11 +127,19 @@ def view_plugin(repo, plugin, username=None, full=True):
             # Set up the main script if necessary
             plugin.set_up(repo)
             # Install the plugin itself
-            plugin.install(repo, dbobj)
-            flask.flash('Hook %s activated' % plugin.name)
+            try:
+                plugin.install(repo, dbobj)
+                flask.flash('Hook %s activated' % plugin.name)
+            except FileNotFoundException as err:
+                pagure.APP.logger.exception(err)
+                flask.abort(404, 'No git repo found')
         else:
-            plugin.remove(repo)
-            flask.flash('Hook %s inactived' % plugin.name)
+            try:
+                plugin.remove(repo)
+                flask.flash('Hook %s inactived' % plugin.name)
+            except FileNotFoundException as err:
+                pagure.APP.logger.exception(err)
+                flask.abort(404, 'No git repo found')
 
         SESSION.commit()
 
diff --git a/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py b/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
index 26931f9..ec3bd34 100644
--- a/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
+++ b/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
@@ -17,7 +17,6 @@ import shutil
 import sys
 import os
 
-
 import pygit2
 from mock import patch
 from pagure.exceptions import FileNotFoundException
@@ -87,7 +86,8 @@ class PagureFlaskPluginPagureRequestHooktests(tests.Modeltests):
 
             data['csrf_token'] = csrf_token
             # No git found
-            self.assertRaises(pagure.exceptions.FileNotFoundException)
+            output = self.app.post('/test/settings/Pagure requests', data=data)
+            self.assertEqual(output.status_code, 404)
 
             # Create both the requests repo
             tests.create_projects_git(os.path.join(tests.HERE, 'requests'))
diff --git a/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py b/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
index 125f076..1cf1658 100644
--- a/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
+++ b/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
@@ -86,7 +86,8 @@ class PagureFlaskPluginPagureTicketHooktests(tests.Modeltests):
 
             data['csrf_token'] = csrf_token
             # No git found
-            self.assertRaises(pagure.exceptions.FileNotFoundException)
+            output = self.app.post('/test/settings/Pagure tickets', data=data)
+            self.assertEqual(output.status_code, 404)
 
             # Create both the tickets repo
             tests.create_projects_git(os.path.join(tests.HERE, 'tickets'))

From 88d19e03c3976983e2520b7efbe93970e98d257e Mon Sep 17 00:00:00 2001
From: Clement Verna <cverna@tutanota.com>
Date: Apr 29 2016 08:00:23 +0000
Subject: [PATCH 12/12] Cleaning up code before merge

Fixes https://pagure.io/pagure/issue/901

---

diff --git a/pagure/hooks/__init__.py b/pagure/hooks/__init__.py
index b44cbed..d1d7215 100644
--- a/pagure/hooks/__init__.py
+++ b/pagure/hooks/__init__.py
@@ -74,7 +74,7 @@ class BaseHook(object):
                            postreceive)
 
     @classmethod
-    def base_install(cls, repopaths, dbobj, hook_name, filein):  # pragma: no cover
+    def base_install(cls, repopaths, dbobj, hook_name, filein):
         ''' Method called to install the hook for a project.
 
         :arg project: a ``pagure.model.Project`` object to which the hook
@@ -106,7 +106,7 @@ class BaseHook(object):
                 )
 
     @classmethod
-    def base_remove(cls, repopaths, hook_name):  # pragma: no cover
+    def base_remove(cls, repopaths, hook_name):
         ''' Method called to remove the hook of a project.
 
         :arg project: a ``pagure.model.Project`` object to which the hook
diff --git a/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py b/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
index ec3bd34..a821bae 100644
--- a/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
+++ b/tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
@@ -19,7 +19,6 @@ import os
 
 import pygit2
 from mock import patch
-from pagure.exceptions import FileNotFoundException
 
 sys.path.insert(0, os.path.join(os.path.dirname(
     os.path.abspath(__file__)), '..'))
diff --git a/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py b/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
index 1cf1658..fcd8baa 100644
--- a/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
+++ b/tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
@@ -19,7 +19,6 @@ import os
 
 import pygit2
 from mock import patch
-from pagure.exceptions import FileNotFoundException
 
 sys.path.insert(0, os.path.join(os.path.dirname(
     os.path.abspath(__file__)), '..'))

