diff -r 2266d6d73de3 -r d67cc4fbc3f1 bt.py --- a/bt.py Sun Nov 09 19:59:10 2014 +0200 +++ b/bt.py Mon Nov 10 02:06:06 2014 +0200 @@ -1,5 +1,9 @@ import suds -import cobalt +import sys +import time +import re +import irc as Irc +from configfile import Config suds_active = False btannounce_active = False @@ -8,7 +12,54 @@ def is_active(): return suds_active +def get_ticket_data (bot, replyto, ticket, withlink): + if suds_active == False: + print "suds is not active" + return + + data = {} + try: + data = get_issue (ticket) + except Exception, e: + bot.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`)) + + if data: + if data['view_state']['name'] == 'private': + allowprivate = False + + for channel in bot.channels: + if channel.get_value ('name') == replyto and channel.get_value ('btprivate', False): + allowprivate = True + break + + if not allowprivate: + bot.privmsg (replyto, 'Error: ticket %s is private' % ticket) + return + + bot.privmsg (replyto, "Issue %s: %s: Reporter: %s, assigned to: %s, status: %s (%s)" % \ + (ticket, \ + data.summary, \ + data.reporter.name if hasattr (data.reporter, 'name') else "", \ + data.handler.name if hasattr (data, 'handler') else "nobody", \ + data.status.name, \ + data.resolution.name)) + + if withlink: + bot.privmsg (replyto, "Read all about it here: " + get_ticket_url (ticket)) + +def process_message (bot, line, replyto): + # Check for tracker url in the message + url = Config.get_node ('bt').get_value ('url') + http_regex = re.compile (r'.*http(s?)://%s/view\.php\?id=([0-9]+).*' % url) + http_match = http_regex.match (line) + + if http_match: + get_ticket_data (bot, replyto, http_match.group (2), False) + def init(): + global suds_active + global suds_client + try: print 'Initializing MantisBT connection...' suds_import = suds.xsd.doctor.Import ('http://schemas.xmlsoap.org/soap/encoding/', 'http://schemas.xmlsoap.org/soap/encoding/') @@ -20,10 +71,10 @@ if suds_active: sys.stdout.write ('Retrieving latest tracker ticket... ') - user, password = bt_credentials() + user, password = credentials() btannounce_id = suds_client.service.mc_issue_get_biggest_id (user, password, 0) btannounce_active = True - bt_updatechecktimeout() + update_checktimeout() print btannounce_id def update_checktimeout(): @@ -36,9 +87,9 @@ password = bt.get_value ('password', '') return [user, password] -def get_issue(ticket): +def get_issue (ticket): global suds_client - user, password = bt_credentials() + user, password = credentials() return suds_client.service.mc_issue_get (user, password, ticket) def poll(): @@ -46,10 +97,10 @@ global btannounce_id if time.time() >= btannounce_timeout: - bt_updatechecktimeout() + update_checktimeout() newid = btannounce_id try: - user, password = bt_credentials() + user, password = credentials() newid = suds_client.service.mc_issue_get_biggest_id (user, password, 0) except Exception as e: pass @@ -57,10 +108,10 @@ while newid > btannounce_id: try: btannounce_id += 1 - data = bt_getissue (btannounce_id) - - for client in cobalt.all_clients: - announce_new_ticket (client, data) + data = get_issue (btannounce_id) + + for client in Irc.all_clients: + announce_new_issue (client, data) except Exception as e: pass @@ -68,7 +119,6 @@ url = Config.get_node ('bt').get_value ('url') return 'https://%s/view.php?id=%s' % (url, ticket) - # # Print a ticket announce to appropriate channels # @@ -84,8 +134,17 @@ if channel.get_value ('btannounce', False): if not isprivate or (channel.get_value ('btprivate', False)): self.write ("PRIVMSG %s :[%s] New issue %s, reported by %s: %s: %s" % \ - (channel['name'], data['project']['name'], idstring, reporter, - data['summary'], self.get_ticket_url (idstring))) - #fi - #fi - #done + (channel.get_value ('name'), + data['project']['name'], + idstring, + reporter, + data['summary'], + get_ticket_url (idstring))) + +def update_issue (ticket_id, ticket_data): + btuser, btpassword = credentials() + suds_client.service.mc_issue_update (btuser, btpassword, ticket_id, ticket_data) + +def post_note (ticket_id, message): + btuser, btpassword = credentials() + suds_client.service.mc_issue_note_add (btuser, btpassword, ticket_id, { 'text': message })