From 26d061f640f797cd4552b68ee9eefde4259c0ec9 Mon Sep 17 00:00:00 2001 From: Farhaan Bukhsh Date: Jul 28 2016 09:11:24 +0000 Subject: [PATCH 1/4] Add docstring for pagure-ci --- diff --git a/pagure/lib/pagure_ci.py b/pagure/lib/pagure_ci.py index 8d29540..4d678a5 100644 --- a/pagure/lib/pagure_ci.py +++ b/pagure/lib/pagure_ci.py @@ -24,10 +24,14 @@ JENKINS_TRIGGER_URL = '{base}job/{project}/buildWithParameters' class HookInactive(Exception): + """To handle when the hook is inactive""" pass def process_pr(logger, cfg, pr_id, repo, branch): + """Function to process the PR, it POST the + data to the Jenkins URL to trigger build + """ if cfg.active: post_data(logger, JENKINS_TRIGGER_URL.format( @@ -41,8 +45,10 @@ def process_pr(logger, cfg, pr_id, repo, branch): def process_build(logger, cfg, build_id): + """Function is used to get the build info + from jenkins and flag that particular PR + """ if cfg.active: - # Get details from Jenkins jenk = jenkins.Jenkins(cfg.jenkins_url) build_info = jenk.get_build_info(cfg.jenkins_name, build_id) result = build_info['result'] @@ -78,6 +84,7 @@ def process_build(logger, cfg, build_id): def post_data(logger, *args, **kwargs): + """POST data to jenkins and reports a response""" resp = requests.post(*args, **kwargs) logger.debug('Received response status %s', resp.status_code) if resp.status_code < 200 or resp.status_code >= 300: @@ -86,6 +93,7 @@ def post_data(logger, *args, **kwargs): def pagure_ci_flag(logger, repo, username, url, result, requestid): + """Flag the pull-request in pagure""" comment, percent = { 'SUCCESS': ('Build successful', 100), diff --git a/pagureCI/consumer.py b/pagureCI/consumer.py index fcdf347..187b894 100644 --- a/pagureCI/consumer.py +++ b/pagureCI/consumer.py @@ -10,6 +10,7 @@ PAGURE_FORK_REPO = '{base}forks/{user}/{name}.git' class Integrator(fedmsg.consumers.FedmsgConsumer): + """Integrates Jenkins with Pagure""" topic = [ 'io.pagure.prod.pagure.pull-request.comment.added', 'io.pagure.prod.pagure.pull-request.new', @@ -23,6 +24,9 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): super(Integrator, self).__init__(hub) def consume(self, msg): + """Pagure CI consumer which consumes message from + fedmsg and triggers Jenkins build. + """ topic, msg = msg['topic'], msg['body'] self.log.info("Received %r, %r", topic, msg.get('msg_id', None)) msg = msg['msg'] @@ -40,6 +44,7 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): self.log.info('Hook Inactive for project %r', str(exc)) def trigger_build(self, msg): + """Triggers or requests to start a build in Jenkins""" pr_id = msg['pullrequest']['id'] project = msg['pullrequest']['project']['name'] branch = msg['pullrequest']['branch_from'] @@ -52,11 +57,13 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): pagure_ci.process_pr(self.log, cfg, pr_id, repo, branch) def process_build(self, msg): + """Extracts the information from the build and flag the pull-request""" for cfg in jenkins_hook.get_configs(msg['project'], jenkins_hook.Service.JENKINS): pagure_ci.process_build(self.log, cfg, msg['build']) def get_repo(cfg, msg): + """Formats the URL for pagure repo""" url = PAGURE_MAIN_REPO if msg['pullrequest']['repo_from']['parent']: url = PAGURE_FORK_REPO @@ -67,10 +74,10 @@ def get_repo(cfg, msg): def is_rebase(msg): + """Returns Rebase if the Pull-request is rebased""" if msg['pullrequest']['status'] != 'Open': return False try: - print msg return msg['pullrequest']['comments'][-1]['notification'] except (IndexError, KeyError): return False From 8195ec3835f995019bd799be693ec923593a7a46 Mon Sep 17 00:00:00 2001 From: Farhaan Bukhsh Date: Jul 28 2016 09:11:24 +0000 Subject: [PATCH 2/4] Move exceptions to pagure exception --- diff --git a/pagure/exceptions.py b/pagure/exceptions.py index 8927952..7c301dc 100644 --- a/pagure/exceptions.py +++ b/pagure/exceptions.py @@ -57,3 +57,8 @@ class GitConflictsException(PagureException): conflicts. ''' pass + + +class HookInactive(PagureException): + """Exception raised then the hook is inactive""" + pass diff --git a/pagure/lib/pagure_ci.py b/pagure/lib/pagure_ci.py index 4d678a5..883c81c 100644 --- a/pagure/lib/pagure_ci.py +++ b/pagure/lib/pagure_ci.py @@ -23,11 +23,6 @@ PAGURE_URL = '{base}api/0/{repo}/pull-request/{pr}/flag' JENKINS_TRIGGER_URL = '{base}job/{project}/buildWithParameters' -class HookInactive(Exception): - """To handle when the hook is inactive""" - pass - - def process_pr(logger, cfg, pr_id, repo, branch): """Function to process the PR, it POST the data to the Jenkins URL to trigger build @@ -41,7 +36,7 @@ def process_pr(logger, cfg, pr_id, repo, branch): 'REPO': repo, 'BRANCH': branch}) else: - raise HookInactive(cfg.pagure_name) + raise pagure.exceptions.HookInactive(cfg.pagure_name) def process_build(logger, cfg, build_id): @@ -80,7 +75,7 @@ def process_build(logger, cfg, build_id): except KeyError as exc: logger.warning('Unknown build status', exc_info=exc) else: - raise HookInactive(cfg.pagure_name) + raise pagure.exceptions.HookInactive(cfg.pagure_name) def post_data(logger, *args, **kwargs): From 07d8099ff79f585457ced0991f35c062ab22f6cf Mon Sep 17 00:00:00 2001 From: Farhaan Bukhsh Date: Jul 28 2016 09:11:24 +0000 Subject: [PATCH 3/4] Fix styling of exception --- diff --git a/pagure/exceptions.py b/pagure/exceptions.py index 7c301dc..db3e7fa 100644 --- a/pagure/exceptions.py +++ b/pagure/exceptions.py @@ -59,6 +59,6 @@ class GitConflictsException(PagureException): pass -class HookInactive(PagureException): - """Exception raised then the hook is inactive""" +class HookInactiveException(PagureException): + ''' Exception raised when the hook is inactive. ''' pass diff --git a/pagure/lib/pagure_ci.py b/pagure/lib/pagure_ci.py index 883c81c..643f533 100644 --- a/pagure/lib/pagure_ci.py +++ b/pagure/lib/pagure_ci.py @@ -24,9 +24,9 @@ JENKINS_TRIGGER_URL = '{base}job/{project}/buildWithParameters' def process_pr(logger, cfg, pr_id, repo, branch): - """Function to process the PR, it POST the - data to the Jenkins URL to trigger build - """ + ''' Function to process the PR, it POST the + data to the Jenkins URL to trigger build. + ''' if cfg.active: post_data(logger, JENKINS_TRIGGER_URL.format( @@ -36,13 +36,13 @@ def process_pr(logger, cfg, pr_id, repo, branch): 'REPO': repo, 'BRANCH': branch}) else: - raise pagure.exceptions.HookInactive(cfg.pagure_name) + raise pagure.exceptions.HookInactiveException(cfg.pagure_name) def process_build(logger, cfg, build_id): - """Function is used to get the build info - from jenkins and flag that particular PR - """ + ''' Function is used to get the build info + from jenkins and flag that particular pull-request. + ''' if cfg.active: jenk = jenkins.Jenkins(cfg.jenkins_url) build_info = jenk.get_build_info(cfg.jenkins_name, build_id) @@ -75,11 +75,11 @@ def process_build(logger, cfg, build_id): except KeyError as exc: logger.warning('Unknown build status', exc_info=exc) else: - raise pagure.exceptions.HookInactive(cfg.pagure_name) + raise pagure.exceptions.HookInactiveException(cfg.pagure_name) def post_data(logger, *args, **kwargs): - """POST data to jenkins and reports a response""" + ''' POST data to jenkins and reports a response. ''' resp = requests.post(*args, **kwargs) logger.debug('Received response status %s', resp.status_code) if resp.status_code < 200 or resp.status_code >= 300: @@ -88,7 +88,7 @@ def post_data(logger, *args, **kwargs): def pagure_ci_flag(logger, repo, username, url, result, requestid): - """Flag the pull-request in pagure""" + ''' Flag the pull-request in pagure. ''' comment, percent = { 'SUCCESS': ('Build successful', 100), diff --git a/pagureCI/consumer.py b/pagureCI/consumer.py index 187b894..ea97f30 100644 --- a/pagureCI/consumer.py +++ b/pagureCI/consumer.py @@ -5,12 +5,13 @@ import pagure.lib from pagure.lib import pagure_ci from pagure.lib.model import BASE, Project, User from pagure import APP, SESSION +import pagure.exceptions PAGURE_MAIN_REPO = '{base}{name}.git' PAGURE_FORK_REPO = '{base}forks/{user}/{name}.git' class Integrator(fedmsg.consumers.FedmsgConsumer): - """Integrates Jenkins with Pagure""" + ''' Integrates Jenkins with Pagure. ''' topic = [ 'io.pagure.prod.pagure.pull-request.comment.added', 'io.pagure.prod.pagure.pull-request.new', @@ -24,9 +25,9 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): super(Integrator, self).__init__(hub) def consume(self, msg): - """Pagure CI consumer which consumes message from + ''' Pagure CI consumer which consumes message from fedmsg and triggers Jenkins build. - """ + ''' topic, msg = msg['topic'], msg['body'] self.log.info("Received %r, %r", topic, msg.get('msg_id', None)) msg = msg['msg'] @@ -40,11 +41,11 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): self.process_build(msg) except jenkins_hook.ConfigNotFound as exc: self.log.info('Unconfigured project %r', str(exc)) - except pagure_ci.HookInactive as exc: + except pagure.exceptions.HookInactiveException as exc: self.log.info('Hook Inactive for project %r', str(exc)) def trigger_build(self, msg): - """Triggers or requests to start a build in Jenkins""" + ''' Triggers or requests to start a build in Jenkins. ''' pr_id = msg['pullrequest']['id'] project = msg['pullrequest']['project']['name'] branch = msg['pullrequest']['branch_from'] @@ -57,13 +58,13 @@ class Integrator(fedmsg.consumers.FedmsgConsumer): pagure_ci.process_pr(self.log, cfg, pr_id, repo, branch) def process_build(self, msg): - """Extracts the information from the build and flag the pull-request""" + ''' Extracts the information from the build and flag the pull-request. ''' for cfg in jenkins_hook.get_configs(msg['project'], jenkins_hook.Service.JENKINS): pagure_ci.process_build(self.log, cfg, msg['build']) def get_repo(cfg, msg): - """Formats the URL for pagure repo""" + ''' Formats the URL for pagure repo. ''' url = PAGURE_MAIN_REPO if msg['pullrequest']['repo_from']['parent']: url = PAGURE_FORK_REPO @@ -74,7 +75,7 @@ def get_repo(cfg, msg): def is_rebase(msg): - """Returns Rebase if the Pull-request is rebased""" + ''' Returns Rebase if the Pull-request is rebased. ''' if msg['pullrequest']['status'] != 'Open': return False try: From c0f8b472f9c140d569b2a75c5edf48d1009a28bc Mon Sep 17 00:00:00 2001 From: Farhaan Bukhsh Date: Jul 28 2016 09:11:24 +0000 Subject: [PATCH 4/4] Fix plugin test --- diff --git a/tests/test_pagure_flask_ui_plugins.py b/tests/test_pagure_flask_ui_plugins.py index 90d80be..bda5f7f 100644 --- a/tests/test_pagure_flask_ui_plugins.py +++ b/tests/test_pagure_flask_ui_plugins.py @@ -71,7 +71,7 @@ class PagureFlaskPluginstests(tests.Modeltests): sorted(names), [ 'Block Un-Signed commits', 'Block non fast-forward pushes', - 'Fedmsg', 'IRC', 'Mail', 'Pagure', 'Pagure requests', + 'Fedmsg', 'IRC', 'Mail', 'Pagure', 'Pagure CI','Pagure requests', 'Pagure tickets', 'Read the Doc', ] )