From 51c3d077bb8f460c33884dccfa8edee386098789 Mon Sep 17 00:00:00 2001 From: Sayan Chowdhury Date: Jan 11 2016 14:11:52 +0000 Subject: plugins, services: add funcionality to autocloud plugin --- diff --git a/bugyou_plugins/plugins/autocloud/plugin.py b/bugyou_plugins/plugins/autocloud/plugin.py index 9a448fa..e4259e4 100644 --- a/bugyou_plugins/plugins/autocloud/plugin.py +++ b/bugyou_plugins/plugins/autocloud/plugin.py @@ -1,12 +1,62 @@ from bugyou_plugins.plugins.base import BasePlugin +from bugyou_plugins.services.pagure import PagureService class AutocloudPlugin(BasePlugin): def __init__(self, *args, **kwargs): self.plugin_name = 'autocloud' super(AutocloudPlugin, self).__init__(*args, **kwargs) - def process(self, task): - print task + def process(self, msg): + for service in self.services: + getattr(self, 'do_%s'%service.SERVICE)(msg) - def do_pagure(self): - print 'Hello' + def do_pagure(self, msg): + pagure_obj = PagureService(repo_name=self.plugin_name) + + issue_content_templ = """ + The image {image_name} for the release - {release} failed. + The output can be seen here - {output_url} + """ + + output_url_tmpl = "https://apps.fedoraproject.org/autocloud/jobs/{job_id}/output" + + """ + issues = pagure_obj.get_issues() + issue_titles = self._get_issues_titles(issues) + + msg_info = msg['body']['msg'] + topic = msg['body']['topic'] + image_name = msg_info['image_name'] + release = msg_info['release'] + job_id = msg_info['job_id'] + + lookup_key = pagure_obj.lookup_key_tmpl.format(image_name=image_name, + release=release) + + lookup_key_exists = lookup_key in issue_titles + + if 'failed' in topic: + output_url = output_url_tmpl.format(job_id=job) + content = issue_content_template.format(image_name=image_name, + release=release, + output_url=output_url) + + if lookup_key_exists: + matched_issue = (issue for issue in issues + if issue['title'] == loopkup_key).next() + issue_id = matched_issue['id'] + + pagure_obj.update_issue_comment(issue_id=issue_id, + content=content) + + elif 'failed' in topic: + pagure_obj.create_issue(title=lookup_key, content=content) + + if 'success' in topic: + if lookup_key_exists: + matched_issue = (issue for issue in issues + if issue['title'] == lookup_key).next() + issue_id = matched_issue['id'] + + pagure_obj.close_issue(issue_id=issue_id) + """ diff --git a/bugyou_plugins/plugins/base.py b/bugyou_plugins/plugins/base.py index c0dafce..69753f5 100644 --- a/bugyou_plugins/plugins/base.py +++ b/bugyou_plugins/plugins/base.py @@ -18,15 +18,16 @@ class BasePlugin(object): def initialize(self): self.init_retask_connection() - self.init_worker() self.load_services() + self.init_worker() def init_retask_connection(self): """ Connect to the retask queue for the plugin """ self.queue = Queue(self.plugin_name) conn = self.queue.connect() + print 'Init retask connection' if not conn: - log.debug('Could not connect to %s queue' % self.plugin_name) + print 'Could not connect to %s queue' % self.plugin_name return False def consume(self): @@ -42,13 +43,20 @@ class BasePlugin(object): def load_services(self): """ Load the services for the plugin """ services = self.config.get(self.plugin_name, 'services').split(',') + print 'Service', services + print 'Plugin Name', self.plugin_name + print 'Active Services', self.active_services for service in services: + print 'Load', service self.services.append(self.active_services[service].load()) + print self.services @abc.abstractmethod def process(self): """ Consumes the messages from retask """ + return @abc.abstractmethod def do_pagure(self): """ Override to do activity related to pagure """ + return diff --git a/bugyou_plugins/services/base.py b/bugyou_plugins/services/base.py index 9974485..c0d4c38 100644 --- a/bugyou_plugins/services/base.py +++ b/bugyou_plugins/services/base.py @@ -6,6 +6,6 @@ class BaseService(object): filepath = '/etc/bugyou/bugyou_services.cfg' self.config = load_config(filepath) - def get_issues_titles(self): + def _get_issues_titles(self): """ Returns a set of all the issues titles """ return {issue['title'] for issue in self.issues} diff --git a/bugyou_plugins/services/pagure/__init__.py b/bugyou_plugins/services/pagure/__init__.py index 32aeff9..916debb 100644 --- a/bugyou_plugins/services/pagure/__init__.py +++ b/bugyou_plugins/services/pagure/__init__.py @@ -5,13 +5,15 @@ import libpagure class PagureService(BaseService): """ Service for Pagure """ - def __init__(self, *args, **kwargs): + SERVICE = 'pagure' + + def __init__(self, repo_name): """ Initiate the Pagure Service """ - super(PagureService, self).__init__(*args, **kwargs) + super(PagureService, self).__init__() - self.section = 'pagure' - self.access_token = self.config.get(section, 'access_token') - self.repo_name = self.config.get(section, 'repo_name') + self.name = self.SERVICE + self.repo_name = repo_name + self.access_token = self.config.get(self.repo_name, 'access_token') self.project = libpagure.Pagure(pagure_token=self.access_token, pagure_repository=self.repo_name) @@ -20,15 +22,17 @@ class PagureService(BaseService): """ Return list of all the issues in the repo """ self.issues = self.project.list_issues() - def create_issue(self, *args, **kwargs): - """ Creates an issue in the service repo """ - title = kwargs.get('title') - content = kwargs.get('content') + def create_issue(self, title, content, private=False): + """ Creates an issue in the service repo + :args title: Title for the issue + :args content: Content for the issue + :args private: Mark issue as private/public + """ params = { 'title': title, 'content': content, - 'private': False, + 'private': private, } try: @@ -36,10 +40,10 @@ class PagureService(BaseService): except Exception as e: pass - def close_issue(self, *args, **kwargs): - """ Closes the issue in the service repo """ - issue_id = kwargs.get('issue_id') - + def close_issue(self, issue_id): + """ Closes the issue in the service repo + :args issue_id: id of the issue + """ params = { 'issue_id': issue_id, 'new_status': 'Fixed', @@ -50,18 +54,17 @@ class PagureService(BaseService): except Exception as e: pass - def update_issue(self, *args, **kwargs): - """ Updates the issue with a comment """ - - issue_id = kwargs.get('issue_id') - content = kwargs.get('content') - + def update_issue(self, issue_id, content): + """ Updates the issue with a comment + :args issue_id: id of the issue + :args content: content of the comment in the issue + """ params = { 'issue_id': issue_id, 'content': content, } try: self.project.update_issue(**params) - except: + except Exception as e: pass