From 762667961c840a3b104f55cf3d775f78f1ae8346 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 23 2018 13:13:11 +0000 Subject: Port the mirror hook to the new base runner architecture Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/hooks/files/mirror.py b/pagure/hooks/files/mirror.py deleted file mode 100755 index 77510cf..0000000 --- a/pagure/hooks/files/mirror.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python - - -"""Pagure specific hook to mirror a repo to another location. -""" -from __future__ import unicode_literals, print_function - - -import logging -import os -import sys - - -if "PAGURE_CONFIG" not in os.environ and os.path.exists( - "/etc/pagure/pagure.cfg" -): - os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg" - - -import pagure.config # noqa: E402 -import pagure.exceptions # noqa: E402 -import pagure.lib # noqa: E402 -import pagure.lib.tasks_mirror # noqa: E402 -import pagure.ui.plugins # noqa: E402 - - -_log = logging.getLogger(__name__) -_config = pagure.config.config -abspath = os.path.abspath(os.environ["GIT_DIR"]) - - -def main(args): - - repo = pagure.lib.git.get_repo_name(abspath) - username = pagure.lib.git.get_username(abspath) - namespace = pagure.lib.git.get_repo_namespace(abspath) - if _config.get("HOOK_DEBUG", False): - print("repo:", repo) - print("user:", username) - print("namespace:", namespace) - - session = pagure.lib.create_session(_config["DB_URL"]) - project = pagure.lib._get_project( - session, repo, user=username, namespace=namespace - ) - - if not project: - print("Could not find a project corresponding to this git repo") - session.close() - return 1 - - pagure.lib.tasks_mirror.mirror_project.delay( - username=project.user.user if project.is_fork else None, - namespace=project.namespace, - name=project.name, - ) - - session.close() - return 0 - - -if __name__ == "__main__": - main(sys.argv[1:]) diff --git a/pagure/hooks/mirror_hook.py b/pagure/hooks/mirror_hook.py index 40dbeb7..7fad829 100644 --- a/pagure/hooks/mirror_hook.py +++ b/pagure/hooks/mirror_hook.py @@ -19,12 +19,16 @@ except ImportError: from sqlalchemy.orm import relation from sqlalchemy.orm import backref +import pagure.config import pagure.lib.tasks_mirror -from pagure.hooks import BaseHook, RequiredIf +from pagure.hooks import BaseHook, BaseRunner, RequiredIf from pagure.lib.model import BASE, Project from pagure.utils import get_repo_path, ssh_urlpattern +_config = pagure.config.reload_config() + + class MirrorTable(BASE): """ Stores information about the mirroring hook deployed on a project. @@ -60,6 +64,31 @@ class MirrorTable(BASE): ) +class MirrorRunner(BaseRunner): + """ Runner for the mirror hook. """ + + @staticmethod + def post_receive(session, username, project, repotype, repodir, changes): + """ Run the default post-receive hook. + + For args, see BaseRunner.runhook. + """ + print("Running the default hook") + if repotype != "main": + if _config.get("HOOK_DEBUG", False): + print( + "Default hook only runs on the main project repository") + return + + pagure.lib.tasks_mirror.mirror_project.delay( + username=project.user.user if project.is_fork else None, + namespace=project.namespace, + name=project.name, + ) + + session.close() + + class CustomRegexp(wtforms.validators.Regexp): def __init__(self, *args, **kwargs): self.optional = kwargs.get("optional") or False @@ -120,6 +149,7 @@ class MirrorHook(BaseHook): backref = "mirror_hook" form_fields = ["active", "target", "public_key", "last_log"] form_fields_readonly = ["public_key", "last_log"] + runner = MirrorRunner @classmethod def install(cls, project, dbobj):