From 7115687610480cf8d9255f100510156e9f9c4769 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 23 2018 13:13:11 +0000 Subject: Port the hook to block unsigned commits to the new runner architecture Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/hooks/files/pagure_block_unsigned.py b/pagure/hooks/files/pagure_block_unsigned.py deleted file mode 100755 index bca674e..0000000 --- a/pagure/hooks/files/pagure_block_unsigned.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python - - -"""Pagure specific hook to block commit not having a 'Signed-off-by' -statement. -""" - -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.ui.plugins # noqa: E402 - -_config = pagure.config.config -abspath = os.path.abspath(os.environ["GIT_DIR"]) - - -def run_as_pre_receive_hook(): - - 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 " - "hook to block unsigned commits" - ) - return - - commits = pagure.lib.git.get_revs_between( - oldrev, newrev, abspath, refname - ) - for commit in commits: - if _config.get("HOOK_DEBUG", False): - print("Processing commit: %s" % commit) - signed = False - for line in pagure.lib.git.read_git_lines( - ["log", "--no-walk", commit], abspath - ): - if line.lower().strip().startswith("signed-off-by"): - signed = True - break - if _config.get("HOOK_DEBUG", False): - print(" - Commit: %s is signed: %s" % (commit, signed)) - if not signed: - print("Commit %s is not signed" % commit) - sys.exit(1) - - -def main(args): - run_as_pre_receive_hook() - - -if __name__ == "__main__": - main(sys.argv[1:]) diff --git a/pagure/hooks/pagure_unsigned_commits.py b/pagure/hooks/pagure_unsigned_commits.py index 56d80c4..35741f8 100644 --- a/pagure/hooks/pagure_unsigned_commits.py +++ b/pagure/hooks/pagure_unsigned_commits.py @@ -10,6 +10,8 @@ from __future__ import unicode_literals +import sys + import sqlalchemy as sa import wtforms @@ -20,11 +22,15 @@ except ImportError: from sqlalchemy.orm import relation from sqlalchemy.orm import backref -from pagure.hooks import BaseHook +import pagure.config +from pagure.hooks import BaseHook, BaseRunner from pagure.lib.model import BASE, Project from pagure.utils import get_repo_path +_config = pagure.config.reload_config() + + class PagureUnsignedCommitTable(BASE): """ Stores information about the pagure hook deployed on a project. @@ -57,6 +63,46 @@ class PagureUnsignedCommitTable(BASE): ) +class PagureUnsignerRunner(BaseRunner): + """ Runner for the hook blocking unsigned commits. """ + + @staticmethod + def pre_receive(session, username, project, repotype, repodir, changes): + """ Run the pre-receive tasks of a hook. + + For args, see BaseRunner.runhook. + """ + + for refname in changes: + (oldrev, newrev) = changes[refname] + + if set(newrev) == set(["0"]): + print( + "Deleting a reference/branch, so we won't run the " + "hook to block unsigned commits" + ) + return + + commits = pagure.lib.git.get_revs_between( + oldrev, newrev, repodir, refname + ) + for commit in commits: + if _config.get("HOOK_DEBUG", False): + print("Processing commit: %s" % commit) + signed = False + for line in pagure.lib.git.read_git_lines( + ["log", "--no-walk", commit], repodir + ): + if line.lower().strip().startswith("signed-off-by"): + signed = True + break + if _config.get("HOOK_DEBUG", False): + print(" - Commit: %s is signed: %s" % (commit, signed)) + if not signed: + print("Commit %s is not signed" % commit) + sys.exit(1) + + class PagureUnsignedCommitForm(FlaskForm): """ Form to configure the pagure hook. """ @@ -76,6 +122,7 @@ class PagureUnsignedCommitHook(BaseHook): backref = "pagure_unsigned_commit_hook" form_fields = ["active"] hook_type = "pre-receive" + runner = PagureUnsignerRunner @classmethod def install(cls, project, dbobj):