| 1 |
import bugzilla |
| 2 |
|
| 3 |
import time |
| 4 |
import pytz |
| 5 |
import datetime |
| 6 |
import six |
| 7 |
|
| 8 |
from bugwarrior.config import die, asbool, aslist |
| 9 |
from bugwarrior.services import IssueService, Issue |
| 10 |
|
| 11 |
import logging |
| 12 |
log = logging.getLogger(__name__) |
| 13 |
|
| 14 |
|
| 15 |
class BugzillaIssue(Issue): |
| 16 |
URL = 'bugzillaurl' |
| 17 |
SUMMARY = 'bugzillasummary' |
| 18 |
BUG_ID = 'bugzillabugid' |
| 19 |
STATUS = 'bugzillastatus' |
| 20 |
NEEDINFO = 'bugzillaneedinfo' |
| 21 |
PRODUCT = 'bugzillaproduct' |
| 22 |
COMPONENT = 'bugzillacomponent' |
| 23 |
ASSIGNED_ON = 'bugzillaassignedon' |
| 24 |
|
| 25 |
UDAS = { |
| 26 |
URL: { |
| 27 |
'type': 'string', |
| 28 |
'label': 'Bugzilla URL', |
| 29 |
}, |
| 30 |
SUMMARY: { |
| 31 |
'type': 'string', |
| 32 |
'label': 'Bugzilla Summary', |
| 33 |
}, |
| 34 |
STATUS: { |
| 35 |
'type': 'string', |
| 36 |
'label': 'Bugzilla Status', |
| 37 |
}, |
| 38 |
BUG_ID: { |
| 39 |
'type': 'numeric', |
| 40 |
'label': 'Bugzilla Bug ID', |
| 41 |
}, |
| 42 |
NEEDINFO: { |
| 43 |
'type': 'date', |
| 44 |
'label': 'Bugzilla Needinfo', |
| 45 |
}, |
| 46 |
PRODUCT: { |
| 47 |
'type': 'string', |
| 48 |
'label': 'Bugzilla Product', |
| 49 |
}, |
| 50 |
COMPONENT: { |
| 51 |
'type': 'string', |
| 52 |
'label': 'Bugzilla Component', |
| 53 |
}, |
| 54 |
ASSIGNED_ON: { |
| 55 |
'type': 'date', |
| 56 |
'label': 'Bugzilla Assigned On', |
| 57 |
}, |
| 58 |
} |
| 59 |
UNIQUE_KEY = (URL, ) |
| 60 |
|
| 61 |
PRIORITY_MAP = { |
| 62 |
'unspecified': 'M', |
| 63 |
'low': 'L', |
| 64 |
'medium': 'M', |
| 65 |
'high': 'H', |
| 66 |
'urgent': 'H', |
| 67 |
} |
| 68 |
|
| 69 |
def to_taskwarrior(self): |
| 70 |
task = { |
| 71 |
'project': self.record['component'], |
| 72 |
'priority': self.get_priority(), |
| 73 |
'annotations': self.extra.get('annotations', []), |
| 74 |
|
| 75 |
self.URL: self.extra['url'], |
| 76 |
self.SUMMARY: self.record['summary'], |
| 77 |
self.BUG_ID: self.record['id'], |
| 78 |
self.STATUS: self.record['status'], |
| 79 |
self.PRODUCT: self.record['product'], |
| 80 |
self.COMPONENT: self.record['component'], |
| 81 |
} |
| 82 |
if self.extra.get('needinfo_since', None) is not None: |
| 83 |
task[self.NEEDINFO] = self.parse_date(self.extra.get('needinfo_since')) |
| 84 |
|
| 85 |
if self.extra.get('assigned_on', None) is not None: |
| 86 |
task[self.ASSIGNED_ON] = self.parse_date(self.extra.get('assigned_on')) |
| 87 |
|
| 88 |
return task |
| 89 |
|
| 90 |
def get_default_description(self): |
| 91 |
return self.build_default_description( |
| 92 |
title=self.record['summary'], |
| 93 |
url=self.get_processed_url(self.extra['url']), |
| 94 |
number=self.record['id'], |
| 95 |
cls='issue', |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
_open_statuses = [ |
| 100 |
'NEW', |
| 101 |
'ASSIGNED', |
| 102 |
'NEEDINFO', |
| 103 |
'ON_DEV', |
| 104 |
'MODIFIED', |
| 105 |
'POST', |
| 106 |
'REOPENED', |
| 107 |
'ON_QA', |
| 108 |
'FAILS_QA', |
| 109 |
'PASSES_QA', |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
class BugzillaService(IssueService): |
| 114 |
ISSUE_CLASS = BugzillaIssue |
| 115 |
CONFIG_PREFIX = 'bugzilla' |
| 116 |
|
| 117 |
COLUMN_LIST = [ |
| 118 |
'id', |
| 119 |
'status', |
| 120 |
'summary', |
| 121 |
'priority', |
| 122 |
'product', |
| 123 |
'component', |
| 124 |
'flags', |
| 125 |
'longdescs', |
| 126 |
'assigned_to', |
| 127 |
] |
| 128 |
|
| 129 |
def __init__(self, *args, **kw): |
| 130 |
super(BugzillaService, self).__init__(*args, **kw) |
| 131 |
self.base_uri = self.config.get('base_uri') |
| 132 |
self.username = self.config.get('username') |
| 133 |
self.ignore_cc = self.config.get('ignore_cc', default=False, |
| 134 |
to_type=lambda x: x == "True") |
| 135 |
self.query_url = self.config.get('query_url', default=None) |
| 136 |
self.include_needinfos = self.config.get( |
| 137 |
'include_needinfos', False, to_type=lambda x: x == "True") |
| 138 |
self.open_statuses = self.config.get('open_statuses', _open_statuses, to_type=aslist) |
| 139 |
log.debug(" filtering on statuses: %r", self.open_statuses) |
| 140 |
|
| 141 |
# So more modern bugzilla's require that we specify |
| 142 |
# query_format=advanced along with the xmlrpc request. |
| 143 |
# https://bugzilla.redhat.com/show_bug.cgi?id=825370 |
| 144 |
# ...but older bugzilla's don't know anything about that argument. |
| 145 |
# Here we make it possible for the user to specify whether they want |
| 146 |
# to pass that argument or not. |
| 147 |
self.advanced = asbool(self.config.get('advanced', 'no')) |
| 148 |
|
| 149 |
force_rest_kwargs = {} |
| 150 |
if asbool(self.config.get("force_rest", "no")): |
| 151 |
force_rest_kwargs = {"force_rest": True} |
| 152 |
|
| 153 |
url = 'https://%s' % self.base_uri |
| 154 |
if self.config.get('api_key'): |
| 155 |
api_key = self.get_password('api_key') |
| 156 |
try: |
| 157 |
self.bz = bugzilla.Bugzilla(url=url, api_key=api_key, **force_rest_kwargs) |
| 158 |
except TypeError: |
| 159 |
raise Exception("Bugzilla API keys require python-bugzilla>=2.1.0") |
| 160 |
else: |
| 161 |
password = self.get_password('password', self.username) |
| 162 |
self.bz = bugzilla.Bugzilla(url=url, **force_rest_kwargs) |
| 163 |
self.bz.login(self.username, password) |
| 164 |
|
| 165 |
@staticmethod |
| 166 |
def get_keyring_service(service_config): |
| 167 |
username = service_config.get('username') |
| 168 |
base_uri = service_config.get('base_uri') |
| 169 |
return "bugzilla://%s@%s" % (username, base_uri) |
| 170 |
|
| 171 |
@classmethod |
| 172 |
def validate_config(cls, service_config, target): |
| 173 |
req = ['username', 'base_uri'] |
| 174 |
for option in req: |
| 175 |
if option not in service_config: |
| 176 |
die("[%s] has no 'bugzilla.%s'" % (target, option)) |
| 177 |
if 'password' not in service_config and 'api_key' not in service_config: |
| 178 |
die("[%s] has neither 'bugzilla.password' nor 'bugzilla.api_key'" % (target,)) |
| 179 |
|
| 180 |
super(BugzillaService, cls).validate_config(service_config, target) |
| 181 |
|
| 182 |
def get_owner(self, issue): |
| 183 |
return issue['assigned_to'] |
| 184 |
|
| 185 |
def annotations(self, tag, issue, issue_obj): |
| 186 |
base_url = "https://%s/show_bug.cgi?id=" % self.base_uri |
| 187 |
long_url = base_url + six.text_type(issue['id']) |
| 188 |
url = issue_obj.get_processed_url(long_url) |
| 189 |
|
| 190 |
if 'comments' in issue: |
| 191 |
comments = issue.get('comments', []) |
| 192 |
return self.build_annotations( |
| 193 |
(( |
| 194 |
c['author'].split('@')[0], |
| 195 |
c['text'], |
| 196 |
) for c in comments), |
| 197 |
url |
| 198 |
) |
| 199 |
else: |
| 200 |
# Backwards compatibility (old python-bugzilla/bugzilla instances) |
| 201 |
# This block handles a million different contingencies that have to |
| 202 |
# do with different version of python-bugzilla and different |
| 203 |
# version of bugzilla itself. :( |
| 204 |
comments = issue.get('longdescs', []) |
| 205 |
|
| 206 |
def _parse_author(obj): |
| 207 |
if isinstance(obj, dict): |
| 208 |
return obj['login_name'].split('@')[0] |
| 209 |
else: |
| 210 |
return obj |
| 211 |
|
| 212 |
def _parse_body(obj): |
| 213 |
return obj.get('text', obj.get('body')) |
| 214 |
|
| 215 |
return self.build_annotations( |
| 216 |
(( |
| 217 |
_parse_author(c['author']), |
| 218 |
_parse_body(c) |
| 219 |
) for c in comments), |
| 220 |
url |
| 221 |
) |
| 222 |
|
| 223 |
def issues(self): |
| 224 |
email = self.username |
| 225 |
# TODO -- doing something with blockedby would be nice. |
| 226 |
|
| 227 |
if self.query_url: |
| 228 |
query = self.bz.url_to_query(self.query_url) |
| 229 |
query['column_list'] = self.COLUMN_LIST |
| 230 |
else: |
| 231 |
query = dict( |
| 232 |
column_list=self.COLUMN_LIST, |
| 233 |
bug_status=self.open_statuses, |
| 234 |
email1=email, |
| 235 |
emailreporter1=1, |
| 236 |
emailassigned_to1=1, |
| 237 |
emailqa_contact1=1, |
| 238 |
emailtype1="substring", |
| 239 |
) |
| 240 |
|
| 241 |
if not self.ignore_cc: |
| 242 |
query['emailcc1'] = 1 |
| 243 |
|
| 244 |
if self.advanced: |
| 245 |
# Required for new bugzilla |
| 246 |
# https://bugzilla.redhat.com/show_bug.cgi?id=825370 |
| 247 |
query['query_format'] = 'advanced' |
| 248 |
|
| 249 |
bugs = self.bz.query(query) |
| 250 |
|
| 251 |
if self.include_needinfos: |
| 252 |
needinfos = self.bz.query(dict( |
| 253 |
column_list=self.COLUMN_LIST, |
| 254 |
quicksearch='flag:needinfo?%s' % email, |
| 255 |
)) |
| 256 |
exists = [b.id for b in bugs] |
| 257 |
for bug in needinfos: |
| 258 |
# don't double-add bugs that have already been found |
| 259 |
if bug.id in exists: |
| 260 |
continue |
| 261 |
bugs.append(bug) |
| 262 |
|
| 263 |
# Convert to dicts |
| 264 |
bugs = [ |
| 265 |
dict( |
| 266 |
((col, _get_bug_attr(bug, col)) for col in self.COLUMN_LIST) |
| 267 |
) for bug in bugs |
| 268 |
] |
| 269 |
|
| 270 |
bugs = filter(self.include, bugs) |
| 271 |
issues = [(self.target, bug) for bug in bugs] |
| 272 |
log.debug(" Found %i total.", len(issues)) |
| 273 |
|
| 274 |
# Build a url for each issue |
| 275 |
base_url = "https://%s/show_bug.cgi?id=" % (self.base_uri) |
| 276 |
for tag, issue in issues: |
| 277 |
issue_obj = self.get_issue_for_record(issue) |
| 278 |
extra = { |
| 279 |
'url': base_url + six.text_type(issue['id']), |
| 280 |
'annotations': self.annotations(tag, issue, issue_obj), |
| 281 |
} |
| 282 |
|
| 283 |
needinfos = [f for f in issue['flags'] if ( |
| 284 |
f['name'] == 'needinfo' and |
| 285 |
f['status'] == '?' and |
| 286 |
f.get('requestee', self.username) == self.username |
| 287 |
)] |
| 288 |
if needinfos: |
| 289 |
last_mod = needinfos[0]['modification_date'] |
| 290 |
# convert from RPC DateTime string to datetime.datetime object |
| 291 |
mod_date = datetime.datetime.fromtimestamp( |
| 292 |
time.mktime(last_mod.timetuple())) |
| 293 |
|
| 294 |
extra['needinfo_since'] = pytz.UTC.localize(mod_date).isoformat() |
| 295 |
|
| 296 |
if issue['status'] == 'ASSIGNED': |
| 297 |
extra['assigned_on'] = self._get_assigned_date(issue) |
| 298 |
else: |
| 299 |
extra['assigned_on'] = None |
| 300 |
|
| 301 |
issue_obj.update_extra(extra) |
| 302 |
yield issue_obj |
| 303 |
|
| 304 |
def _get_assigned_date(self, issue): |
| 305 |
assigned_date = None |
| 306 |
|
| 307 |
bug = self.bz.getbug(issue['id']) |
| 308 |
history = bug.get_history_raw()['bugs'][0]['history'] |
| 309 |
|
| 310 |
# this is already in chronological order, so the last change is the one we want |
| 311 |
for h in reversed(history): |
| 312 |
for change in h['changes']: |
| 313 |
if change['field_name'] == 'status' and change['added'] == 'ASSIGNED': |
| 314 |
assigned_date = h['when'] |
| 315 |
|
| 316 |
# messy conversion :( |
| 317 |
# TODO: create method that's used here and in needinfos time conv above |
| 318 |
assigned_date_datetime = datetime.datetime.fromtimestamp( |
| 319 |
time.mktime(assigned_date.timetuple())) |
| 320 |
assigned_date_str = pytz.UTC.localize(assigned_date_datetime).isoformat() |
| 321 |
|
| 322 |
return assigned_date_str |
| 323 |
|
| 324 |
|
| 325 |
def _get_bug_attr(bug, attr): |
| 326 |
"""Default longdescs/flags case to [] since they may not be present.""" |
| 327 |
if attr in ("longdescs", "flags"): |
| 328 |
return getattr(bug, attr, []) |
| 329 |
return getattr(bug, attr) |