Zionetrix::Git
Repositories
Help
Report an Issue
mantisbt-smtp-gateway
Code
Commits
Branches
Tags
Search
Tree:
0f36124
Branches
Tags
master
mantisbt-smtp-gateway
src
usr
share
mantis-smtp
MantisWS.py
MantisWS : rename duplicated file on error
Benjamin Renard
commited
0f36124
at 2012-04-20 15:32:11
MantisWS.py
Blame
History
Raw
#!/usr/bin/python import suds import logging import re import MantisMail import base64 import time 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 Exception, e: logging.error('Problem adding issue : %s' % e) 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 suds.WebFault, e: logging.error('Problem adding note to issue %s : %s' % (issue_id,e)) err=str(e) if re.search('Issue .* does not exist.',err): logging.error('The issue %s does not exists' % issue_id) raise MantisWSError('unknown_issue_note_add','The issue %s does not exists' % issue_id) else: raise MantisWSError('general_note_add','Problem adding note to issue %s : e' % (issue_id,e)) return None except Exception, e: logging.error('Problem adding note to issue %s : %s' % (issue_id,e)) raise MantisWSError('general_note_add','Problem adding note to issue %s : e' % (issue_id,e)) 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' % (attachment['filename'],issue_id)) err=str(e) if re.search('Duplicate filename',err): newname="%s-%s" % (time.time(),attachment['filename']) logging.info('Duplicate attachment filename %s in issue %s. Rename it to %s and add again.' % (attachment['filename'],issue_id,newname)) attachment['filename']=newname try: self.add_attachment_to_issue(issue_id,attachment) except Exception, e: raise MantisWSError('general_attachment','Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id)) else: raise MantisWSError('general_attachment','Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id)) except Exception, e: logging.error('Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id)) raise MantisWSError('general_attachment','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