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

mozilla / relman-auto-nag / #5156

09 Jul 2024 07:35PM CUT coverage: 21.54% (-0.2%) from 21.783%
#5156

push

coveralls-python

benjaminmah
Removed initialization of `patch_activity_months` arg

716 of 3671 branches covered (19.5%)

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

286 existing lines in 6 files now uncovered.

1933 of 8974 relevant lines covered (21.54%)

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

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

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

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

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

36
    def sort_columns(self):
×
UNCOV
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

UNCOV
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

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

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

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

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

UNCOV
92
        results = {}
×
93

94
        for bug_id in sorted(bugs.keys()):
×
UNCOV
95
            bug_data = bugs[bug_id]
×
96

97
            if not bug_data.get("available", True):
×
98
                # The bug was not available, it was either removed or is a
99
                # security bug
100
                continue
×
101

UNCOV
102
            if not {"prob", "index", "class", "extra_data"}.issubset(bug_data.keys()):
×
103
                raise Exception(f"Invalid bug response {bug_id}: {bug_data!r}")
×
104

105
            bug = raw_bugs[bug_id]
×
UNCOV
106
            prob = bug_data["prob"]
×
UNCOV
107
            index = bug_data["index"]
×
UNCOV
108
            suggestion = bug_data["class"]
×
UNCOV
109
            conflated_components_mapping = bug_data["extra_data"][
×
110
                "conflated_components_mapping"
111
            ]
112

113
            # Skip product-only suggestions that are not useful.
UNCOV
114
            if "::" not in suggestion and bug["product"] == suggestion:
×
UNCOV
115
                continue
×
116

UNCOV
117
            suggestion = conflated_components_mapping.get(suggestion, suggestion)
×
118

119
            if "::" not in suggestion:
×
UNCOV
120
                logger.error(
×
121
                    f"There is something wrong with this component suggestion! {suggestion}"
122
                )
UNCOV
123
                continue
×
124

UNCOV
125
            i = suggestion.index("::")
×
126
            suggested_product = suggestion[:i]
×
127
            suggested_component = suggestion[i + 2 :]
×
128

129
            # When moving bugs out of the 'General' component, we don't want to change the product (unless it is Firefox).
130
            if bug["component"] == "General" and bug["product"] not in {
×
131
                suggested_product,
132
                "Firefox",
133
            }:
UNCOV
134
                continue
×
135

136
            # Don't move bugs from Firefox::General to Core::Internationalization.
UNCOV
137
            if (
×
138
                bug["product"] == "Firefox"
139
                and bug["component"] == "General"
140
                and suggested_product == "Core"
141
                and suggested_component == "Internationalization"
142
            ):
143
                continue
×
144

UNCOV
145
            result = {
×
146
                "id": bug_id,
147
                "summary": bug["summary"],
148
                "component": suggestion,
149
                "confidence": nice_round(prob[index]),
150
                "autofixed": False,
151
            }
152

153
            # In daily mode, we send an email with all results.
UNCOV
154
            if self.frequency == "daily":
×
UNCOV
155
                results[bug_id] = result
×
156

157
            confidence_threshold_conf = (
×
158
                "confidence_threshold"
159
                if bug["component"] != "General"
160
                else "general_confidence_threshold"
161
            )
162

UNCOV
163
            if prob[index] >= self.get_config(confidence_threshold_conf):
×
UNCOV
164
                self.autofix_component[bug_id] = {
×
165
                    "product": suggested_product,
166
                    "component": suggested_component,
167
                }
168

169
                result["autofixed"] = True
×
170

171
                # In hourly mode, we send an email with only the bugs we acted upon.
172
                if self.frequency == "hourly":
×
UNCOV
173
                    results[bug_id] = result
×
174

175
        # Don't move bugs back into components they were moved out of.
176
        # TODO: Use the component suggestion from the service with the second highest confidence instead.
UNCOV
177
        def history_handler(bug):
×
UNCOV
178
            bug_id = str(bug["id"])
×
179

UNCOV
180
            previous_product_components = set()
×
181

UNCOV
182
            current_product = raw_bugs[bug_id]["product"]
×
UNCOV
183
            current_component = raw_bugs[bug_id]["component"]
×
184

185
            for history in bug["history"]:
×
UNCOV
186
                for change in history["changes"][::-1]:
×
187
                    if change["field_name"] == "product":
×
UNCOV
188
                        current_product = change["removed"]
×
UNCOV
189
                    elif change["field_name"] == "component":
×
UNCOV
190
                        current_component = change["removed"]
×
191

UNCOV
192
                previous_product_components.add((current_product, current_component))
×
193

UNCOV
194
            suggested_product = self.autofix_component[bug_id]["product"]
×
UNCOV
195
            suggested_component = self.autofix_component[bug_id]["component"]
×
196

197
            if (suggested_product, suggested_component) in previous_product_components:
×
UNCOV
198
                results[bug_id]["autofixed"] = False
×
199
                del self.autofix_component[bug_id]
×
200

UNCOV
201
        bugids = list(self.autofix_component.keys())
×
UNCOV
202
        Bugzilla(
×
203
            bugids=bugids,
204
            historyhandler=history_handler,
205
        ).get_data().wait()
206

UNCOV
207
        return results
×
208

UNCOV
209
    def get_autofix_change(self):
×
UNCOV
210
        cc = self.get_config("cc")
×
211
        return {
×
212
            bug_id: (
213
                data.update(
214
                    {
215
                        "cc": {"add": cc},
216
                        "comment": {
217
                            "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."
218
                        },
219
                    }
220
                )
221
                or data
222
            )
223
            for bug_id, data in self.autofix_component.items()
224
        }
225

UNCOV
226
    def get_db_extra(self):
×
227
        return {
×
228
            bugid: "{}::{}".format(v["product"], v["component"])
229
            for bugid, v in self.get_autofix_change().items()
230
        }
231

232

UNCOV
233
if __name__ == "__main__":
×
234
    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