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

mozilla / relman-auto-nag / #4077

pending completion
#4077

push

coveralls-python

suhaibmujahid
Merge remote-tracking branch 'upstream/master' into wiki-missed

549 of 3109 branches covered (17.66%)

615 of 615 new or added lines in 27 files covered. (100.0%)

1773 of 8016 relevant lines covered (22.12%)

0.22 hits per line

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

46.86
/auto_nag/user_activity.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 datetime import timedelta
1✔
6
from enum import Enum, auto
1✔
7
from typing import List, Optional
1✔
8

9
from libmozdata import utils as lmdutils
1✔
10
from libmozdata.bugzilla import BugzillaUser
1✔
11
from libmozdata.connection import Connection
1✔
12
from libmozdata.phabricator import PhabricatorAPI
1✔
13
from tenacity import retry, stop_after_attempt, wait_exponential
1✔
14

15
from auto_nag import utils
1✔
16
from auto_nag.people import People
1✔
17

18
# The chunk size here should not be more than 100; which is the maximum number of
19
# items that Phabricator could return in one response.
20
PHAB_CHUNK_SIZE = 100
1✔
21

22

23
class UserStatus(Enum):
1✔
24
    ACTIVE = auto()
1✔
25
    UNDEFINED = auto()
1✔
26
    DISABLED = auto()
1✔
27
    INACTIVE = auto()
1✔
28
    ABSENT = auto()
1✔
29
    UNAVAILABLE = auto()
1✔
30

31

32
class UserActivity:
1✔
33
    """Check the user activity on Bugzilla and Phabricator"""
34

35
    def __init__(
1✔
36
        self,
37
        activity_weeks_count: int = 26,
38
        absent_weeks_count: int = 26,
39
        unavailable_max_days: int = 7,
40
        include_fields: list = None,
41
        phab: PhabricatorAPI = None,
42
        people: People = None,
43
    ) -> None:
44
        """
45
        Constructor
46

47
        Args:
48
            activity_weeks_count: the number of weeks since last made a change
49
                to a bug before a user being considered as inactive.
50
            absent_weeks_count: the number of weeks since last loaded any page
51
                from Bugzilla before a user being considered as inactive.
52
            unavailable_max_days: a user will be considered inactive if they
53
                have more days left to be available than `unavailable_max_days`.
54
            include_fields: the list of fields to include with the the Bugzilla
55
                user object.
56
            phab: if an instance of PhabricatorAPI is not provided, it will be
57
                created when it is needed.
58
            people: if an instance of People is not provided, the global
59
                instance will be used.
60
        """
61
        self.activity_weeks_count = activity_weeks_count
1✔
62
        self.absent_weeks_count = absent_weeks_count
1✔
63
        self.include_fields = include_fields or []
1✔
64
        self.people = people if people is not None else People.get_instance()
1✔
65
        self.phab = phab
1✔
66
        self.availability_limit = (
1✔
67
            lmdutils.get_date_ymd("today") + timedelta(unavailable_max_days)
68
        ).timestamp()
69

70
        self.activity_limit = lmdutils.get_date("today", self.activity_weeks_count * 7)
1✔
71
        self.activity_limit_ts = lmdutils.get_date_ymd(self.activity_limit).timestamp()
1✔
72
        self.seen_limit = lmdutils.get_date("today", self.absent_weeks_count * 7)
1✔
73

74
    def _get_phab(self):
1✔
75
        if not self.phab:
×
76
            self.phab = PhabricatorAPI(utils.get_login_info()["phab_api_key"])
×
77

78
        return self.phab
×
79

80
    def check_users(
1✔
81
        self,
82
        user_emails: List[str],
83
        keep_active: bool = False,
84
        ignore_bots: bool = False,
85
    ) -> dict:
86
        """Check user activity using their emails
87

88
        Args:
89
            user_emails: the email addresses of the users.
90
            keep_active: whether the returned results should include the active
91
                users.
92
            ignore_bots: whether the returned results should include bot and
93
            component-watching accounts.
94

95
        Returns:
96
            A dictionary where the key is the user email and the value is the
97
                user info with the status.
98
        """
99

100
        user_statuses = {
1✔
101
            user_email: {
102
                "status": (
103
                    UserStatus.UNDEFINED
104
                    if utils.is_no_assignee(user_email)
105
                    else UserStatus.ACTIVE
106
                ),
107
                "is_employee": self.people.is_mozilla(user_email),
108
            }
109
            for user_email in user_emails
110
            if not ignore_bots or not utils.is_bot_email(user_email)
111
        }
112

113
        # Employees will always be considered active
114
        user_emails = [
1✔
115
            user_email
116
            for user_email, info in user_statuses.items()
117
            if not info["is_employee"] and info["status"] == UserStatus.ACTIVE
118
        ]
119

120
        if not keep_active:
1✔
121
            user_statuses = {
1✔
122
                user_email: info
123
                for user_email, info in user_statuses.items()
124
                if info["status"] != UserStatus.ACTIVE
125
            }
126

127
        if user_emails:
1!
128
            user_statuses.update(
1✔
129
                self.get_bz_users_with_status(user_emails, keep_active)
130
            )
131

132
        return user_statuses
1✔
133

134
    def get_status_from_bz_user(self, user: dict) -> UserStatus:
1✔
135
        """Get the user status from a Bugzilla user object."""
136

137
        if not user["can_login"]:
1✔
138
            return UserStatus.DISABLED
1✔
139

140
        if user["creation_time"] > self.seen_limit:
1✔
141
            return UserStatus.ACTIVE
1✔
142

143
        if user["last_seen_date"] is None or user["last_seen_date"] < self.seen_limit:
1✔
144
            return UserStatus.ABSENT
1✔
145

146
        if (
1!
147
            user["last_activity_time"] is None
148
            or user["last_activity_time"] < self.activity_limit
149
        ):
150
            return UserStatus.INACTIVE
×
151

152
        return UserStatus.ACTIVE
1✔
153

154
    def get_bz_users_with_status(
1✔
155
        self, id_or_name: list, keep_active: bool = True
156
    ) -> dict:
157
        """Get Bugzilla users with their activity statuses.
158

159
        Args:
160
            id_or_name: An integer user ID or login name of the user on
161
                bugzilla.
162
            keep_active: whether the returned results should include the active
163
                users.
164

165
        Returns:
166
            A dictionary where the key is the user login name and the value is
167
            the user info with the status.
168
        """
169

170
        def handler(user, data):
1✔
171
            status = self.get_status_from_bz_user(user)
1✔
172
            if keep_active or status != UserStatus.ACTIVE:
1✔
173
                user["status"] = status
1✔
174
                data[user["name"]] = user
1✔
175

176
        users: dict = {}
1✔
177
        BugzillaUser(
1✔
178
            user_data=users,
179
            user_names=id_or_name,
180
            user_handler=handler,
181
            include_fields=[
182
                "name",
183
                "can_login",
184
                "last_activity_time",
185
                "last_seen_date",
186
                "creation_time",
187
            ]
188
            + self.include_fields,
189
        ).wait()
190

191
        return users
1✔
192

193
    def _get_status_from_phab_user(self, user: dict) -> Optional[UserStatus]:
1✔
194
        if "disabled" in user["fields"]["roles"]:
×
195
            return UserStatus.DISABLED
×
196

197
        availability = user["attachments"]["availability"]
×
198
        if availability["value"] != "available":
×
199
            # We do not need to consider the user inactive they will be
200
            # available again soon.
201
            if (
×
202
                not availability["until"]
203
                or availability["until"] > self.availability_limit
204
            ):
205
                return UserStatus.UNAVAILABLE
×
206

207
        return None
×
208

209
    def get_phab_users_with_status(
1✔
210
        self, user_phids: List[str], keep_active: bool = False
211
    ) -> dict:
212
        """Get Phabricator users with their activity statuses.
213

214
        Args:
215
            user_phids: A list of user PHIDs.
216
            keep_active: whether the returned results should include the active
217
                users.
218

219
        Returns:
220
            A dictionary where the key is the user PHID and the value is
221
            the user info with the status.
222
        """
223

224
        bzid_to_phid = {
×
225
            int(user["id"]): user["phid"]
226
            for _user_phids in Connection.chunks(user_phids, PHAB_CHUNK_SIZE)
227
            for user in self._fetch_bz_user_ids(user_phids=_user_phids)
228
        }
229
        if not bzid_to_phid:
×
230
            return {}
×
231

232
        if "id" not in self.include_fields:
×
233
            self.include_fields.append("id")
×
234

235
        user_bz_ids = list(bzid_to_phid.keys())
×
236
        users = self.get_bz_users_with_status(user_bz_ids, keep_active=True)
×
237
        users = {bzid_to_phid[user["id"]]: user for user in users.values()}
×
238

239
        # To cover cases where a person is temporary off (e.g., long PTO), we
240
        # will rely on the calendar from phab.
241
        for _user_phids in Connection.chunks(user_phids, PHAB_CHUNK_SIZE):
×
242
            for phab_user in self._fetch_phab_users(_user_phids):
×
243
                user = users[phab_user["phid"]]
×
244
                phab_status = self._get_status_from_phab_user(phab_user)
×
245
                if phab_status:
×
246
                    user["status"] = phab_status
×
247

248
                elif user["status"] in (
×
249
                    UserStatus.ABSENT,
250
                    UserStatus.INACTIVE,
251
                ) and self.is_active_on_phab(phab_user["phid"]):
252
                    user["status"] = UserStatus.ACTIVE
×
253

254
                if not keep_active and user["status"] == UserStatus.ACTIVE:
×
255
                    del users[phab_user["phid"]]
×
256
                    continue
×
257

258
                user["phab_username"] = phab_user["fields"]["username"]
×
259
                user["unavailable_until"] = phab_user["attachments"]["availability"][
×
260
                    "until"
261
                ]
262

263
        return users
×
264

265
    def is_active_on_phab(self, user_phid: str) -> bool:
1✔
266
        """Check if the user has recent activities on Phabricator.
267

268
        Args:
269
            user_phid: The user PHID.
270

271
        Returns:
272
            True if the user is active on Phabricator, False otherwise.
273
        """
274

275
        feed = self._get_phab().request(
×
276
            "feed.query",
277
            filterPHIDs=[user_phid],
278
            limit=1,
279
        )
280
        for story in feed.values():
×
281
            if story["epoch"] >= self.activity_limit_ts:
×
282
                return True
×
283

284
        return False
×
285

286
    def get_string_status(self, status: UserStatus):
1✔
287
        """Get a string representation of the user status."""
288

289
        if status == UserStatus.UNDEFINED:
×
290
            return "Not specified"
×
291
        if status == UserStatus.DISABLED:
×
292
            return "Account disabled"
×
293
        if status == UserStatus.INACTIVE:
×
294
            return f"Inactive on Bugzilla in last {self.activity_weeks_count} weeks"
×
295
        if status == UserStatus.ABSENT:
×
296
            return f"Not seen on Bugzilla in last {self.absent_weeks_count} weeks"
×
297

298
        return status.name
×
299

300
    @retry(
1✔
301
        wait=wait_exponential(min=4),
302
        stop=stop_after_attempt(5),
303
    )
304
    def _fetch_phab_users(self, phids: list):
1✔
305
        if len(phids) == 0:
×
306
            return []
×
307

308
        return self._get_phab().search_users(
×
309
            constraints={"phids": phids},
310
            attachments={"availability": True},
311
        )
312

313
    @retry(
1✔
314
        wait=wait_exponential(min=4),
315
        stop=stop_after_attempt(5),
316
    )
317
    def _fetch_bz_user_ids(self, *args, **kwargs):
318
        return self._get_phab().load_bz_account(*args, **kwargs)
×
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