From 9c66762227341710a7b2b6dd280be52a9417c03c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: May 18 2017 09:07:41 +0000 Subject: Improve the mirror hook - Add support for namespaced projects - Specify the absolute path to the repo in the message - Notify the service instead of doing the mirroring ourself --- diff --git a/pagure/hooks/files/mirror.py b/pagure/hooks/files/mirror.py index aae03e1..72a63e3 100755 --- a/pagure/hooks/files/mirror.py +++ b/pagure/hooks/files/mirror.py @@ -3,7 +3,10 @@ """Pagure specific hook to mirror a repo to another location. """ +from __future__ import print_function + +import json import os import sys @@ -25,48 +28,29 @@ import pagure.ui.plugins abspath = os.path.abspath(os.environ['GIT_DIR']) -def mirror_repo(): +def main(args): - reponame = pagure.lib.git.get_repo_name(abspath) + repo = pagure.lib.git.get_repo_name(abspath) username = pagure.lib.git.get_username(abspath) + namespace = pagure.lib.git.get_repo_namespace(abspath) if pagure.APP.config.get('HOOK_DEBUG', False): - print 'repo:', reponame, username - - repo = pagure.lib.get_project(pagure.SESSION, reponame, user=username) - if not repo: - print 'Unknown repo %s of username: %s' % (reponame, username) + print('repo:', repo) + print('user:', username) + print('namespace:', namespace) + + project = pagure.lib.get_project( + pagure.SESSION, repo, user=username, namespace=namespace) + if not project: + fullname = reponame + if namespace: + fullname = '%s/%s' % (namespace, reponame) + print('Unknown repo %s of username: %s' % (fullname, username)) sys.exit(1) - plugin = pagure.ui.plugins.get_plugin('Mirroring') - dbobj = plugin.db_object() - - # Get the list of remotes - remotes = [ - remote.strip() - for remote in repo.mirror_hook[0].target.split('\n') - if repo.mirror_hook and remote.strip() - ] - - public_key_name = werkzeug.secure_filename(repo.fullname) - - # Add the remotes - for idx, remote in enumerate(remotes): - lines = pagure.lib.git.read_git_lines( - ['remote', 'add', '%s_%s' % (public_key_name, idx), remote, - '--mirror=push'], abspath) - if pagure.APP.config.get('HOOK_DEBUG', False): - print '\n'.join(lines) - - # Push - for idx, remote in enumerate(remotes): - lines = pagure.lib.git.read_git_lines( - ['push', '%s_%s' % (public_key_name, idx)], abspath) - if pagure.APP.config.get('HOOK_DEBUG', False): - print '\n'.join(lines) - - -def main(args): - mirror_repo() + data = project.to_json(public=True) + data['topic'] = 'pagure.mirror.postcommit' + data['abspath'] = abspath + pagure.lib.REDIS.publish('pagure.mirror', json.dumps(data)) if __name__ == '__main__':