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

mozilla / relman-auto-nag / #5122

26 Jun 2024 07:16PM CUT coverage: 21.692% (-0.2%) from 21.862%
#5122

push

coveralls-python

benjaminmah
Reverted changes

716 of 3628 branches covered (19.74%)

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

182 existing lines in 4 files now uncovered.

1933 of 8911 relevant lines covered (21.69%)

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

NEW
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

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

20

NEW
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

NEW
40
        logging.info(f"BUGS RETRIEVED > {bugs}")
×
41
        rev_ids = {rev_id for bug in bugs.values() for rev_id in bug["rev_ids"]}
×
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
                print(f"Bug {bugid} has inactive patches: {inactive_patches}")
×
55
            else:
56
                del bugs[bugid]
×
57

58
        return bugs
×
59

60
    def unassign_inactive_author(self, bugid, bug, inactive_patches):
×
61
        # print(f"\n BUG >> {bugid} >> {bug}\n")
62
        # prod = bug["product"]
63
        # comp = bug["component"]
NEW
64
        default_assignee = "bmah@mozilla.com"
×
65
        autofix = {"assigned_to": default_assignee}
×
66

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

72
        self.autofix_changes[bugid] = autofix
×
73

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

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

91
        user_phids = set()
×
92

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

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

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

104
            if author_phid not in users:
×
105
                continue
×
106

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

115
        return result
×
116

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

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

136
        if not rev_ids:
×
137
            return
×
138

139
        bugid = str(bug["id"])
×
140
        data[bugid] = {
×
141
            "rev_ids": rev_ids,
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
        ]
155
        params = {
×
156
            "include_fields": fields,
157
            "resolution": "---",
158
            "f1": "attachments.ispatch",
159
            "o1": "equals",
160
            "v1": "1",
161
        }
162

163
        return params
×
164

165

166
if __name__ == "__main__":
×
167
    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