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

mozilla / relman-auto-nag / #5466

22 Apr 2025 04:20PM UTC coverage: 21.669%. Remained the same
#5466

push

coveralls-python

benjaminmah
Removed docstring, as we already have description

716 of 3630 branches covered (19.72%)

1935 of 8930 relevant lines covered (21.67%)

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 ConduitError, 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
    def __init__(self):
×
24
        super().__init__()
×
25
        self.phab = PhabricatorAPI(utils.get_login_info()["phab_api_key"])
×
26
        self.user_activity = UserActivity(phab=self.phab)
×
27
        self.default_assignees = utils.get_default_assignees()
×
28
        self.people = people.People.get_instance()
×
29
        self.no_bugmail = True
×
30

31
    def description(self):
×
32
        return "Bugs with inactive patch authors"
×
33

34
    def columns(self):
×
35
        return ["id", "summary"]
×
36

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

40
        rev_ids = {rev_id for bug in bugs.values() for rev_id in bug["rev_ids"]}
×
41

42
        inactive_authors = self._get_inactive_patch_authors(list(rev_ids))
×
43

44
        for bugid, bug in list(bugs.items()):
×
45
            inactive_patches = [
×
46
                {"rev_id": rev_id, "author": inactive_authors[rev_id]}
47
                for rev_id in bug["rev_ids"]
48
                if rev_id in inactive_authors
49
            ]
50

51
            if inactive_patches:
×
52
                bug["inactive_patches"] = inactive_patches
×
53
                self.unassign_inactive_author(bugid, bug, inactive_patches)
×
54
                self.add([bug["assigned_to"], bug["triage_owner"]], bug)
×
55
            else:
56
                del bugs[bugid]
×
57

58
        return bugs
×
59

60
    def nag_template(self):
×
61
        return self.name() + ".html"
×
62

63
    def unassign_inactive_author(self, bugid, bug, inactive_patches):
×
64
        prod = bug["product"]
×
65
        comp = bug["component"]
×
66
        default_assignee = self.default_assignees[prod][comp]
×
67
        autofix = {"assigned_to": default_assignee}
×
68

69
        comment = (
×
70
            "The patch author is inactive on Bugzilla, so the assignee is being reset."
71
        )
72
        autofix["comment"] = {"body": comment}
×
73

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

97
            except ConduitError as e:
×
98
                logging.error(f"Failed to abandon patch {rev_id} for bug {bugid}: {e}")
×
99

100
        self.autofix_changes[bugid] = autofix
×
101

102
    def _get_inactive_patch_authors(self, rev_ids: list) -> Dict[int, dict]:
×
103
        revisions: List[dict] = []
×
104

105
        for _rev_ids in Connection.chunks(rev_ids, PHAB_CHUNK_SIZE):
×
106
            for revision in self._fetch_revisions(_rev_ids):
×
107
                author_phid = revision["fields"]["authorPHID"]
×
108
                created_at = revision["fields"]["dateCreated"]
×
109
                revisions.append(
×
110
                    {
111
                        "rev_id": revision["id"],
112
                        "author_phid": author_phid,
113
                        "created_at": created_at,
114
                        "status": revision["fields"]["status"]["value"],
115
                    }
116
                )
117

118
        user_phids = set()
×
119

120
        for revision in revisions:
×
121
            user_phids.add(revision["author_phid"])
×
122

123
        users = self.user_activity.get_phab_users_with_status(
×
124
            list(user_phids), keep_active=False
125
        )
126

127
        result: Dict[int, dict] = {}
×
128
        for revision in revisions:
×
129
            author_phid = revision["author_phid"]
×
130

131
            if author_phid not in users:
×
132
                continue
×
133

134
            author_info = users[author_phid]
×
135
            if author_info["status"] == UserStatus.INACTIVE:
×
136
                result[revision["rev_id"]] = {
×
137
                    "name": author_info["name"],
138
                    "status": author_info["status"],
139
                    "last_active": author_info.get("last_seen_date"),
140
                }
141

142
        return result
×
143

144
    @retry(
×
145
        wait=wait_exponential(min=4),
146
        stop=stop_after_attempt(5),
147
    )
148
    def _fetch_revisions(self, ids: list):
×
149
        return self.phab.request(
×
150
            "differential.revision.search",
151
            constraints={"ids": ids},
152
        )["data"]
153

154
    def handle_bug(self, bug, data):
×
155
        rev_ids = [
×
156
            int(attachment["file_name"][13:-8])
157
            for attachment in bug["attachments"]
158
            if attachment["content_type"] == "text/x-phabricator-request"
159
            and PHAB_FILE_NAME_PAT.match(attachment["file_name"])
160
            and not attachment["is_obsolete"]
161
        ]
162

163
        if not rev_ids:
×
164
            return
×
165

166
        bugid = str(bug["id"])
×
167
        data[bugid] = {
×
168
            "rev_ids": rev_ids,
169
            "product": bug["product"],
170
            "component": bug["component"],
171
            "assigned_to": bug["assigned_to"],
172
            "triage_owner": bug["triage_owner"],
173
        }
174
        return bug
×
175

176
    def get_bz_params(self, date):
×
177
        fields = [
×
178
            "comments.raw_text",
179
            "comments.creator",
180
            "attachments.file_name",
181
            "attachments.content_type",
182
            "attachments.is_obsolete",
183
            "product",
184
            "component",
185
            "assigned_to",
186
            "triage_owner",
187
        ]
188
        params = {
×
189
            "include_fields": fields,
190
            "resolution": "---",
191
            "f1": "attachments.ispatch",
192
            "o1": "equals",
193
            "v1": "1",
194
        }
195

196
        return params
×
197

198

199
if __name__ == "__main__":
×
200
    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

© 2026 Coveralls, Inc