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

mozilla / relman-auto-nag / #5123

26 Jun 2024 08:41PM CUT coverage: 21.623% (-0.07%) from 21.692%
#5123

push

coveralls-python

benjaminmah
Added product and component into data

710 of 3628 branches covered (19.57%)

0 of 3 new or added lines in 1 file covered. (0.0%)

7 existing lines in 2 files now uncovered.

1927 of 8912 relevant lines covered (21.62%)

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.user_activity import PHAB_CHUNK_SIZE, UserActivity, UserStatus
×
16

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

20

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

24
    def __init__(self):
×
25
        super(InactivePatchAuthors, self).__init__()
×
26
        self.phab = PhabricatorAPI(utils.get_login_info()["phab_api_key"])
×
27
        self.user_activity = UserActivity(include_fields=["nick"], phab=self.phab)
×
28
        self.default_assignees = utils.get_default_assignees()
×
29
        self.people = people.People.get_instance()
×
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
        inactive_authors = self._get_inactive_patch_authors(list(rev_ids))
×
42

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

50
            if inactive_patches:
×
51
                bug["inactive_patches"] = inactive_patches
×
52
                self.unassign_inactive_author(bugid, bug, inactive_patches)
×
53
                print(f"Bug {bugid} has inactive patches: {inactive_patches}")
×
54
            else:
55
                del bugs[bugid]
×
56

57
        return bugs
×
58

59
    def unassign_inactive_author(self, bugid, bug, inactive_patches):
×
NEW
60
        prod = bug["product"]
×
NEW
61
        comp = bug["component"]
×
NEW
62
        default_assignee = self.default_assignees[prod][comp]
×
UNCOV
63
        autofix = {"assigned_to": default_assignee}
×
64

65
        comment = (
×
66
            "The patch author is inactive on Bugzilla, so the assignee is being reset."
67
        )
68
        autofix["comment"] = {"body": comment}
×
69

70
        self.autofix_changes[bugid] = autofix
×
71

72
    def _get_inactive_patch_authors(self, rev_ids: list) -> Dict[int, dict]:
×
73
        revisions: List[dict] = []
×
74

75
        for _rev_ids in Connection.chunks(rev_ids, PHAB_CHUNK_SIZE):
×
76
            for revision in self._fetch_revisions(_rev_ids):
×
77
                author_phid = revision["fields"]["authorPHID"]
×
78
                created_at = revision["fields"]["dateCreated"]
×
79
                if author_phid == "PHID-USER-eltrc7x5oplwzfguutrb":
×
80
                    continue
×
81
                revisions.append(
×
82
                    {
83
                        "rev_id": revision["id"],
84
                        "author_phid": author_phid,
85
                        "created_at": created_at,
86
                    }
87
                )
88

89
        user_phids = set()
×
90

91
        for revision in revisions:
×
92
            user_phids.add(revision["author_phid"])
×
93

94
        users = self.user_activity.get_phab_users_with_status(
×
95
            list(user_phids), keep_active=False
96
        )
97

98
        result: Dict[int, dict] = {}
×
99
        for revision in revisions:
×
100
            author_phid = revision["author_phid"]
×
101

102
            if author_phid not in users:
×
103
                continue
×
104

105
            author_info = users[author_phid]
×
106
            if author_info["status"] == UserStatus.INACTIVE:
×
107
                result[revision["rev_id"]] = {
×
108
                    "name": author_info["name"],
109
                    "status": author_info["status"],
110
                    "last_active": author_info.get("last_seen_date"),
111
                }
112

113
        return result
×
114

115
    @retry(
×
116
        wait=wait_exponential(min=4),
117
        stop=stop_after_attempt(5),
118
    )
119
    def _fetch_revisions(self, ids: list):
×
120
        return self.phab.request(
×
121
            "differential.revision.search",
122
            constraints={"ids": ids},
123
        )["data"]
124

125
    def handle_bug(self, bug, data):
×
126
        rev_ids = [
×
127
            int(attachment["file_name"][13:-8])
128
            for attachment in bug["attachments"]
129
            if attachment["content_type"] == "text/x-phabricator-request"
130
            and PHAB_FILE_NAME_PAT.match(attachment["file_name"])
131
            and not attachment["is_obsolete"]
132
        ]
133

134
        if not rev_ids:
×
135
            return
×
136

137
        bugid = str(bug["id"])
×
138
        data[bugid] = {
×
139
            "rev_ids": rev_ids,
140
            "product": bug["product"],
141
            "component": bug["component"],
142
        }
143
        return bug
×
144

145
    def get_bz_params(self, date):
×
146
        fields = [
×
147
            "comments.raw_text",
148
            "comments.creator",
149
            "attachments.file_name",
150
            "attachments.content_type",
151
            "attachments.is_obsolete",
152
            "product",
153
            "component",
154
            "assigned_to",
155
            "triage_owner",
156
        ]
157
        params = {
×
158
            "include_fields": fields,
159
            "resolution": "---",
160
            "f1": "attachments.ispatch",
161
            "o1": "equals",
162
            "v1": "1",
163
        }
164

165
        return params
×
166

167

168
if __name__ == "__main__":
×
169
    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