Discord Notification on Issue & PR Comment #363
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Discord Notification on Issue & PR Comment | |
on: | |
issue_comment: | |
types: [ created ] | |
pull_request_review: | |
types: [ submitted ] | |
jobs: | |
notify-discord: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set Environment Variables | |
run: | | |
echo "WEB_HOOK=${{ secrets.DISCORD_WEB_HOOK }}" >> $GITHUB_ENV | |
- name: Set Environment Variables for Issue | |
if: ${{ !github.event.issue.pull_request && contains(github.event.comment.body, '/noti') }} | |
run: | | |
echo "CONTENT=$(echo '${{ github.event.comment.body }}' | base64 -w 0)" >> $GITHUB_ENV | |
echo "POST_URL=${{ github.event.issue.html_url }}" >> $GITHUB_ENV | |
echo "COMMENT_AUTHOR=${{ github.event.comment.user.login }}" >> $GITHUB_ENV | |
echo "COMMENT_AUTHOR_AVATAR=${{ github.event.comment.user.avatar_url }}" >> $GITHUB_ENV | |
echo "POST_AUTHOR=${{ github.event.issue.user.login }}" >> $GITHUB_ENV | |
echo "POST_AUTHOR_AVATAR=${{ github.event.issue.user.avatar_url }}" >> $GITHUB_ENV | |
echo "TITLE=${{ github.event.issue.title }}" >> $GITHUB_ENV | |
echo "POST_TYPE=Issue" >> $GITHUB_ENV | |
- name: Set Environment Variables for PR Review Comments | |
if: ${{ contains(github.event.review.body, '/noti') }} | |
run: | | |
echo "CONTENT=$(echo '${{ github.event.review.body }}' | base64 -w 0)" >> $GITHUB_ENV | |
echo "POST_URL=${{ github.event.pull_request._links.html.href }}" >> $GITHUB_ENV | |
echo "COMMENT_AUTHOR=${{ github.event.review.user.login }}" >> $GITHUB_ENV | |
echo "COMMENT_AUTHOR_AVATAR=${{ github.event.review.user.avatar_url }}" >> $GITHUB_ENV | |
echo "POST_AUTHOR=${{ github.event.pull_request.base.user.login }}" >> $GITHUB_ENV | |
echo "POST_AUTHOR_AVATAR=${{ github.event.pull_request.base.user.avatar_url }}" >> $GITHUB_ENV | |
echo "TITLE=${{ github.event.pull_request.title }}" >> $GITHUB_ENV | |
echo "POST_TYPE=Pull Request" >> $GITHUB_ENV | |
- name: Set Environment Variables for PR Comments | |
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/noti') }} | |
run: | | |
echo "CONTENT=$(echo '${{ github.event.comment.body }}' | base64 -w 0)" >> $GITHUB_ENV | |
echo "POST_URL=${{ github.event.issue.html_url }}" >> $GITHUB_ENV | |
echo "COMMENT_AUTHOR=${{ github.event.comment.user.login }}" >> $GITHUB_ENV | |
echo "COMMENT_AUTHOR_AVATAR=${{ github.event.comment.user.avatar_url }}" >> $GITHUB_ENV | |
echo "POST_AUTHOR=${{ github.event.issue.user.login }}" >> $GITHUB_ENV | |
echo "POST_AUTHOR_AVATAR=${{ github.event.issue.user.avatar_url }}" >> $GITHUB_ENV | |
echo "TITLE=${{ github.event.issue.title }}" >> $GITHUB_ENV | |
echo "POST_TYPE=Pull Request" >> $GITHUB_ENV | |
- name: Set Discord Content | |
if: ${{ env.CONTENT }} | |
env: | |
CONTENT: ${{ env.CONTENT }} | |
run: | | |
IFS=',' read -r -a user_github_discord_pair <<< "${{ secrets.PAIR_GITHUB_DISCORD_USER }}" | |
IFS=',' read -r -a role_github_discord_pair <<< "${{ secrets.PAIR_GITHUB_DISCORD_ROLE }}" | |
content=$(echo "$CONTENT" | base64 --decode) | |
for ((i = 0; i < ${#user_github_discord_pair[@]}; i += 2)); do | |
search=${user_github_discord_pair[i]} | |
replace=${user_github_discord_pair[i+1]} | |
content=$(echo "$content" | sed "s|@${search}|<@${replace}>|g") | |
done | |
for ((i = 0; i < ${#role_github_discord_pair[@]}; i += 2)); do | |
search=${role_github_discord_pair[i]} | |
replace=${role_github_discord_pair[i+1]} | |
content=$(echo "$content" | sed "s|@${search}|<@\&${replace}>|g") | |
done | |
echo "DISCORD_CONTENT=$(echo $content | base64 -w 0)" >> $GITHUB_ENV | |
- name: Notify Discord | |
if: ${{ env.WEB_HOOK && env.POST_URL && env.DISCORD_CONTENT }} | |
env: | |
DISCORD_CONTENT: ${{ env.DISCORD_CONTENT }} | |
COMMENT_AUTHOR: ${{ env.COMMENT_AUTHOR }} | |
COMMENT_AUTHOR_AVATAR: ${{ env.COMMENT_AUTHOR_AVATAR }} | |
TITLE: ${{ env.TITLE }} | |
POST_URL: ${{ env.POST_URL }} | |
POST_AUTHOR: ${{ env.POST_AUTHOR }} | |
POST_AUTHOR_AVATAR: ${{ env.POST_AUTHOR_AVATAR }} | |
POST_TYPE: ${{ env.POST_TYPE }} | |
WEB_HOOK: ${{ env.WEB_HOOK }} | |
run: | | |
CONTENT_DECODED=$(echo "$DISCORD_CONTENT" | base64 --decode) | |
if [ -n "$WEB_HOOK" ]; then | |
JSON_PAYLOAD=$(jq -n \ | |
--arg content "$CONTENT_DECODED" \ | |
--arg comment_author "$COMMENT_AUTHOR" \ | |
--arg comment_author_avatar "$COMMENT_AUTHOR_AVATAR" \ | |
--arg embeds_title "$TITLE" \ | |
--arg post_url "$POST_URL" \ | |
--arg post_author "$POST_AUTHOR" \ | |
--arg post_author_avatar "$POST_AUTHOR_AVATAR" \ | |
--arg description "$POST_TYPE" \ | |
--arg color "5814783" \ | |
--arg timestamp "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ | |
'{ | |
content: $content, | |
username: $comment_author, | |
avatar_url: $comment_author_avatar, | |
embeds: [{ | |
title: $embeds_title, | |
url: $post_url, | |
author: { | |
name: $post_author, | |
icon_url: $post_author_avatar | |
}, | |
description: $description, | |
color: ($color | tonumber), | |
timestamp: $timestamp | |
}], | |
allowed_mentions: { | |
parse: ["users", "roles"] | |
} | |
}') | |
curl -X POST -H "Content-Type: application/json" -d "$JSON_PAYLOAD" "$WEB_HOOK" | |
else | |
echo "No matching title found. Skipping notification." | |
fi |