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

mozilla / relman-auto-nag / #5081

12 Jun 2024 08:34PM CUT coverage: 21.82% (-0.006%) from 21.826%
#5081

push

coveralls-python

benjaminmah
Added bugs that were original `Fenix::General` that were reclassified with low confidence and removed bugs that were reclassified with low confidence

716 of 3608 branches covered (19.84%)

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

1 existing line in 1 file now uncovered.

1930 of 8845 relevant lines covered (21.82%)

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

UNCOV
6
from libmozdata.bugzilla import Bugzilla
×
7

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

13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

93
        # Collect bugs classified as Fenix::General
94
        fenix_general_bug_ids = [
×
95
            bug_id
96
            for bug_id, bug_data in bugs.items()
97
            if bug_data.get("class") == "Fenix"
98
            and bug_data["prob"][bug_data["index"]]
99
            >= self.get_config("general_confidence_threshold")
100
        ]
101

NEW
102
        def get_confidence_threshold(
×
103
            bug_data, general_confidence_threshold, confidence_threshold
104
        ):
NEW
105
            if bug_data["class"] == "General":
×
NEW
106
                return general_confidence_threshold
×
NEW
107
            return confidence_threshold
×
108

109
        # Collection bugs that were originally Fenix::General but reclassified to another product with low confidence
NEW
110
        originally_fenix_general_bug_ids = [
×
111
            bug_id
112
            for bug_id, bug_data in bugs.items()
113
            if raw_bugs[bug_id]["product"] == "Fenix"
114
            and raw_bugs[bug_id]["component"] == "General"
115
            and bug_data["prob"][bug_data["index"]]
116
            <= get_confidence_threshold(
117
                bug_data,
118
                self.get_config("general_confidence_threshold"),
119
                self.get_config("confidence_threshold"),
120
            )
121
        ]
122

NEW
123
        fenix_general_bug_ids.extend(originally_fenix_general_bug_ids)
×
NEW
124
        fenix_general_bug_ids = set(fenix_general_bug_ids)
×
125

126
        # Reclassify the Fenix::General bugs using the fenixcomponent model
127
        if fenix_general_bug_ids:
×
128
            fenix_general_classification = get_bug_ids_classification(
×
129
                "fenixcomponent", fenix_general_bug_ids
130
            )
131

132
            fenix_confidence_threshold = self.get_config("fenix_confidence_threshold")
×
133

134
            for bug_id, data in fenix_general_classification.items():
×
135
                new_confidence = data["prob"][data["index"]]
×
136

137
                # Only reclassify if the new confidence meets the Fenix component confidence threshold
138
                if new_confidence > fenix_confidence_threshold:
×
139
                    data["class"] = f"Fenix::{data['class']}"
×
140
                    bugs[bug_id] = data
×
141

142
        results = {}
×
143

144
        for bug_id in sorted(bugs.keys()):
×
145
            bug_data = bugs[bug_id]
×
146

147
            if not bug_data.get("available", True):
×
148
                # The bug was not available, it was either removed or is a
149
                # security bug
150
                continue
×
151

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

155
            bug = raw_bugs[bug_id]
×
156
            prob = bug_data["prob"]
×
157
            index = bug_data["index"]
×
158
            suggestion = bug_data["class"]
×
159

160
            conflated_components_mapping = bug_data["extra_data"].get(
×
161
                "conflated_components_mapping", {}
162
            )
163

164
            # Skip product-only suggestions that are not useful.
165
            if "::" not in suggestion and bug["product"] == suggestion:
×
166
                continue
×
167

168
            if "Fenix" not in suggestion:
×
169
                suggestion = conflated_components_mapping.get(suggestion, suggestion)
×
170

171
            if "::" not in suggestion:
×
172
                logger.error(
×
173
                    f"There is something wrong with this component suggestion! {suggestion}"
174
                )
175
                continue
×
176

177
            i = suggestion.index("::")
×
178
            suggested_product = suggestion[:i]
×
179
            suggested_component = suggestion[i + 2 :]
×
180

181
            # When moving bugs out of the 'General' component, we don't want to change the product (unless it is Firefox).
182
            if bug["component"] == "General" and bug["product"] not in {
×
183
                suggested_product,
184
                "Firefox",
185
            }:
186
                continue
×
187

188
            # Don't move bugs from Firefox::General to Core::Internationalization.
189
            if (
×
190
                bug["product"] == "Firefox"
191
                and bug["component"] == "General"
192
                and suggested_product == "Core"
193
                and suggested_component == "Internationalization"
194
            ):
195
                continue
×
196

197
            result = {
×
198
                "id": bug_id,
199
                "summary": bug["summary"],
200
                "component": suggestion,
201
                "confidence": nice_round(prob[index]),
202
                "autofixed": False,
203
            }
204

205
            # In daily mode, we send an email with all results.
206
            if self.frequency == "daily":
×
207
                results[bug_id] = result
×
208

209
            confidence_threshold_conf = (
×
210
                "confidence_threshold"
211
                if bug["component"] != "General"
212
                else "general_confidence_threshold"
213
            )
214

215
            if prob[index] >= self.get_config(confidence_threshold_conf):
×
216
                self.autofix_component[bug_id] = {
×
217
                    "product": suggested_product,
218
                    "component": suggested_component,
219
                }
220

221
                result["autofixed"] = True
×
222

223
                # In hourly mode, we send an email with only the bugs we acted upon.
224
                if self.frequency == "hourly":
×
225
                    results[bug_id] = result
×
226

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

232
            previous_product_components = set()
×
233

234
            current_product = raw_bugs[bug_id]["product"]
×
235
            current_component = raw_bugs[bug_id]["component"]
×
236

237
            for history in bug["history"]:
×
238
                for change in history["changes"][::-1]:
×
239
                    if change["field_name"] == "product":
×
240
                        current_product = change["removed"]
×
241
                    elif change["field_name"] == "component":
×
242
                        current_component = change["removed"]
×
243

244
                previous_product_components.add((current_product, current_component))
×
245

246
            suggested_product = self.autofix_component[bug_id]["product"]
×
247
            suggested_component = self.autofix_component[bug_id]["component"]
×
248

249
            if (suggested_product, suggested_component) in previous_product_components:
×
250
                results[bug_id]["autofixed"] = False
×
251
                del self.autofix_component[bug_id]
×
252

253
        bugids = list(self.autofix_component.keys())
×
254
        Bugzilla(
×
255
            bugids=bugids,
256
            historyhandler=history_handler,
257
        ).get_data().wait()
258

259
        return results
×
260

261
    def get_autofix_change(self):
×
262
        cc = self.get_config("cc")
×
263
        return {
×
264
            bug_id: (
265
                data.update(
266
                    {
267
                        "cc": {"add": cc},
268
                        "comment": {
269
                            "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."
270
                        },
271
                    }
272
                )
273
                or data
274
            )
275
            for bug_id, data in self.autofix_component.items()
276
        }
277

278
    def get_db_extra(self):
×
279
        return {
×
280
            bugid: "{}::{}".format(v["product"], v["component"])
281
            for bugid, v in self.get_autofix_change().items()
282
        }
283

284

285
if __name__ == "__main__":
×
286
    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