Skip to content

Commit f8f23d6

Browse files
committed
Introduce queries helper
1 parent 487b470 commit f8f23d6

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

action.yml

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ runs:
99
- name: Runs alt text check and adds comment
1010
run: |
1111
source ${{ github.action_path }}/flag-alt-text.sh
12+
source ${{ github.action_path }}/queries.sh
1213
1314
if [ ${{ github.event.comment }} ]; then
1415
content=$COMMENT
@@ -19,7 +20,12 @@ runs:
1920
elif ${{ github.event.discussion.id != '' }}; then
2021
type=discussion_comment
2122
discussion_node_id='${{ github.event.discussion.node_id }}'
22-
reply_to_id='${{ github.event.comment.node_id }}'
23+
comment_node_id='${{ github.event.comment.node_id }}'
24+
if ${{ github.event.comment.parent_id != '' }}; then
25+
reply_to_id=$(getDiscussionReplyToId $comment_node_id)
26+
else
27+
reply_to_id=$comment_node_id
28+
fi
2329
else
2430
type=issue_comment
2531
issue_url=${{ github.event.issue.html_url }}
@@ -62,25 +68,9 @@ runs:
6268
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
6369
gh issue comment $issue_url --body "$message"
6470
elif [[ $type = discussion_description ]]; then
65-
gh api graphql -F discussionId="$discussion_node_id" -F body="$message" -f query='
66-
mutation($discussionId: ID!, $body: String!) {
67-
addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {
68-
comment {
69-
id
70-
}
71-
}
72-
}
73-
'
71+
addDiscussionComment $discussion_node_id "$message"
7472
elif [[ $type = discussion_comment ]]; then
75-
gh api graphql -F discussionId="$discussion_node_id" -F replyToId="$reply_to_id" -F body="$message" -f query='
76-
mutation($discussionId: ID!, , $replyToId: ID, $body: String!) {
77-
addDiscussionComment(input: {discussionId: $discussionId, replyToId: $replyToId, body: $body}) {
78-
comment {
79-
id
80-
}
81-
}
82-
}
83-
'
73+
addDiscussionCommentAsReply $discussion_node_id $reply_to_id "$message"
8474
fi
8575
fi
8676
shell: bash

queries.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# Given a node_id for a Discussion Comment with a parent comment, return the parent comment's node ID.
4+
function getDiscussionReplyToId() {
5+
local NODE_ID=$1
6+
local REPLY_TO_DATA=$(gh api graphql -f query='
7+
query($nodeId: ID!) {
8+
node(id: $nodeId) {
9+
... on DiscussionComment {
10+
replyTo {
11+
id
12+
}
13+
}
14+
}
15+
}' -F nodeId=$NODE_ID)
16+
echo $REPLY_TO_DATA | jq -r '.data.node.replyTo.id'
17+
}
18+
19+
# Adds a top-level discussion comment given a discussion node ID and a message.
20+
function addDiscussionComment() {
21+
local DISCUSSION_NODE_ID=$1
22+
local MESSAGE=$2
23+
gh api graphql -F discussionId="$discussion_node_id" -F body="$message" -f query='
24+
mutation($discussionId: ID!, $body: String!) {
25+
addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {
26+
comment {
27+
id
28+
}
29+
}
30+
}
31+
'
32+
}
33+
34+
# Adds a Discussion Comment as a reply to an existing discussion comment given a discussion node ID, discussion comment node ID, and a message.
35+
function addDiscussionCommentAsReply() {
36+
local DISCUSSION_NODE_ID=$1
37+
local REPLY_TO_ID=$2
38+
local MESSAGE=$3
39+
gh api graphql -F discussionId="$DISCUSSION_NODE_ID" -F replyToId="$REPLY_TO_ID" -F body="$MESSAGE" -f query='
40+
mutation($discussionId: ID!, , $replyToId: ID, $body: String!) {
41+
addDiscussionComment(input: {discussionId: $discussionId, replyToId: $replyToId, body: $body}) {
42+
comment {
43+
id
44+
}
45+
}
46+
}
47+
'
48+
}

0 commit comments

Comments
 (0)