-
Notifications
You must be signed in to change notification settings - Fork 16
/
action.yml
117 lines (111 loc) · 7.29 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
name: Accessibility alt text bot
description: "This action will check a repos issue, discussion, or PR for correct alt text usage."
branding:
icon: "eye"
color: "purple"
runs:
using: "composite"
steps:
- name: Runs alt text check and adds comment
run: |
source ${{ github.action_path }}/queries.sh
if [ ${{ github.event.comment }} ]; then
content=$COMMENT
user=${{ github.event.comment.user.login }}
target_id=${{ github.event.comment.id }}
if ${{ github.event.issue.pull_request.url != '' }}; then
type=pr_comment
issue_url=${{ github.event.issue.html_url }}
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')
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 }}'
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')
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 }}
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')
fi
target=${{ github.event.comment.html_url }}
else
if [ ${{ github.event.issue }} ]; then
type=issue_description
content=$ISSUE_BODY
issue_url=${{ github.event.issue.html_url }}
user=${{ github.event.issue.user.login }}
target="your issue body"
target_id=${{ github.event.issue.id }}
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')
elif [ ${{ github.event.pull_request }} ]; then
type=pr_description
content=$PR_BODY
issue_url=${{ github.event.pull_request.html_url }}
user=${{ github.event.pull_request.user.login }}
target="your pull request body"
target_id=${{ github.event.pull_request.id }}
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')
elif [ ${{ github.event.discussion }} ]; then
type=discussion_description
content=$DISCUSSION_BODY
discussion_node_id='${{ github.event.discussion.node_id }}'
user=${{ github.event.discussion.user.login }}
target="your discussion body"
target_id=${{ github.event.discussion.id }}
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')
fi
fi
flag=$(node ${{ github.action_path }}/src/index.js "$content")
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:
$flag
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.
Learn more about alt text at [Basic writing and formatting syntax: images on GitHub Docs](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#images).
> 🤖 Beep boop! This comment was added automatically by [github/accessibility-alt-text-bot](https://github.com/github/accessibility-alt-text-bot).
"
echo "Detected bad alt text: ${flag}"
echo "Event type: $type"
if [[ $flag && ${{ github.event.action }} != 'deleted' ]]; then
if [ $bot_comment_id ]; then
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
gh api repos/${{ github.repository }}/issues/comments/$bot_comment_id -X PATCH -f body="$message"
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
gh api repos/${{ github.repository }}/issues/comments/$bot_comment_id -X PATCH -f body="$message"
elif [[ $type = discussion_description ]] || [[ $type = discussion_comment ]]; then
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"
fi
else
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
gh pr comment $issue_url --body "$message"
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
gh issue comment $issue_url --body "$message"
elif [[ $type = discussion_description ]]; then
addDiscussionComment $discussion_node_id "$message"
elif [[ $type = discussion_comment ]]; then
addDiscussionComment $discussion_node_id "$message" $reply_to_id
fi
fi
else
echo "bot_comment_id: $bot_comment_id"
if [ $bot_comment_id ]; then
echo "Deleting bot comment..."
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
gh api -X DELETE /repos/${{ github.repository }}/issues/comments/$bot_comment_id
elif [[ $type = issue_comment ]] || [[ $type = issue_description ]]; then
gh api -X DELETE /repos/${{ github.repository }}/issues/comments/$bot_comment_id
elif [[ $type = discussion_description ]] || [[ $type = discussion_comment ]]; then
gh api graphql -f query='mutation($id: ID!) { deleteDiscussionComment(input: {id: $id}) { clientMutationId } }' -f id=$bot_comment_id
fi
fi
fi
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
COMMENT: ${{ github.event.comment.body }}
ISSUE_BODY: ${{ github.event.issue.body }}
PR_BODY: ${{ github.event.pull_request.body }}
DISCUSSION_BODY: ${{ github.event.discussion.body }}