arrow-zerobus-sdk-wrapper 0.8.0

Cross-platform Rust SDK wrapper for Databricks Zerobus with Python bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
#!/usr/bin/env python3
"""
Fetch all comments from a GitHub Pull Request.

This script retrieves all types of comments from a PR:
- Issue comments (general PR comments)
- Review comments (comments on code)
- Review body comments (top-level review comments)

Usage:
    python fetch_pr_comments.py <owner> <repo> <pr_number> [--token TOKEN] [--status STATUS]
    python fetch_pr_comments.py <pr_url> [--token TOKEN] [--status STATUS]

Examples:
    python fetch_pr_comments.py pixie79 otlp-rust-service 123
    python fetch_pr_comments.py https://github.com/pixie79/otlp-rust-service/pull/123
    python fetch_pr_comments.py pixie79 otlp-rust-service 123 --token ghp_xxxxx
    python fetch_pr_comments.py pixie79 otlp-rust-service 123 --status open
    python fetch_pr_comments.py pixie79 otlp-rust-service 123 --status all

Environment Variables:
    GITHUB_TOKEN - GitHub personal access token (optional, can use --token instead)
"""

import argparse
import csv
import json as json_lib
import os
import re
import ssl
import sys
from datetime import datetime
from typing import Dict, List, Optional
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError

# Try to use certifi for SSL certificates (more reliable on macOS)
# certifi provides Mozilla's carefully curated collection of Root Certificates,
# which is more reliable than system certificates on macOS, especially in virtual environments.
# If certifi is not installed, we fall back to the default SSL context.
try:
    import certifi

    SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where())
except ImportError:
    # Fallback to default SSL context if certifi is not available
    SSL_CONTEXT = ssl.create_default_context()


class GitHubPRComments:
    """Fetch comments from a GitHub Pull Request."""

    def __init__(self, owner: str, repo: str, token: Optional[str] = None):
        """
        Initialize GitHub API client.

        Args:
            owner: Repository owner (username or organization)
            repo: Repository name
            token: GitHub personal access token (optional, uses GITHUB_TOKEN env var if not provided)
        """
        self.owner = owner
        self.repo = repo
        self.token = token or os.getenv("GITHUB_TOKEN")
        self.base_url = "https://api.github.com"
        self.headers = {
            "Accept": "application/vnd.github.v3+json",
            "User-Agent": "PR-Comments-Fetcher",
        }
        if self.token:
            self.headers["Authorization"] = f"token {self.token}"

    def _make_request(
        self, endpoint: str, method: str = "GET", data: Optional[Dict] = None
    ):
        """
        Make a request to GitHub API and handle pagination (for GET) or single request (for POST).

        Args:
            endpoint: API endpoint (relative to base_url)
            method: HTTP method (GET or POST)
            data: Optional data to send in POST request body

        Returns:
            List of all items from paginated response (GET) or single Dict response (POST)
        """
        if method == "POST":
            # POST request - single response, no pagination
            url = f"{self.base_url}{endpoint}"
            req_data = json_lib.dumps(data).encode("utf-8") if data else None
            req = Request(url, data=req_data, headers=self.headers, method="POST")

            try:
                with urlopen(req, context=SSL_CONTEXT) as response:
                    status_code = response.getcode()
                    headers = dict(response.headers)

                    if status_code == 401:
                        print(
                            "Error: Authentication failed (HTTP 401).",
                            file=sys.stderr,
                        )
                        if not self.token:
                            print(
                                "No GitHub token provided. Set GITHUB_TOKEN environment variable or use --token flag.",
                                file=sys.stderr,
                            )
                            print(
                                "Create a token at: https://github.com/settings/tokens",
                                file=sys.stderr,
                            )
                        else:
                            print(
                                "GitHub token is invalid or expired. Check your token or create a new one at: https://github.com/settings/tokens",
                                file=sys.stderr,
                            )
                        sys.exit(1)
                    elif status_code == 404:
                        print(
                            "Error: Not found. Check owner/repo/PR number/comment ID.",
                            file=sys.stderr,
                        )
                        sys.exit(1)
                    elif status_code == 403:
                        print(
                            "Error: Rate limit exceeded or access denied.",
                            file=sys.stderr,
                        )
                        if "X-RateLimit-Remaining" in headers:
                            print(
                                f"Rate limit remaining: {headers['X-RateLimit-Remaining']}",
                                file=sys.stderr,
                            )
                        sys.exit(1)
                    elif status_code not in (200, 201):
                        print(f"Error: HTTP {status_code}", file=sys.stderr)
                        error_body = response.read().decode("utf-8")
                        print(f"Response: {error_body}", file=sys.stderr)
                        sys.exit(1)

                    response_data = response.read().decode("utf-8")
                    return json_lib.loads(response_data)
            except HTTPError as e:
                print(f"Error: HTTP {e.code}: {e.reason}", file=sys.stderr)
                error_body = e.read().decode("utf-8") if hasattr(e, "read") else ""
                print(f"Response: {error_body}", file=sys.stderr)
                sys.exit(1)
            except URLError as e:
                print(
                    f"Error: Failed to connect to GitHub API: {e.reason}",
                    file=sys.stderr,
                )
                sys.exit(1)

        # GET request - handle pagination
        all_items = []
        page = 1
        per_page = 100

        while True:
            url = f"{self.base_url}{endpoint}?page={page}&per_page={per_page}"
            req = Request(url, headers=self.headers)

            try:
                with urlopen(req, context=SSL_CONTEXT) as response:
                    status_code = response.getcode()
                    headers = dict(response.headers)

                    if status_code == 401:
                        print(
                            "Error: Authentication failed (HTTP 401).",
                            file=sys.stderr,
                        )
                        if not self.token:
                            print(
                                "No GitHub token provided. Set GITHUB_TOKEN environment variable or use --token flag.",
                                file=sys.stderr,
                            )
                            print(
                                "Create a token at: https://github.com/settings/tokens",
                                file=sys.stderr,
                            )
                        else:
                            print(
                                "GitHub token is invalid or expired. Check your token or create a new one at: https://github.com/settings/tokens",
                                file=sys.stderr,
                            )
                        sys.exit(1)
                    elif status_code == 404:
                        print(
                            "Error: Not found. Check owner/repo/PR number.",
                            file=sys.stderr,
                        )
                        sys.exit(1)
                    elif status_code == 403:
                        print(
                            "Error: Rate limit exceeded or access denied.",
                            file=sys.stderr,
                        )
                        if "X-RateLimit-Remaining" in headers:
                            print(
                                f"Rate limit remaining: {headers['X-RateLimit-Remaining']}",
                                file=sys.stderr,
                            )
                        sys.exit(1)
                    elif status_code != 200:
                        print(f"Error: HTTP {status_code}", file=sys.stderr)
                        sys.exit(1)

                    data = response.read().decode("utf-8")
                    items = json_lib.loads(data)

                    if not items:
                        break

                    all_items.extend(items)

                    # Check if there are more pages
                    if len(items) < per_page:
                        break

                    page += 1
            except HTTPError as e:
                print(f"Error: HTTP {e.code} - {e.reason}", file=sys.stderr)
                sys.exit(1)
            except URLError as e:
                print(f"Error: {e.reason}", file=sys.stderr)
                sys.exit(1)

        return all_items

    def get_pr_info(self, pr_number: int) -> Dict:
        """
        Get basic PR information.

        Args:
            pr_number: Pull request number

        Returns:
            PR information dictionary
        """
        endpoint = f"/repos/{self.owner}/{self.repo}/pulls/{pr_number}"
        url = f"{self.base_url}{endpoint}"
        req = Request(url, headers=self.headers)

        try:
            with urlopen(req, context=SSL_CONTEXT) as response:
                status_code = response.getcode()
                if status_code == 401:
                    print(
                        "Error: Authentication failed (HTTP 401).",
                        file=sys.stderr,
                    )
                    if not self.token:
                        print(
                            "No GitHub token provided. Set GITHUB_TOKEN environment variable or use --token flag.",
                            file=sys.stderr,
                        )
                        print(
                            "Create a token at: https://github.com/settings/tokens",
                            file=sys.stderr,
                        )
                    else:
                        print(
                            "GitHub token is invalid or expired. Check your token or create a new one at: https://github.com/settings/tokens",
                            file=sys.stderr,
                        )
                    sys.exit(1)
                elif status_code != 200:
                    print(f"Error: HTTP {status_code}", file=sys.stderr)
                    sys.exit(1)
                data = response.read().decode("utf-8")
                return json_lib.loads(data)
        except HTTPError as e:
            if e.code == 401:
                print(
                    "Error: Authentication failed (HTTP 401).",
                    file=sys.stderr,
                )
                if not self.token:
                    print(
                        "No GitHub token provided. Set GITHUB_TOKEN environment variable or use --token flag.",
                        file=sys.stderr,
                    )
                    print(
                        "Create a token at: https://github.com/settings/tokens",
                        file=sys.stderr,
                    )
                else:
                    print(
                        "GitHub token is invalid or expired. Check your token or create a new one at: https://github.com/settings/tokens",
                        file=sys.stderr,
                    )
            else:
                print(f"Error: HTTP {e.code} - {e.reason}", file=sys.stderr)
            sys.exit(1)
        except URLError as e:
            print(f"Error: {e.reason}", file=sys.stderr)
            sys.exit(1)

    def get_issue_comments(self, pr_number: int) -> List[Dict]:
        """
        Get all issue comments (general PR comments).

        Args:
            pr_number: Pull request number

        Returns:
            List of issue comments
        """
        endpoint = f"/repos/{self.owner}/{self.repo}/issues/{pr_number}/comments"
        return self._make_request(endpoint)

    def get_review_comments(self, pr_number: int) -> List[Dict]:
        """
        Get all review comments (comments on code).

        Args:
            pr_number: Pull request number

        Returns:
            List of review comments
        """
        endpoint = f"/repos/{self.owner}/{self.repo}/pulls/{pr_number}/comments"
        comments = self._make_request(endpoint)

        # GitHub API doesn't include resolved status in the comments endpoint
        # We need to fetch conversation status separately
        # For now, we'll fetch all comments and mark them based on conversation status
        # Note: This requires checking conversations which may need additional API calls
        return comments

    def get_conversations(self, pr_number: int) -> List[Dict]:
        """
        Get conversation status for review comments.

        Note: GitHub's REST API doesn't directly expose conversation resolved status.
        This method would need to use GraphQL API or check conversations endpoint.

        Args:
            pr_number: Pull request number

        Returns:
            List of conversation statuses (if available)
        """
        # GitHub REST API doesn't have a direct endpoint for conversation status
        # This would require GraphQL API or checking individual conversation threads
        # For now, return empty list - filtering by resolved status is not available via REST API
        return []

    def get_reviews(self, pr_number: int) -> List[Dict]:
        """
        Get all reviews (including review body comments).

        Args:
            pr_number: Pull request number

        Returns:
            List of reviews
        """
        endpoint = f"/repos/{self.owner}/{self.repo}/pulls/{pr_number}/reviews"
        return self._make_request(endpoint)

    def get_comment_by_id(self, comment_id: int, comment_type: str) -> Optional[Dict]:
        """
        Get a specific comment by ID and type.

        Args:
            comment_id: Comment ID
            comment_type: Type of comment ("issue", "review_comment", or "review")

        Returns:
            Comment dictionary or None if not found
        """
        try:
            if comment_type == "issue":
                endpoint = (
                    f"/repos/{self.owner}/{self.repo}/issues/comments/{comment_id}"
                )
            elif comment_type == "review_comment":
                endpoint = (
                    f"/repos/{self.owner}/{self.repo}/pulls/comments/{comment_id}"
                )
            elif comment_type == "review":
                # Reviews don't have a direct endpoint by ID, need to fetch all and filter
                # For now, we'll try to get it from the PR reviews
                # This is a limitation - we'd need the PR number to fetch reviews
                print(
                    "Warning: Getting review by ID requires PR number. Use fetch_all_comments() instead.",
                    file=sys.stderr,
                )
                return None
            else:
                print(
                    f"Error: Unknown comment type: {comment_type}",
                    file=sys.stderr,
                )
                return None

            url = f"{self.base_url}{endpoint}"
            req = Request(url, headers=self.headers)

            with urlopen(req, context=SSL_CONTEXT) as response:
                if response.getcode() == 200:
                    data = response.read().decode("utf-8")
                    return json_lib.loads(data)
                elif response.getcode() == 404:
                    return None
                else:
                    print(
                        f"Error: HTTP {response.getcode()}",
                        file=sys.stderr,
                    )
                    return None
        except HTTPError as e:
            if e.code == 404:
                return None
            print(f"Error: HTTP {e.code} - {e.reason}", file=sys.stderr)
            return None
        except URLError as e:
            print(f"Error: {e.reason}", file=sys.stderr)
            return None
        except Exception as e:
            print(f"Error: {e}", file=sys.stderr)
            return None

    def _get_resolved_comment_ids(self, pr_number: int) -> set:
        """
        Get set of resolved review comment IDs using GraphQL API.

        Args:
            pr_number: Pull request number

        Returns:
            Set of resolved comment IDs (empty set if GraphQL not available or error)
        """
        if not self.token:
            # Can't use GraphQL without token
            print(
                "Warning: No GitHub token provided. Cannot check resolved status. Showing all comments.",
                file=sys.stderr,
            )
            return set()

        try:
            # Use GraphQL API to get resolved comment IDs
            graphql_url = "https://api.github.com/graphql"
            query = """
            query($owner: String!, $repo: String!, $prNumber: Int!) {
              repository(owner: $owner, name: $repo) {
                pullRequest(number: $prNumber) {
                  reviewThreads(first: 100) {
                    nodes {
                      isResolved
                      comments(first: 100) {
                        nodes {
                          id
                          databaseId
                        }
                      }
                    }
                  }
                }
              }
            }
            """

            variables = {"owner": self.owner, "repo": self.repo, "prNumber": pr_number}

            payload = json_lib.dumps({"query": query, "variables": variables}).encode(
                "utf-8"
            )
            # GraphQL API requires Bearer token format
            graphql_headers = {
                "Accept": "application/json",
                "Content-Type": "application/json",
                "User-Agent": "PR-Comments-Fetcher",
                "Authorization": f"Bearer {self.token}",
            }
            req = Request(
                graphql_url, data=payload, headers=graphql_headers, method="POST"
            )

            with urlopen(req, context=SSL_CONTEXT) as response:
                if response.getcode() != 200:
                    error_body = response.read().decode("utf-8")
                    print(
                        f"GraphQL API returned status {response.getcode()}: {error_body}",
                        file=sys.stderr,
                    )
                    return set()

                data = json_lib.loads(response.read().decode("utf-8"))
                if "errors" in data:
                    print(
                        f"GraphQL API errors: {json_lib.dumps(data['errors'], indent=2)}",
                        file=sys.stderr,
                    )
                    return set()

                resolved_ids = set()
                threads = (
                    data.get("data", {})
                    .get("repository", {})
                    .get("pullRequest", {})
                    .get("reviewThreads", {})
                    .get("nodes", [])
                )
                total_threads = len(threads)
                resolved_threads = 0

                for thread in threads:
                    is_resolved = thread.get("isResolved", False)
                    if is_resolved:
                        resolved_threads += 1
                        comments = thread.get("comments", {}).get("nodes", [])
                        for comment in comments:
                            # GraphQL returns databaseId which matches the REST API id
                            comment_id = comment.get("databaseId")
                            if comment_id:
                                resolved_ids.add(comment_id)

                if total_threads > 0:
                    print(
                        f"Found {total_threads} review threads, {resolved_threads} resolved, {len(resolved_ids)} resolved comments",
                        file=sys.stderr,
                    )
                else:
                    print(
                        "Warning: No review threads found via GraphQL. Showing all comments.",
                        file=sys.stderr,
                    )

                return resolved_ids
        except Exception as e:
            # If GraphQL fails, return empty set (show all comments)
            print(
                f"Warning: Could not fetch resolved status via GraphQL: {e}",
                file=sys.stderr,
            )
            return set()

    def fetch_all_comments(self, pr_number: int, status: str = "open") -> Dict:
        """
        Fetch all comments from a PR.

        Args:
            pr_number: Pull request number
            status: Filter by comment status - "open" (default, unresolved), "resolved", or "all"

        Returns:
            Dictionary containing PR info and all types of comments (filtered by status)
        """
        print(f"Fetching comments for PR #{pr_number}...", file=sys.stderr)

        pr_info = self.get_pr_info(pr_number)
        issue_comments = self.get_issue_comments(pr_number)
        review_comments = self.get_review_comments(pr_number)
        reviews = self.get_reviews(pr_number)

        # Filter review comments by status
        # GitHub REST API doesn't provide resolved status directly
        # We need to use GraphQL API to check conversation resolved status
        if status != "all":
            # Get conversation status using GraphQL API
            resolved_comment_ids = self._get_resolved_comment_ids(pr_number)

            filtered_review_comments = []
            for comment in review_comments:
                comment_id = comment.get("id")
                is_resolved = (
                    comment_id in resolved_comment_ids
                    if resolved_comment_ids
                    else False
                )

                if status == "open" and not is_resolved:
                    filtered_review_comments.append(comment)
                elif status == "resolved" and is_resolved:
                    filtered_review_comments.append(comment)

            review_comments = filtered_review_comments
            if status == "open":
                print(
                    "Filtering to show only open (unresolved) comments...",
                    file=sys.stderr,
                )
            elif status == "resolved":
                print("Filtering to show only resolved comments...", file=sys.stderr)

        return {
            "pr_info": pr_info,
            "issue_comments": issue_comments,
            "review_comments": review_comments,
            "reviews": reviews,
        }

    def reply_to_comment(
        self, pr_number: int, comment_id: int, comment_type: str, reply_body: str
    ) -> Dict:
        """
        Reply to a comment on a PR.

        Args:
            pr_number: Pull request number
            comment_id: ID of the comment to reply to
            comment_type: Type of comment ("issue", "review_comment", or "review")
            reply_body: Text of the reply

        Returns:
            Dictionary containing the created reply comment
        """
        if not self.token:
            print(
                "Error: GitHub token is required for posting replies", file=sys.stderr
            )
            sys.exit(1)

        if comment_type == "review_comment":
            # Reply to a review comment (code comment)
            endpoint = f"/repos/{self.owner}/{self.repo}/pulls/{pr_number}/comments/{comment_id}/replies"
            data = {"body": reply_body}
            result = self._make_request(endpoint, method="POST", data=data)
            return result
        elif comment_type == "issue":
            # Reply to an issue comment (general PR comment)
            # Note: GitHub API uses issue_number for PR comments
            endpoint = f"/repos/{self.owner}/{self.repo}/issues/{pr_number}/comments"
            data = {"body": reply_body, "in_reply_to": comment_id}
            result = self._make_request(endpoint, method="POST", data=data)
            return result
        elif comment_type == "review":
            # Reviews can't be directly replied to via API
            # Instead, we can post a general issue comment
            print(
                "Warning: Reviews cannot be directly replied to. Posting as issue comment instead.",
                file=sys.stderr,
            )
            endpoint = f"/repos/{self.owner}/{self.repo}/issues/{pr_number}/comments"
            data = {"body": reply_body}
            result = self._make_request(endpoint, method="POST", data=data)
            return result
        else:
            print(f"Error: Unknown comment type: {comment_type}", file=sys.stderr)
            sys.exit(1)

    @staticmethod
    def format_comment(comment: Dict, comment_type: str = "comment") -> str:
        """
        Format a comment for display.

        Args:
            comment: Comment dictionary from API
            comment_type: Type of comment (issue, review, review_comment)

        Returns:
            Formatted string
        """
        user = comment.get("user", {})
        user_login = (
            user.get("login", "Unknown") if isinstance(user, dict) else str(user)
        )
        created_at = comment.get("created_at", "")
        body = comment.get("body", "")
        # Handle None body
        if body is None:
            body = ""

        # Parse and format date
        if created_at:
            try:
                dt = datetime.fromisoformat(created_at.replace("Z", "+00:00"))
                date_str = dt.strftime("%Y-%m-%d %H:%M:%S UTC")
            except ValueError:
                date_str = created_at
        else:
            date_str = "Unknown date"

        # Format based on comment type
        if comment_type == "review_comment":
            path = comment.get("path", "Unknown file")
            line = comment.get("line", "?")
            original_line = comment.get("original_line")
            start_line = comment.get("start_line")
            diff_hunk = comment.get("diff_hunk", "")
            in_reply_to_id = comment.get("in_reply_to_id")
            pull_request_review_id = comment.get("pull_request_review_id")

            result = f"[Review Comment] {user_login} on {date_str}\n"
            result += f"File: {path}"
            if line:
                result += f" (line {line})"
            if original_line and original_line != line:
                result += f" (original line {original_line})"
            if start_line:
                result += f" (start line {start_line})"
            result += "\n"

            if in_reply_to_id:
                result += f"Reply to comment ID: {in_reply_to_id}\n"
            if pull_request_review_id:
                result += f"Review ID: {pull_request_review_id}\n"

            if diff_hunk:
                result += f"\nCode Context:\n{diff_hunk}\n"

            if body:
                result += f"\nComment:\n{body}\n"
            else:
                result += "\n(No comment text)\n"
        elif comment_type == "review":
            state = comment.get("state", "unknown")
            result = f"[Review] {user_login} ({state}) on {date_str}\n"
            if body:
                result += f"\nReview Body:\n{body}\n"
            else:
                result += "\n(No review body)\n"
        else:
            result = f"[Comment] {user_login} on {date_str}\n"
            if body:
                result += f"\n{body}\n"
            else:
                result += "\n(No comment text)\n"

        return result + "\n" + "-" * 80 + "\n"

    def print_csv_view(self, data: Dict, output_file: Optional[str] = None):
        """
        Print comments in CSV format.

        Args:
            data: Dictionary containing PR info and comments
            output_file: Optional file path to write CSV to (default: stdout)
        """

        pr_info = data["pr_info"]
        issue_comments = data["issue_comments"]
        review_comments = data["review_comments"]
        reviews = data["reviews"]

        if output_file:
            output = open(output_file, "w", newline="", encoding="utf-8")
        else:
            output = sys.stdout

        try:
            writer = csv.writer(output)

            # Write PR info header
            writer.writerow(
                ["PR Number", "PR Title", "Author", "State", "Created At", "URL"]
            )
            writer.writerow(
                [
                    pr_info["number"],
                    pr_info["title"],
                    (
                        pr_info["user"]["login"]
                        if isinstance(pr_info["user"], dict)
                        else "Unknown"
                    ),
                    pr_info["state"],
                    pr_info["created_at"],
                    pr_info["html_url"],
                ]
            )
            writer.writerow([])  # Empty row

            # Write issue comments
            if issue_comments:
                writer.writerow(
                    ["Comment Type", "ID", "Author", "Created At", "Body", "URL"]
                )
                for comment in issue_comments:
                    writer.writerow(
                        [
                            "issue_comment",
                            comment.get("id", ""),
                            (
                                comment.get("user", {}).get("login", "Unknown")
                                if isinstance(comment.get("user"), dict)
                                else "Unknown"
                            ),
                            comment.get("created_at", ""),
                            comment.get("body", "")
                            .replace("\n", " ")
                            .replace("\r", ""),
                            comment.get("html_url", ""),
                        ]
                    )
                writer.writerow([])  # Empty row

            # Write review comments
            if review_comments:
                writer.writerow(
                    [
                        "Comment Type",
                        "ID",
                        "Author",
                        "Created At",
                        "File",
                        "Line",
                        "Body",
                        "URL",
                    ]
                )
                for comment in review_comments:
                    writer.writerow(
                        [
                            "review_comment",
                            comment.get("id", ""),
                            (
                                comment.get("user", {}).get("login", "Unknown")
                                if isinstance(comment.get("user"), dict)
                                else "Unknown"
                            ),
                            comment.get("created_at", ""),
                            comment.get("path", ""),
                            comment.get("line", ""),
                            comment.get("body", "")
                            .replace("\n", " ")
                            .replace("\r", ""),
                            comment.get("html_url", ""),
                        ]
                    )
                writer.writerow([])  # Empty row

            # Write reviews
            if reviews:
                writer.writerow(
                    [
                        "Comment Type",
                        "ID",
                        "Author",
                        "State",
                        "Created At",
                        "Body",
                        "URL",
                    ]
                )
                for review in reviews:
                    writer.writerow(
                        [
                            "review",
                            review.get("id", ""),
                            (
                                review.get("user", {}).get("login", "Unknown")
                                if isinstance(review.get("user"), dict)
                                else "Unknown"
                            ),
                            review.get("state", ""),
                            review.get("submitted_at") or review.get("created_at", ""),
                            review.get("body", "").replace("\n", " ").replace("\r", ""),
                            review.get("html_url", ""),
                        ]
                    )
        finally:
            if output_file and output:
                output.close()
                print(f"CSV exported to {output_file}", file=sys.stderr)

    def print_table_view(self, data: Dict):
        """
        Print comments in a formatted table view.

        Args:
            data: Dictionary containing PR info and comments
        """
        pr_info = data["pr_info"]
        issue_comments = data["issue_comments"]
        review_comments = data["review_comments"]
        # Note: reviews are intentionally not included in table view (see summary comment below)

        # PR Header
        print(f"\n{'=' * 100}")
        print(f"PR #{pr_info['number']}: {pr_info['title']}")
        print(
            f"Author: {pr_info['user']['login']} | State: {pr_info['state']} | Created: {pr_info['created_at']}"
        )
        print(f"URL: {pr_info['html_url']}")
        print(f"{'=' * 100}\n")

        # Helper function to truncate text
        def truncate(text: str, max_len: int = 60) -> str:
            if not text:
                return ""
            text = text.replace("\n", " ").replace("\r", "")
            if len(text) > max_len:
                return text[: max_len - 3] + "..."
            return text

        # Issue Comments Table
        if issue_comments:
            print(f"\n{'─' * 100}")
            print(f"ISSUE COMMENTS ({len(issue_comments)} total)")
            print(f"{'─' * 100}")
            print(f"{'ID':<12} {'Author':<20} {'Date':<20} {'Comment':<48}")
            print(f"{'─' * 100}")
            for comment in issue_comments:
                comment_id = str(comment.get("id", "N/A"))
                user = (
                    comment.get("user", {}).get("login", "Unknown")
                    if isinstance(comment.get("user"), dict)
                    else "Unknown"
                )
                created = (
                    comment.get("created_at", "")[:19]
                    if comment.get("created_at")
                    else "Unknown"
                )
                body = truncate(comment.get("body", ""), 48)
                print(f"{comment_id:<12} {user:<20} {created:<20} {body:<48}")
            print(f"{'─' * 100}\n")

        # Review Comments Table
        if review_comments:
            print(f"\n{'─' * 100}")
            print(f"REVIEW COMMENTS - Code Comments ({len(review_comments)} total)")
            print(f"{'─' * 100}")
            print(f"{'ID':<12} {'Author':<20} {'File':<35} {'Line':<8} {'Comment':<25}")
            print(f"{'─' * 100}")
            for comment in review_comments:
                comment_id = str(comment.get("id", "N/A"))
                user = (
                    comment.get("user", {}).get("login", "Unknown")
                    if isinstance(comment.get("user"), dict)
                    else "Unknown"
                )
                path = comment.get("path", "Unknown")
                if len(path) > 33:
                    path = "..." + path[-30:]
                line = str(comment.get("line", "?"))
                body = truncate(comment.get("body", ""), 25)
                print(f"{comment_id:<12} {user:<20} {path:<35} {line:<8} {body:<25}")
            print(f"{'─' * 100}\n")

        # Summary (reviews not shown in table view - only comments are displayed)
        total = len(issue_comments) + len(review_comments)
        print(f"{'=' * 100}")
        print(
            f"SUMMARY: {len(issue_comments)} Issue Comments | {len(review_comments)} Review Comments | Total: {total}"
        )
        print(f"{'=' * 100}\n")

    def print_all_comments(self, data: Dict):
        """
        Print all comments in a readable format.

        Args:
            data: Dictionary containing PR info and comments
        """
        pr_info = data["pr_info"]
        print(f"\n{'=' * 80}")
        print(f"PR #{pr_info['number']}: {pr_info['title']}")
        print(f"Author: {pr_info['user']['login']}")
        print(f"State: {pr_info['state']}")
        print(f"Created: {pr_info['created_at']}")
        print(f"URL: {pr_info['html_url']}")
        print(f"{'=' * 80}\n")

        # Print issue comments
        issue_comments = data["issue_comments"]
        if issue_comments:
            print(f"\n{'=' * 80}")
            print(f"ISSUE COMMENTS ({len(issue_comments)} total)")
            print(f"{'=' * 80}\n")
            for comment in issue_comments:
                print(self.format_comment(comment, "issue"))

        # Print review comments
        review_comments = data["review_comments"]
        if review_comments:
            print(f"\n{'=' * 80}")
            print(f"REVIEW COMMENTS (Code Comments) ({len(review_comments)} total)")
            print(f"{'=' * 80}\n")
            for comment in review_comments:
                print(self.format_comment(comment, "review_comment"))

        # Print reviews
        reviews = data["reviews"]
        if reviews:
            print(f"\n{'=' * 80}")
            print(f"REVIEWS ({len(reviews)} total)")
            print(f"{'=' * 80}\n")
            for review in reviews:
                print(self.format_comment(review, "review"))

        # Summary
        total = len(issue_comments) + len(review_comments) + len(reviews)
        print(f"\n{'=' * 80}")
        print("SUMMARY")
        print(f"{'=' * 80}")
        print(f"Issue Comments: {len(issue_comments)}")
        print(f"Review Comments: {len(review_comments)}")
        print(f"Reviews: {len(reviews)}")
        print(f"Total: {total}")
        print(f"{'=' * 80}\n")


def parse_pr_url(url: str) -> tuple:
    """
    Parse a GitHub PR URL to extract owner, repo, and PR number.

    Args:
        url: GitHub PR URL

    Returns:
        Tuple of (owner, repo, pr_number)
    """
    # Pattern: https://github.com/owner/repo/pull/123
    pattern = r"github\.com/([^/]+)/([^/]+)/pull/(\d+)"
    match = re.search(pattern, url)
    if match:
        return match.groups()[0], match.groups()[1], int(match.groups()[2])
    raise ValueError(f"Invalid PR URL format: {url}")


def main():
    """Main entry point."""
    parser = argparse.ArgumentParser(
        description="Fetch all comments from a GitHub Pull Request",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=__doc__,
    )
    parser.add_argument(
        "owner_or_url",
        nargs="?",
        help="Repository owner (username/org) or full PR URL",
    )
    parser.add_argument(
        "repo",
        nargs="?",
        help="Repository name (required if owner is provided, not if URL is provided)",
    )
    parser.add_argument(
        "pr_number",
        nargs="?",
        type=int,
        help="Pull request number (required if owner/repo provided, not if URL is provided)",
    )
    parser.add_argument(
        "--token",
        help="GitHub personal access token (or set GITHUB_TOKEN env var)",
    )
    parser.add_argument(
        "--json",
        action="store_true",
        help="Output as JSON (useful for AI/automation)",
    )
    parser.add_argument(
        "--table",
        action="store_true",
        help="Output as formatted table (default: detailed text format)",
    )
    parser.add_argument(
        "--csv",
        nargs="?",
        const="",
        metavar="FILE",
        help="Output as CSV (optionally specify output file, default: stdout)",
    )
    parser.add_argument(
        "--detail",
        type=int,
        metavar="COMMENT_ID",
        help="Get details for a specific comment by ID",
    )
    parser.add_argument(
        "--comment-type",
        choices=["issue", "review_comment", "review"],
        help="Comment type for --detail or --reply (required with --detail or --reply)",
    )
    parser.add_argument(
        "--reply",
        type=int,
        metavar="COMMENT_ID",
        help="Reply to a specific comment by ID",
    )
    parser.add_argument(
        "--reply-body",
        help="Reply body text (required with --reply)",
    )
    parser.add_argument(
        "--reply-body-file",
        help="File containing reply body text (alternative to --reply-body)",
    )
    parser.add_argument(
        "--status",
        choices=["open", "resolved", "all"],
        default="open",
        help="Filter comments by status: 'open' (default, unresolved comments), 'resolved', or 'all' (all comments). Note: Requires --token for status filtering to work.",
    )

    args = parser.parse_args()

    # Parse arguments - support both URL and owner/repo/number formats
    if not args.owner_or_url:
        parser.print_help()
        sys.exit(1)

    # Check if it's a URL
    if args.owner_or_url.startswith("http"):
        try:
            owner, repo, pr_number = parse_pr_url(args.owner_or_url)
        except ValueError as e:
            print(f"Error: {e}", file=sys.stderr)
            sys.exit(1)
    else:
        if not args.repo or not args.pr_number:
            print(
                "Error: Must provide both repo and pr_number when using owner format",
                file=sys.stderr,
            )
            parser.print_help()
            sys.exit(1)
        owner = args.owner_or_url
        repo = args.repo
        pr_number = args.pr_number

    # Initialize client
    client = GitHubPRComments(owner, repo, token=args.token)

    # Handle reply to comment
    if args.reply:
        if not args.comment_type:
            print("Error: --comment-type is required with --reply", file=sys.stderr)
            sys.exit(1)
        if not args.reply_body and not args.reply_body_file:
            print(
                "Error: --reply-body or --reply-body-file is required with --reply",
                file=sys.stderr,
            )
            sys.exit(1)
        if not args.token:
            print("Error: --token is required for --reply", file=sys.stderr)
            sys.exit(1)

        reply_body = args.reply_body
        if args.reply_body_file:
            with open(args.reply_body_file, "r", encoding="utf-8") as f:
                reply_body = f.read()

        result = client.reply_to_comment(
            pr_number, args.reply, args.comment_type, reply_body
        )
        print("Reply posted successfully!")
        print(f"Comment ID: {result.get('id')}")
        print(f"URL: {result.get('html_url')}")
        sys.exit(0)

    # Handle get single comment detail
    if args.detail:
        if not args.comment_type:
            print("Error: --comment-type is required with --detail", file=sys.stderr)
            sys.exit(1)

        comment = client.get_comment_by_id(args.detail, args.comment_type)
        if not comment:
            print(f"Error: Comment {args.detail} not found", file=sys.stderr)
            sys.exit(1)

        if args.json:
            print(json_lib.dumps(comment, indent=2))
        else:
            # Format single comment
            comment_type_map = {
                "issue": "issue",
                "review_comment": "review_comment",
                "review": "review",
            }
            print(
                client.format_comment(
                    comment, comment_type_map.get(args.comment_type, "issue")
                )
            )
        sys.exit(0)

    # Fetch all comments (with status filter)
    data = client.fetch_all_comments(pr_number, status=args.status)

    # Output results
    if args.json:
        # Enhanced JSON output for AI consumption
        json_output = {
            "pr": {
                "number": data["pr_info"]["number"],
                "title": data["pr_info"]["title"],
                "author": (
                    data["pr_info"]["user"]["login"]
                    if isinstance(data["pr_info"]["user"], dict)
                    else "Unknown"
                ),
                "state": data["pr_info"]["state"],
                "created_at": data["pr_info"]["created_at"],
                "url": data["pr_info"]["html_url"],
            },
            "comments": {
                "issue_comments": [
                    {
                        "id": c.get("id"),
                        "user": (
                            c.get("user", {}).get("login", "Unknown")
                            if isinstance(c.get("user"), dict)
                            else "Unknown"
                        ),
                        "created_at": c.get("created_at"),
                        "body": c.get("body", ""),
                        "url": c.get("html_url"),
                    }
                    for c in data["issue_comments"]
                ],
                "review_comments": [
                    {
                        "id": c.get("id"),
                        "user": (
                            c.get("user", {}).get("login", "Unknown")
                            if isinstance(c.get("user"), dict)
                            else "Unknown"
                        ),
                        "created_at": c.get("created_at"),
                        "path": c.get("path"),
                        "line": c.get("line"),
                        "body": c.get("body", ""),
                        "diff_hunk": c.get("diff_hunk"),
                        "url": c.get("html_url"),
                    }
                    for c in data["review_comments"]
                ],
                "reviews": [
                    {
                        "id": r.get("id"),
                        "user": (
                            r.get("user", {}).get("login", "Unknown")
                            if isinstance(r.get("user"), dict)
                            else "Unknown"
                        ),
                        "state": r.get("state"),
                        "created_at": r.get("created_at"),
                        "body": r.get("body", ""),
                        "url": r.get("html_url"),
                    }
                    for r in data["reviews"]
                ],
            },
            "summary": {
                "total_issue_comments": len(data["issue_comments"]),
                "total_review_comments": len(data["review_comments"]),
                "total_reviews": len(data["reviews"]),
                "total_comments": len(data["issue_comments"])
                + len(data["review_comments"])
                + len(data["reviews"]),
            },
        }
        print(json_lib.dumps(json_output, indent=2))
    elif args.csv is not None:
        output_file = args.csv if args.csv else None
        client.print_csv_view(data, output_file)
    elif args.table:
        client.print_table_view(data)
    else:
        client.print_all_comments(data)


if __name__ == "__main__":
    main()