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

mozilla / relman-auto-nag / #3908

pending completion
#3908

push

coveralls-python

web-flow
Merge branch 'master' into topcrash-old

542 of 3008 branches covered (18.02%)

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

1761 of 7774 relevant lines covered (22.65%)

0.23 hits per line

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

23.56
/auto_nag/scripts/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

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

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

20

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

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

29

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

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

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

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

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

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

59
        return bugs
×
60

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

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

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

79
        inactive_requestee_bugs = {
×
80
            bugid
81
            for requestee, bugids in requestee_bugs.items()
82
            if requestee in inactive_users
83
            for bugid in bugids
84
        }
85

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

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

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

124
            inactive_ni = get_inactive_ni(bug)
×
125
            if len(inactive_ni) == 0:
×
126
                continue
×
127

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

137
        return res
×
138

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

146
        if (
×
147
            bug["priority"] in HIGH_PRIORITY
148
            or bug["severity"] in HIGH_SEVERITY
149
            or bug["last_change_time"] >= RECENT_BUG_LIMIT
150
        ):
151
            return NeedinfoAction.FORWARD
×
152

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

166
        if bug["severity"] == "--":
×
167
            return NeedinfoAction.FORWARD
×
168

169
        return NeedinfoAction.CLEAR
×
170

171
    @staticmethod
1✔
172
    def _clear_inactive_ni_flags(bug):
173
        return [
×
174
            {
175
                "id": flag["id"],
176
                "status": "X",
177
            }
178
            for flag in bug["inactive_ni"]
179
        ]
180

181
    @staticmethod
1✔
182
    def _needinfo_triage_owner_flag(bug):
183
        return [
×
184
            {
185
                "name": "needinfo",
186
                "requestee": bug["triage_owner"],
187
                "status": "?",
188
                "new": "true",
189
            }
190
        ]
191

192
    @staticmethod
1✔
193
    def _request_from_triage_owner(bug):
194
        reasons = []
×
195
        if bug["priority"] in HIGH_PRIORITY:
×
196
            reasons.append("high priority")
×
197
        if bug["severity"] in HIGH_SEVERITY:
×
198
            reasons.append("high severity")
×
199
        if bug["last_change_time"] >= RECENT_BUG_LIMIT:
×
200
            reasons.append("recent activity")
×
201

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

205
        comment = []
×
206
        if reasons:
×
207
            comment.append(f"since the bug has {utils.english_list(reasons)}")
×
208

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

219
        return ", ".join(comment)
×
220

221
    def add_action(self, bug):
1✔
222
        users_num = len(set([flag["requestee"] for flag in bug["inactive_ni"]]))
×
223

224
        if bug["action"] == NeedinfoAction.FORWARD:
×
225
            autofix = {
×
226
                "flags": (
227
                    self._clear_inactive_ni_flags(bug)
228
                    + self._needinfo_triage_owner_flag(bug)
229
                ),
230
                "comment": {
231
                    "body": (
232
                        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.'
233
                        f'\n:{ bug["triage_owner_nic"] }, {self._request_from_triage_owner(bug)}'
234
                    )
235
                },
236
            }
237

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

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

261
        autofix["comment"]["body"] += f"\n\n{self.get_documentation()}\n"
×
262
        self.add_prioritized_action(bug, bug["triage_owner"], autofix=autofix)
×
263

264
    def get_bug_sort_key(self, bug):
1✔
265
        return (
×
266
            bug["action"],
267
            utils.get_sort_by_bug_importance_key(bug),
268
        )
269

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

290
        return bug
×
291

292
    def get_bz_params(self, date):
1✔
293
        fields = [
1✔
294
            "type",
295
            "attachments.content_type",
296
            "attachments.is_obsolete",
297
            "triage_owner",
298
            "flags",
299
            "priority",
300
            "severity",
301
            "creation_time",
302
            "comments",
303
            "creator",
304
        ]
305

306
        params = {
1✔
307
            "include_fields": fields,
308
            "resolution": "---",
309
            "f1": "flagtypes.name",
310
            "o1": "equals",
311
            "v1": "needinfo?",
312
        }
313

314
        # Run monthly on all bugs and weekly on recently changed bugs
315
        if lmdutils.get_date_ymd(date).day > 7:
1!
316
            params.update(
×
317
                {
318
                    "f2": "anything",
319
                    "o2": "changedafter",
320
                    "v2": "-1m",
321
                }
322
            )
323

324
        return params
1✔
325

326

327
if __name__ == "__main__":
1!
328
    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