From fd7659eff019ba9847a5b8436168993d2930f847 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Apr 28 2016 18:39:17 +0000 Subject: 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')