From aabc84f011f586dad3795962bed55f3ef54b42df Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 08 2016 17:30:38 +0000 Subject: Replace the jenkins hook by a more generic CI one --- diff --git a/pagure/hooks/jenkins_hook.py b/pagure/hooks/jenkins_hook.py deleted file mode 100644 index ad2521c..0000000 --- a/pagure/hooks/jenkins_hook.py +++ /dev/null @@ -1,140 +0,0 @@ -# -*- coding: utf-8 -*- - - -import os -import uuid - -import sqlalchemy as sa -import pygit2 - -from wtforms import validators, TextField, BooleanField -from flask.ext import wtf -from sqlalchemy.orm import relation, backref -from sqlalchemy.ext.declarative import declarative_base - -from pagure.hooks import BaseHook, RequiredIf -from pagure.lib.model import BASE, Project -from pagure import get_repo_path -from pagure import APP, SESSION - - -class PagureCI(BASE): - - __tablename__ = 'hook_pagure_ci' - - id = sa.Column(sa.Integer, primary_key=True) - project_id = sa.Column( - sa.Integer, - sa.ForeignKey('projects.id', onupdate='CASCADE',ondelete='CASCADE'), - nullable=False, - unique=False, - index=True) - pagure_ci_token = sa.Column(sa.String(32), nullable=True, unique=True, - index=True) - - active = sa.Column(sa.Boolean, nullable=False, default=False) - display_name = sa.Column(sa.String(64), nullable=False, default='Jenkins') - pagure_name = sa.Column(sa.String(255)) - - jenkins_name = sa.Column(sa.String(255)) - jenkins_url = sa.Column(sa.String(255), nullable=False, - default='http://jenkins.fedorainfracloud.org/') - jenkins_token = sa.Column(sa.String(64)) - - project = relation( - 'Project', - foreign_keys=[project_id], - remote_side=[Project.id], - backref=backref( - 'hook_pagure_ci', cascade="delete, delete-orphan", - single_parent=True) - ) - - def __init__(self): - self.pagure_ci_token = uuid.uuid4().hex - - -class ConfigNotFound(Exception): - pass - - -class Service(object): - PAGURE = PagureCI.pagure_name - JENKINS = PagureCI.jenkins_name - - -def get_configs(project_name, service): - """Returns all configurations with given name on a service. - - :raises ConfigNotFound: when no configuration matches - """ - cfg = SESSION.query(PagureCI).filter( - service == project_name).all() - if not cfg: - raise ConfigNotFound(project_name) - return cfg - - -class JenkinsForm(wtf.Form): - - '''Form to configure Jenkins hook''' - - pagure_name = TextField('Name of project in Pagure', - [validators.Required(), - validators.Length(max=255)]) - - jenkins_name = TextField('Name of project in Jenkins', - [validators.Required(), - validators.Length(max=255)]) - - jenkins_url = TextField('Jenkins URL', - [validators.Required(), - validators.Length(max=255)], - default='http://jenkins.fedorainfracloud.org/') - - jenkins_token = TextField('Jenkins token', - [validators.Required()]) - - active = BooleanField('Active', [validators.Optional()]) - - -class PagureCiHook(BaseHook): - ''' Jenkins hooks. ''' - - name = 'Pagure CI' - description = 'This hook help to set up CI for the project'\ - ' the changes made by the pushes to the git repository.' - form = JenkinsForm - db_object = PagureCI - backref = 'hook_pagure_ci' - form_fields = [ - 'pagure_name', 'jenkins_name', - 'jenkins_url', 'jenkins_token', 'active' - ] - - @classmethod - def set_up(cls, project): - ''' Install the generic post-receive hook that allow us to call - multiple post-receive hooks as set per plugin. - ''' - pass - - @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 - - ''' - pass - - @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 - - ''' - pass diff --git a/pagure/hooks/pagure_ci.py b/pagure/hooks/pagure_ci.py new file mode 100644 index 0000000..247e66b --- /dev/null +++ b/pagure/hooks/pagure_ci.py @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2016 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +import os + +import sqlalchemy as sa +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, TypeCi +from pagure import get_repo_path, SESSION + + +class PagureCITable(BASE): + """ Stores information about the CI linked to on a project. + + Table -- hook_pagure_ci + """ + + __tablename__ = 'hook_pagure_ci' + + id = sa.Column(sa.Integer, primary_key=True) + project_id = sa.Column( + sa.Integer, + sa.ForeignKey( + 'projects.id', onupdate='CASCADE', ondelete='CASCADE'), + nullable=False, + unique=True, + index=True) + pagure_ci_token = sa.Column( + sa.String(32), + nullable=True, + unique=True, + index=True) + type_id = sa.Column( + sa.Integer, + sa.ForeignKey( + 'type_ci.id', onupdate='CASCADE', ondelete='CASCADE'), + nullable=False, + unique=True) + active = sa.Column(sa.Boolean, nullable=False, default=False) + + project = relation( + 'Project', remote_side=[Project.id], + backref=backref( + 'ci_hook', cascade="delete, delete-orphan", + single_parent=True) + ) + + type_ci = relation( + 'TypeCi', remote_side=[TypeCi.id], + ) + + +class PagureCiForm(wtf.Form): + ''' Form to configure the CI hook. ''' + type_ci = wtforms.SelectField( + 'Type of CI service', + [RequiredIf('active')], + choices=[] + ) + jenkins_url = wtforms.TextField( + 'URL to the project on the CI service', + [RequiredIf('active'), wtforms.validators.Length(max=255)], + ) + jenkins_token = wtforms.TextField( + 'CI token', + [RequiredIf('active')], + ) + active = wtforms.BooleanField( + 'Active', + [wtforms.validators.Optional()] + ) + + def __init__(self, *args, **kwargs): + """ Calls the default constructor with the normal argument but + uses the list of collection provided to fill the choices of the + drop-down list. + """ + super(PagureCiForm, self).__init__(*args, **kwargs) + + types = SESSION.query(TypeCi).order_by(TypeCi.type).all() + self.type_ci.choices = [ + (ci_type.type, ci_type.type) for ci_type in types + ] + + +class PagureCi(BaseHook): + ''' Mail hooks. ''' + + name = 'Pagure CI' + description = 'Generate notification emails for pushes to a git repository. '\ + 'This hook sends emails describing changes introduced by pushes to a git repository.' + form = PagureCiForm + db_object = PagureCITable + backref = 'ci_hook' + form_fields = ['type_ci', 'jenkins_url', 'jenkins_token', 'active'] + + @classmethod + def set_up(cls, project): + ''' Install the generic post-receive hook that allow us to call + multiple post-receive hooks as set per plugin. + ''' + pass + + @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 + + ''' + pass + + @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 + + ''' + pass