diff --git a/pagure/hooks/files/pagure_block_unsigned.py b/pagure/hooks/files/pagure_block_unsigned.py new file mode 100755 index 0000000..bd366f3 --- /dev/null +++ b/pagure/hooks/files/pagure_block_unsigned.py @@ -0,0 +1,72 @@ +#! /usr/bin/env python2 + + +"""Pagure specific hook to block commit not having a 'Signed-off-by' +statement. +""" + +import os +import sys + +from sqlalchemy.exc import SQLAlchemyError + +import sys +sys.path.insert(0, '/home/pierrey/repos/gitrepo/pagure/') + +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 +import pagure.exceptions +import pagure.lib.link +import pagure.ui.plugins + + +abspath = os.path.abspath(os.environ['GIT_DIR']) + + +def run_as_pre_receive_hook(): + + for line in sys.stdin: + if pagure.APP.config.get('HOOK_DEBUG', False): + print line + (oldrev, newrev, refname) = line.strip().split(' ', 2) + + if pagure.APP.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) + for commit in commits: + if pagure.APP.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 'signed-off-by' in line.lower(): + signed = True + break + if pagure.APP.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 new file mode 100644 index 0000000..00fb76a --- /dev/null +++ b/pagure/hooks/pagure_unsigned_commits.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2016 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +import os + +import sqlalchemy as sa +import pygit2 +import wtforms +from flask.ext import wtf +from sqlalchemy.orm import relation +from sqlalchemy.orm import backref + +from pagure.hooks import BaseHook, RequiredIf +from pagure.lib.model import BASE, Project +from pagure import APP, get_repo_path + + +class PagureUnsignedCommitTable(BASE): + """ Stores information about the pagure hook deployed on a project. + + Table -- hook_pagure_unsigned_commit + """ + + __tablename__ = 'hook_pagure_unsigned_commit' + + id = sa.Column(sa.Integer, primary_key=True) + project_id = sa.Column( + sa.Integer, + sa.ForeignKey('projects.id', onupdate='CASCADE'), + nullable=False, + unique=True, + index=True) + + active = sa.Column(sa.Boolean, nullable=False, default=False) + + project = relation( + 'Project', foreign_keys=[project_id], remote_side=[Project.id], + backref=backref( + 'pagure_unsigned_commit_hook', cascade="delete, delete-orphan", + single_parent=True) + ) + + +class PagureUnsignedCommitForm(wtf.Form): + ''' Form to configure the pagure hook. ''' + + active = wtforms.BooleanField( + 'Active', + [wtforms.validators.Optional()] + ) + + +class PagureUnsignedCommitHook(BaseHook): + ''' PagurPagureUnsignedCommit hook. ''' + + name = 'Block Un-Signed commits' + description = 'Using this hook you can block any push with commits '\ + 'missing a "Signed-Off-By"' + form = PagureUnsignedCommitForm + db_object = PagureUnsignedCommitTable + backref = 'pagure_unsigned_commit_hook' + form_fields = ['active'] + hook_type = 'pre-receive' + + @classmethod + def install(cls, project, dbobj): + ''' Method called to install the hook for a project. + + :arg project: a ``pagure.model.Project`` object to which the hook + should be installed + + ''' + repopath = get_repo_path(project) + + hook_files = os.path.join( + os.path.dirname(os.path.realpath(__file__)), 'files') + hook_file = os.path.join(hook_files, 'pagure_block_unsigned.py') + + # Init the git repo in case + pygit2.Repository(repopath) + + # Install the hook itself + hook_path = os.path.join( + repopath, 'hooks', 'pre-receive.pagureunsignedcommit') + if not os.path.exists(hook_path): + os.symlink(hook_file, hook_path) + + @classmethod + def remove(cls, project): + ''' Method called to remove the hook of a project. + + :arg project: a ``pagure.model.Project`` object to which the hook + should be installed + + ''' + repopath = get_repo_path(project) + hook_path = os.path.join( + repopath, 'hooks', 'pre-receive.pagureunsignedcommit') + if os.path.exists(hook_path): + os.unlink(hook_path)