annotate src/plugins/plugin_tickets_import_bugzilla.py @ 2387:a59f2abd970e

frontends (xmlui): added ValueGetter.keys
author Goffi <goffi@goffi.org>
date Fri, 20 Oct 2017 08:39:58 +0200
parents a49a19f06e38
children f57a8eaec8ed
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
2382
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
51 STATUS_MAP = {
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
52 'NEW': 'queued',
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
53 'ASSIGNED': 'started',
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
54 'RESOLVED': 'review',
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
55 'CLOSED': 'closed',
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
56 'REOPENED': 'started' # we loose data here because there is no need on basic workflow to have a reopened status
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
57 }
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
58
2373
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 class BugzillaParser(object):
2382
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
61 # TODO: add a way to reassign values
2373
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def parse(self, file_path):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 tickets = []
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 root = etree.parse(file_path)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 for bug in root.xpath('bug'):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 ticket = {}
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 ticket['id'] = bug.findtext('bug_id')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 ticket['creation'] = bug.findtext('creation_ts')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 ticket['update'] = bug.findtext('delta_ts')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 ticket['title'] = bug.findtext('short_desc')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 reporter_elt = bug.find('reporter')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 ticket['reporter_name'] = reporter_elt.get('name')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 ticket['reporter_email'] = reporter_elt.text
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 assigned_to_elt = bug.find('assigned_to')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 ticket['assigned_to_name'] = assigned_to_elt.get('name')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 ticket['assigned_to_email'] = assigned_to_elt.text
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 ticket['cc_emails'] = [e.text for e in bug.findall('cc')]
2382
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
80 ticket['priority'] = bug.findtext('priority').lower().strip()
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
81 ticket['severity'] = bug.findtext('bug_severity').lower().strip()
2373
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 ticket['product'] = bug.findtext('product')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 ticket['component'] = bug.findtext('component')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 ticket['version'] = bug.findtext('version')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 ticket['platform'] = bug.findtext('rep_platform')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 ticket['os'] = bug.findtext('op_sys')
2382
a49a19f06e38 plugin import ticket, import ticket bugzilla: some normalization in values:
Goffi <goffi@goffi.org>
parents: 2373
diff changeset
87 ticket['status'] = STATUS_MAP.get(bug.findtext('bug_status'), 'queued')
2373
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 ticket['milestone'] = bug.findtext('target_milestone')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 body = None
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 comments = []
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 for longdesc in bug.findall('long_desc'):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if body is None:
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 body = longdesc.findtext('thetext')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 else:
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 who = longdesc.find('who')
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 comment = {'from': who.text,
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 'date': longdesc.findtext('bug_when'),
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 'nick': who.get('name'),
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 'body': longdesc.findtext('thetext')}
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 comments.append(comment)
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 ticket['body'] = body
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 ticket['comments'] = comments
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 tickets.append(ticket)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 tickets.sort(key = lambda t: int(t['id']))
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 return (tickets, len(tickets))
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
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 class BugzillaImport(object):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def __init__(self, host):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 log.info(_(u"Bugilla Import plugin initialization"))
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.host = host
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 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
118
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def Import(self, client, location, options=None):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if not os.path.isabs(location):
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 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
122 bugzilla_parser = BugzillaParser()
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 # d = threads.deferToThread(bugzilla_parser.parse, location)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 d = defer.maybeDeferred(bugzilla_parser.parse, location)
d2476dc2d55d plugin tickets import Bugzilla: Bugzilla tickets importer:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return d