From 359776021519e744698a07eaaa961c4c39da5113 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Mar 01 2018 08:11:56 +0000 Subject: Cleanup logic used to trigger the pull-requests Signed-off-by: Clement Verna --- diff --git a/app/webhooks.py b/app/webhooks.py index 32cc36b..68bc84d 100644 --- a/app/webhooks.py +++ b/app/webhooks.py @@ -30,7 +30,8 @@ def openshift_generic(): if is_signature_valid(webhook_key, signature, data): topic = request.headers.get('X-Pagure-Topic') - log.debug('Webhook received : Repo: {}, Topic: {}'.format(project_fullname, topic)) + log.debug('Webhook received : Repo: {}, Topic: {}' + .format(project_fullname, topic)) if topic == 'git.receive': url = 'https://{cluster}/oapi/v1/namespaces/{project}/buildconfigs'\ @@ -101,7 +102,8 @@ def openshift_jenkins(): if is_signature_valid(webhook_key, signature, data): topic = request.headers.get('X-Pagure-Topic') - log.debug('Webhook received : Repo: {}, Topic: {}'.format(project_fullname, topic)) + log.debug('Webhook received : Repo: {}, Topic: {}' + .format(project_fullname, topic)) if topic in ['git.receive', 'pull-request.new', 'pull-request.comment.added', 'pull-request.closed']: @@ -119,24 +121,30 @@ def openshift_jenkins(): if 'pull-request' in topic: - pr_comments = json_data['msg']['pullrequest'].get('comments', None) + data['PR'] = json_data['msg']['pullrequest']['id'] + data['BRANCH'] = json_data['msg']['pullrequest']['branch_from'] + jobname = current_app.config['JENKINS_JOB_PR'] - # Following logic is used to retrigger a build on PR updates. - # The update is seen as a comment with the notification field set to - # True. Also we should make sure the status is of the PR is Open. - if pr_comments and not pr_comments[0].get('notification', False)\ - and not json_data['msg']['pullrequest']['status'] == 'Open': - log.debug('Did not receive a notification ! Bye'.format(topic)) + if 'comment.added' in topic: - return jsonify({}), 200 + pr_comments = json_data['msg']['pullrequest'].get('comments', None) - data['PR'] = json_data['msg']['pullrequest']['id'] - data['BRANCH'] = json_data['msg']['pullrequest']['branch_from'] - if json_data['msg'].get('merged') is not None: - data['STATUS'] = 'MERGED' if json_data['msg']['merged'] else 'CLOSED' + if json_data['msg']['pullrequest']['status'] != 'Open': + log.debug('PR is not Open no need to trigger ! Bye'.format(topic)) + return jsonify({}), 200 + + if not pr_comments\ + or pr_comments[-1].get('notification', False) is False: + log.debug('Did not receive a notification ! Bye'.format(topic)) + return jsonify({}), 200 + + elif 'closed' in topic: + + if json_data['msg'].get('merged') is not None: + data['STATUS'] = \ + 'MERGED' if json_data['msg']['merged'] else 'CLOSED' else: data['STATUS'] = 'OPEN' - jobname = current_app.config['JENKINS_JOB_PR'] else: