consumer.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. import fedmsg.consumers
  3. from pagure.hooks import jenkins_hook
  4. import pagure.lib
  5. from pagure.lib import pagure_ci
  6. from pagure.lib.model import BASE, Project, User
  7. from pagure import APP, SESSION
  8. import pagure.exceptions
  9. PAGURE_MAIN_REPO = '{base}{name}.git'
  10. PAGURE_FORK_REPO = '{base}forks/{user}/{name}.git'
  11. class Integrator(fedmsg.consumers.FedmsgConsumer):
  12. ''' Integrates Jenkins with Pagure. '''
  13. topic = [
  14. 'io.pagure.prod.pagure.pull-request.comment.added',
  15. 'io.pagure.prod.pagure.pull-request.new',
  16. 'org.fedoraproject.prod.jenkins.build',
  17. ]
  18. config_key = 'integrator.enabled'
  19. def __init__(self, hub):
  20. super(Integrator, self).__init__(hub)
  21. def consume(self, msg):
  22. ''' Pagure CI consumer which consumes message from
  23. fedmsg and triggers Jenkins build.
  24. '''
  25. topic, msg = msg['topic'], msg['body']
  26. self.log.info("Received %r, %r", topic, msg.get('msg_id', None))
  27. msg = msg['msg']
  28. try:
  29. if topic.endswith('.pull-request.comment.added'):
  30. if is_rebase(msg):
  31. self.trigger_build(msg)
  32. elif topic.endswith('.pull-request.new'):
  33. self.trigger_build(msg)
  34. else:
  35. self.process_build(msg)
  36. except jenkins_hook.ConfigNotFound as exc:
  37. self.log.info('Unconfigured project %r', str(exc))
  38. except pagure.exceptions.HookInactiveException as exc:
  39. self.log.info('Hook Inactive for project %r', str(exc))
  40. def trigger_build(self, msg):
  41. ''' Triggers or requests to start a build in Jenkins. '''
  42. pr_id = msg['pullrequest']['id']
  43. project = msg['pullrequest']['project']['name']
  44. branch = msg['pullrequest']['branch_from']
  45. for cfg in jenkins_hook.get_configs(project, jenkins_hook.Service.PAGURE):
  46. repo = msg['pullrequest'].get('remote_git') or get_repo(cfg, msg)
  47. self.log.info("Trigger on %s PR #%s from %s: %s",
  48. project, pr_id, repo, branch)
  49. pagure_ci.process_pr(self.log, cfg, pr_id, repo, branch)
  50. def process_build(self, msg):
  51. ''' Extracts the information from the build and flag the pull-request. '''
  52. for cfg in jenkins_hook.get_configs(msg['project'], jenkins_hook.Service.JENKINS):
  53. pagure_ci.process_build(self.log, cfg, msg['build'])
  54. def get_repo(cfg, msg):
  55. ''' Formats the URL for pagure repo. '''
  56. url = PAGURE_MAIN_REPO
  57. if msg['pullrequest']['repo_from']['parent']:
  58. url = PAGURE_FORK_REPO
  59. return url.format(
  60. base=APP.config['APP_URL'],
  61. user=msg['pullrequest']['repo_from']['user']['name'],
  62. name=msg['pullrequest']['repo_from']['name'])
  63. def is_rebase(msg):
  64. ''' Returns Rebase if the Pull-request is rebased. '''
  65. if msg['pullrequest']['status'] != 'Open':
  66. return False
  67. try:
  68. return msg['pullrequest']['comments'][-1]['notification']
  69. except (IndexError, KeyError):
  70. return False