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 82dc5a
Pierre-Yves Chibon 82dc5a
from progit.hooks import BaseHook
Pierre-Yves Chibon 26b4f4
from progit.lib.model import BASE, Project
Pierre-Yves Chibon 82dc5a
from progit import SESSION, APP
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 016952
        'Project', remote_side=[Project.id], backref='mail_hook',
Pierre-Yves Chibon 016952
        cascade="delete, delete-orphan", single_parent=True)
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 82dc5a
        'Mail to *',
Pierre-Yves Chibon 82dc5a
        [wtforms.validators.Required()]
Pierre-Yves Chibon 82dc5a
    )
Pierre-Yves Chibon 82dc5a
    active = wtforms.BooleanField(
Pierre-Yves Chibon 82dc5a
        'Acive',
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 82dc5a
        repopath = os.path.join(APP.config['GIT_FOLDER'], project.path)
Pierre-Yves Chibon 82dc5a
        if project.is_fork:
Pierre-Yves Chibon 82dc5a
            repopath = os.path.join(APP.config['FORK_FOLDER'], project.path)
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 82dc5a
        repo_obj.config.set_multivar(
Pierre-Yves Chibon 82dc5a
            'multimailhook.mailingList',
Pierre-Yves Chibon 82dc5a
            '',
Pierre-Yves Chibon 82dc5a
            dbobj.mail_to
Pierre-Yves Chibon 82dc5a
        )
Pierre-Yves Chibon 82dc5a
        repo_obj.config.set_multivar(
Pierre-Yves Chibon 82dc5a
            'multimailhook.environment', '', 'gitolite')
Pierre-Yves Chibon 82dc5a
Pierre-Yves Chibon 82dc5a
        # Install the hook itself
Pierre-Yves Chibon 82dc5a
        shutil.copyfile(
Pierre-Yves Chibon 82dc5a
            os.path.join(hook_files, 'git_multimail.py'),
Pierre-Yves Chibon 82dc5a
            os.path.join(repopath, 'hooks', 'post-receive.mail')
Pierre-Yves Chibon 82dc5a
        )
Pierre-Yves Chibon 82dc5a
        os.chmod(os.path.join(repopath, 'hooks', 'post-receive.mail'), 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 ad7311
        repopath = os.path.join(APP.config['GIT_FOLDER'], project.path)
Pierre-Yves Chibon ad7311
        if project.is_fork:
Pierre-Yves Chibon ad7311
            repopath = os.path.join(APP.config['FORK_FOLDER'], project.path)
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)