Skip to content

Update README.md

Update README.md #3

Workflow file for this run

name: 'GitHub Discussions Notifier'
description: 'Notify GitHub Discussion & Comment to Slack'
branding:
icon: 'at-sign'
color: 'orange'
inputs:
SLACK_CHANNEL_ID:
description: 'Slack Channel Id'
required: true
SLACK_BOT_TOKEN:
description: 'Slack Bot Token'
required: true
send-discussion-create:
description: 'send discussion create notification?'
required: false
default: true
send-discussion-comment:
description: 'send discussion comment notification?'
required: false
default: true
unfurl-links:
description: 'Pass true to enable unfurling of primarily text-based content.'
required: false
default: false
unfurl-media:
description: 'Pass false to disable unfurling of media content.'
required: false
default: false
runs:
using: "composite"
steps:
- name: Send GitHub Discussion Notification to Slack
if: ${{ github.event.discussion && !github.event.comment && inputs.send-discussion-create }}
# Note:
# - 一度変数に入れることで、$や`をエスケープしている
# - https://stackoverflow.com/questions/69982822/is-there-a-way-to-escape-and-single-quotes-together-in-bash
# - printfしないといい感じに"\n"が変換されない
run: |
title=$(
cat <<"EOF"
${{ env.DISCUSSION_TITLE }}
EOF
)
body=$(
cat <<"EOF"
${{ env.DISCUSSION_BODY }}
EOF
)
printf -v markdown_message_unescaped %b "<${{ env.DISCUSSION_URL }}|*$title*>\n$body"
jq -n -r --arg text "$markdown_message_unescaped" '{
"token":"${{ inputs.SLACK_BOT_TOKEN }}",
"channel":"${{ inputs.SLACK_CHANNEL_ID }}",
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"New Discussion by <${{ env.USER_URL }}|${{ env.USER_NAME }}>"
}
}
],
"attachments":[
{
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":$text
}
}
]
}
],
"unfurl_links": ${{ inputs.unfurl-links }},
"unfurl_media": ${{ inputs.unfurl-media }}
}
'|curl -X POST -H 'Authorization: Bearer ${{ inputs.SLACK_BOT_TOKEN }}' -H "Content-Type: application/json" -d @- https://slack.com/api/chat.postMessage
shell: bash
env:
DISCUSSION_URL: ${{ github.event.discussion.html_url }}
DISCUSSION_TITLE: ${{ github.event.discussion.title }}
DISCUSSION_BODY: ${{ github.event.discussion.body }}
USER_URL: ${{ github.event.discussion.user.html_url }}
USER_NAME: ${{ github.event.discussion.user.login }}
- name: Send GitHub Discussions Comment Notification to Slack
if: ${{ github.event.comment && inputs.send-discussion-comment }}
run: |
title=$(cat << "EOF"
${{ env.DISCUSSION_TITLE }}
EOF
)
body=$(cat << "EOF"
${{ env.COMMENT_BODY }}
EOF
)
printf -v markdown_message_unescaped %b "*<${{ env.COMMENT_URL }}|Comment on $title>*\n$body"
jq -n -r --arg text "$markdown_message_unescaped" '{
"token":"${{ inputs.SLACK_BOT_TOKEN }}",
"channel":"${{ inputs.SLACK_CHANNEL_ID }}",
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"New comment by <${{ env.USER_URL }}|${{ env.USER_NAME }}>"
}
}
],
"attachments":[
{
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":$text
}
}
]
}
],
"unfurl_links": ${{ inputs.unfurl-links }},
"unfurl_media": ${{ inputs.unfurl-media }}
}
'|curl -X POST -H 'Authorization: Bearer ${{ inputs.SLACK_BOT_TOKEN }}' -H "Content-Type: application/json" -d @- https://slack.com/api/chat.postMessage
shell: bash
env:
DISCUSSION_TITLE: ${{ github.event.discussion.title }}
COMMENT_URL: ${{ github.event.comment.html_url }}
COMMENT_BODY: ${{ github.event.comment.body }}
USER_URL: ${{ github.event.comment.user.html_url }}
USER_NAME: ${{ github.event.comment.user.login }}