1# Copyright (c) 2009 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
31from .attachment import Attachment
32
33
34class Bug(object):
35    # FIXME: This class is kinda a hack for now.  It exists so we have one
36    # place to hold bug logic, even if much of the code deals with
37    # dictionaries still.
38
39    def __init__(self, bug_dictionary, bugzilla):
40        self.bug_dictionary = bug_dictionary
41        self._bugzilla = bugzilla
42
43    def id(self):
44        return self.bug_dictionary["id"]
45
46    def title(self):
47        # FIXME: Do we need to HTML unescape the title?
48        return self.bug_dictionary["title"]
49
50    def reporter_email(self):
51        return self.bug_dictionary["reporter_email"]
52
53    def assigned_to_email(self):
54        return self.bug_dictionary["assigned_to_email"]
55
56    # FIXME: This information should be stored in some sort of webkit_config.py instead of here.
57    unassigned_emails = frozenset([
58        "webkit-unassigned@lists.webkit.org",
59        "webkit-qt-unassigned@trolltech.com",
60    ])
61
62    def is_unassigned(self):
63        return self.assigned_to_email() in self.unassigned_emails
64
65    def status(self):
66        return self.bug_dictionary["bug_status"]
67
68    # Bugzilla has many status states we don't really use in WebKit:
69    # https://bugs.webkit.org/page.cgi?id=fields.html#status
70    _open_states = ["UNCONFIRMED", "NEW", "ASSIGNED", "REOPENED"]
71    _closed_states = ["RESOLVED", "VERIFIED", "CLOSED"]
72
73    def is_open(self):
74        return self.status() in self._open_states
75
76    def is_closed(self):
77        return not self.is_open()
78
79    def duplicate_of(self):
80        return self.bug_dictionary.get('dup_id', None)
81
82    # Rarely do we actually want obsolete attachments
83    def attachments(self, include_obsolete=False):
84        attachments = self.bug_dictionary["attachments"]
85        if not include_obsolete:
86            attachments = filter(lambda attachment:
87                                 not attachment["is_obsolete"], attachments)
88        return [Attachment(attachment, self) for attachment in attachments]
89
90    def patches(self, include_obsolete=False):
91        return [patch for patch in self.attachments(include_obsolete)
92                                   if patch.is_patch()]
93
94    def unreviewed_patches(self):
95        return [patch for patch in self.patches() if patch.review() == "?"]
96
97    def reviewed_patches(self, include_invalid=False):
98        patches = [patch for patch in self.patches() if patch.review() == "+"]
99        if include_invalid:
100            return patches
101        # Checking reviewer() ensures that it was both reviewed and has a valid
102        # reviewer.
103        return filter(lambda patch: patch.reviewer(), patches)
104
105    def commit_queued_patches(self, include_invalid=False):
106        patches = [patch for patch in self.patches()
107                                      if patch.commit_queue() == "+"]
108        if include_invalid:
109            return patches
110        # Checking committer() ensures that it was both commit-queue+'d and has
111        # a valid committer.
112        return filter(lambda patch: patch.committer(), patches)
113