diff --git a/pagure/hooks/files/pagure_no_new_branches b/pagure/hooks/files/pagure_no_new_branches deleted file mode 100755 index 759c9c4..0000000 --- a/pagure/hooks/files/pagure_no_new_branches +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# adapted from https://stackoverflow.com/a/29880516 - -retcode=0 -while read -r old new refname; do - if [[ $refname == refs/heads/* && $old != *[^0]* ]]; then - retcode=1 - echo "Creating new branches is disabled on this project." - fi -done - -exit $retcode diff --git a/pagure/hooks/pagure_no_new_branches.py b/pagure/hooks/pagure_no_new_branches.py index e0b2e41..7b223ba 100644 --- a/pagure/hooks/pagure_no_new_branches.py +++ b/pagure/hooks/pagure_no_new_branches.py @@ -9,6 +9,7 @@ """ from __future__ import unicode_literals +import sys import sqlalchemy as sa import wtforms @@ -20,7 +21,7 @@ except ImportError: from sqlalchemy.orm import relation from sqlalchemy.orm import backref -from pagure.hooks import BaseHook +from pagure.hooks import BaseHook, BaseRunner from pagure.lib.model import BASE, Project from pagure.utils import get_repo_path @@ -56,6 +57,27 @@ class PagureNoNewBranchesTable(BASE): ) +class PagureNoNewBranchRunner(BaseRunner): + """ Runner for the hook blocking new branches from being created. """ + + @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(oldrev) == set(["0"]): + print( + "Creating a new reference/branch is not allowed in this" + " project." + ) + sys.exit(1) + + class PagureNoNewBranchesForm(FlaskForm): """ Form to configure the pagure hook. """ @@ -72,6 +94,7 @@ class PagureNoNewBranchesHook(BaseHook): backref = "pagure_hook_no_new_branches" form_fields = ["active"] hook_type = "pre-receive" + runner = PagureNoNewBranchRunner @classmethod def install(cls, project, dbobj):