webkit-patch apply-attachment is very slow for big patches
[WebKit-https.git] / Tools / Scripts / webkitpy / common / net / bugzilla / bugzilla.py
1 # Copyright (c) 2011 Google Inc. All rights reserved.
2 # Copyright (c) 2009 Apple Inc. All rights reserved.
3 # Copyright (c) 2010 Research In Motion Limited. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 #     * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31 # WebKit's Python module for interacting with Bugzilla
32
33 import mimetypes
34 import re
35 import StringIO
36 import urllib
37
38 from datetime import datetime # used in timestamp()
39
40 from .attachment import Attachment
41 from .bug import Bug
42
43 from webkitpy.common.system.deprecated_logging import log
44 from webkitpy.common.config import committers
45 import webkitpy.common.config.urls as config_urls
46 from webkitpy.common.net.credentials import Credentials
47 from webkitpy.common.system.user import User
48 from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, SoupStrainer
49
50
51 class EditUsersParser(object):
52     def __init__(self):
53         self._group_name_to_group_string_cache = {}
54
55     def _login_and_uid_from_row(self, row):
56         first_cell = row.find("td")
57         # The first row is just headers, we skip it.
58         if not first_cell:
59             return None
60         # When there were no results, we have a fake "<none>" entry in the table.
61         if first_cell.find(text="<none>"):
62             return None
63         # Otherwise the <td> contains a single <a> which contains the login name or a single <i> with the string "<none>".
64         anchor_tag = first_cell.find("a")
65         login = unicode(anchor_tag.string).strip()
66         user_id = int(re.search(r"userid=(\d+)", str(anchor_tag['href'])).group(1))
67         return (login, user_id)
68
69     def login_userid_pairs_from_edit_user_results(self, results_page):
70         soup = BeautifulSoup(results_page, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
71         results_table = soup.find(id="admin_table")
72         login_userid_pairs = [self._login_and_uid_from_row(row) for row in results_table('tr')]
73         # Filter out None from the logins.
74         return filter(lambda pair: bool(pair), login_userid_pairs)
75
76     def _group_name_and_string_from_row(self, row):
77         label_element = row.find('label')
78         group_string = unicode(label_element['for'])
79         group_name = unicode(label_element.find('strong').string).rstrip(':')
80         return (group_name, group_string)
81
82     def user_dict_from_edit_user_page(self, page):
83         soup = BeautifulSoup(page, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
84         user_table = soup.find("table", {'class': 'main'})
85         user_dict = {}
86         for row in user_table('tr'):
87             label_element = row.find('label')
88             if not label_element:
89                 continue  # This must not be a row we know how to parse.
90             if row.find('table'):
91                 continue  # Skip the <tr> holding the groups table.
92
93             key = label_element['for']
94             if "group" in key:
95                 key = "groups"
96                 value = user_dict.get('groups', set())
97                 # We must be parsing a "tr" inside the inner group table.
98                 (group_name, _) = self._group_name_and_string_from_row(row)
99                 if row.find('input', {'type': 'checkbox', 'checked': 'checked'}):
100                     value.add(group_name)
101             else:
102                 value = unicode(row.find('td').string).strip()
103             user_dict[key] = value
104         return user_dict
105
106     def _group_rows_from_edit_user_page(self, edit_user_page):
107         soup = BeautifulSoup(edit_user_page, convertEntities=BeautifulSoup.HTML_ENTITIES)
108         return soup('td', {'class': 'groupname'})
109
110     def group_string_from_name(self, edit_user_page, group_name):
111         # Bugzilla uses "group_NUMBER" strings, which may be different per install
112         # so we just look them up once and cache them.
113         if not self._group_name_to_group_string_cache:
114             rows = self._group_rows_from_edit_user_page(edit_user_page)
115             name_string_pairs = map(self._group_name_and_string_from_row, rows)
116             self._group_name_to_group_string_cache = dict(name_string_pairs)
117         return self._group_name_to_group_string_cache[group_name]
118
119
120 def timestamp():
121     return datetime.now().strftime("%Y%m%d%H%M%S")
122
123
124 # A container for all of the logic for making and parsing bugzilla queries.
125 class BugzillaQueries(object):
126
127     def __init__(self, bugzilla):
128         self._bugzilla = bugzilla
129
130     def _is_xml_bugs_form(self, form):
131         # ClientForm.HTMLForm.find_control throws if the control is not found,
132         # so we do a manual search instead:
133         return "xml" in [control.id for control in form.controls]
134
135     # This is kinda a hack.  There is probably a better way to get this information from bugzilla.
136     def _parse_result_count(self, results_page):
137         result_count_text = BeautifulSoup(results_page).find(attrs={'class': 'bz_result_count'}).string
138         result_count_parts = result_count_text.strip().split(" ")
139         if result_count_parts[0] == "Zarro":
140             return 0
141         if result_count_parts[0] == "One":
142             return 1
143         return int(result_count_parts[0])
144
145     # Note: _load_query, _fetch_bug and _fetch_bugs_from_advanced_query
146     # are the only methods which access self._bugzilla.
147
148     def _load_query(self, query):
149         self._bugzilla.authenticate()
150         full_url = "%s%s" % (config_urls.bug_server_url, query)
151         return self._bugzilla.browser.open(full_url)
152
153     def _fetch_bugs_from_advanced_query(self, query):
154         results_page = self._load_query(query)
155         # Some simple searches can return a single result.
156         results_url = results_page.geturl()
157         if results_url.find("/show_bug.cgi?id=") != -1:
158             bug_id = int(results_url.split("=")[-1])
159             return [self._fetch_bug(bug_id)]
160         if not self._parse_result_count(results_page):
161             return []
162         # Bugzilla results pages have an "XML" submit button at the bottom
163         # which can be used to get an XML page containing all of the <bug> elements.
164         # This is slighty lame that this assumes that _load_query used
165         # self._bugzilla.browser and that it's in an acceptable state.
166         self._bugzilla.browser.select_form(predicate=self._is_xml_bugs_form)
167         bugs_xml = self._bugzilla.browser.submit()
168         return self._bugzilla._parse_bugs_from_xml(bugs_xml)
169
170     def _fetch_bug(self, bug_id):
171         return self._bugzilla.fetch_bug(bug_id)
172
173     def _fetch_bug_ids_advanced_query(self, query):
174         soup = BeautifulSoup(self._load_query(query))
175         # The contents of the <a> inside the cells in the first column happen
176         # to be the bug id.
177         return [int(bug_link_cell.find("a").string)
178                 for bug_link_cell in soup('td', "first-child")]
179
180     def _parse_attachment_ids_request_query(self, page):
181         digits = re.compile("\d+")
182         attachment_href = re.compile("attachment.cgi\?id=\d+&action=review")
183         attachment_links = SoupStrainer("a", href=attachment_href)
184         return [int(digits.search(tag["href"]).group(0))
185                 for tag in BeautifulSoup(page, parseOnlyThese=attachment_links)]
186
187     def _fetch_attachment_ids_request_query(self, query):
188         return self._parse_attachment_ids_request_query(self._load_query(query))
189
190     def _parse_quips(self, page):
191         soup = BeautifulSoup(page, convertEntities=BeautifulSoup.HTML_ENTITIES)
192         quips = soup.find(text=re.compile(r"Existing quips:")).findNext("ul").findAll("li")
193         return [unicode(quip_entry.string) for quip_entry in quips]
194
195     def fetch_quips(self):
196         return self._parse_quips(self._load_query("/quips.cgi?action=show"))
197
198     # List of all r+'d bugs.
199     def fetch_bug_ids_from_pending_commit_list(self):
200         needs_commit_query_url = "buglist.cgi?query_format=advanced&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&field0-0-0=flagtypes.name&type0-0-0=equals&value0-0-0=review%2B"
201         return self._fetch_bug_ids_advanced_query(needs_commit_query_url)
202
203     def fetch_bugs_matching_quicksearch(self, search_string):
204         # We may want to use a more explicit query than "quicksearch".
205         # If quicksearch changes we should probably change to use
206         # a normal buglist.cgi?query_format=advanced query.
207         quicksearch_url = "buglist.cgi?quicksearch=%s" % urllib.quote(search_string)
208         return self._fetch_bugs_from_advanced_query(quicksearch_url)
209
210     # Currently this returns all bugs across all components.
211     # In the future we may wish to extend this API to construct more restricted searches.
212     def fetch_bugs_matching_search(self, search_string, author_email=None):
213         query = "buglist.cgi?query_format=advanced"
214         if search_string:
215             query += "&short_desc_type=allwordssubstr&short_desc=%s" % urllib.quote(search_string)
216         if author_email:
217             query += "&emailreporter1=1&emailtype1=substring&email1=%s" % urllib.quote(search_string)
218         return self._fetch_bugs_from_advanced_query(query)
219
220     def fetch_patches_from_pending_commit_list(self):
221         return sum([self._fetch_bug(bug_id).reviewed_patches()
222             for bug_id in self.fetch_bug_ids_from_pending_commit_list()], [])
223
224     def fetch_bug_ids_from_commit_queue(self):
225         commit_queue_url = "buglist.cgi?query_format=advanced&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&field0-0-0=flagtypes.name&type0-0-0=equals&value0-0-0=commit-queue%2B&order=Last+Changed"
226         return self._fetch_bug_ids_advanced_query(commit_queue_url)
227
228     def fetch_patches_from_commit_queue(self):
229         # This function will only return patches which have valid committers
230         # set.  It won't reject patches with invalid committers/reviewers.
231         return sum([self._fetch_bug(bug_id).commit_queued_patches()
232                     for bug_id in self.fetch_bug_ids_from_commit_queue()], [])
233
234     def fetch_bug_ids_from_review_queue(self):
235         review_queue_url = "buglist.cgi?query_format=advanced&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&field0-0-0=flagtypes.name&type0-0-0=equals&value0-0-0=review?"
236         return self._fetch_bug_ids_advanced_query(review_queue_url)
237
238     # This method will make several requests to bugzilla.
239     def fetch_patches_from_review_queue(self, limit=None):
240         # [:None] returns the whole array.
241         return sum([self._fetch_bug(bug_id).unreviewed_patches()
242             for bug_id in self.fetch_bug_ids_from_review_queue()[:limit]], [])
243
244     # NOTE: This is the only client of _fetch_attachment_ids_request_query
245     # This method only makes one request to bugzilla.
246     def fetch_attachment_ids_from_review_queue(self):
247         review_queue_url = "request.cgi?action=queue&type=review&group=type"
248         return self._fetch_attachment_ids_request_query(review_queue_url)
249
250     # This only works if your account has edituser privileges.
251     # We could easily parse https://bugs.webkit.org/userprefs.cgi?tab=permissions to
252     # check permissions, but bugzilla will just return an error if we don't have them.
253     def fetch_login_userid_pairs_matching_substring(self, search_string):
254         review_queue_url = "editusers.cgi?action=list&matchvalue=login_name&matchstr=%s&matchtype=substr" % urllib.quote(search_string)
255         results_page = self._load_query(review_queue_url)
256         # We could pull the EditUsersParser off Bugzilla if needed.
257         return EditUsersParser().login_userid_pairs_from_edit_user_results(results_page)
258
259     # FIXME: We should consider adding a BugzillaUser class.
260     def fetch_logins_matching_substring(self, search_string):
261         pairs = self.fetch_login_userid_pairs_matching_substring(search_string)
262         return map(lambda pair: pair[0], pairs)
263
264
265 class Bugzilla(object):
266     def __init__(self, committers=committers.CommitterList()):
267         self.authenticated = False
268         self.queries = BugzillaQueries(self)
269         self.committers = committers
270         self.cached_quips = []
271         self.edit_user_parser = EditUsersParser()
272         self._browser = None
273
274     def _get_browser(self):
275         if not self._browser:
276             from webkitpy.thirdparty.autoinstalled.mechanize import Browser
277             self._browser = Browser()
278             # Ignore bugs.webkit.org/robots.txt until we fix it to allow this script.
279             self._browser.set_handle_robots(False)
280         return self._browser
281
282     def _set_browser(self, value):
283         self._browser = value
284
285     browser = property(_get_browser, _set_browser)
286
287     def fetch_user(self, user_id):
288         self.authenticate()
289         edit_user_page = self.browser.open(self.edit_user_url_for_id(user_id))
290         return self.edit_user_parser.user_dict_from_edit_user_page(edit_user_page)
291
292     def add_user_to_groups(self, user_id, group_names):
293         self.authenticate()
294         user_edit_page = self.browser.open(self.edit_user_url_for_id(user_id))
295         self.browser.select_form(nr=1)
296         for group_name in group_names:
297             group_string = self.edit_user_parser.group_string_from_name(user_edit_page, group_name)
298             self.browser.find_control(group_string).items[0].selected = True
299         self.browser.submit()
300
301     def quips(self):
302         # We only fetch and parse the list of quips once per instantiation
303         # so that we do not burden bugs.webkit.org.
304         if not self.cached_quips:
305             self.cached_quips = self.queries.fetch_quips()
306         return self.cached_quips
307
308     def bug_url_for_bug_id(self, bug_id, xml=False):
309         if not bug_id:
310             return None
311         content_type = "&ctype=xml&excludefield=attachmentdata" if xml else ""
312         return "%sshow_bug.cgi?id=%s%s" % (config_urls.bug_server_url, bug_id, content_type)
313
314     def short_bug_url_for_bug_id(self, bug_id):
315         if not bug_id:
316             return None
317         return "http://webkit.org/b/%s" % bug_id
318
319     def add_attachment_url(self, bug_id):
320         return "%sattachment.cgi?action=enter&bugid=%s" % (config_urls.bug_server_url, bug_id)
321
322     def attachment_url_for_id(self, attachment_id, action="view"):
323         if not attachment_id:
324             return None
325         action_param = ""
326         if action and action != "view":
327             action_param = "&action=%s" % action
328         return "%sattachment.cgi?id=%s%s" % (config_urls.bug_server_url,
329                                              attachment_id,
330                                              action_param)
331
332     def edit_user_url_for_id(self, user_id):
333         return "%seditusers.cgi?action=edit&userid=%s" % (config_urls.bug_server_url, user_id)
334
335     def _parse_attachment_flag(self,
336                                element,
337                                flag_name,
338                                attachment,
339                                result_key):
340         flag = element.find('flag', attrs={'name': flag_name})
341         if flag:
342             attachment[flag_name] = flag['status']
343             if flag['status'] == '+':
344                 attachment[result_key] = flag['setter']
345         # Sadly show_bug.cgi?ctype=xml does not expose the flag modification date.
346
347     def _string_contents(self, soup):
348         # WebKit's bugzilla instance uses UTF-8.
349         # BeautifulStoneSoup always returns Unicode strings, however
350         # the .string method returns a (unicode) NavigableString.
351         # NavigableString can confuse other parts of the code, so we
352         # convert from NavigableString to a real unicode() object using unicode().
353         return unicode(soup.string)
354
355     # Example: 2010-01-20 14:31 PST
356     # FIXME: Some bugzilla dates seem to have seconds in them?
357     # Python does not support timezones out of the box.
358     # Assume that bugzilla always uses PST (which is true for bugs.webkit.org)
359     _bugzilla_date_format = "%Y-%m-%d %H:%M:%S"
360
361     @classmethod
362     def _parse_date(cls, date_string):
363         (date, time, time_zone) = date_string.split(" ")
364         if time.count(':') == 1:
365             # Add seconds into the time.
366             time += ':0'
367         # Ignore the timezone because python doesn't understand timezones out of the box.
368         date_string = "%s %s" % (date, time)
369         return datetime.strptime(date_string, cls._bugzilla_date_format)
370
371     def _date_contents(self, soup):
372         return self._parse_date(self._string_contents(soup))
373
374     def _parse_attachment_element(self, element, bug_id):
375         attachment = {}
376         attachment['bug_id'] = bug_id
377         attachment['is_obsolete'] = (element.has_key('isobsolete') and element['isobsolete'] == "1")
378         attachment['is_patch'] = (element.has_key('ispatch') and element['ispatch'] == "1")
379         attachment['id'] = int(element.find('attachid').string)
380         # FIXME: No need to parse out the url here.
381         attachment['url'] = self.attachment_url_for_id(attachment['id'])
382         attachment["attach_date"] = self._date_contents(element.find("date"))
383         attachment['name'] = self._string_contents(element.find('desc'))
384         attachment['attacher_email'] = self._string_contents(element.find('attacher'))
385         attachment['type'] = self._string_contents(element.find('type'))
386         self._parse_attachment_flag(
387                 element, 'review', attachment, 'reviewer_email')
388         self._parse_attachment_flag(
389                 element, 'commit-queue', attachment, 'committer_email')
390         return attachment
391
392     def _parse_log_descr_element(self, element):
393         comment = {}
394         comment['comment_email'] = self._string_contents(element.find('who'))
395         comment['comment_date'] = self._date_contents(element.find('bug_when'))
396         comment['text'] = self._string_contents(element.find('thetext'))
397         return comment
398
399     def _parse_bugs_from_xml(self, page):
400         soup = BeautifulSoup(page)
401         # Without the unicode() call, BeautifulSoup occasionally complains of being
402         # passed None for no apparent reason.
403         return [Bug(self._parse_bug_dictionary_from_xml(unicode(bug_xml)), self) for bug_xml in soup('bug')]
404
405     def _parse_bug_dictionary_from_xml(self, page):
406         soup = BeautifulStoneSoup(page, convertEntities=BeautifulStoneSoup.XML_ENTITIES)
407         bug = {}
408         bug["id"] = int(soup.find("bug_id").string)
409         bug["title"] = self._string_contents(soup.find("short_desc"))
410         bug["bug_status"] = self._string_contents(soup.find("bug_status"))
411         dup_id = soup.find("dup_id")
412         if dup_id:
413             bug["dup_id"] = self._string_contents(dup_id)
414         bug["reporter_email"] = self._string_contents(soup.find("reporter"))
415         bug["assigned_to_email"] = self._string_contents(soup.find("assigned_to"))
416         bug["cc_emails"] = [self._string_contents(element) for element in soup.findAll('cc')]
417         bug["attachments"] = [self._parse_attachment_element(element, bug["id"]) for element in soup.findAll('attachment')]
418         bug["comments"] = [self._parse_log_descr_element(element) for element in soup.findAll('long_desc')]
419
420         return bug
421
422     # Makes testing fetch_*_from_bug() possible until we have a better
423     # BugzillaNetwork abstration.
424
425     def _fetch_bug_page(self, bug_id):
426         bug_url = self.bug_url_for_bug_id(bug_id, xml=True)
427         log("Fetching: %s" % bug_url)
428         return self.browser.open(bug_url)
429
430     def fetch_bug_dictionary(self, bug_id):
431         try:
432             return self._parse_bug_dictionary_from_xml(self._fetch_bug_page(bug_id))
433         except KeyboardInterrupt:
434             raise
435         except:
436             self.authenticate()
437             return self._parse_bug_dictionary_from_xml(self._fetch_bug_page(bug_id))
438
439     # FIXME: A BugzillaCache object should provide all these fetch_ methods.
440
441     def fetch_bug(self, bug_id):
442         return Bug(self.fetch_bug_dictionary(bug_id), self)
443
444     def fetch_attachment_contents(self, attachment_id):
445         attachment_url = self.attachment_url_for_id(attachment_id)
446         # We need to authenticate to download patches from security bugs.
447         self.authenticate()
448         return self.browser.open(attachment_url).read()
449
450     def _parse_bug_id_from_attachment_page(self, page):
451         # The "Up" relation happens to point to the bug.
452         up_link = BeautifulSoup(page).find('link', rel='Up')
453         if not up_link:
454             # This attachment does not exist (or you don't have permissions to
455             # view it).
456             return None
457         match = re.search("show_bug.cgi\?id=(?P<bug_id>\d+)", up_link['href'])
458         return int(match.group('bug_id'))
459
460     def bug_id_for_attachment_id(self, attachment_id):
461         self.authenticate()
462
463         attachment_url = self.attachment_url_for_id(attachment_id, 'edit')
464         log("Fetching: %s" % attachment_url)
465         page = self.browser.open(attachment_url)
466         return self._parse_bug_id_from_attachment_page(page)
467
468     # FIXME: This should just return Attachment(id), which should be able to
469     # lazily fetch needed data.
470
471     def fetch_attachment(self, attachment_id):
472         # We could grab all the attachment details off of the attachment edit
473         # page but we already have working code to do so off of the bugs page,
474         # so re-use that.
475         bug_id = self.bug_id_for_attachment_id(attachment_id)
476         if not bug_id:
477             return None
478         attachments = self.fetch_bug(bug_id).attachments(include_obsolete=True)
479         for attachment in attachments:
480             if attachment.id() == int(attachment_id):
481                 return attachment
482         return None # This should never be hit.
483
484     def authenticate(self):
485         if self.authenticated:
486             return
487
488         credentials = Credentials(config_urls.bug_server_host, git_prefix="bugzilla")
489
490         attempts = 0
491         while not self.authenticated:
492             attempts += 1
493             username, password = credentials.read_credentials()
494
495             log("Logging in as %s..." % username)
496             self.browser.open(config_urls.bug_server_url +
497                               "index.cgi?GoAheadAndLogIn=1")
498             self.browser.select_form(name="login")
499             self.browser['Bugzilla_login'] = username
500             self.browser['Bugzilla_password'] = password
501             self.browser.find_control("Bugzilla_restrictlogin").items[0].selected = False
502             response = self.browser.submit()
503
504             match = re.search("<title>(.+?)</title>", response.read())
505             # If the resulting page has a title, and it contains the word
506             # "invalid" assume it's the login failure page.
507             if match and re.search("Invalid", match.group(1), re.IGNORECASE):
508                 errorMessage = "Bugzilla login failed: %s" % match.group(1)
509                 # raise an exception only if this was the last attempt
510                 if attempts < 5:
511                     log(errorMessage)
512                 else:
513                     raise Exception(errorMessage)
514             else:
515                 self.authenticated = True
516                 self.username = username
517
518     def _commit_queue_flag(self, mark_for_landing, mark_for_commit_queue):
519         if mark_for_landing:
520             return '+'
521         elif mark_for_commit_queue:
522             return '?'
523         return 'X'
524
525     # FIXME: mark_for_commit_queue and mark_for_landing should be joined into a single commit_flag argument.
526     def _fill_attachment_form(self,
527                               description,
528                               file_object,
529                               mark_for_review=False,
530                               mark_for_commit_queue=False,
531                               mark_for_landing=False,
532                               is_patch=False,
533                               filename=None,
534                               mimetype=None):
535         self.browser['description'] = description
536         if is_patch:
537             self.browser['ispatch'] = ("1",)
538         # FIXME: Should this use self._find_select_element_for_flag?
539         self.browser['flag_type-1'] = ('?',) if mark_for_review else ('X',)
540         self.browser['flag_type-3'] = (self._commit_queue_flag(mark_for_landing, mark_for_commit_queue),)
541
542         filename = filename or "%s.patch" % timestamp()
543         if not mimetype:
544             mimetypes.add_type('text/plain', '.patch')  # Make sure mimetypes knows about .patch
545             mimetype, _ = mimetypes.guess_type(filename)
546         if not mimetype:
547             mimetype = "text/plain"  # Bugzilla might auto-guess for us and we might not need this?
548         self.browser.add_file(file_object, mimetype, filename, 'data')
549
550     def _file_object_for_upload(self, file_or_string):
551         if hasattr(file_or_string, 'read'):
552             return file_or_string
553         # Only if file_or_string is not already encoded do we want to encode it.
554         if isinstance(file_or_string, unicode):
555             file_or_string = file_or_string.encode('utf-8')
556         return StringIO.StringIO(file_or_string)
557
558     # timestamp argument is just for unittests.
559     def _filename_for_upload(self, file_object, bug_id, extension="txt", timestamp=timestamp):
560         if hasattr(file_object, "name"):
561             return file_object.name
562         return "bug-%s-%s.%s" % (bug_id, timestamp(), extension)
563
564     def add_attachment_to_bug(self,
565                               bug_id,
566                               file_or_string,
567                               description,
568                               filename=None,
569                               comment_text=None):
570         self.authenticate()
571         log('Adding attachment "%s" to %s' % (description, self.bug_url_for_bug_id(bug_id)))
572         self.browser.open(self.add_attachment_url(bug_id))
573         self.browser.select_form(name="entryform")
574         file_object = self._file_object_for_upload(file_or_string)
575         filename = filename or self._filename_for_upload(file_object, bug_id)
576         self._fill_attachment_form(description, file_object, filename=filename)
577         if comment_text:
578             log(comment_text)
579             self.browser['comment'] = comment_text
580         self.browser.submit()
581
582     # FIXME: The arguments to this function should be simplified and then
583     # this should be merged into add_attachment_to_bug
584     def add_patch_to_bug(self,
585                          bug_id,
586                          file_or_string,
587                          description,
588                          comment_text=None,
589                          mark_for_review=False,
590                          mark_for_commit_queue=False,
591                          mark_for_landing=False):
592         self.authenticate()
593         log('Adding patch "%s" to %s' % (description, self.bug_url_for_bug_id(bug_id)))
594
595         self.browser.open(self.add_attachment_url(bug_id))
596         self.browser.select_form(name="entryform")
597         file_object = self._file_object_for_upload(file_or_string)
598         filename = self._filename_for_upload(file_object, bug_id, extension="patch")
599         self._fill_attachment_form(description,
600                                    file_object,
601                                    mark_for_review=mark_for_review,
602                                    mark_for_commit_queue=mark_for_commit_queue,
603                                    mark_for_landing=mark_for_landing,
604                                    is_patch=True,
605                                    filename=filename)
606         if comment_text:
607             log(comment_text)
608             self.browser['comment'] = comment_text
609         self.browser.submit()
610
611     # FIXME: There has to be a more concise way to write this method.
612     def _check_create_bug_response(self, response_html):
613         match = re.search("<title>Bug (?P<bug_id>\d+) Submitted</title>",
614                           response_html)
615         if match:
616             return match.group('bug_id')
617
618         match = re.search(
619             '<div id="bugzilla-body">(?P<error_message>.+)<div id="footer">',
620             response_html,
621             re.DOTALL)
622         error_message = "FAIL"
623         if match:
624             text_lines = BeautifulSoup(
625                     match.group('error_message')).findAll(text=True)
626             error_message = "\n" + '\n'.join(
627                     ["  " + line.strip()
628                      for line in text_lines if line.strip()])
629         raise Exception("Bug not created: %s" % error_message)
630
631     def create_bug(self,
632                    bug_title,
633                    bug_description,
634                    component=None,
635                    diff=None,
636                    patch_description=None,
637                    cc=None,
638                    blocked=None,
639                    assignee=None,
640                    mark_for_review=False,
641                    mark_for_commit_queue=False):
642         self.authenticate()
643
644         log('Creating bug with title "%s"' % bug_title)
645         self.browser.open(config_urls.bug_server_url + "enter_bug.cgi?product=WebKit")
646         self.browser.select_form(name="Create")
647         component_items = self.browser.find_control('component').items
648         component_names = map(lambda item: item.name, component_items)
649         if not component:
650             component = "New Bugs"
651         if component not in component_names:
652             component = User.prompt_with_list("Please pick a component:", component_names)
653         self.browser["component"] = [component]
654         if cc:
655             self.browser["cc"] = cc
656         if blocked:
657             self.browser["blocked"] = unicode(blocked)
658         if not assignee:
659             assignee = self.username
660         if assignee and not self.browser.find_control("assigned_to").disabled:
661             self.browser["assigned_to"] = assignee
662         self.browser["short_desc"] = bug_title
663         self.browser["comment"] = bug_description
664
665         if diff:
666             # _fill_attachment_form expects a file-like object
667             # Patch files are already binary, so no encoding needed.
668             assert(isinstance(diff, str))
669             patch_file_object = StringIO.StringIO(diff)
670             self._fill_attachment_form(
671                     patch_description,
672                     patch_file_object,
673                     mark_for_review=mark_for_review,
674                     mark_for_commit_queue=mark_for_commit_queue,
675                     is_patch=True)
676
677         response = self.browser.submit()
678
679         bug_id = self._check_create_bug_response(response.read())
680         log("Bug %s created." % bug_id)
681         log("%sshow_bug.cgi?id=%s" % (config_urls.bug_server_url, bug_id))
682         return bug_id
683
684     def _find_select_element_for_flag(self, flag_name):
685         # FIXME: This will break if we ever re-order attachment flags
686         if flag_name == "review":
687             return self.browser.find_control(type='select', nr=0)
688         elif flag_name == "commit-queue":
689             return self.browser.find_control(type='select', nr=1)
690         raise Exception("Don't know how to find flag named \"%s\"" % flag_name)
691
692     def clear_attachment_flags(self,
693                                attachment_id,
694                                additional_comment_text=None):
695         self.authenticate()
696
697         comment_text = "Clearing flags on attachment: %s" % attachment_id
698         if additional_comment_text:
699             comment_text += "\n\n%s" % additional_comment_text
700         log(comment_text)
701
702         self.browser.open(self.attachment_url_for_id(attachment_id, 'edit'))
703         self.browser.select_form(nr=1)
704         self.browser.set_value(comment_text, name='comment', nr=0)
705         self._find_select_element_for_flag('review').value = ("X",)
706         self._find_select_element_for_flag('commit-queue').value = ("X",)
707         self.browser.submit()
708
709     def set_flag_on_attachment(self,
710                                attachment_id,
711                                flag_name,
712                                flag_value,
713                                comment_text=None,
714                                additional_comment_text=None):
715         # FIXME: We need a way to test this function on a live bugzilla
716         # instance.
717
718         self.authenticate()
719
720         # FIXME: additional_comment_text seems useless and should be merged into comment-text.
721         if additional_comment_text:
722             comment_text += "\n\n%s" % additional_comment_text
723         log(comment_text)
724
725         self.browser.open(self.attachment_url_for_id(attachment_id, 'edit'))
726         self.browser.select_form(nr=1)
727
728         if comment_text:
729             self.browser.set_value(comment_text, name='comment', nr=0)
730
731         self._find_select_element_for_flag(flag_name).value = (flag_value,)
732         self.browser.submit()
733
734     # FIXME: All of these bug editing methods have a ridiculous amount of
735     # copy/paste code.
736
737     def obsolete_attachment(self, attachment_id, comment_text=None):
738         self.authenticate()
739
740         log("Obsoleting attachment: %s" % attachment_id)
741         self.browser.open(self.attachment_url_for_id(attachment_id, 'edit'))
742         self.browser.select_form(nr=1)
743         self.browser.find_control('isobsolete').items[0].selected = True
744         # Also clear any review flag (to remove it from review/commit queues)
745         self._find_select_element_for_flag('review').value = ("X",)
746         self._find_select_element_for_flag('commit-queue').value = ("X",)
747         if comment_text:
748             log(comment_text)
749             # Bugzilla has two textareas named 'comment', one is somehow
750             # hidden.  We want the first.
751             self.browser.set_value(comment_text, name='comment', nr=0)
752         self.browser.submit()
753
754     def add_cc_to_bug(self, bug_id, email_address_list):
755         self.authenticate()
756
757         log("Adding %s to the CC list for bug %s" % (email_address_list, bug_id))
758         self.browser.open(self.bug_url_for_bug_id(bug_id))
759         self.browser.select_form(name="changeform")
760         self.browser["newcc"] = ", ".join(email_address_list)
761         self.browser.submit()
762
763     def post_comment_to_bug(self, bug_id, comment_text, cc=None):
764         self.authenticate()
765
766         log("Adding comment to bug %s" % bug_id)
767         self.browser.open(self.bug_url_for_bug_id(bug_id))
768         self.browser.select_form(name="changeform")
769         self.browser["comment"] = comment_text
770         if cc:
771             self.browser["newcc"] = ", ".join(cc)
772         self.browser.submit()
773
774     def close_bug_as_fixed(self, bug_id, comment_text=None):
775         self.authenticate()
776
777         log("Closing bug %s as fixed" % bug_id)
778         self.browser.open(self.bug_url_for_bug_id(bug_id))
779         self.browser.select_form(name="changeform")
780         if comment_text:
781             self.browser['comment'] = comment_text
782         self.browser['bug_status'] = ['RESOLVED']
783         self.browser['resolution'] = ['FIXED']
784         self.browser.submit()
785
786     def _has_control(self, form, id):
787         return id in [control.id for control in form.controls]
788
789     def reassign_bug(self, bug_id, assignee=None, comment_text=None):
790         self.authenticate()
791
792         if not assignee:
793             assignee = self.username
794
795         log("Assigning bug %s to %s" % (bug_id, assignee))
796         self.browser.open(self.bug_url_for_bug_id(bug_id))
797         self.browser.select_form(name="changeform")
798
799         if not self._has_control(self.browser, "assigned_to"):
800             log("""Failed to assign bug to you (can't find assigned_to) control.
801 Do you have EditBugs privileges at bugs.webkit.org?
802 https://bugs.webkit.org/userprefs.cgi?tab=permissions
803
804 If not, you should email webkit-committers@lists.webkit.org or ask in #webkit
805 for someone to add EditBugs to your bugs.webkit.org account.""")
806             return
807
808         if comment_text:
809             log(comment_text)
810             self.browser["comment"] = comment_text
811         self.browser["assigned_to"] = assignee
812         self.browser.submit()
813
814     def reopen_bug(self, bug_id, comment_text):
815         self.authenticate()
816
817         log("Re-opening bug %s" % bug_id)
818         # Bugzilla requires a comment when re-opening a bug, so we know it will
819         # never be None.
820         log(comment_text)
821         self.browser.open(self.bug_url_for_bug_id(bug_id))
822         self.browser.select_form(name="changeform")
823         bug_status = self.browser.find_control("bug_status", type="select")
824         # This is a hack around the fact that ClientForm.ListControl seems to
825         # have no simpler way to ask if a control has an item named "REOPENED"
826         # without using exceptions for control flow.
827         possible_bug_statuses = map(lambda item: item.name, bug_status.items)
828         if "REOPENED" in possible_bug_statuses:
829             bug_status.value = ["REOPENED"]
830         # If the bug was never confirmed it will not have a "REOPENED"
831         # state, but only an "UNCONFIRMED" state.
832         elif "UNCONFIRMED" in possible_bug_statuses:
833             bug_status.value = ["UNCONFIRMED"]
834         else:
835             # FIXME: This logic is slightly backwards.  We won't print this
836             # message if the bug is already open with state "UNCONFIRMED".
837             log("Did not reopen bug %s, it appears to already be open with status %s." % (bug_id, bug_status.value))
838         self.browser['comment'] = comment_text
839         self.browser.submit()