From 6d2f2506dcb39c570b68c3365a1290565962b5d3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 17 2016 10:17:30 +0000 Subject: Add a Read the Doc plugin and git hook This plugin allows to trigger a build on readthedocs.org upon git push --- diff --git a/pagure/hooks/files/rtd_hook.py b/pagure/hooks/files/rtd_hook.py new file mode 100755 index 0000000..98525c6 --- /dev/null +++ b/pagure/hooks/files/rtd_hook.py @@ -0,0 +1,81 @@ +#! /usr/bin/env python2 + + +"""Pagure specific hook to trigger a build on a readthedocs.org project. +""" + +import os +import sys + +import requests + +from sqlalchemy.exc import SQLAlchemyError + + +if 'PAGURE_CONFIG' not in os.environ \ + and os.path.exists('/etc/pagure/pagure.cfg'): + os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg' + + +import pagure +import pagure.exceptions +import pagure.lib.link +import pagure.ui.plugins + + +abspath = os.path.abspath(os.environ['GIT_DIR']) + + +def run_as_post_receive_hook(): + reponame = pagure.lib.git.get_repo_name(abspath) + username = pagure.lib.git.get_username(abspath) + if pagure.APP.config.get('HOOK_DEBUG', False): + print 'repo:', reponame + print 'user:', username + + repo = pagure.lib.get_project(pagure.SESSION, reponame, user=username) + if not repo: + print 'Unknown repo %s of username: %s' % (reponame, username) + sys.exit(1) + + plugin = pagure.ui.plugins.get_plugin('Read the Doc') + dbobj = plugin.db_object() + # Get the list of branches + branches = [ + branch.strip() + for branch in repo.rtd_hook[0].branches.split(',') + if repo.rtd_hook] + + # Remove empty branches + branches = [ + branch.strip() + for branch in branches + if branch] + + url = 'http://readthedocs.org/build/%s' % ( + repo.rtd_hook[0].project_name.strip() + ) + + for line in sys.stdin: + if pagure.APP.config.get('HOOK_DEBUG', False): + print line + (oldrev, newrev, refname) = line.strip().split(' ', 2) + + refname = refname.replace('refs/heads/', '') + if branches: + if refname in branches: + print 'Starting RTD build for %s' % ( + repo.rtd_hook[0].project_name.strip()) + requests.post(url) + else: + print 'Starting RTD build for %s' % ( + repo.rtd_hook[0].project_name.strip()) + requests.post(url) + + +def main(args): + run_as_post_receive_hook() + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/pagure/hooks/rtd.py b/pagure/hooks/rtd.py new file mode 100644 index 0000000..ca287cb --- /dev/null +++ b/pagure/hooks/rtd.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- + +""" + (c) 2016 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +import os + +import sqlalchemy as sa +import pygit2 +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 +from pagure import APP, get_repo_path + + +class RtdTable(BASE): + """ Stores information about the pagure hook deployed on a project. + + Table -- hook_rtd + """ + + __tablename__ = 'hook_rtd' + + 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) + + active = sa.Column(sa.Boolean, nullable=False, default=False) + + project_name = sa.Column(sa.Text, nullable=False) + branches = sa.Column(sa.Text, nullable=True) + + project = relation( + 'Project', remote_side=[Project.id], + backref=backref( + 'rtd_hook', cascade="delete, delete-orphan", + single_parent=True) + ) + + +class RtdForm(wtf.Form): + ''' Form to configure the pagure hook. ''' + project_name = wtforms.TextField( + 'Project name on readthedoc.org', + [RequiredIf('active')] + ) + branches = wtforms.TextField( + 'Restrict build to these branches only (coma separated)', + [wtforms.validators.Optional()] + ) + + active = wtforms.BooleanField( + 'Active', + [wtforms.validators.Optional()] + ) + + +class RtdHook(BaseHook): + ''' Read The Doc hook. ''' + + name = 'Read the Doc' + description = 'Kick off a build of the documentation on readthedocs.org.' + form = RtdForm + db_object = RtdTable + backref = 'rtd_hook' + form_fields = ['active', 'project_name', 'branches'] + + @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 + + ''' + repopath = get_repo_path(project) + + hook_files = os.path.join( + os.path.dirname(os.path.realpath(__file__)), 'files') + hook_file = os.path.join(hook_files, 'rtd_hook.py') + + # Init the git repo in case + pygit2.Repository(repopath) + + # Install the hook itself + hook_path = os.path.join( + repopath, 'hooks', 'post-receive.rtd') + hook_file = os.path.join(hook_files, 'rtd_hook.py') + if not os.path.exists(hook_path): + os.symlink(hook_file, hook_path) + + @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 + + ''' + repopath = get_repo_path(project) + hook_path = os.path.join( + repopath, 'hooks', 'post-receive.rtd') + if os.path.exists(hook_path): + os.unlink(hook_path)