Skip to content

Commit fa169a7

Browse files
authored
Merge pull request #17 from github/kh-discussion-comment-support
Add support for discussion comment
2 parents 78defe3 + 438305a commit fa169a7

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

.github/workflows/test-accessibility-alt-text-bot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ on:
88
types: [created, edited]
99
discussion:
1010
types: [created, edited]
11+
discussion_comment:
12+
types: [created, edited]
1113

1214
jobs:
1315
accessibility_alt_text_bot:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ on:
2626
types: [created, edited]
2727
discussion:
2828
types: [created, edited]
29+
discussion_comment:
30+
types: [created, edited]
2931

3032
permissions:
3133
issues: write

action.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,26 @@ 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
15-
issue_url=${{ github.event.issue.html_url }}
1616
user=${{ github.event.comment.user.login }}
1717
if ${{ github.event.issue.pull_request.url != '' }}; then
1818
type=pr_comment
19+
issue_url=${{ github.event.issue.html_url }}
20+
elif ${{ github.event.discussion.id != '' }}; then
21+
type=discussion_comment
22+
discussion_node_id='${{ github.event.discussion.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
1929
else
2030
type=issue_comment
31+
issue_url=${{ github.event.issue.html_url }}
2132
fi
2233
target=${{ github.event.comment.html_url }}
2334
else
@@ -57,15 +68,9 @@ runs:
5768
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
5869
gh issue comment $issue_url --body "$message"
5970
elif [[ $type = discussion_description ]]; then
60-
gh api graphql -F discussionId="$discussion_node_id" -F body="$message" -f query='
61-
mutation($discussionId: ID!, $body: String!) {
62-
addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {
63-
comment {
64-
id
65-
}
66-
}
67-
}
68-
'
71+
addDiscussionComment $discussion_node_id "$message"
72+
elif [[ $type = discussion_comment ]]; then
73+
addDiscussionComment $discussion_node_id "$message" $reply_to_id
6974
fi
7075
fi
7176
shell: bash

queries.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# Given a node_id for a discussion comment that is a reply in thread, 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+
# Given a discussion node ID, a message, and an optional reply to node ID, adds a discussion comment.
20+
function addDiscussionComment() {
21+
local DISCUSSION_NODE_ID=$1
22+
local MESSAGE=$2
23+
local REPLY_TO_ID=$3
24+
25+
if [ -n "$REPLY_TO_ID" ]; then
26+
gh api graphql -F discussionId="$DISCUSSION_NODE_ID" -F replyToId="$REPLY_TO_ID" -F body="$MESSAGE" -f query='
27+
mutation($discussionId: ID!, $replyToId: ID, $body: String!) {
28+
addDiscussionComment(input: {discussionId: $discussionId, replyToId: $replyToId, body: $body}) {
29+
comment {
30+
id
31+
}
32+
}
33+
}
34+
'
35+
else
36+
gh api graphql -F discussionId="$discussion_node_id" -F body="$message" -f query='
37+
mutation($discussionId: ID!, $body: String!) {
38+
addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {
39+
comment {
40+
id
41+
}
42+
}
43+
}
44+
'
45+
fi
46+
}

0 commit comments

Comments
 (0)