diff --git a/alembic/versions/eab41ce5f92a_add_active_commit_attribute_to_the_hook_.py b/alembic/versions/eab41ce5f92a_add_active_commit_attribute_to_the_hook_.py new file mode 100644 index 0000000..69a970a --- /dev/null +++ b/alembic/versions/eab41ce5f92a_add_active_commit_attribute_to_the_hook_.py @@ -0,0 +1,32 @@ +"""add active_commit attribute to the hook_pagure_ci table + +Revision ID: eab41ce5f92a +Revises: e18d5b78d782 +Create Date: 2018-03-21 13:37:24.117434 + +""" + +# revision identifiers, used by Alembic. +revision = 'eab41ce5f92a' +down_revision = 'e18d5b78d782' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ''' Add active_commit column to hook_pagure_ci table''' + op.add_column( + 'hook_pagure_ci', + sa.Column('active_commit', sa.Boolean, nullable=True, default=False) + ) + op.add_column( + 'hook_pagure_ci', + sa.Column('active_pr', sa.Boolean, nullable=True, default=False) + ) + + +def downgrade(): + ''' Revert the active_commit column added''' + op.drop_column('hook_pagure_ci', 'active_commit') + op.drop_column('hook_pagure_ci', 'active_pr') \ No newline at end of file diff --git a/pagure/hooks/files/default_hook.py b/pagure/hooks/files/default_hook.py index 984471f..4694560 100644 --- a/pagure/hooks/files/default_hook.py +++ b/pagure/hooks/files/default_hook.py @@ -116,6 +116,17 @@ def send_notifications(session, project, refname, revs, forced): _log.exception( 'Error sending web-hook notifications on commit push') + if _config.get('PAGURE_CI_SERVICES') \ + and project.ci_hook \ + and project.ci_hook.active_commit \ + and not project.private: + pagure.lib.tasks_services.trigger_ci_build.delay( + project_name=project.fullname, + cause=revs[-1], + branch=refname, + ci_type=project.ci_hook.ci_type + ) + def inform_pull_request_urls( session, project, commits, refname, default_branch): diff --git a/pagure/hooks/pagure_ci.py b/pagure/hooks/pagure_ci.py index 1d61e3a..691b1f8 100644 --- a/pagure/hooks/pagure_ci.py +++ b/pagure/hooks/pagure_ci.py @@ -55,6 +55,8 @@ class PagureCITable(BASE): nullable=True, unique=False) active = sa.Column(sa.Boolean, nullable=False, default=False) + active_commit = sa.Column(sa.Boolean, nullable=False, default=False) + active_pr = sa.Column(sa.Boolean, nullable=False, default=False) project = relation( 'Project', remote_side=[Project.id], @@ -97,6 +99,7 @@ class PagureCiForm(FlaskForm): [RequiredIf('active')], choices=[] ) + ci_url = wtforms.TextField( 'URL to the project on the CI service', [RequiredIf('active'), wtforms.validators.Length(max=255)], @@ -106,8 +109,19 @@ class PagureCiForm(FlaskForm): 'Name of the job to trigger', [RequiredIf('active'), wtforms.validators.Length(max=255)], ) + active = wtforms.BooleanField( - 'Active', + 'Activate Pagure CI service', + [wtforms.validators.Optional()] + ) + + active_commit = wtforms.BooleanField( + 'Trigger CI job on commits', + [wtforms.validators.Optional()] + ) + + active_pr = wtforms.BooleanField( + 'Trigger CI job on pull-requests', [wtforms.validators.Optional()] ) @@ -135,7 +149,8 @@ class PagureCi(BaseHook): form = PagureCiForm db_object = PagureCITable backref = 'ci_hook' - form_fields = ['ci_type', 'ci_url', 'ci_job', 'active'] + form_fields = ['ci_type', 'ci_url', 'ci_job', 'active_commit', 'active_pr', + 'active'] @classmethod def set_up(cls, project): diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index f4e0877..382fa29 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -1257,10 +1257,11 @@ def add_pull_request_comment(session, request, commit, tree_id, filename, and request.status == 'Open' \ and pagure_config.get('PAGURE_CI_SERVICES') \ and request.project.ci_hook \ + and request.project.ci_hook.active_pr \ and not request.project.private: pagure.lib.tasks_services.trigger_ci_build.delay( project_name=request.project_from.fullname, - pr_id=request.id, + cause=request.id, branch=request.branch_from, ci_type=request.project.ci_hook.ci_type ) @@ -1278,10 +1279,11 @@ def add_pull_request_comment(session, request, commit, tree_id, filename, if trigger_ci \ and comment.strip().lower() in trigger_ci \ and pagure_config.get('PAGURE_CI_SERVICES') \ - and request.project.ci_hook: + and request.project.ci_hook \ + and request.project.ci_hook.active_pr: pagure.lib.tasks_services.trigger_ci_build.delay( project_name=request.project_from.fullname, - pr_id=request.id, + cause=request.id, branch=request.branch_from, ci_type=request.project.ci_hook.ci_type ) @@ -1757,10 +1759,11 @@ def new_pull_request(session, branch_from, # Send notification to the CI server if pagure_config.get('PAGURE_CI_SERVICES') \ and request.project.ci_hook \ + and request.project.ci_hook.active_pr \ and not request.project.private: pagure.lib.tasks_services.trigger_ci_build.delay( project_name=request.project_from.fullname, - pr_id=request.id, + cause=request.id, branch=request.branch_from, ci_type=request.project.ci_hook.ci_type ) diff --git a/pagure/lib/lib_ci.py b/pagure/lib/lib_ci.py index eda1dc7..32b4688 100644 --- a/pagure/lib/lib_ci.py +++ b/pagure/lib/lib_ci.py @@ -84,7 +84,7 @@ def process_jenkins_build(session, project, build_id, requestfolder): session.commit() -def trigger_jenkins_build(project_path, url, job, token, branch, pr_id): +def trigger_jenkins_build(project_path, url, job, token, branch, cause): """ Trigger a build on a jenkins instance.""" try: import jenkins @@ -100,7 +100,7 @@ def trigger_jenkins_build(project_path, url, job, token, branch, pr_id): project_path) data = { - 'cause': pr_id, + 'cause': cause, 'REPO': repo, 'BRANCH': branch } diff --git a/pagure/lib/tasks_services.py b/pagure/lib/tasks_services.py index 9248661..e666432 100644 --- a/pagure/lib/tasks_services.py +++ b/pagure/lib/tasks_services.py @@ -333,7 +333,7 @@ def load_json_commits_to_db( @conn.task(queue=pagure_config.get('CI_CELERY_QUEUE', None), bind=True) @pagure_task -def trigger_ci_build(self, session, project_name, pr_id, branch, ci_type): +def trigger_ci_build(self, session, project_name, cause, branch, ci_type): ''' Triggers a new run of the CI system on the specified pull-request. @@ -357,8 +357,8 @@ def trigger_ci_build(self, session, project_name, pr_id, branch, ci_type): _log.info('Pagure-CI: project retrieved: %s', project.fullname) _log.info( - "Pagure-CI: Trigger from %s PR #%s branch: %s", - project.fullname, pr_id, branch) + "Pagure-CI: Trigger from %s cause (PR# or commit) %s branch: %s", + project.fullname, cause, branch) if ci_type == 'jenkins': @@ -376,7 +376,7 @@ def trigger_ci_build(self, session, project_name, pr_id, branch, ci_type): job=job, token=token, branch=branch, - pr_id=pr_id) + cause=cause) else: _log.warning('Pagure-CI:Un-supported CI type')