#!/usr/bin/python import suds import logging import re import MantisMail import base64 class MantisWS(object): _options = {} _options['default_category']='other' _options['mail_from']='postmaster-mantis@example.net' _options['smtp_host']='127.0.0.1' _options['smtp_port']='25' def __init__(self,url,user,password): self.url = url self.user = user self.password = password try: self.client = suds.client.Client(url) except: logging.error('Error connecting to Mantis WS with URL %s' % url) def add_issue_to_project(self,reporter_email,project_infos,summary,description): issue = self.client.factory.create('IssueData') issue.reporter.email = reporter_email issue.project.name = project_infos['project'] issue.project.id = 0 # Auto-detect if 'category' in project_infos: issue.category = project_infos['category'] else: issue.category = self._options['default_category'] issue.summary = summary issue.description = description try: res = self.client.service.mc_issue_add(self.user,self.password,issue) return res except: logging.error('Problem adding issue') return None def add_note_to_issue(self,issue_id,text): note = self.client.factory.create('IssueNoteData') note.text=text try: res = self.client.service.mc_issue_note_add(self.user,self.password,issue_id,note) return res except: logging.error('Problem adding note to issue %s' % issue_id) return None def add_attachment_to_issue(self,issue_id,attachment): try: res = self.client.service.mc_issue_attachment_add( self.user, self.password, issue_id, attachment['filename'], attachment['type'], base64.b64encode(attachment['content']) ) return res except suds.WebFault, e: logging.error('Problem adding attachment %s to issue %s : %s' % (attachment['filename'],issue_id,e)) err=str(e) if re.search('Duplicate filename',err): raise MantisWSError('duplicate_attachment_filename','Duplicate attachment filename %s in issue %s' % (attachment['filename'],issue_id)) except: logging.error('Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id)) return None def set_option(self,option,value): if option in self._options: self._options[option]=value class MantisWSError(Exception): def __init__(self,type,msg): Exception.__init__(self, msg) self.type = type