From 0cae8b0fadb08b47a884eff11f06d3a823ef696c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 23 2018 13:13:11 +0000 Subject: Port the pagure ticket hook to the new runner architecture Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/hooks/files/pagure_hook_tickets.py b/pagure/hooks/files/pagure_hook_tickets.py deleted file mode 100755 index c9577b2..0000000 --- a/pagure/hooks/files/pagure_hook_tickets.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python - - -"""Pagure specific hook to update tickets stored in the database based on -the information pushed in the tickets git repository. -""" -from __future__ import print_function, unicode_literals - -import os -import sys - - -# We need to access the database -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.lib.tasks_services # noqa: E402 - - -_config = pagure.config.config -abspath = os.path.abspath(os.environ["GIT_DIR"]) - - -def run_as_post_receive_hook(): - - repo = pagure.lib.git.get_repo_name(abspath) - username = pagure.lib.git.get_username(abspath) - namespace = pagure.lib.git.get_repo_namespace( - abspath, gitfolder=_config["TICKETS_FOLDER"] - ) - if _config.get("HOOK_DEBUG", False): - print("repo:", repo) - print("user:", username) - print("namespace:", namespace) - - for line in sys.stdin: - if _config.get("HOOK_DEBUG", False): - print(line) - (oldrev, newrev, refname) = line.strip().split(" ", 2) - - if _config.get("HOOK_DEBUG", False): - print(" -- Old rev") - print(oldrev) - print(" -- New rev") - print(newrev) - print(" -- Ref name") - print(refname) - - if set(newrev) == set(["0"]): - print( - "Deleting a reference/branch, so we won't run the " - "pagure hook" - ) - return - - commits = pagure.lib.git.get_revs_between( - oldrev, newrev, abspath, refname - ) - - pagure.lib.tasks_services.load_json_commits_to_db.delay( - name=repo, - commits=commits, - abspath=abspath, - data_type="ticket", - agent=os.environ.get("GL_USER"), - namespace=namespace, - username=username, - ) - - -def main(args): - run_as_post_receive_hook() - - -if __name__ == "__main__": - main(sys.argv[1:]) diff --git a/pagure/hooks/pagure_ticket_hook.py b/pagure/hooks/pagure_ticket_hook.py index 8e30ad8..dddf7fb 100644 --- a/pagure/hooks/pagure_ticket_hook.py +++ b/pagure/hooks/pagure_ticket_hook.py @@ -23,8 +23,9 @@ except ImportError: from sqlalchemy.orm import relation from sqlalchemy.orm import backref +import pagure.lib from pagure.config import config as pagure_config -from pagure.hooks import BaseHook +from pagure.hooks import BaseHook, BaseRunner from pagure.lib.model import BASE, Project @@ -59,6 +60,45 @@ class PagureTicketsTable(BASE): ) +class PagureTicketRunner(BaseRunner): + """ Runner for the git hook updating the DB of tickets on push. """ + + @staticmethod + def pre_receive(session, username, project, repotype, repodir, changes): + """ Run the pre-receive tasks of a hook. + + For args, see BaseRunner.runhook. + """ + + if repotype != "tickets": + print("The ticket hook only runs on the ticket git repository.") + return + + for refname in changes: + (oldrev, newrev) = changes[refname] + + if set(newrev) == set(["0"]): + print( + "Deleting a reference/branch, so we won't run the " + "pagure hook" + ) + return + + commits = pagure.lib.git.get_revs_between( + oldrev, newrev, repodir, refname + ) + + pagure.lib.tasks_services.load_json_commits_to_db.delay( + name=project.name, + commits=commits, + abspath=repodir, + data_type="ticket", + agent=username, + namespace=project.namespace, + username=project.user.user if project.is_fork else None, + ) + + class PagureTicketsForm(FlaskForm): """ Form to configure the pagure hook. """ @@ -78,6 +118,7 @@ class PagureTicketHook(BaseHook): db_object = PagureTicketsTable backref = "pagure_hook_tickets" form_fields = ["active"] + runner = PagureTicketRunner @classmethod def set_up(cls, project):