• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / relman-auto-nag / #5058

04 Jun 2024 04:28PM CUT coverage: 21.831% (-0.004%) from 21.835%
#5058

push

coveralls-python

benjaminmah
Introduced Fenix-specific component confidence threshold

716 of 3602 branches covered (19.88%)

0 of 2 new or added lines in 1 file covered. (0.0%)

2 existing lines in 2 files now uncovered.

1929 of 8836 relevant lines covered (21.83%)

0.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/bugbot/rules/component.py
1
# This Source Code Form is subject to the terms of the Mozilla Public
2
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
3
# You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
from libmozdata.bugzilla import Bugzilla
×
6

7
from bugbot import logger
×
8
from bugbot.bugbug_utils import get_bug_ids_classification
×
9
from bugbot.bzcleaner import BzCleaner
×
10
from bugbot.utils import get_config, nice_round
×
11

12

13
class Component(BzCleaner):
×
14
    def __init__(self):
×
15
        super().__init__()
×
16
        self.autofix_component = {}
×
17
        self.frequency = "daily"
×
18

19
    def add_custom_arguments(self, parser):
×
20
        parser.add_argument(
×
21
            "--frequency",
22
            help="Daily (noisy) or Hourly",
23
            choices=["daily", "hourly"],
24
            default="daily",
25
        )
26

27
    def parse_custom_arguments(self, args):
×
28
        self.frequency = args.frequency
×
29

30
    def description(self):
×
31
        return f"[Using ML] Assign a component to untriaged bugs ({self.frequency})"
×
32

33
    def columns(self):
×
34
        return ["id", "summary", "component", "confidence", "autofixed"]
×
35

36
    def sort_columns(self):
×
37
        return lambda p: (-p[3], -int(p[0]))
×
38

39
    def has_product_component(self):
×
40
        # Inject product and components when calling BzCleaner.get_bugs
41
        return True
×
42

43
    def get_bz_params(self, date):
×
44
        start_date, end_date = self.get_dates(date)
×
45

46
        bot = get_config("common", "bot_bz_mail")[0]
×
47

48
        return {
×
49
            "include_fields": ["id", "groups", "summary", "product", "component"],
50
            # Ignore bugs for which we ever modified the product or the component.
51
            "n1": 1,
52
            "f1": "product",
53
            "o1": "changedby",
54
            "v1": bot,
55
            "n2": 1,
56
            "f2": "component",
57
            "o2": "changedby",
58
            "v2": bot,
59
            # Ignore closed bugs.
60
            "bug_status": "__open__",
61
            # Get recent General bugs, and all Untriaged bugs.
62
            "j3": "OR",
63
            "f3": "OP",
64
            "j4": "AND",
65
            "f4": "OP",
66
            "f5": "component",
67
            "o5": "equals",
68
            "v5": "General",
69
            "f6": "creation_ts",
70
            "o6": "greaterthan",
71
            "v6": start_date,
72
            "f7": "CP",
73
            "f8": "component",
74
            "o8": "anyexact",
75
            "v8": "Untriaged,Foxfooding",
76
            "f9": "CP",
77
        }
78

79
    def get_bugs(self, date="today", bug_ids=[]):
×
80
        # Retrieve the bugs with the fields defined in get_bz_params
81
        raw_bugs = super().get_bugs(date=date, bug_ids=bug_ids, chunk_size=7000)
×
82

83
        if len(raw_bugs) == 0:
×
84
            return {}
×
85

86
        # Extract the bug ids
87
        bug_ids = list(raw_bugs.keys())
×
88

89
        # Classify those bugs
90
        bugs = get_bug_ids_classification("component", bug_ids)
×
91

92
        # Collect bugs classified as Fenix::General
93
        fenix_general_bug_ids = [
×
94
            bug_id
95
            for bug_id, bug_data in bugs.items()
96
            if bug_data.get("class") == "Fenix"
97
        ]
98

99
        # Reclassify the Fenix::General bugs using the fenixcomponent model
100
        if fenix_general_bug_ids:
×
101
            fenix_general_classification = get_bug_ids_classification(
×
102
                "fenixcomponent", fenix_general_bug_ids
103
            )
104

NEW
105
            fenix_confidence_threshold = self.get_config(
×
106
                name="component", entry="fenix_confidence_threshold"
107
            )
108

109
            for bug_id, data in fenix_general_classification.items():
×
110
                new_confidence = data["prob"][data["index"]]
×
111

112
                # Only reclassify if the new confidence meets the Fenix component confidence threshold
NEW
113
                if new_confidence > fenix_confidence_threshold:
×
UNCOV
114
                    bugs[bug_id] = data
×
115

116
        results = {}
×
117

118
        for bug_id in sorted(bugs.keys()):
×
119
            bug_data = bugs[bug_id]
×
120

121
            if not bug_data.get("available", True):
×
122
                # The bug was not available, it was either removed or is a
123
                # security bug
124
                continue
×
125

126
            if not {"prob", "index", "class", "extra_data"}.issubset(bug_data.keys()):
×
127
                raise Exception(f"Invalid bug response {bug_id}: {bug_data!r}")
×
128

129
            bug = raw_bugs[bug_id]
×
130
            prob = bug_data["prob"]
×
131
            index = bug_data["index"]
×
132
            suggestion = bug_data["class"]
×
133
            conflated_components_mapping = bug_data["extra_data"][
×
134
                "conflated_components_mapping"
135
            ]
136

137
            # Skip product-only suggestions that are not useful.
138
            if "::" not in suggestion and bug["product"] == suggestion:
×
139
                continue
×
140

141
            suggestion = conflated_components_mapping.get(suggestion, suggestion)
×
142

143
            if "::" not in suggestion:
×
144
                logger.error(
×
145
                    f"There is something wrong with this component suggestion! {suggestion}"
146
                )
147
                continue
×
148

149
            i = suggestion.index("::")
×
150
            suggested_product = suggestion[:i]
×
151
            suggested_component = suggestion[i + 2 :]
×
152

153
            # When moving bugs out of the 'General' component, we don't want to change the product (unless it is Firefox).
154
            if bug["component"] == "General" and bug["product"] not in {
×
155
                suggested_product,
156
                "Firefox",
157
            }:
158
                continue
×
159

160
            # Don't move bugs from Firefox::General to Core::Internationalization.
161
            if (
×
162
                bug["product"] == "Firefox"
163
                and bug["component"] == "General"
164
                and suggested_product == "Core"
165
                and suggested_component == "Internationalization"
166
            ):
167
                continue
×
168

169
            result = {
×
170
                "id": bug_id,
171
                "summary": bug["summary"],
172
                "component": suggestion,
173
                "confidence": nice_round(prob[index]),
174
                "autofixed": False,
175
            }
176

177
            # In daily mode, we send an email with all results.
178
            if self.frequency == "daily":
×
179
                results[bug_id] = result
×
180

181
            confidence_threshold_conf = (
×
182
                "confidence_threshold"
183
                if bug["component"] != "General"
184
                else "general_confidence_threshold"
185
            )
186

187
            if prob[index] >= self.get_config(confidence_threshold_conf):
×
188
                self.autofix_component[bug_id] = {
×
189
                    "product": suggested_product,
190
                    "component": suggested_component,
191
                }
192

193
                result["autofixed"] = True
×
194

195
                # In hourly mode, we send an email with only the bugs we acted upon.
196
                if self.frequency == "hourly":
×
197
                    results[bug_id] = result
×
198

199
        # Don't move bugs back into components they were moved out of.
200
        # TODO: Use the component suggestion from the service with the second highest confidence instead.
201
        def history_handler(bug):
×
202
            bug_id = str(bug["id"])
×
203

204
            previous_product_components = set()
×
205

206
            current_product = raw_bugs[bug_id]["product"]
×
207
            current_component = raw_bugs[bug_id]["component"]
×
208

209
            for history in bug["history"]:
×
210
                for change in history["changes"][::-1]:
×
211
                    if change["field_name"] == "product":
×
212
                        current_product = change["removed"]
×
213
                    elif change["field_name"] == "component":
×
214
                        current_component = change["removed"]
×
215

216
                previous_product_components.add((current_product, current_component))
×
217

218
            suggested_product = self.autofix_component[bug_id]["product"]
×
219
            suggested_component = self.autofix_component[bug_id]["component"]
×
220

221
            if (suggested_product, suggested_component) in previous_product_components:
×
222
                results[bug_id]["autofixed"] = False
×
223
                del self.autofix_component[bug_id]
×
224

225
        bugids = list(self.autofix_component.keys())
×
226
        Bugzilla(
×
227
            bugids=bugids,
228
            historyhandler=history_handler,
229
        ).get_data().wait()
230

231
        return results
×
232

233
    def get_autofix_change(self):
×
234
        cc = self.get_config("cc")
×
235
        return {
×
236
            bug_id: (
237
                data.update(
238
                    {
239
                        "cc": {"add": cc},
240
                        "comment": {
241
                            "body": f"The [Bugbug](https://github.com/mozilla/bugbug/) bot thinks this bug should belong to the '{data['product']}::{data['component']}' component, and is moving the bug to that component. Please correct in case you think the bot is wrong."
242
                        },
243
                    }
244
                )
245
                or data
246
            )
247
            for bug_id, data in self.autofix_component.items()
248
        }
249

250
    def get_db_extra(self):
×
251
        return {
×
252
            bugid: "{}::{}".format(v["product"], v["component"])
253
            for bugid, v in self.get_autofix_change().items()
254
        }
255

256

257
if __name__ == "__main__":
×
258
    Component().run()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc