1+ # Create a repository with a file so it has a default branch
2+ exec gh repo create $ORG/$SCRIPT_NAME-$RANDOM_STRING --add-readme --private
3+
4+ # Defer repo cleanup
5+ defer gh repo delete --yes $ORG/$SCRIPT_NAME-$RANDOM_STRING
6+
7+ # Clone the repo
8+ exec gh repo clone $ORG/$SCRIPT_NAME-$RANDOM_STRING
9+ cd $SCRIPT_NAME-$RANDOM_STRING
10+
11+ # Enable discussions
12+ exec gh api repos/$ORG/$SCRIPT_NAME-$RANDOM_STRING -X PATCH -F has_discussions=true
13+
14+ # Create a discussion to comment on
15+ exec gh discussion create --title 'Comment Test' --body 'Discussion for comment tests' --category 'General'
16+ stdout2env DISCUSSION_URL
17+
18+ # Add a top-level comment
19+ exec gh discussion comment $DISCUSSION_URL --body 'Comment from flag'
20+ stdout 'discussioncomment'
21+
22+ # Add another comment from file
23+ exec gh discussion comment $DISCUSSION_URL --body-file $WORK/comment-body.txt
24+ stdout 'discussioncomment'
25+
26+ # Verify comments appear in view
27+ exec gh discussion view $DISCUSSION_URL --comments --order oldest --json comments
28+ stdout2env COMMENTS_JSON
29+ jq-assert COMMENTS_JSON '.comments.nodes | length' '^2$'
30+ jq-assert COMMENTS_JSON '.comments.nodes[0].body' 'Comment from flag'
31+ jq-assert COMMENTS_JSON '.comments.nodes[1].body' 'Comment from file'
32+
33+ # Get the first comment ID for reply and edit tests
34+ jq2env COMMENTS_JSON '.comments.nodes[0].id' FIRST_COMMENT_ID
35+ jq2env COMMENTS_JSON '.comments.nodes[1].id' SECOND_COMMENT_ID
36+
37+ # Add a reply to the first comment
38+ exec gh discussion comment $FIRST_COMMENT_ID --body 'Reply to first'
39+ stdout 'discussioncomment'
40+
41+ # Add a reply to the second comment from file
42+ exec gh discussion comment $SECOND_COMMENT_ID --body-file $WORK/reply-body.txt
43+ stdout 'discussioncomment'
44+
45+ # Verify the reply appears
46+ exec gh discussion view $FIRST_COMMENT_ID --json comments
47+ stdout2env REPLIES_JSON
48+ jq-assert REPLIES_JSON '.comments.nodes[0].replies.nodes | length' '^1$'
49+ jq-assert REPLIES_JSON '.comments.nodes[0].replies.nodes[0].body' 'Reply to first'
50+ jq2env REPLIES_JSON '.comments.nodes[0].replies.nodes[0].id' FIRST_REPLY_ID
51+
52+ exec gh discussion view $SECOND_COMMENT_ID --json comments
53+ stdout2env REPLIES_JSON
54+ jq-assert REPLIES_JSON '.comments.nodes[0].replies.nodes | length' '^1$'
55+ jq-assert REPLIES_JSON '.comments.nodes[0].replies.nodes[0].body' 'Reply from file'
56+ jq2env REPLIES_JSON '.comments.nodes[0].replies.nodes[0].id' SECOND_REPLY_ID
57+
58+ # Edit the first comment
59+ exec gh discussion comment $FIRST_COMMENT_ID --edit --body 'Edited first comment'
60+ stdout 'discussioncomment'
61+
62+ # Edit the second comment from file
63+ exec gh discussion comment $SECOND_COMMENT_ID --edit --body-file $WORK/edit-comment-body.txt
64+ stdout 'discussioncomment'
65+
66+ # Edit the first reply
67+ exec gh discussion comment $FIRST_REPLY_ID --edit --body 'Edited first reply'
68+ stdout 'discussioncomment'
69+
70+ # Edit the second reply from file
71+ exec gh discussion comment $SECOND_REPLY_ID --edit --body-file $WORK/edit-reply-body.txt
72+ stdout 'discussioncomment'
73+
74+ # Verify edits appear
75+ exec gh discussion view $DISCUSSION_URL --comments --order oldest --json comments
76+ stdout2env EDITED_COMMENTS_JSON
77+ jq-assert EDITED_COMMENTS_JSON '.comments.nodes[0].body' 'Edited first comment'
78+ jq-assert EDITED_COMMENTS_JSON '.comments.nodes[0].replies.nodes[0].body' 'Edited first reply'
79+ jq-assert EDITED_COMMENTS_JSON '.comments.nodes[1].body' 'Edited comment from file'
80+ jq-assert EDITED_COMMENTS_JSON '.comments.nodes[1].replies.nodes[0].body' 'Edited reply from file'
81+
82+ # Delete with --yes should fail when run non-interactively
83+ ! exec gh discussion comment $SECOND_COMMENT_ID --delete
84+ stderr '--yes'
85+
86+ # Delete the second comment and its reply
87+ #
88+ # Note that if we only delete the comment, the reply will still exist and the API will keep returning the comment
89+ # (parent), but with an empty body. We delete them both here to avoid confusing assertions later.
90+ exec gh discussion comment $SECOND_COMMENT_ID --delete --yes
91+ exec gh discussion comment $SECOND_REPLY_ID --delete --yes
92+
93+ # Verify deletion
94+ exec gh discussion view $DISCUSSION_URL --comments --order oldest --json comments
95+ stdout2env DELETED_COMMENTS_JSON
96+ jq-assert DELETED_COMMENTS_JSON '.comments.nodes | length' '^1$'
97+ jq-assert DELETED_COMMENTS_JSON '.comments.nodes[0].body' 'Edited first comment'
98+ jq-assert DELETED_COMMENTS_JSON '.comments.nodes[0].replies.nodes | length' '^1$'
99+ jq-assert DELETED_COMMENTS_JSON '.comments.nodes[0].replies.nodes[0].body' 'Edited first reply'
100+
101+ # Test add, edit, and delete using comment URLs instead of node IDs
102+
103+ # Get the URL of the remaining comment
104+ jq2env DELETED_COMMENTS_JSON '.comments.nodes[0].url' FIRST_COMMENT_URL
105+
106+ # Add a reply using the comment URL
107+ exec gh discussion comment $FIRST_COMMENT_URL --body 'Reply via URL'
108+ stdout2env REPLY_URL
109+ stdout 'discussioncomment'
110+
111+ # Verify the reply
112+ exec gh discussion view $FIRST_COMMENT_URL --order oldest --json comments
113+ stdout2env URL_REPLIES_JSON
114+ jq-assert URL_REPLIES_JSON '.comments.nodes[0].replies.nodes | length' '^2$'
115+
116+ # Edit the reply using its URL
117+ exec gh discussion comment $REPLY_URL --edit --body 'Edited reply via URL'
118+ stdout 'discussioncomment'
119+
120+ # Verify the edit
121+ exec gh discussion view $FIRST_COMMENT_URL --order oldest --json comments
122+ stdout2env EDITED_URL_REPLIES_JSON
123+ jq-assert EDITED_URL_REPLIES_JSON '.comments.nodes[0].replies.nodes[0].body' 'Edited first reply'
124+ jq-assert EDITED_URL_REPLIES_JSON '.comments.nodes[0].replies.nodes[1].body' 'Edited reply via URL'
125+
126+ # Delete the reply using its URL
127+ exec gh discussion comment $REPLY_URL --delete --yes
128+
129+ # Verify deletion
130+ exec gh discussion view $FIRST_COMMENT_URL --order oldest --json comments
131+ stdout2env FINAL_URL_REPLIES_JSON
132+ jq-assert FINAL_URL_REPLIES_JSON '.comments.nodes[0].replies.nodes | length' '^1$'
133+ jq-assert FINAL_URL_REPLIES_JSON '.comments.nodes[0].replies.nodes[0].body' 'Edited first reply'
134+
135+ -- comment-body.txt --
136+ Comment from file
137+ -- reply-body.txt --
138+ Reply from file
139+ -- edit-comment-body.txt --
140+ Edited comment from file
141+ -- edit-reply-body.txt --
142+ Edited reply from file
0 commit comments