Blame progit/hooks/mail.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
"""
Pierre-Yves Chibon 82dc5a
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
 Authors:
Pierre-Yves Chibon 82dc5a
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
"""
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
import os
Pierre-Yves Chibon 82dc5a
import shutil
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
import sqlalchemy as sa
Pierre-Yves Chibon 82dc5a
import pygit2
Pierre-Yves Chibon 82dc5a
import wtforms
Pierre-Yves Chibon 82dc5a
from flask.ext import wtf
Pierre-Yves Chibon 82dc5a
from sqlalchemy.orm import relation
Pierre-Yves Chibon 848b42
from sqlalchemy.orm import backref
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 6a6e31
from progit.hooks import BaseHook, RequiredIf
Pierre-Yves Chibon 26b4f4
from progit.lib.model import BASE, Project
Pierre-Yves Chibon d7f343
from progit import SESSION, APP, get_repo_path
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
class MailTable(BASE):
Pierre-Yves Chibon 82dc5a
    """ Stores information about the irc hook deployed on a project.
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    Table -- hook_mail
Pierre-Yves Chibon 82dc5a
    """
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    __tablename__ = 'hook_mail'
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 82dc5a
    project_id = sa.Column(
Pierre-Yves Chibon 82dc5a
        sa.Integer,
Pierre-Yves Chibon 82dc5a
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon 82dc5a
        nullable=False,
Pierre-Yves Chibon 82dc5a
        unique=True,
Pierre-Yves Chibon 82dc5a
        index=True)
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    mail_to = sa.Column(sa.Text, nullable=False)
Pierre-Yves Chibon 82dc5a
    active = sa.Column(sa.Boolean, nullable=False, default=False)
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    project = relation(
Pierre-Yves Chibon 848b42
        'Project', remote_side=[Project.id],
Pierre-Yves Chibon 848b42
        backref=backref(
Pierre-Yves Chibon 848b42
            'mail_hook', cascade="delete, delete-orphan",
Pierre-Yves Chibon 848b42
            single_parent=True)
Pierre-Yves Chibon 848b42
        )
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
class MailForm(wtf.Form):
Pierre-Yves Chibon 82dc5a
    ''' Form to configure the mail hook. '''
Pierre-Yves Chibon 82dc5a
    mail_to = wtforms.TextField(
Pierre-Yves Chibon 6bfc36
        'Mail to',
Pierre-Yves Chibon 6a6e31
        [RequiredIf('active')]
Pierre-Yves Chibon 82dc5a
    )
Pierre-Yves Chibon 82dc5a
    active = wtforms.BooleanField(
Pierre-Yves Chibon ac32e9
        'Active',
Pierre-Yves Chibon 82dc5a
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 82dc5a
    )
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
class Mail(BaseHook):
Pierre-Yves Chibon 82dc5a
    ''' Mail hooks. '''
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    name = 'Mail'
Pierre-Yves Chibon 82dc5a
    form = MailForm
Pierre-Yves Chibon 82dc5a
    db_object = MailTable
Pierre-Yves Chibon 82dc5a
    backref = 'mail_hook'
Pierre-Yves Chibon 82dc5a
    form_fields = ['mail_to', 'active']
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
    @classmethod
Pierre-Yves Chibon 82dc5a
    def install(cls, project, dbobj):
Pierre-Yves Chibon 82dc5a
        ''' Method called to install the hook for a project.
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon 82dc5a
            should be installed
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
        '''
Pierre-Yves Chibon d7f343
        repopath = get_repo_path(project)
Pierre-Yves Chibon d7f343
Pierre-Yves Chibon 82dc5a
        hook_files = os.path.join(
Pierre-Yves Chibon 82dc5a
            os.path.dirname(os.path.realpath(__file__)), 'files')
Pierre-Yves Chibon 82dc5a
        repo_obj = pygit2.Repository(repopath)
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
        # Configure the hook
Pierre-Yves Chibon 6addf0
        repo_obj.config.set_multivar(
Pierre-Yves Chibon 6addf0
            'multimailhook.mailingList',
Pierre-Yves Chibon 6addf0
            '',
Pierre-Yves Chibon 6addf0
            dbobj.mail_to
Pierre-Yves Chibon 6addf0
        )
Pierre-Yves Chibon 6addf0
        repo_obj.config.set_multivar(
Pierre-Yves Chibon 6addf0
            'multimailhook.environment', '', 'gitolite')
Pierre-Yves Chibon 6addf0
Pierre-Yves Chibon 6addf0
        # Install the hook itself
Pierre-Yves Chibon 6addf0
        shutil.copyfile(
Pierre-Yves Chibon 6addf0
            os.path.join(hook_files, 'git_multimail.py'),
Pierre-Yves Chibon 6addf0
            os.path.join(repopath, 'hooks', 'post-receive.mail')
Pierre-Yves Chibon 6addf0
        )
Pierre-Yves Chibon 6addf0
        os.chmod(
Pierre-Yves Chibon 6addf0
            os.path.join(repopath, 'hooks', 'post-receive.mail'),
Pierre-Yves Chibon 6addf0
            0755)
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon ad7311
    @classmethod
Pierre-Yves Chibon ad7311
    def remove(cls, project):
Pierre-Yves Chibon ad7311
        ''' Method called to remove the hook of a project.
Pierre-Yves Chibon ad7311
Pierre-Yves Chibon ad7311
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon ad7311
            should be installed
Pierre-Yves Chibon ad7311
Pierre-Yves Chibon ad7311
        '''
Pierre-Yves Chibon d7f343
        repopath = get_repo_path(project)
Pierre-Yves Chibon d7f343
Pierre-Yves Chibon 0e8d15
        hook_path = os.path.join(repopath, 'hooks', 'post-receive.mail')
Pierre-Yves Chibon 0e8d15
        if os.path.exists(hook_path):
Pierre-Yves Chibon 0e8d15
            os.unlink(hook_path)