From 82dc5ab23aeba5fe07f1e80fb31ab8cb899c1aef Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: May 27 2014 09:29:14 +0000 Subject: Add a progit Mail hook --- diff --git a/progit/hooks/mail.py b/progit/hooks/mail.py new file mode 100644 index 0000000..00116c1 --- /dev/null +++ b/progit/hooks/mail.py @@ -0,0 +1,99 @@ +#-*- coding: utf-8 -*- + +""" + (c) 2014 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +import os +import shutil + +import sqlalchemy as sa +import pygit2 +import wtforms +from flask.ext import wtf +from sqlalchemy.orm import relation + +from progit.hooks import BaseHook +from progit.model import BASE, Project +from progit import SESSION, APP + + +class MailTable(BASE): + """ Stores information about the irc hook deployed on a project. + + Table -- hook_mail + """ + + __tablename__ = 'hook_mail' + + 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) + + mail_to = sa.Column(sa.Text, nullable=False) + active = sa.Column(sa.Boolean, nullable=False, default=False) + + project = relation( + 'Project', remote_side=[Project.id], backref='mail_hook') + + +class MailForm(wtf.Form): + ''' Form to configure the mail hook. ''' + mail_to = wtforms.TextField( + 'Mail to *', + [wtforms.validators.Required()] + ) + active = wtforms.BooleanField( + 'Acive', + [wtforms.validators.Optional()] + ) + + +class Mail(BaseHook): + ''' Mail hooks. ''' + + name = 'Mail' + form = MailForm + db_object = MailTable + backref = 'mail_hook' + form_fields = ['mail_to', 'active'] + + @classmethod + def install(cls, project, dbobj): + ''' Method called to install the hook for a project. + + :arg project: a ``progit.model.Project`` object to which the hook + should be installed + + ''' + repopath = os.path.join(APP.config['GIT_FOLDER'], project.path) + if project.is_fork: + repopath = os.path.join(APP.config['FORK_FOLDER'], project.path) + hook_files = os.path.join( + os.path.dirname(os.path.realpath(__file__)), 'files') + repo_obj = pygit2.Repository(repopath) + + # Configure the hook + repo_obj.config.set_multivar( + 'multimailhook.mailingList', + '', + dbobj.mail_to + ) + repo_obj.config.set_multivar( + 'multimailhook.environment', '', 'gitolite') + + # Install the hook itself + shutil.copyfile( + os.path.join(hook_files, 'git_multimail.py'), + os.path.join(repopath, 'hooks', 'post-receive.mail') + ) + os.chmod(os.path.join(repopath, 'hooks', 'post-receive.mail'), 0755) +