Blame progit/hooks/irc.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
"""
Pierre-Yves Chibon 009d48
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
 Authors:
Pierre-Yves Chibon 009d48
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
"""
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon b045d6
import os
Pierre-Yves Chibon b045d6
Pierre-Yves Chibon 5808d7
import sqlalchemy as sa
Pierre-Yves Chibon b045d6
import pygit2
Pierre-Yves Chibon 009d48
import wtforms
Pierre-Yves Chibon 996c28
from flask.ext import wtf
Pierre-Yves Chibon 996c28
from sqlalchemy.orm import relation
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 5808d7
from progit.hooks import BaseHook
Pierre-Yves Chibon 26b4f4
from progit.lib.model import BASE, Project
Pierre-Yves Chibon b045d6
from progit import SESSION, APP
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 5808d7
class IrcTable(BASE):
Pierre-Yves Chibon 5808d7
    """ Stores information about the irc hook deployed on a project.
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 5808d7
    Table -- hook_irc
Pierre-Yves Chibon 5808d7
    """
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 5808d7
    __tablename__ = 'hook_irc'
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 5808d7
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 5808d7
    project_id = sa.Column(
Pierre-Yves Chibon 5808d7
        sa.Integer,
Pierre-Yves Chibon 5808d7
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon 5808d7
        nullable=False,
Pierre-Yves Chibon 996c28
        unique=True,
Pierre-Yves Chibon 5808d7
        index=True)
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 5808d7
    server = sa.Column(sa.Text, nullable=False)
Pierre-Yves Chibon 5808d7
    port = sa.Column(sa.Text, nullable=False)
Pierre-Yves Chibon 5808d7
    room = sa.Column(sa.Text, nullable=False)
Pierre-Yves Chibon 5808d7
    nick = sa.Column(sa.Text, nullable=True, default=None)
Pierre-Yves Chibon 5808d7
    nick_pass = sa.Column(sa.Text, nullable=True, default=None)
Pierre-Yves Chibon 5808d7
    active = sa.Column(sa.Boolean, nullable=False, default=False)
Pierre-Yves Chibon 5808d7
    join = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon 5808d7
    ssl = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon 5808d7
Pierre-Yves Chibon 996c28
    project = relation(
Pierre-Yves Chibon 016952
        'Project', remote_side=[Project.id], backref='irc_hook',
Pierre-Yves Chibon 016952
        cascade="delete, delete-orphan", single_parent=True)
Pierre-Yves Chibon 996c28
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
class IrcForm(wtf.Form):
Pierre-Yves Chibon 009d48
    ''' Form to configure the irc hook. '''
Pierre-Yves Chibon 009d48
    server = wtforms.TextField(
Pierre-Yves Chibon 009d48
        'Server *',
Pierre-Yves Chibon 009d48
        [wtforms.validators.Required()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
    port = wtforms.TextField(
Pierre-Yves Chibon 009d48
        'Port *',
Pierre-Yves Chibon 009d48
        [wtforms.validators.Required()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
    room = wtforms.TextField(
Pierre-Yves Chibon 009d48
        'Room *',
Pierre-Yves Chibon 009d48
        [wtforms.validators.Required()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
    nick = wtforms.TextField(
Pierre-Yves Chibon 486958
        'Nick',
Pierre-Yves Chibon 486958
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
    nick_pass = wtforms.TextField(
Pierre-Yves Chibon 486958
        'Nickserv Password',
Pierre-Yves Chibon 486958
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
    active = wtforms.BooleanField(
Pierre-Yves Chibon 009d48
        'Acive',
Pierre-Yves Chibon 486958
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
    join = wtforms.BooleanField(
Pierre-Yves Chibon 009d48
        'Message Without Join',
Pierre-Yves Chibon 486958
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
    ssl = wtforms.BooleanField(
Pierre-Yves Chibon 009d48
        'Use SSL',
Pierre-Yves Chibon 486958
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 009d48
    )
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
class Hook(BaseHook):
Pierre-Yves Chibon 009d48
    ''' IRC hooks. '''
Pierre-Yves Chibon 009d48
Pierre-Yves Chibon 009d48
    name = 'IRC'
Pierre-Yves Chibon d66f9b
    form = IrcForm
Pierre-Yves Chibon 996c28
    db_object = IrcTable
Pierre-Yves Chibon 996c28
    backref = 'irc_hook'
Pierre-Yves Chibon 009d48
    form_fields = [
Pierre-Yves Chibon 009d48
        'server', 'port', 'room', 'nick', 'nick_pass', 'active', 'join',
Pierre-Yves Chibon 009d48
        'ssl'
Pierre-Yves Chibon 009d48
    ]
Pierre-Yves Chibon b045d6
Pierre-Yves Chibon b045d6
    @classmethod
Pierre-Yves Chibon b045d6
    def install(cls, project, dbobj):
Pierre-Yves Chibon b045d6
        ''' Method called to install the hook for a project.
Pierre-Yves Chibon b045d6
Pierre-Yves Chibon b045d6
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon b045d6
            should be installed
Pierre-Yves Chibon b045d6
Pierre-Yves Chibon b045d6
        '''
Pierre-Yves Chibon b045d6
        repopath = os.path.join(APP.config['GIT_FOLDER'], project.path)
Pierre-Yves Chibon b045d6
        if project.is_fork:
Pierre-Yves Chibon b045d6
            repopath = os.path.join(APP.config['FORK_FOLDER'], project.path)
Pierre-Yves Chibon b045d6
        hook_files = os.path.join(
Pierre-Yves Chibon b045d6
            os.path.dirname(os.path.realpath(__file__)), 'files')
Pierre-Yves Chibon b045d6
        repo_obj = pygit2.Repository(repopath)
Pierre-Yves Chibon b045d6
Pierre-Yves Chibon b045d6
        # Configure the hook
Pierre-Yves Chibon b045d6
        # repo_obj.config.set_multivar()
Pierre-Yves Chibon b045d6
Pierre-Yves Chibon b045d6
        # Install the hook itself
Pierre-Yves Chibon b045d6
        #shutil.copyfile(
Pierre-Yves Chibon b045d6
            #os.path.join(hook_files, 'git_irc.py'),
Pierre-Yves Chibon b045d6
            #os.path.join(repopath, 'hooks', 'post-receive.irc')
Pierre-Yves Chibon b045d6
        #)
Pierre-Yves Chibon b045d6
        #os.chmod(os.path.join(repopath, 'hooks', 'post-receive.irc'), 0755)