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

mozilla / relman-auto-nag / #5418

12 Feb 2025 08:39PM CUT coverage: 21.196%. Remained the same
#5418

push

coveralls-python

benjaminmah
Added a continue for Fenix::General bugs being moved to Fenix::General

426 of 2952 branches covered (14.43%)

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

2 existing lines 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

131
                if confidence > self.fenix_confidence_threshold:
×
132
                    data["class"] = f"Fenix::{data['class']}"
×
133
                    bugs[bug_id] = data
×
134

135
        results = {}
×
136

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

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

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

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

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

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

161
            # No need to move a Fenix::General bug to Fenix::General
NEW
162
            if (
×
163
                bug["product"] == "Fenix" and bug["component"] == "General"
164
            ) and suggestion == "Fenix::General":
NEW
165
                continue
×
166

UNCOV
167
            suggestion = conflated_components_mapping.get(suggestion, suggestion)
×
168

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

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

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

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

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

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

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

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

219
                result["autofixed"] = True
×
220

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

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

230
            previous_product_components = set()
×
231

232
            current_product = raw_bugs[bug_id]["product"]
×
233
            current_component = raw_bugs[bug_id]["component"]
×
234

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

242
                previous_product_components.add((current_product, current_component))
×
243

244
            suggested_product = self.autofix_component[bug_id]["product"]
×
245
            suggested_component = self.autofix_component[bug_id]["component"]
×
246

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

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

257
        return results
×
258

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

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

282

283
if __name__ == "__main__":
×
284
    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