diff -r f8cc57c608e2 -r 2266d6d73de3 bt.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bt.py Sun Nov 09 19:59:10 2014 +0200 @@ -0,0 +1,91 @@ +import suds +import cobalt + +suds_active = False +btannounce_active = False +btannounce_timeout = 0 + +def is_active(): + return suds_active + +def init(): + try: + print 'Initializing MantisBT connection...' + suds_import = suds.xsd.doctor.Import ('http://schemas.xmlsoap.org/soap/encoding/', 'http://schemas.xmlsoap.org/soap/encoding/') + suds_client = suds.client.Client ('https://zandronum.com/tracker/api/soap/mantisconnect.php?wsdl', plugins=[suds.xsd.doctor.ImportDoctor (suds_import)]) + suds_active = True + except Exception as e: + print 'Failed to establish MantisBT connection: ' + `e` + pass + + if suds_active: + sys.stdout.write ('Retrieving latest tracker ticket... ') + user, password = bt_credentials() + btannounce_id = suds_client.service.mc_issue_get_biggest_id (user, password, 0) + btannounce_active = True + bt_updatechecktimeout() + print btannounce_id + +def update_checktimeout(): + global btannounce_timeout + btannounce_timeout = time.time() + (Config.get_node ('bt').get_value ('checkinterval', default=5) * 60) + +def credentials(): + bt = Config.get_node ('bt') + user = bt.get_value ('username', '') + password = bt.get_value ('password', '') + return [user, password] + +def get_issue(ticket): + global suds_client + user, password = bt_credentials() + return suds_client.service.mc_issue_get (user, password, ticket) + +def poll(): + global btannounce_timeout + global btannounce_id + + if time.time() >= btannounce_timeout: + bt_updatechecktimeout() + newid = btannounce_id + try: + user, password = bt_credentials() + newid = suds_client.service.mc_issue_get_biggest_id (user, password, 0) + except Exception as e: + pass + + while newid > btannounce_id: + try: + btannounce_id += 1 + data = bt_getissue (btannounce_id) + + for client in cobalt.all_clients: + announce_new_ticket (client, data) + except Exception as e: + pass + +def get_ticket_url (ticket): + url = Config.get_node ('bt').get_value ('url') + return 'https://%s/view.php?id=%s' % (url, ticket) + + +# +# Print a ticket announce to appropriate channels +# +def announce_new_issue (bot, data): + idstring = "%d" % data.id + while len(idstring) < 7: + idstring = "0" + idstring + + isprivate = data['view_state']['name'] == 'private' + reporter = data['reporter']['name'] if hasattr (data['reporter'], 'name') else '' + + for channel in self.channels: + 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