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

mozilla / relman-auto-nag / #5112

21 Jun 2024 06:38PM CUT coverage: 21.722% (-0.2%) from 21.876%
#5112

push

coveralls-python

benjaminmah
Added check for inactive patch authors

716 of 3628 branches covered (19.74%)

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

144 existing lines in 3 files now uncovered.

1933 of 8899 relevant lines covered (21.72%)

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 re
×
6
from typing import Dict, List
×
7

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

12
from bugbot import utils
×
13
from bugbot.bzcleaner import BzCleaner
×
14
from bugbot.user_activity import PHAB_CHUNK_SIZE, UserActivity, UserStatus
×
15

16
PHAB_FILE_NAME_PAT = re.compile(r"phabricator-D([0-9]+)-url\.txt")
×
17

18

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

22
    def __init__(self):
×
23
        super(InactivePatchAuthors, self).__init__()
×
24
        self.phab = PhabricatorAPI(utils.get_login_info()["phab_api_key"])
×
25
        self.user_activity = UserActivity(include_fields=["nick"], phab=self.phab)
×
26

27
    def description(self):
×
28
        return "Bugs with inactive patch authors"
×
29

30
    def columns(self):
×
31
        return ["id", "summary", "authors"]
×
32

33
    def get_bugs(self, date="today", bug_ids=[], chunk_size=None):
×
34
        bugs = super().get_bugs(date, bug_ids, chunk_size)
×
35
        rev_ids = {rev_id for bug in bugs.values() for rev_id in bug["rev_ids"]}
×
36
        inactive_authors = self._get_inactive_patch_authors(list(rev_ids))
×
37

38
        for bugid, bug in list(bugs.items()):
×
39
            inactive_patches = [
×
40
                {"rev_id": rev_id, "author": inactive_authors[rev_id]}
41
                for rev_id in bug["rev_ids"]
42
                if rev_id in inactive_authors
43
            ]
44

45
            if inactive_patches:
×
NEW
46
                bug["inactive_patches"] = inactive_patches
×
NEW
47
                print(f"Bug {bugid} has inactive patches: {inactive_patches}")
×
48
            else:
49
                del bugs[bugid]
×
50

51
        return bugs
×
52

53
    def _get_inactive_patch_authors(self, rev_ids: list) -> Dict[int, dict]:
×
54
        revisions: List[dict] = []
×
55

56
        for _rev_ids in Connection.chunks(rev_ids, PHAB_CHUNK_SIZE):
×
57
            for revision in self._fetch_revisions(_rev_ids):
×
58
                author_phid = revision["fields"]["authorPHID"]
×
59
                created_at = revision["fields"]["dateCreated"]
×
NEW
60
                if author_phid == "PHID-USER-eltrc7x5oplwzfguutrb":
×
NEW
61
                    continue
×
62
                revisions.append(
×
63
                    {
64
                        "rev_id": revision["id"],
65
                        "author_phid": author_phid,
66
                        "created_at": created_at,
67
                    }
68
                )
69

NEW
70
        user_phids = set()
×
71

NEW
72
        for revision in revisions:
×
NEW
73
            user_phids.add(revision["author_phid"])
×
74

75
        users = self.user_activity.get_phab_users_with_status(
×
76
            list(user_phids), keep_active=False
77
        )
78

79
        result: Dict[int, dict] = {}
×
80
        for revision in revisions:
×
NEW
81
            author_phid = revision["author_phid"]
×
82

NEW
83
            if author_phid not in users:
×
NEW
84
                continue
×
85

NEW
86
            author_info = users[author_phid]
×
NEW
87
            if author_info["status"] == UserStatus.INACTIVE:
×
88
                result[revision["rev_id"]] = {
×
89
                    "name": author_info["name"],
90
                    "status": author_info["status"],
91
                    "last_active": author_info.get("last_seen_date"),
92
                }
93

94
        return result
×
95

96
    @retry(
×
97
        wait=wait_exponential(min=4),
98
        stop=stop_after_attempt(5),
99
    )
100
    def _fetch_revisions(self, ids: list):
×
101
        return self.phab.request(
×
102
            "differential.revision.search",
103
            constraints={"ids": ids},
104
        )["data"]
105

106
    def handle_bug(self, bug, data):
×
107
        rev_ids = [
×
108
            int(attachment["file_name"][13:-8])
109
            for attachment in bug["attachments"]
110
            if attachment["content_type"] == "text/x-phabricator-request"
111
            and PHAB_FILE_NAME_PAT.match(attachment["file_name"])
112
            and not attachment["is_obsolete"]
113
        ]
114

115
        if not rev_ids:
×
116
            return
×
117

118
        bugid = str(bug["id"])
×
119
        data[bugid] = {
×
120
            "rev_ids": rev_ids,
121
        }
122
        return bug
×
123

124
    def get_bz_params(self, date):
×
125
        fields = [
×
126
            "comments.raw_text",
127
            "comments.creator",
128
            "attachments.file_name",
129
            "attachments.content_type",
130
            "attachments.is_obsolete",
131
        ]
132
        params = {
×
133
            "include_fields": fields,
134
            "resolution": "---",
135
            "f1": "attachments.ispatch",
136
            "o1": "equals",
137
            "v1": "1",
138
        }
139

140
        return params
×
141

142

143
if __name__ == "__main__":
×
144
    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