0f3612474315c17a24029d5e69b3ef4df5673e0e
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

1) #!/usr/bin/python
2) 
3) import suds
4) import logging
5) import re
6) import MantisMail
Benjamin Renard Mail & WS : Decode base64 a...

Benjamin Renard authored 12 years ago

7) import base64
Benjamin Renard MantisWS : rename duplicate...

Benjamin Renard authored 12 years ago

8) import time
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

9) 
10) class MantisWS(object):
11) 
12) 	_options = {}
13) 	_options['default_category']='other'
14)         _options['mail_from']='postmaster-mantis@example.net'
15)         _options['smtp_host']='127.0.0.1'
16)         _options['smtp_port']='25'
17) 
18) 	def __init__(self,url,user,password):
19) 		self.url = url
20) 		self.user = user
21) 		self.password = password
22) 
23) 		try:
24) 			self.client = suds.client.Client(url)
25) 		except:
26) 			logging.error('Error connecting to Mantis WS with URL %s' % url)
27) 
28) 	def add_issue_to_project(self,reporter_email,project_infos,summary,description):
29) 		issue = self.client.factory.create('IssueData')
30) 		issue.reporter.email = reporter_email
31) 		issue.project.name = project_infos['project']
32) 		issue.project.id = 0 # Auto-detect
33) 
34) 		if 'category' in project_infos:
35) 			issue.category = project_infos['category']
36) 		else:
37) 			issue.category = self._options['default_category']
38) 		issue.summary = summary
39) 		issue.description = description
40) 		try:
41) 			res = self.client.service.mc_issue_add(self.user,self.password,issue)
42) 			return res
Benjamin Renard MantisWS : Increase error l...

Benjamin Renard authored 12 years ago

43) 		except Exception, e:
44) 			logging.error('Problem adding issue : %s' % e)
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

45) 			return None
46) 
47) 	def add_note_to_issue(self,issue_id,text):
48) 		note = self.client.factory.create('IssueNoteData')
49) 		note.text=text
50) 
51) 		try:
52) 			res = self.client.service.mc_issue_note_add(self.user,self.password,issue_id,note)
53) 			return res
Benjamin Renard MantisWS : improve error lo...

Benjamin Renard authored 12 years ago

54) 		except suds.WebFault, e:
55) 			logging.error('Problem adding note to issue %s : %s' % (issue_id,e))
56) 			err=str(e)
57) 			if re.search('Issue .* does not exist.',err):
58) 				logging.error('The issue %s does not exists' % issue_id)
59) 				raise MantisWSError('unknown_issue_note_add','The issue %s does not exists' % issue_id)
60) 			else:
61) 				raise MantisWSError('general_note_add','Problem adding note to issue %s : e' % (issue_id,e))
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

62) 			return None
Benjamin Renard MantisWS : improve error lo...

Benjamin Renard authored 12 years ago

63) 		except Exception, e:
64) 			logging.error('Problem adding note to issue %s : %s' % (issue_id,e))
65) 			raise MantisWSError('general_note_add','Problem adding note to issue %s : e' % (issue_id,e))
66) 			return None
67) 
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

68) 	def add_attachment_to_issue(self,issue_id,attachment):
69) 		try:
70) 			res = self.client.service.mc_issue_attachment_add(
71) 				self.user,
72) 				self.password,
73) 				issue_id,
74) 				attachment['filename'],
75) 				attachment['type'],
Benjamin Renard Mail & WS : Decode base64 a...

Benjamin Renard authored 12 years ago

76) 				base64.b64encode(attachment['content'])
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

77) 			)
78) 			return res
79) 		except suds.WebFault, e:
Benjamin Renard MantisWS : improve error lo...

Benjamin Renard authored 12 years ago

80) 			logging.error('Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id))
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

81) 			err=str(e)
82) 			if re.search('Duplicate filename',err):
Benjamin Renard MantisWS : rename duplicate...

Benjamin Renard authored 12 years ago

83) 				newname="%s-%s" % (time.time(),attachment['filename'])
84) 				logging.info('Duplicate attachment filename %s in issue %s. Rename it to %s and add again.' % (attachment['filename'],issue_id,newname))
85) 				attachment['filename']=newname
86) 				try:
87) 					self.add_attachment_to_issue(issue_id,attachment)
88) 				except Exception, e:
89) 					raise MantisWSError('general_attachment','Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id))
90) 			else:
91) 				raise MantisWSError('general_attachment','Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id))
92) 		except Exception, e:
Benjamin Renard Initial commit

Benjamin Renard authored 12 years ago

93) 			logging.error('Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id))
Benjamin Renard MantisWS : improve error lo...

Benjamin Renard authored 12 years ago

94) 			raise MantisWSError('general_attachment','Problem adding attachment %s to issue %s' % (attachment['filename'],issue_id))