diff --git a/.github/workflows/test-accessibility-alt-text-bot.yml b/.github/workflows/test-accessibility-alt-text-bot.yml index bb5d580..eff925f 100644 --- a/.github/workflows/test-accessibility-alt-text-bot.yml +++ b/.github/workflows/test-accessibility-alt-text-bot.yml @@ -8,6 +8,8 @@ on: types: [created, edited] discussion: types: [created, edited] + discussion_comment: + types: [created, edited] jobs: accessibility_alt_text_bot: diff --git a/README.md b/README.md index 5e3fdcc..719aaf5 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ on: types: [created, edited] discussion: types: [created, edited] + discussion_comment: + types: [created, edited] permissions: issues: write diff --git a/action.yml b/action.yml index 072d799..e2c6757 100644 --- a/action.yml +++ b/action.yml @@ -9,15 +9,26 @@ runs: - name: Runs alt text check and adds comment run: | source ${{ github.action_path }}/flag-alt-text.sh + source ${{ github.action_path }}/queries.sh if [ ${{ github.event.comment }} ]; then content=$COMMENT - issue_url=${{ github.event.issue.html_url }} user=${{ github.event.comment.user.login }} if ${{ github.event.issue.pull_request.url != '' }}; then type=pr_comment + issue_url=${{ github.event.issue.html_url }} + elif ${{ github.event.discussion.id != '' }}; then + type=discussion_comment + discussion_node_id='${{ github.event.discussion.node_id }}' + comment_node_id='${{ github.event.comment.node_id }}' + if ${{ github.event.comment.parent_id != '' }}; then + reply_to_id=$(getDiscussionReplyToId $comment_node_id) + else + reply_to_id=$comment_node_id + fi else type=issue_comment + issue_url=${{ github.event.issue.html_url }} fi target=${{ github.event.comment.html_url }} else @@ -57,15 +68,9 @@ runs: elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then gh issue comment $issue_url --body "$message" elif [[ $type = discussion_description ]]; then - gh api graphql -F discussionId="$discussion_node_id" -F body="$message" -f query=' - mutation($discussionId: ID!, $body: String!) { - addDiscussionComment(input: {discussionId: $discussionId, body: $body}) { - comment { - id - } - } - } - ' + addDiscussionComment $discussion_node_id "$message" + elif [[ $type = discussion_comment ]]; then + addDiscussionComment $discussion_node_id "$message" $reply_to_id fi fi shell: bash diff --git a/queries.sh b/queries.sh new file mode 100644 index 0000000..bd6b7d0 --- /dev/null +++ b/queries.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Given a node_id for a discussion comment that is a reply in thread, return the parent comment's node ID. +function getDiscussionReplyToId() { + local NODE_ID=$1 + local REPLY_TO_DATA=$(gh api graphql -f query=' + query($nodeId: ID!) { + node(id: $nodeId) { + ... on DiscussionComment { + replyTo { + id + } + } + } + }' -F nodeId=$NODE_ID) + echo $REPLY_TO_DATA | jq -r '.data.node.replyTo.id' +} + +# Given a discussion node ID, a message, and an optional reply to node ID, adds a discussion comment. +function addDiscussionComment() { + local DISCUSSION_NODE_ID=$1 + local MESSAGE=$2 + local REPLY_TO_ID=$3 + + if [ -n "$REPLY_TO_ID" ]; then + gh api graphql -F discussionId="$DISCUSSION_NODE_ID" -F replyToId="$REPLY_TO_ID" -F body="$MESSAGE" -f query=' + mutation($discussionId: ID!, $replyToId: ID, $body: String!) { + addDiscussionComment(input: {discussionId: $discussionId, replyToId: $replyToId, body: $body}) { + comment { + id + } + } + } + ' + else + gh api graphql -F discussionId="$discussion_node_id" -F body="$message" -f query=' + mutation($discussionId: ID!, $body: String!) { + addDiscussionComment(input: {discussionId: $discussionId, body: $body}) { + comment { + id + } + } + } + ' + fi +}