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

mozilla / relman-auto-nag / #5127

28 Jun 2024 04:52PM CUT coverage: 21.649% (+0.02%) from 21.627%
#5127

push

coveralls-python

benjaminmah
Added exception handler for fetching user status from Phabricator

716 of 3630 branches covered (19.72%)

2 of 17 new or added lines in 2 files covered. (11.76%)

2 existing lines in 2 files now uncovered.

1935 of 8938 relevant lines covered (21.65%)

0.22 hits per line

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

0.0
/bugbot/rules/inactive_patch_author.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
import logging
×
6
import re
×
7
from typing import Dict, List
×
8

9
from libmozdata.connection import Connection
×
10
from libmozdata.phabricator import PhabricatorAPI
×
11
from tenacity import retry, stop_after_attempt, wait_exponential
×
12

13
from bugbot import people, utils
×
14
from bugbot.bzcleaner import BzCleaner
×
15
from bugbot.nag_me import Nag
×
16
from bugbot.user_activity import PHAB_CHUNK_SIZE, UserActivity, UserStatus
×
17

18
logging.basicConfig(level=logging.DEBUG)
×
19
PHAB_FILE_NAME_PAT = re.compile(r"phabricator-D([0-9]+)-url\.txt")
×
20

21

22
class InactivePatchAuthors(BzCleaner, Nag):
×
23
    """Bugs with patches authored by inactive patch authors"""
24

25
    def __init__(self):
×
26
        super(InactivePatchAuthors, self).__init__()
×
27
        self.phab = PhabricatorAPI(utils.get_login_info()["phab_api_key"])
×
28
        self.user_activity = UserActivity(include_fields=["nick"], phab=self.phab)
×
29
        self.default_assignees = utils.get_default_assignees()
×
30
        self.people = people.People.get_instance()
×
31
        self.no_bugmail = True
×
32

33
    def description(self):
×
34
        return "Bugs with inactive patch authors"
×
35

36
    def columns(self):
×
37
        return ["id", "summary"]
×
38

39
    def get_bugs(self, date="today", bug_ids=[], chunk_size=None):
×
40
        bugs = super().get_bugs(date, bug_ids, chunk_size)
×
41

42
        rev_ids = {rev_id for bug in bugs.values() for rev_id in bug["rev_ids"]}
×
43
        try:
×
44
            inactive_authors = self._get_inactive_patch_authors(list(rev_ids))
×
45
        except Exception as e:
×
46
            logging.error(f"Error fetching inactive patch authors: {e}")
×
47
            inactive_authors = {}
×
48

49
        for bugid, bug in list(bugs.items()):
×
50
            inactive_patches = [
×
51
                {"rev_id": rev_id, "author": inactive_authors[rev_id]}
52
                for rev_id in bug["rev_ids"]
53
                if rev_id in inactive_authors
54
            ]
55

56
            if inactive_patches:
×
57
                bug["inactive_patches"] = inactive_patches
×
58
                self.unassign_inactive_author(bugid, bug, inactive_patches)
×
59
                self.add([bug["assigned_to"], bug["triage_owner"]], bug)
×
60
            else:
61
                del bugs[bugid]
×
62

63
        return bugs
×
64

65
    def nag_template(self):
×
66
        return self.name() + ".html"
×
67

68
    def unassign_inactive_author(self, bugid, bug, inactive_patches):
×
69
        prod = bug["product"]
×
70
        comp = bug["component"]
×
71
        default_assignee = self.default_assignees[prod][comp]
×
72
        autofix = {"assigned_to": default_assignee}
×
73

74
        comment = (
×
75
            "The patch author is inactive on Bugzilla, so the assignee is being reset."
76
        )
77
        autofix["comment"] = {"body": comment}
×
78

79
        # Abandon the patches
80
        for patch in inactive_patches:
×
81
            rev_id = patch["rev_id"]
×
82
            revision = self.phab.request(
×
83
                "differential.revision.search",
84
                constraints={"ids": [rev_id]},
85
            )["data"][0]
86
            try:
×
87
                if revision["fields"]["status"]["value"] in [
×
88
                    "needs-review",
89
                    "needs-revision",
90
                    "accepted",
91
                    "changed-planned",
92
                ]:
93
                    self.phab.request(
×
94
                        "differential.revision.edit",
95
                        objectIdentifier=rev_id,
96
                        transactions=[{"type": "abandon", "value": True}],
97
                    )
98
                    logging.info(f"Abandoned patch {rev_id} for bug {bugid}.")
×
99
                else:
100
                    logging.info(f"Patch {rev_id} for bug {bugid} is already closed.")
×
101

102
            except Exception as e:
×
103
                logging.error(f"Failed to abandon patch {rev_id} for bug {bugid}: {e}")
×
104

105
        self.autofix_changes[bugid] = autofix
×
106

107
    def _get_inactive_patch_authors(self, rev_ids: list) -> Dict[int, dict]:
×
108
        revisions: List[dict] = []
×
109

110
        for _rev_ids in Connection.chunks(rev_ids, PHAB_CHUNK_SIZE):
×
111
            try:
×
112
                for revision in self._fetch_revisions(_rev_ids):
×
113
                    author_phid = revision["fields"]["authorPHID"]
×
114
                    created_at = revision["fields"]["dateCreated"]
×
115
                    # if author_phid == "PHID-USER-eltrc7x5oplwzfguutrb":
116
                    #     continue
UNCOV
117
                    revisions.append(
×
118
                        {
119
                            "rev_id": revision["id"],
120
                            "author_phid": author_phid,
121
                            "created_at": created_at,
122
                            "status": revision["fields"]["status"]["value"],
123
                        }
124
                    )
125
            except Exception as e:
×
126
                logging.error(f"Error fetching revisions: {e}")
×
127
                continue
×
128

129
        user_phids = set()
×
130

131
        for revision in revisions:
×
132
            user_phids.add(revision["author_phid"])
×
133

NEW
134
        users = self.user_activity.get_phab_users_with_status(
×
135
            list(user_phids), keep_active=False
136
        )
137

138
        result: Dict[int, dict] = {}
×
139
        for revision in revisions:
×
140
            author_phid = revision["author_phid"]
×
141

142
            if author_phid not in users:
×
143
                continue
×
144

145
            author_info = users[author_phid]
×
146
            if author_info["status"] == UserStatus.INACTIVE:
×
147
                result[revision["rev_id"]] = {
×
148
                    "name": author_info["name"],
149
                    "status": author_info["status"],
150
                    "last_active": author_info.get("last_seen_date"),
151
                }
152

153
        return result
×
154

155
    @retry(
×
156
        wait=wait_exponential(min=4),
157
        stop=stop_after_attempt(5),
158
    )
159
    def _fetch_revisions(self, ids: list):
×
160
        return self.phab.request(
×
161
            "differential.revision.search",
162
            constraints={"ids": ids},
163
        )["data"]
164

165
    def handle_bug(self, bug, data):
×
166
        rev_ids = [
×
167
            int(attachment["file_name"][13:-8])
168
            for attachment in bug["attachments"]
169
            if attachment["content_type"] == "text/x-phabricator-request"
170
            and PHAB_FILE_NAME_PAT.match(attachment["file_name"])
171
            and not attachment["is_obsolete"]
172
        ]
173

174
        if not rev_ids:
×
175
            return
×
176

177
        bugid = str(bug["id"])
×
178
        data[bugid] = {
×
179
            "rev_ids": rev_ids,
180
            "product": bug["product"],
181
            "component": bug["component"],
182
            "assigned_to": bug["assigned_to"],
183
            "triage_owner": bug["triage_owner"],
184
        }
185
        return bug
×
186

187
    def get_bz_params(self, date):
×
188
        fields = [
×
189
            "comments.raw_text",
190
            "comments.creator",
191
            "attachments.file_name",
192
            "attachments.content_type",
193
            "attachments.is_obsolete",
194
            "product",
195
            "component",
196
            "assigned_to",
197
            "triage_owner",
198
        ]
199
        params = {
×
200
            "include_fields": fields,
201
            "resolution": "---",
202
            "f1": "attachments.ispatch",
203
            "o1": "equals",
204
            "v1": "1",
205
        }
206

207
        return params
×
208

209

210
if __name__ == "__main__":
×
211
    InactivePatchAuthors().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