annotate src/plugins/plugin_tickets_import_bugzilla.py @ 2381:72c30e73a9a5

plugin schema: use new "prepend" argument to put "id" widget first.
author Goffi <goffi@goffi.org>
date Mon, 16 Oct 2017 07:36:41 +0200
parents d2476dc2d55d
children a49a19f06e38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2373
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT plugin for import external blogs
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 # from twisted.internet import threads
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.internet import defer
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import os.path
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from lxml import etree
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 PLUGIN_INFO = {
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_NAME: "Bugzilla import",
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_IMPORT_NAME: "IMPORT_BUGZILLA",
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_TYPE: C.PLUG_TYPE_BLOG,
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_DEPENDENCIES: ["TICKETS_IMPORT"],
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MAIN: "BugzillaImport",
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_HANDLER: "no",
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DESCRIPTION: _("""Tickets importer for Bugzilla""")
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 SHORT_DESC = D_(u"import tickets from Bugzilla xml export file")
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 LONG_DESC = D_(u"""This importer handle Bugzilla xml export file.
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 To use it, you'll need to export tickets using XML.
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 Tickets will be uploaded with the same ID as for Bugzilla, any existing ticket with this ID will be replaced.
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 location: you must use the absolute path to your .xml file
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 class BugzillaParser(object):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def parse(self, file_path):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 tickets = []
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 root = etree.parse(file_path)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 for bug in root.xpath('bug'):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 ticket = {}
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 ticket['id'] = bug.findtext('bug_id')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 ticket['creation'] = bug.findtext('creation_ts')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 ticket['update'] = bug.findtext('delta_ts')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 ticket['title'] = bug.findtext('short_desc')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 reporter_elt = bug.find('reporter')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 ticket['reporter_name'] = reporter_elt.get('name')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 ticket['reporter_email'] = reporter_elt.text
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 assigned_to_elt = bug.find('assigned_to')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 ticket['assigned_to_name'] = assigned_to_elt.get('name')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 ticket['assigned_to_email'] = assigned_to_elt.text
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 ticket['cc_emails'] = [e.text for e in bug.findall('cc')]
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 ticket['priority'] = bug.findtext('priority')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 ticket['severity'] = bug.findtext('bug_severity')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 ticket['product'] = bug.findtext('product')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 ticket['component'] = bug.findtext('component')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 ticket['version'] = bug.findtext('version')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 ticket['platform'] = bug.findtext('rep_platform')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 ticket['os'] = bug.findtext('op_sys')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 ticket['status'] = bug.findtext('bug_status')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 ticket['milestone'] = bug.findtext('target_milestone')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 body = None
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 comments = []
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 for longdesc in bug.findall('long_desc'):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if body is None:
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 body = longdesc.findtext('thetext')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 else:
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 who = longdesc.find('who')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 comment = {'from': who.text,
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 'date': longdesc.findtext('bug_when'),
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 'nick': who.get('name'),
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 'body': longdesc.findtext('thetext')}
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 comments.append(comment)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 ticket['body'] = body
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 ticket['comments'] = comments
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 tickets.append(ticket)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
99
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 tickets.sort(key = lambda t: int(t['id']))
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return (tickets, len(tickets))
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 class BugzillaImport(object):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def __init__(self, host):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 log.info(_(u"Bugilla Import plugin initialization"))
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.host = host
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 host.plugins['TICKETS_IMPORT'].register('bugzilla', self.Import, SHORT_DESC, LONG_DESC)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def Import(self, client, location, options=None):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 if not os.path.isabs(location):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 raise exceptions.DataError(u"An absolute path to XML data need to be given as location")
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 bugzilla_parser = BugzillaParser()
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 # d = threads.deferToThread(bugzilla_parser.parse, location)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 d = defer.maybeDeferred(bugzilla_parser.parse, location)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return d