@@ -60,6 +60,44 @@ class PRReview(TypedDict):
60
60
comments : List [PRReviewComment ]
61
61
62
62
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
+
63
101
def add_auth_arguments (parser : argparse .ArgumentParser ):
64
102
# Token
65
103
parser .add_argument ("--token" , help = "github auth token" )
@@ -952,39 +990,11 @@ def load_and_merge_reviews(review_files: List[pathlib.Path]) -> Optional[PRRevie
952
990
953
991
result = reviews [0 ]
954
992
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
-
983
993
comments = set ()
984
994
for review in reviews :
985
- comments .update (map (Comment , review ["comments" ]))
995
+ comments .update (map (lambda c : HashableComment ( ** c ) , review ["comments" ]))
986
996
987
- result ["comments" ] = [c .data for c in sorted (comments )]
997
+ result ["comments" ] = [c .__dict__ for c in sorted (comments )]
988
998
989
999
return result
990
1000
@@ -1032,19 +1042,14 @@ def cull_comments(pull_request: PullRequest, review, max_comments):
1032
1042
1033
1043
"""
1034
1044
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
+ ]
1048
1053
1049
1054
if len (review ["comments" ]) > max_comments :
1050
1055
review ["body" ] += (
0 commit comments