Skip to content

Commit 34208bf

Browse files
Reuse and delete alt-text-bot comment to update lint issues. (#55)
* Reuse and delete alt-text-bot comment to update lint issues. * update readme * clean
1 parent 05db932 commit 34208bf

29 files changed

+421
-676
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ on:
55
pull_request:
66
types: [opened, edited]
77
issue_comment:
8-
types: [created, edited]
8+
types: [created, edited, deleted]
99
discussion:
1010
types: [created, edited]
1111
discussion_comment:
12-
types: [created, edited]
12+
types: [created, edited, deleted]
1313

1414
jobs:
1515
accessibility_alt_text_bot:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ on:
3636
pull_request:
3737
types: [opened, edited]
3838
issue_comment:
39-
types: [created, edited]
39+
types: [created, edited, deleted]
4040
discussion:
4141
types: [created, edited]
4242
discussion_comment:
43-
types: [created, edited]
43+
types: [created, edited, deleted]
4444

4545
permissions:
4646
issues: write

action.yml

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ runs:
1313
if [ ${{ github.event.comment }} ]; then
1414
content=$COMMENT
1515
user=${{ github.event.comment.user.login }}
16+
target_id=${{ github.event.comment.id }}
1617
if ${{ github.event.issue.pull_request.url != '' }}; then
1718
type=pr_comment
1819
issue_url=${{ github.event.issue.html_url }}
20+
bot_comment_id=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments | jq -r '.[] | select(.user.login == "github-actions[bot]") | select(.body | test("<div alt-text-bot-id=\"${{ github.event.comment.id }}\"")) | .id')
1921
elif ${{ github.event.discussion.id != '' }}; then
2022
type=discussion_comment
2123
discussion_node_id='${{ github.event.discussion.node_id }}'
2224
comment_node_id='${{ github.event.comment.node_id }}'
25+
bot_comment_id=$(gh api repos/${{ github.repository }}/discussions/${{ github.event.discussion.number }}/comments | jq -r '.[] | select(.user.login == "github-actions[bot]") | select(.body | test("<div alt-text-bot-id=\"${{ github.event.comment.id }}\"")) | .node_id')
2326
if ${{ github.event.comment.parent_id != '' }}; then
2427
reply_to_id=$(getDiscussionReplyToId $comment_node_id)
2528
else
@@ -28,6 +31,7 @@ runs:
2831
else
2932
type=issue_comment
3033
issue_url=${{ github.event.issue.html_url }}
34+
bot_comment_id=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments | jq -r '.[] | select(.user.login == "github-actions[bot]") | select(.body | test("<div alt-text-bot-id=\"${{ github.event.comment.id }}\"")) | .id')
3135
fi
3236
target=${{ github.event.comment.html_url }}
3337
else
@@ -36,23 +40,31 @@ runs:
3640
content=$ISSUE_BODY
3741
issue_url=${{ github.event.issue.html_url }}
3842
user=${{ github.event.issue.user.login }}
39-
target=" your issue body"
43+
target="your issue body"
44+
target_id=${{ github.event.issue.id }}
45+
bot_comment_id=$(gh api repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments | jq -r '.[] | select(.user.login == "github-actions[bot]") | select(.body | test("<div alt-text-bot-id=\"${{ github.event.issue.id }}\"")) | .id')
4046
elif [ ${{ github.event.pull_request }} ]; then
4147
type=pr_description
4248
content=$PR_BODY
4349
issue_url=${{ github.event.pull_request.html_url }}
4450
user=${{ github.event.pull_request.user.login }}
45-
target=" your pull request body"
51+
target="your pull request body"
52+
target_id=${{ github.event.pull_request.id }}
53+
bot_comment_id=$(gh api repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments | jq -r '.[] | select(.user.login == "github-actions[bot]") | select(.body | test("<div alt-text-bot-id=\"${{ github.event.pull_request.id }}\"")) | .id')
4654
elif [ ${{ github.event.discussion }} ]; then
4755
type=discussion_description
4856
content=$DISCUSSION_BODY
4957
discussion_node_id='${{ github.event.discussion.node_id }}'
5058
user=${{ github.event.discussion.user.login }}
51-
target=" your discussion body"
59+
target="your discussion body"
60+
target_id=${{ github.event.discussion.id }}
61+
bot_comment_id=$(gh api repos/${{ github.repository }}/discussions/${{ github.event.discussion.number }}/comments | jq -r '.[] | select(.user.login == "github-actions[bot]") | select(.body | test("<div alt-text-bot-id=\"${{ github.event.discussion.id }}\"")) | .node_id')
5262
fi
5363
fi
54-
flag=$(node ${{ github.action_path }}/src/index.js "$content")
55-
message="Uh oh! @$user, the image you shared is missing helpful alt text. Check $target.
64+
flag=$(node ${{ github.action_path }}/src/index.js "$content")
65+
message="<div alt-text-bot-id=\"$target_id\" /> Uh oh! @$user, at least one image you shared is missing helpful alt text. Check $target to fix the following violations:
66+
67+
$flag
5668
5769
Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.
5870
@@ -63,16 +75,37 @@ runs:
6375
6476
echo "Detected bad alt text: ${flag}"
6577
echo "Event type: $type"
66-
67-
if [[ $flag = true ]]; then
68-
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
69-
gh pr comment $issue_url --body "$message"
70-
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
71-
gh issue comment $issue_url --body "$message"
72-
elif [[ $type = discussion_description ]]; then
73-
addDiscussionComment $discussion_node_id "$message"
74-
elif [[ $type = discussion_comment ]]; then
75-
addDiscussionComment $discussion_node_id "$message" $reply_to_id
78+
if [[ $flag && ${{ github.event.action }} != 'deleted' ]]; then
79+
if [ $bot_comment_id ]; then
80+
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
81+
gh api repos/${{ github.repository }}/issues/comments/$bot_comment_id -X PATCH -f body="$message"
82+
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
83+
gh api repos/${{ github.repository }}/issues/comments/$bot_comment_id -X PATCH -f body="$message"
84+
elif [[ $type = discussion_description ]] || [[ $type = discussion_comment ]]; then
85+
gh api graphql -f query='mutation($commentId: ID!, $body: String!) { updateDiscussionComment(input: {commentId: $commentId, body: $body}) { comment { id body }}}' -f commentId=$bot_comment_id -f body="$message"
86+
fi
87+
else
88+
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
89+
gh pr comment $issue_url --body "$message"
90+
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
91+
gh issue comment $issue_url --body "$message"
92+
elif [[ $type = discussion_description ]]; then
93+
addDiscussionComment $discussion_node_id "$message"
94+
elif [[ $type = discussion_comment ]]; then
95+
addDiscussionComment $discussion_node_id "$message" $reply_to_id
96+
fi
97+
fi
98+
else
99+
echo "bot_comment_id: $bot_comment_id"
100+
if [ $bot_comment_id ]; then
101+
echo "Deleting bot comment..."
102+
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
103+
gh api -X DELETE /repos/${{ github.repository }}/issues/comments/$bot_comment_id
104+
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
105+
gh api -X DELETE /repos/${{ github.repository }}/issues/comments/$bot_comment_id
106+
elif [[ $type = discussion_description ]] || [[ $type = discussion_comment ]]; then
107+
gh api graphql -f query='mutation($id: ID!) { deleteDiscussionComment(input: {id: $id}) { clientMutationId } }' -f id=$bot_comment_id
108+
fi
76109
fi
77110
fi
78111
shell: bash

node_modules/.package-lock.json

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/braces/CHANGELOG.md

Lines changed: 0 additions & 184 deletions
This file was deleted.

node_modules/braces/LICENSE

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)