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

mozilla / relman-auto-nag / #5422

14 Feb 2025 04:58PM CUT coverage: 21.196% (-0.004%) from 21.2%
#5422

push

coveralls-python

benjaminmah
Changed classification from Fenix::General to GeckoView::General

426 of 2952 branches covered (14.43%)

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

1 existing line in 1 file now uncovered.

1943 of 9167 relevant lines covered (21.2%)

0.21 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
        self.general_confidence_threshold = self.get_config(
×
19
            "general_confidence_threshold"
20
        )
21
        self.component_confidence_threshold = self.get_config("confidence_threshold")
×
22
        self.fenix_confidence_threshold = self.get_config("fenix_confidence_threshold")
×
23

24
    def add_custom_arguments(self, parser):
×
25
        parser.add_argument(
×
26
            "--frequency",
27
            help="Daily (noisy) or Hourly",
28
            choices=["daily", "hourly"],
29
            default="daily",
30
        )
31

32
    def parse_custom_arguments(self, args):
×
33
        self.frequency = args.frequency
×
34

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

38
    def columns(self):
×
39
        return ["id", "summary", "component", "confidence", "autofixed"]
×
40

41
    def sort_columns(self):
×
42
        return lambda p: (-p[3], -int(p[0]))
×
43

44
    def has_product_component(self):
×
45
        # Inject product and components when calling BzCleaner.get_bugs
46
        return True
×
47

48
    def get_bz_params(self, date):
×
49
        start_date, end_date = self.get_dates(date)
×
50

51
        bot = get_config("common", "bot_bz_mail")[0]
×
52

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

84
    def get_bugs(self, date="today", bug_ids=[]):
×
85
        def meets_threshold(bug_data):
×
86
            threshold = (
×
87
                self.general_confidence_threshold
88
                if bug_data["class"] == "Fenix" or bug_data["class"] == "General"
89
                else self.component_confidence_threshold
90
            )
91
            return bug_data["prob"][bug_data["index"]] >= threshold
×
92

93
        # Retrieve the bugs with the fields defined in get_bz_params
94
        raw_bugs = super().get_bugs(date=date, bug_ids=bug_ids, chunk_size=7000)
×
95

96
        if len(raw_bugs) == 0:
×
97
            return {}
×
98

99
        # Extract the bug ids
100
        bug_ids = list(raw_bugs.keys())
×
101

102
        # Classify those bugs
103
        bugs = get_bug_ids_classification("component", bug_ids)
×
104

105
        fenix_general_bug_ids = []
×
106
        for bug_id, bug_data in bugs.items():
×
107
            if not bug_data.get("available", True):
×
108
                # The bug was not available, it was either removed or is a
109
                # security bug.
110
                continue
×
111
            if meets_threshold(bug_data):
×
112
                if bug_data.get("class") == "Fenix":
×
113
                    fenix_general_bug_ids.append(bug_id)
×
114
            else:
115
                current_bug_data = raw_bugs[bug_id]
×
116
                if (
×
117
                    current_bug_data["product"] == "Fenix"
118
                    and current_bug_data["component"] == "General"
119
                ):
120
                    fenix_general_bug_ids.append(bug_id)
×
121

122
        if fenix_general_bug_ids:
×
123
            fenix_general_classification = get_bug_ids_classification(
×
124
                "fenixcomponent", fenix_general_bug_ids
125
            )
126

127
            for bug_id, data in fenix_general_classification.items():
×
128
                confidence = data["prob"][data["index"]]
×
129

130
                if confidence > self.fenix_confidence_threshold:
×
NEW
131
                    if data["class"] == "General":
×
NEW
132
                        data["class"] = "GeckoView::General"
×
133
                    else:
NEW
134
                        data["class"] = f"Fenix::{data['class']}"
×
UNCOV
135
                    bugs[bug_id] = data
×
136

137
        results = {}
×
138

139
        for bug_id in sorted(bugs.keys()):
×
140
            bug_data = bugs[bug_id]
×
141

142
            if not bug_data.get("available", True):
×
143
                # The bug was not available, it was either removed or is a
144
                # security bug
145
                continue
×
146

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

150
            bug = raw_bugs[bug_id]
×
151
            prob = bug_data["prob"]
×
152
            index = bug_data["index"]
×
153
            suggestion = bug_data["class"]
×
154

155
            conflated_components_mapping = bug_data["extra_data"].get(
×
156
                "conflated_components_mapping", {}
157
            )
158

159
            # Skip product-only suggestions that are not useful.
160
            if "::" not in suggestion and bug["product"] == suggestion:
×
161
                continue
×
162

163
            suggestion = conflated_components_mapping.get(suggestion, suggestion)
×
164

165
            if "::" not in suggestion:
×
166
                logger.error(
×
167
                    f"There is something wrong with this component suggestion! {suggestion}"
168
                )
169
                continue
×
170

171
            i = suggestion.index("::")
×
172
            suggested_product = suggestion[:i]
×
173
            suggested_component = suggestion[i + 2 :]
×
174

175
            # When moving bugs out of the 'General' component, we don't want to change the product (unless it is Firefox).
176
            if bug["component"] == "General" and bug["product"] not in {
×
177
                suggested_product,
178
                "Firefox",
179
            }:
180
                continue
×
181

182
            # Don't move bugs from Firefox::General to Core::Internationalization.
183
            if (
×
184
                bug["product"] == "Firefox"
185
                and bug["component"] == "General"
186
                and suggested_product == "Core"
187
                and suggested_component == "Internationalization"
188
            ):
189
                continue
×
190

191
            result = {
×
192
                "id": bug_id,
193
                "summary": bug["summary"],
194
                "component": suggestion,
195
                "confidence": nice_round(prob[index]),
196
                "autofixed": False,
197
            }
198

199
            # In daily mode, we send an email with all results.
200
            if self.frequency == "daily":
×
201
                results[bug_id] = result
×
202

203
            confidence_threshold_conf = (
×
204
                "confidence_threshold"
205
                if bug["component"] != "General"
206
                else "general_confidence_threshold"
207
            )
208

209
            if prob[index] >= self.get_config(confidence_threshold_conf):
×
210
                self.autofix_component[bug_id] = {
×
211
                    "product": suggested_product,
212
                    "component": suggested_component,
213
                }
214

215
                result["autofixed"] = True
×
216

217
                # In hourly mode, we send an email with only the bugs we acted upon.
218
                if self.frequency == "hourly":
×
219
                    results[bug_id] = result
×
220

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

226
            previous_product_components = set()
×
227

228
            current_product = raw_bugs[bug_id]["product"]
×
229
            current_component = raw_bugs[bug_id]["component"]
×
230

231
            for history in bug["history"]:
×
232
                for change in history["changes"][::-1]:
×
233
                    if change["field_name"] == "product":
×
234
                        current_product = change["removed"]
×
235
                    elif change["field_name"] == "component":
×
236
                        current_component = change["removed"]
×
237

238
                previous_product_components.add((current_product, current_component))
×
239

240
            suggested_product = self.autofix_component[bug_id]["product"]
×
241
            suggested_component = self.autofix_component[bug_id]["component"]
×
242

243
            if (suggested_product, suggested_component) in previous_product_components:
×
244
                results[bug_id]["autofixed"] = False
×
245
                del self.autofix_component[bug_id]
×
246

247
        bugids = list(self.autofix_component.keys())
×
248
        Bugzilla(
×
249
            bugids=bugids,
250
            historyhandler=history_handler,
251
        ).get_data().wait()
252

253
        return results
×
254

255
    def get_autofix_change(self):
×
256
        cc = self.get_config("cc")
×
257
        return {
×
258
            bug_id: (
259
                data.update(
260
                    {
261
                        "cc": {"add": cc},
262
                        "comment": {
263
                            "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."
264
                        },
265
                    }
266
                )
267
                or data
268
            )
269
            for bug_id, data in self.autofix_component.items()
270
        }
271

272
    def get_db_extra(self):
×
273
        return {
×
274
            bugid: "{}::{}".format(v["product"], v["component"])
275
            for bugid, v in self.get_autofix_change().items()
276
        }
277

278

279
if __name__ == "__main__":
×
280
    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