|
farhaanbukhsh |
fa5ca9 |
# -*- coding: utf-8 -*-
|
|
farhaanbukhsh |
fa5ca9 |
import fedmsg.consumers
|
|
farhaanbukhsh |
fa5ca9 |
from pagure.hooks import jenkins_hook
|
|
farhaanbukhsh |
1a3870 |
import pagure.lib
|
|
farhaanbukhsh |
fa5ca9 |
from pagure.lib import pagure_ci
|
|
farhaanbukhsh |
fa5ca9 |
from pagure.lib.model import BASE, Project, User
|
|
farhaanbukhsh |
19c574 |
from pagure import APP, SESSION
|
|
Farhaan Bukhsh |
07d809 |
import pagure.exceptions
|
|
farhaanbukhsh |
fa5ca9 |
PAGURE_MAIN_REPO = '{base}{name}.git'
|
|
farhaanbukhsh |
fa5ca9 |
PAGURE_FORK_REPO = '{base}forks/{user}/{name}.git'
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
class Integrator(fedmsg.consumers.FedmsgConsumer):
|
|
Farhaan Bukhsh |
07d809 |
''' Integrates Jenkins with Pagure. '''
|
|
farhaanbukhsh |
fa5ca9 |
topic = [
|
|
farhaanbukhsh |
fa5ca9 |
'io.pagure.prod.pagure.pull-request.comment.added',
|
|
farhaanbukhsh |
fa5ca9 |
'io.pagure.prod.pagure.pull-request.new',
|
|
farhaanbukhsh |
fa5ca9 |
'org.fedoraproject.prod.jenkins.build',
|
|
farhaanbukhsh |
fa5ca9 |
]
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
config_key = 'integrator.enabled'
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
1a3870 |
|
|
farhaanbukhsh |
fa5ca9 |
def __init__(self, hub):
|
|
farhaanbukhsh |
fa5ca9 |
super(Integrator, self).__init__(hub)
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
def consume(self, msg):
|
|
Farhaan Bukhsh |
07d809 |
''' Pagure CI consumer which consumes message from
|
|
Farhaan Bukhsh |
26d061 |
fedmsg and triggers Jenkins build.
|
|
Farhaan Bukhsh |
07d809 |
'''
|
|
farhaanbukhsh |
fa5ca9 |
topic, msg = msg['topic'], msg['body']
|
|
farhaanbukhsh |
fa5ca9 |
self.log.info("Received %r, %r", topic, msg.get('msg_id', None))
|
|
farhaanbukhsh |
fa5ca9 |
msg = msg['msg']
|
|
farhaanbukhsh |
fa5ca9 |
try:
|
|
farhaanbukhsh |
fa5ca9 |
if topic.endswith('.pull-request.comment.added'):
|
|
farhaanbukhsh |
fa5ca9 |
if is_rebase(msg):
|
|
farhaanbukhsh |
fa5ca9 |
self.trigger_build(msg)
|
|
farhaanbukhsh |
fa5ca9 |
elif topic.endswith('.pull-request.new'):
|
|
farhaanbukhsh |
fa5ca9 |
self.trigger_build(msg)
|
|
farhaanbukhsh |
fa5ca9 |
else:
|
|
farhaanbukhsh |
fa5ca9 |
self.process_build(msg)
|
|
farhaanbukhsh |
fa5ca9 |
except jenkins_hook.ConfigNotFound as exc:
|
|
farhaanbukhsh |
fa5ca9 |
self.log.info('Unconfigured project %r', str(exc))
|
|
Farhaan Bukhsh |
07d809 |
except pagure.exceptions.HookInactiveException as exc:
|
|
Farhaan Bukhsh |
c5c8d4 |
self.log.info('Hook Inactive for project %r', str(exc))
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
def trigger_build(self, msg):
|
|
Farhaan Bukhsh |
07d809 |
''' Triggers or requests to start a build in Jenkins. '''
|
|
farhaanbukhsh |
fa5ca9 |
pr_id = msg['pullrequest']['id']
|
|
farhaanbukhsh |
fa5ca9 |
project = msg['pullrequest']['project']['name']
|
|
farhaanbukhsh |
fa5ca9 |
branch = msg['pullrequest']['branch_from']
|
|
farhaanbukhsh |
ebf17a |
|
|
farhaanbukhsh |
fa5ca9 |
for cfg in jenkins_hook.get_configs(project, jenkins_hook.Service.PAGURE):
|
|
farhaanbukhsh |
fa5ca9 |
repo = msg['pullrequest'].get('remote_git') or get_repo(cfg, msg)
|
|
farhaanbukhsh |
fa5ca9 |
self.log.info("Trigger on %s PR #%s from %s: %s",
|
|
farhaanbukhsh |
fa5ca9 |
project, pr_id, repo, branch)
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
pagure_ci.process_pr(self.log, cfg, pr_id, repo, branch)
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
def process_build(self, msg):
|
|
Farhaan Bukhsh |
07d809 |
''' Extracts the information from the build and flag the pull-request. '''
|
|
farhaanbukhsh |
fa5ca9 |
for cfg in jenkins_hook.get_configs(msg['project'], jenkins_hook.Service.JENKINS):
|
|
farhaanbukhsh |
fa5ca9 |
pagure_ci.process_build(self.log, cfg, msg['build'])
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
def get_repo(cfg, msg):
|
|
Farhaan Bukhsh |
07d809 |
''' Formats the URL for pagure repo. '''
|
|
farhaanbukhsh |
fa5ca9 |
url = PAGURE_MAIN_REPO
|
|
farhaanbukhsh |
fa5ca9 |
if msg['pullrequest']['repo_from']['parent']:
|
|
farhaanbukhsh |
fa5ca9 |
url = PAGURE_FORK_REPO
|
|
farhaanbukhsh |
fa5ca9 |
return url.format(
|
|
farhaanbukhsh |
1a3870 |
base=APP.config['APP_URL'],
|
|
farhaanbukhsh |
fa5ca9 |
user=msg['pullrequest']['repo_from']['user']['name'],
|
|
farhaanbukhsh |
fa5ca9 |
name=msg['pullrequest']['repo_from']['name'])
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
|
|
farhaanbukhsh |
fa5ca9 |
def is_rebase(msg):
|
|
Farhaan Bukhsh |
07d809 |
''' Returns Rebase if the Pull-request is rebased. '''
|
|
farhaanbukhsh |
fa5ca9 |
if msg['pullrequest']['status'] != 'Open':
|
|
farhaanbukhsh |
fa5ca9 |
return False
|
|
farhaanbukhsh |
fa5ca9 |
try:
|
|
farhaanbukhsh |
fa5ca9 |
return msg['pullrequest']['comments'][-1]['notification']
|
|
farhaanbukhsh |
fa5ca9 |
except (IndexError, KeyError):
|
|
farhaanbukhsh |
fa5ca9 |
return False
|