Skip to content

Commit baadf42

Browse files
committed
Use set difference to cull comments
1 parent 40e754e commit baadf42

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

post/clang_tidy_review/clang_tidy_review/__init__.py

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,44 @@ class PRReview(TypedDict):
6060
comments: List[PRReviewComment]
6161

6262

63+
class HashableComment:
64+
def __init__(self, body: str, line: int, path: str, side: str, **kwargs):
65+
self.body = body
66+
self.line = line
67+
self.path = path
68+
self.side = side
69+
70+
def __hash__(self):
71+
return hash(
72+
(
73+
self.body,
74+
self.line,
75+
self.path,
76+
self.side,
77+
)
78+
)
79+
80+
def __eq__(self, other):
81+
return (
82+
type(self) is type(other)
83+
and self.body == other.body
84+
and self.line == self.line
85+
and other.path == other.path
86+
and self.side == other.side
87+
)
88+
89+
def __lt__(self, other):
90+
if self.path != other.path:
91+
return self.path < other.path
92+
if self.line != other.line:
93+
return self.line < other.line
94+
if self.side != other.side:
95+
return self.side < other.side
96+
if self.body != other.body:
97+
return self.body < other.body
98+
return id(self) < id(other)
99+
100+
63101
def add_auth_arguments(parser: argparse.ArgumentParser):
64102
# Token
65103
parser.add_argument("--token", help="github auth token")
@@ -952,39 +990,11 @@ def load_and_merge_reviews(review_files: List[pathlib.Path]) -> Optional[PRRevie
952990

953991
result = reviews[0]
954992

955-
class Comment:
956-
def __init__(self, data):
957-
self.data = data
958-
959-
def __hash__(self):
960-
return hash(
961-
(
962-
self.data["body"],
963-
self.data["line"],
964-
self.data["path"],
965-
self.data["side"],
966-
)
967-
)
968-
969-
def __eq__(self, other):
970-
return type(other) is Comment and self.data == other.data
971-
972-
def __lt__(self, other):
973-
if self.data["path"] != other.data["path"]:
974-
return self.data["path"] < other.data["path"]
975-
if self.data["line"] != other.data["line"]:
976-
return self.data["line"] < other.data["line"]
977-
if self.data["side"] != other.data["side"]:
978-
return self.data["side"] < other.data["side"]
979-
if self.data["body"] != other.data["body"]:
980-
return self.data["body"] < other.data["body"]
981-
return hash(self) < hash(other)
982-
983993
comments = set()
984994
for review in reviews:
985-
comments.update(map(Comment, review["comments"]))
995+
comments.update(map(lambda c: HashableComment(**c), review["comments"]))
986996

987-
result["comments"] = [c.data for c in sorted(comments)]
997+
result["comments"] = [c.__dict__ for c in sorted(comments)]
988998

989999
return result
9901000

@@ -1032,19 +1042,14 @@ def cull_comments(pull_request: PullRequest, review, max_comments):
10321042
10331043
"""
10341044

1035-
comments = pull_request.get_pr_comments()
1036-
1037-
for comment in comments:
1038-
review["comments"] = list(
1039-
filter(
1040-
lambda review_comment: not (
1041-
review_comment["path"] == comment["path"]
1042-
and review_comment["line"] == comment["line"]
1043-
and review_comment["body"] == comment["body"]
1044-
),
1045-
review["comments"],
1046-
)
1047-
)
1045+
unposted_comments = set(map(lambda c: HashableComment(**c), review["comments"]))
1046+
posted_comments = set(
1047+
map(lambda c: HashableComment(**c), pull_request.get_pr_comments())
1048+
)
1049+
1050+
review["comments"] = [
1051+
c.__dict__ for c in sorted(unposted_comments - posted_comments)
1052+
]
10481053

10491054
if len(review["comments"]) > max_comments:
10501055
review["body"] += (

0 commit comments

Comments
 (0)