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

mozilla / relman-auto-nag / #5124

27 Jun 2024 02:30PM CUT coverage: 21.678% (+0.06%) from 21.623%
#5124

push

coveralls-python

benjaminmah
Removed sending bugmail and added nagging

716 of 3628 branches covered (19.74%)

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

1 existing line in 1 file now uncovered.

1933 of 8917 relevant lines covered (21.68%)

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
×
NEW
15
from bugbot.nag_me import Nag
×
UNCOV
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

NEW
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()
×
NEW
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
        inactive_authors = self._get_inactive_patch_authors(list(rev_ids))
×
44

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

52
            if inactive_patches:
×
53
                bug["inactive_patches"] = inactive_patches
×
54
                self.unassign_inactive_author(bugid, bug, inactive_patches)
×
55
                print(f"Bug {bugid} has inactive patches: {inactive_patches}")
×
NEW
56
                self.add([bug["assigned_to"], bug["triage_owner"]], bug)
×
57
            else:
58
                del bugs[bugid]
×
59

60
        return bugs
×
61

NEW
62
    def nag_template(self):
×
NEW
63
        return self.name() + ".html"
×
64

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

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

76
        self.autofix_changes[bugid] = autofix
×
77

78
    def _get_inactive_patch_authors(self, rev_ids: list) -> Dict[int, dict]:
×
79
        revisions: List[dict] = []
×
80

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

95
        user_phids = set()
×
96

97
        for revision in revisions:
×
98
            user_phids.add(revision["author_phid"])
×
99

100
        users = self.user_activity.get_phab_users_with_status(
×
101
            list(user_phids), keep_active=False
102
        )
103

104
        result: Dict[int, dict] = {}
×
105
        for revision in revisions:
×
106
            author_phid = revision["author_phid"]
×
107

108
            if author_phid not in users:
×
109
                continue
×
110

111
            author_info = users[author_phid]
×
112
            if author_info["status"] == UserStatus.INACTIVE:
×
113
                result[revision["rev_id"]] = {
×
114
                    "name": author_info["name"],
115
                    "status": author_info["status"],
116
                    "last_active": author_info.get("last_seen_date"),
117
                }
118

119
        return result
×
120

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

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

140
        if not rev_ids:
×
141
            return
×
142

143
        bugid = str(bug["id"])
×
144
        data[bugid] = {
×
145
            "rev_ids": rev_ids,
146
            "product": bug["product"],
147
            "component": bug["component"],
148
            "assigned_to": bug["assigned_to"],
149
            "triage_owner": bug["triage_owner"],
150
        }
151
        return bug
×
152

153
    def get_bz_params(self, date):
×
154
        fields = [
×
155
            "comments.raw_text",
156
            "comments.creator",
157
            "attachments.file_name",
158
            "attachments.content_type",
159
            "attachments.is_obsolete",
160
            "product",
161
            "component",
162
            "assigned_to",
163
            "triage_owner",
164
        ]
165
        params = {
×
166
            "include_fields": fields,
167
            "resolution": "---",
168
            "f1": "attachments.ispatch",
169
            "o1": "equals",
170
            "v1": "1",
171
        }
172

173
        return params
×
174

175

176
if __name__ == "__main__":
×
177
    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