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

mozilla / relman-auto-nag / #4639

pending completion
#4639

push

coveralls-python

suhaibmujahid
[inactive_ni_pending] Don't close bugs whose status was confirmed

646 of 3460 branches covered (18.67%)

17 of 17 new or added lines in 1 file covered. (100.0%)

1829 of 8571 relevant lines covered (21.34%)

0.21 hits per line

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

22.6
/bugbot/rules/inactive_ni_pending.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 collections import defaultdict
1✔
6
from datetime import timedelta
1✔
7
from enum import IntEnum, auto
1✔
8

9
from libmozdata import utils as lmdutils
1✔
10
from libmozdata.bugzilla import Bugzilla
1✔
11

12
from bugbot import utils
1✔
13
from bugbot.bzcleaner import BzCleaner
1✔
14
from bugbot.constants import HIGH_PRIORITY, HIGH_SEVERITY, SECURITY_KEYWORDS
1✔
15
from bugbot.user_activity import UserActivity, UserStatus
1✔
16
from bugbot.utils import plural
1✔
17

18
RECENT_BUG_LIMIT = lmdutils.get_date("today", timedelta(weeks=5).days)
1✔
19
RECENT_NEEDINFO_LIMIT = lmdutils.get_date("today", timedelta(weeks=2).days)
1✔
20

21

22
class NeedinfoAction(IntEnum):
1✔
23
    FORWARD = auto()
1✔
24
    CLEAR = auto()
1✔
25
    CLOSE_BUG = auto()
1✔
26

27
    def __str__(self):
1✔
28
        return self.name.title().replace("_", " ")
×
29

30

31
class InactiveNeedinfoPending(BzCleaner):
1✔
32
    def __init__(self):
1✔
33
        super(InactiveNeedinfoPending, self).__init__()
1✔
34
        self.max_actions = utils.get_config(self.name(), "max_actions", 7)
1✔
35

36
    def get_max_actions(self):
1✔
37
        return self.max_actions
×
38

39
    def description(self):
1✔
40
        return "Bugs with needinfo pending on inactive people"
1✔
41

42
    def columns(self):
1✔
43
        return [
×
44
            "id",
45
            "summary",
46
            "inactive_ni",
47
            "inactive_ni_count",
48
            "action",
49
            "triage_owner",
50
        ]
51

52
    def get_bugs(self, *args, **kwargs):
1✔
53
        bugs = super().get_bugs(*args, **kwargs)
×
54
        bugs = self.handle_inactive_requestee(bugs)
×
55

56
        # Resolving https://github.com/mozilla/bugbot/issues/1300 should clean this
57
        # including improve the wording in the template (i.e., "See the search query on Bugzilla").
58
        self.query_url = utils.get_bz_search_url({"bug_id": ",".join(bugs.keys())})
×
59

60
        return bugs
×
61

62
    def handle_inactive_requestee(self, bugs):
1✔
63
        """
64
        Detect inactive users and filter bugs to keep only the ones with needinfo pending on
65
        inactive users.
66
        """
67
        requestee_bugs = defaultdict(list)
×
68
        for bugid, bug in bugs.items():
×
69
            for flag in bug["needinfo_flags"]:
×
70
                if "requestee" not in flag:
×
71
                    flag["requestee"] = ""
×
72

73
                requestee_bugs[flag["requestee"]].append(bugid)
×
74

75
        user_activity = UserActivity(include_fields=["groups"])
×
76
        needinfo_requestees = set(requestee_bugs.keys())
×
77
        triage_owners = {bug["triage_owner"] for bug in bugs.values()}
×
78
        inactive_users = user_activity.check_users(
×
79
            needinfo_requestees | triage_owners, ignore_bots=True
80
        )
81

82
        inactive_requestee_bugs = {
×
83
            bugid
84
            for requestee, bugids in requestee_bugs.items()
85
            if requestee in inactive_users
86
            for bugid in bugids
87
        }
88

89
        def has_canconfirm_group(user_email):
×
90
            for group in inactive_users[user_email].get("groups", []):
×
91
                if group["name"] == "canconfirm":
×
92
                    return True
×
93
            return False
×
94

95
        def get_inactive_ni(bug):
×
96
            return [
×
97
                {
98
                    "id": flag["id"],
99
                    "setter": flag["setter"],
100
                    "requestee": flag["requestee"],
101
                    "requestee_status": user_activity.get_string_status(
102
                        inactive_users[flag["requestee"]]["status"]
103
                    ),
104
                    "requestee_canconfirm": has_canconfirm_group(flag["requestee"]),
105
                }
106
                for flag in bug["needinfo_flags"]
107
                if flag["requestee"] in inactive_users
108
                and (
109
                    # Exclude recent needinfos to allow some time for external
110
                    # users to respond.
111
                    flag["modification_date"] < RECENT_NEEDINFO_LIMIT
112
                    or inactive_users[flag["requestee"]]["status"]
113
                    in [UserStatus.DISABLED, UserStatus.UNDEFINED]
114
                )
115
            ]
116

117
        res = {}
×
118
        skiplist = self.get_auto_ni_skiplist()
×
119
        for bugid, bug in bugs.items():
×
120
            if (
×
121
                bugid not in inactive_requestee_bugs
122
                or bug["triage_owner"] in inactive_users
123
                or bug["triage_owner"] in skiplist
124
            ):
125
                continue
×
126

127
            inactive_ni = get_inactive_ni(bug)
×
128
            if len(inactive_ni) == 0:
×
129
                continue
×
130

131
            bug = {
×
132
                **bug,
133
                "inactive_ni": inactive_ni,
134
                "inactive_ni_count": len(inactive_ni),
135
                "action": self.get_action_type(bug, inactive_ni),
136
            }
137
            res[bugid] = bug
×
138
            self.add_action(bug)
×
139

140
        return res
×
141

142
    @staticmethod
1✔
143
    def get_action_type(bug, inactive_ni):
1✔
144
        """
145
        Determine if should forward needinfos to the triage owner, clear the
146
        needinfos, or close the bug.
147
        """
148

149
        if (
×
150
            bug["priority"] in HIGH_PRIORITY
151
            or bug["severity"] in HIGH_SEVERITY
152
            or bug["last_change_time"] >= RECENT_BUG_LIMIT
153
            or any(keyword in SECURITY_KEYWORDS for keyword in bug["keywords"])
154
        ):
155
            return NeedinfoAction.FORWARD
×
156

157
        if (
×
158
            len(bug["needinfo_flags"]) == 1
159
            and bug["type"] == "defect"
160
            and inactive_ni[0]["requestee"] == bug["creator"]
161
            and not inactive_ni[0]["requestee_canconfirm"]
162
            and not any(
163
                attachment["content_type"] == "text/x-phabricator-request"
164
                and not attachment["is_obsolete"]
165
                for attachment in bug["attachments"]
166
            )
167
            and not was_unconfirmed(bug)
168
        ):
169
            return NeedinfoAction.CLOSE_BUG
×
170

171
        if bug["severity"] == "--":
×
172
            return NeedinfoAction.FORWARD
×
173

174
        return NeedinfoAction.CLEAR
×
175

176
    @staticmethod
1✔
177
    def _clear_inactive_ni_flags(bug):
1✔
178
        return [
×
179
            {
180
                "id": flag["id"],
181
                "status": "X",
182
            }
183
            for flag in bug["inactive_ni"]
184
        ]
185

186
    @staticmethod
1✔
187
    def _needinfo_triage_owner_flag(bug):
1✔
188
        return [
×
189
            {
190
                "name": "needinfo",
191
                "requestee": bug["triage_owner"],
192
                "status": "?",
193
                "new": "true",
194
            }
195
        ]
196

197
    @staticmethod
1✔
198
    def _request_from_triage_owner(bug):
1✔
199
        reasons = []
×
200
        if bug["priority"] in HIGH_PRIORITY:
×
201
            reasons.append("high priority")
×
202
        if bug["severity"] in HIGH_SEVERITY:
×
203
            reasons.append("high severity")
×
204
        if bug["last_change_time"] >= RECENT_BUG_LIMIT:
×
205
            reasons.append("recent activity")
×
206

207
        if len(reasons) == 0 and bug["severity"] == "--":
×
208
            return "since the bug doesn't have a severity set, could you please set the severity or close the bug?"
×
209

210
        comment = []
×
211
        if reasons:
×
212
            comment.append(f"since the bug has {utils.english_list(reasons)}")
×
213

214
        if (
×
215
            len(bug["inactive_ni"]) == 1
216
            and bug["inactive_ni"][0]["setter"] == bug["triage_owner"]
217
        ):
218
            comment.append(
×
219
                "could you please find another way to get the information or close the bug as `INCOMPLETE` if it is not actionable?"
220
            )
221
        else:
222
            comment.append("could you have a look please?")
×
223

224
        return ", ".join(comment)
×
225

226
    def add_action(self, bug):
1✔
227
        users_num = len(set([flag["requestee"] for flag in bug["inactive_ni"]]))
×
228

229
        if bug["action"] == NeedinfoAction.FORWARD:
×
230
            autofix = {
×
231
                "flags": (
232
                    self._clear_inactive_ni_flags(bug)
233
                    + self._needinfo_triage_owner_flag(bug)
234
                ),
235
                "comment": {
236
                    "body": (
237
                        f'Redirect { plural("a needinfo that is", bug["inactive_ni"], "needinfos that are") } pending on { plural("an inactive user", users_num, "inactive users") } to the triage owner.'
238
                        f'\n:{ bug["triage_owner_nic"] }, {self._request_from_triage_owner(bug)}'
239
                    )
240
                },
241
            }
242

243
        elif bug["action"] == NeedinfoAction.CLEAR:
×
244
            autofix = {
×
245
                "flags": self._clear_inactive_ni_flags(bug),
246
                "comment": {
247
                    "body": (
248
                        f'Clear { plural("a needinfo that is", bug["inactive_ni"], "needinfos that are") } pending on { plural("an inactive user", users_num, "inactive users") }.'
249
                        "\n\nInactive users most likely will not respond; "
250
                        "if the missing information is essential and cannot be collected another way, "
251
                        "the bug maybe should be closed as `INCOMPLETE`."
252
                    ),
253
                },
254
            }
255

256
        elif bug["action"] == NeedinfoAction.CLOSE_BUG:
×
257
            autofix = {
×
258
                "flags": self._clear_inactive_ni_flags(bug),
259
                "status": "RESOLVED",
260
                "resolution": "INCOMPLETE",
261
                "comment": {
262
                    "body": "A needinfo is requested from the reporter, however, the reporter is inactive on Bugzilla. Closing the bug as incomplete."
263
                },
264
            }
265

266
        autofix["comment"]["body"] += f"\n\n{self.get_documentation()}\n"
×
267
        self.add_prioritized_action(bug, bug["triage_owner"], autofix=autofix)
×
268

269
    def get_bug_sort_key(self, bug):
1✔
270
        return (
×
271
            bug["action"],
272
            utils.get_sort_by_bug_importance_key(bug),
273
        )
274

275
    def handle_bug(self, bug, data):
1✔
276
        bugid = str(bug["id"])
×
277
        triage_owner_nic = (
×
278
            bug["triage_owner_detail"]["nick"] if "triage_owner_detail" in bug else ""
279
        )
280
        data[bugid] = {
×
281
            "priority": bug["priority"],
282
            "severity": bug["severity"],
283
            "creation_time": bug["creation_time"],
284
            "last_change_time": utils.get_last_no_bot_comment_date(bug),
285
            "creator": bug["creator"],
286
            "type": bug["type"],
287
            "attachments": bug["attachments"],
288
            "triage_owner": bug["triage_owner"],
289
            "triage_owner_nic": triage_owner_nic,
290
            "is_confirmed": bug["is_confirmed"],
291
            "needinfo_flags": [
292
                flag for flag in bug["flags"] if flag["name"] == "needinfo"
293
            ],
294
            "keywords": bug["keywords"],
295
        }
296

297
        return bug
×
298

299
    def get_bz_params(self, date):
1✔
300
        fields = [
1✔
301
            "type",
302
            "attachments.content_type",
303
            "attachments.is_obsolete",
304
            "triage_owner",
305
            "flags",
306
            "priority",
307
            "severity",
308
            "creation_time",
309
            "comments",
310
            "creator",
311
            "keywords",
312
            "is_confirmed",
313
        ]
314

315
        params = {
1✔
316
            "include_fields": fields,
317
            "resolution": "---",
318
            "f1": "flagtypes.name",
319
            "o1": "equals",
320
            "v1": "needinfo?",
321
        }
322

323
        # Run monthly on all bugs and weekly on recently changed bugs
324
        if lmdutils.get_date_ymd(date).day > 7:
1!
325
            params.update(
×
326
                {
327
                    "f2": "anything",
328
                    "o2": "changedafter",
329
                    "v2": "-1m",
330
                }
331
            )
332

333
        return params
1✔
334

335

336
def was_unconfirmed(bug: dict) -> bool:
1✔
337
    """Check if a bug was unconfirmed.
338

339
    Returns:
340
        True if the bug was unconfirmed and now is confirmed, False otherwise.
341
    """
342
    if not bug["is_confirmed"]:
×
343
        return False
×
344

345
    had_unconfirmed_status = False
×
346

347
    def check_unconfirmed_in_history(bug):
×
348
        nonlocal had_unconfirmed_status
349
        for history in bug["history"]:
×
350
            for change in history["changes"]:
×
351
                if change["field_name"] == "status":
×
352
                    if change["removed"] == "UNCONFIRMED":
×
353
                        had_unconfirmed_status = True
×
354
                        return
×
355
                    break
×
356

357
    if "history" in bug:
×
358
        check_unconfirmed_in_history(bug)
×
359
    else:
360
        Bugzilla(bug["id"], historyhandler=check_unconfirmed_in_history).wait()
×
361

362
    return had_unconfirmed_status
×
363

364

365
if __name__ == "__main__":
1!
366
    InactiveNeedinfoPending().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