From fee3d8366574ee99f67733ef4bd82fc8288601a7 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 23 2018 13:13:11 +0000 Subject: Port the hook blocking force push to the new runner architecture Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/hooks/files/pagure_force_commit_hook.py b/pagure/hooks/files/pagure_force_commit_hook.py deleted file mode 100755 index 8bb246c..0000000 --- a/pagure/hooks/files/pagure_force_commit_hook.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env python - - -"""Pagure specific hook to block non-fastforward pushes. -""" - -from __future__ import print_function, unicode_literals - -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 # noqa: E402 -import pagure.exceptions # noqa: E402 -import pagure.lib.link # noqa: E402 -import pagure.lib.plugins # noqa: E402 - - -_config = pagure.config.config -abspath = os.path.abspath(os.environ["GIT_DIR"]) - - -def run_as_pre_receive_hook(): - reponame = pagure.lib.git.get_repo_name(abspath) - namespace = pagure.lib.git.get_repo_namespace(abspath) - username = pagure.lib.git.get_username(abspath) - session = pagure.lib.create_session(_config["DB_URL"]) - if _config.get("HOOK_DEBUG", False): - print("repo: ", reponame) - print("user: ", username) - print("namspaces:", namespace) - - repo = pagure.lib._get_project( - session, reponame, user=username, namespace=namespace - ) - - if not repo: - print( - "Unknown repo %s of username: %s in namespace %s" - % (reponame, username, namespace) - ) - session.close() - sys.exit(1) - - plugin = pagure.lib.plugins.get_plugin("Block non fast-forward pushes") - plugin.db_object() - # Get the list of branches - branches = [] - if repo.pagure_force_commit_hook: - branches = [ - branch.strip() - for branch in repo.pagure_force_commit_hook.branches.split(",") - if branch.strip() - ] - - for line in sys.stdin: - if _config.get("HOOK_DEBUG", False): - print(line) - (oldrev, newrev, refname) = line.strip().split(" ", 2) - - refname = refname.replace("refs/heads/", "") - if refname in branches or branches == ["*"]: - 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("Deletion is forbidden") - session.close() - sys.exit(1) - elif pagure.lib.git.is_forced_push(oldrev, newrev, abspath): - print("Non fast-forward push are forbidden") - session.close() - sys.exit(1) - - session.close() - - -def main(args): - run_as_pre_receive_hook() - - -if __name__ == "__main__": - main(sys.argv[1:]) diff --git a/pagure/hooks/pagure_force_commit.py b/pagure/hooks/pagure_force_commit.py index 6f80f13..120ef1b 100644 --- a/pagure/hooks/pagure_force_commit.py +++ b/pagure/hooks/pagure_force_commit.py @@ -10,6 +10,8 @@ from __future__ import unicode_literals +import sys + import sqlalchemy as sa import pygit2 import wtforms @@ -21,7 +23,8 @@ except ImportError: from sqlalchemy.orm import relation from sqlalchemy.orm import backref -from pagure.hooks import BaseHook, RequiredIf +import pagure.lib.git +from pagure.hooks import BaseHook, BaseRunner, RequiredIf from pagure.lib.model import BASE, Project from pagure.utils import get_repo_path @@ -60,6 +63,43 @@ class PagureForceCommitTable(BASE): ) +class PagureForceCommitRunner(BaseRunner): + """ Runner for the hook blocking force push. """ + + @staticmethod + def pre_receive(session, username, project, repotype, repodir, changes): + """ Run the pre-receive tasks of a hook. + + For args, see BaseRunner.runhook. + """ + + # Get the list of branches + branches = [] + if project.pagure_force_commit_hook: + branches = [ + branch.strip() + for branch in project.pagure_force_commit_hook.branches.split( + "," + ) + if branch.strip() + ] + + for refname in changes: + (oldrev, newrev) = changes[refname] + + refname = refname.replace("refs/heads/", "") + if refname in branches or branches == ["*"]: + + if set(newrev) == set(["0"]): + print("Deletion is forbidden") + session.close() + sys.exit(1) + elif pagure.lib.git.is_forced_push(oldrev, newrev, repodir): + print("Non fast-forward push are forbidden") + session.close() + sys.exit(1) + + class PagureForceCommitForm(FlaskForm): """ Form to configure the pagure hook. """ @@ -83,6 +123,7 @@ class PagureForceCommitHook(BaseHook): backref = "pagure_force_commit_hook" form_fields = ["branches", "active"] hook_type = "pre-receive" + runner = PagureForceCommitRunner @classmethod def install(cls, project, dbobj):