From 613b67ba7e45d1a3ba40f3e075740d0a653172d5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 08 2018 09:07:10 +0000 Subject: Update the RTD integration to rely on the new /v2/ API We're now asking the users for: - an API URL provided by RTD - an API token provided by RTD - a list of branches that should trigger a build Fixes https://pagure.io/pagure/issue/2522 Signed-off-by: Pierre-Yves Chibon --- diff --git a/alembic/versions/22fb5256f555_update_rtd_table.py b/alembic/versions/22fb5256f555_update_rtd_table.py new file mode 100644 index 0000000..dfddda4 --- /dev/null +++ b/alembic/versions/22fb5256f555_update_rtd_table.py @@ -0,0 +1,39 @@ +"""Update RTD table + +Revision ID: 22fb5256f555 +Revises: 5affe6f5d94f +Create Date: 2018-03-07 15:46:26.478238 + +""" + +# revision identifiers, used by Alembic. +revision = '22fb5256f555' +down_revision = '5affe6f5d94f' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ''' Update the hook_rtd table for the new data structure it should use. + ''' + op.add_column( + 'hook_rtd', + sa.Column('api_url', sa.Text, nullable=True) + ) + op.add_column( + 'hook_rtd', + sa.Column('api_token', sa.Text, nullable=True) + ) + op.drop_column('hook_rtd', 'project_name') + + +def downgrade(): + ''' Downgrade the structure of the hook_rtd table. + ''' + op.drop_column('hook_rtd', 'api_url') + op.drop_column('hook_rtd', 'api_token') + op.add_column( + 'hook_rtd', + sa.Column('project_name', sa.Text, nullable=True) + ) diff --git a/pagure/hooks/files/rtd_hook.py b/pagure/hooks/files/rtd_hook.py index c2514cd..e4fab00 100755 --- a/pagure/hooks/files/rtd_hook.py +++ b/pagure/hooks/files/rtd_hook.py @@ -58,9 +58,17 @@ def run_as_post_receive_hook(): for branch in branches if branch] - url = 'http://readthedocs.org/build/%s' % ( - repo.rtd_hook.project_name.strip() - ) + url = repo.rtd_hook.api_url + if not url: + print('No API url specified to trigger the build, please update ' + 'the configuration') + session.close() + return 1 + if not repo.rtd_hook.api_token: + print('No API token specified to trigger the build, please update ' + 'the configuration') + session.close() + return 1 for line in sys.stdin: if _config.get('HOOK_DEBUG', False): @@ -70,13 +78,25 @@ def run_as_post_receive_hook(): refname = refname.replace('refs/heads/', '') if branches: if refname in branches: - print('Starting RTD build for %s' % ( - repo.rtd_hook.project_name.strip())) - requests.post(url) + print('Starting RTD build at %s' % (url) + requests.post( + url, + data={ + 'branches': refname, + 'token'=repo.rtd_hook.api_token + }, + timeout=60, + ) else: - print('Starting RTD build for %s' % ( - repo.rtd_hook.project_name.strip())) - requests.post(url) + print('Starting RTD build at %s' % (url) + requests.post( + url, + data={ + 'branches': refname, + 'token'=repo.rtd_hook.api_token + }, + timeout=60, + ) session.close() diff --git a/pagure/hooks/rtd.py b/pagure/hooks/rtd.py index b3b25ee..9a61115 100644 --- a/pagure/hooks/rtd.py +++ b/pagure/hooks/rtd.py @@ -43,8 +43,9 @@ class RtdTable(BASE): active = sa.Column(sa.Boolean, nullable=False, default=False) - project_name = sa.Column(sa.Text, nullable=False) branches = sa.Column(sa.Text, nullable=True) + api_url = sa.Column(sa.Text, nullable=False) + api_token = sa.Column(sa.Text, nullable=False) project = relation( 'Project', remote_side=[Project.id], @@ -56,9 +57,13 @@ class RtdTable(BASE): class RtdForm(FlaskForm): ''' Form to configure the pagure hook. ''' - project_name = wtforms.TextField( - 'Project name on readthedocs.org', - [RequiredIf('active')] + api_url = wtforms.TextField( + 'URL endpoint used to trigger the builds', + [wtforms.validators.Optional()] + ) + api_token = wtforms.TextField( + 'API token provided by readthedocs', + [wtforms.validators.Optional()] ) branches = wtforms.TextField( 'Restrict build to these branches only (comma separated)', @@ -71,15 +76,32 @@ class RtdForm(FlaskForm): ) +DESCRIPTION = ''' +Git hook to trigger building documentation on the readthedocs.org service +when a commit is pushed to the repository. + +If you specify one or more branches (using commas `,` to separate them) only +pushes made to these branches will trigger a new build of the documentation. + +To set up this hook, you will need to login to https://readthedocs.org/ +Go to your project's adming settings, and in the ``Integrations`` section +add a new ``Generic API incoming webhook``. + +This will give you access to one URL and one API token, both of which you +will have to provide below. + +''' + + class RtdHook(BaseHook): ''' Read The Doc hook. ''' name = 'Read the Doc' - description = 'Kick off a build of the documentation on readthedocs.org.' + description = DESCRIPTION form = RtdForm db_object = RtdTable backref = 'rtd_hook' - form_fields = ['active', 'project_name', 'branches'] + form_fields = ['active', 'api_url', 'api_token', 'branches'] @classmethod def install(cls, project, dbobj):