Skip to content

Commit

Permalink
Merge pull request #17 from github/kh-discussion-comment-support
Browse files Browse the repository at this point in the history
Add support for discussion comment
  • Loading branch information
khiga8 authored May 31, 2023
2 parents 78defe3 + 438305a commit fa169a7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-accessibility-alt-text-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
types: [created, edited]
discussion:
types: [created, edited]
discussion_comment:
types: [created, edited]

jobs:
accessibility_alt_text_bot:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ on:
types: [created, edited]
discussion:
types: [created, edited]
discussion_comment:
types: [created, edited]

permissions:
issues: write
Expand Down
25 changes: 15 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions queries.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fa169a7

Please sign in to comment.