Skip to content

Discord Notification on Issue & PR Comment #100

Discord Notification on Issue & PR Comment

Discord Notification on Issue & PR Comment #100

name: Discord Notification on Issue & PR Comment
on:
issue_comment:
types: [ created, edited ]
pull_request_review:
types: [ submitted ]
jobs:
notify-discord:
runs-on: ubuntu-latest
steps:
- name: Set Environment Variables
run: |
echo "AVATAR_URL=${{ secrets.DISCORD_AVATAR_URL }}" >> $GITHUB_ENV
echo "USERNAME=디코봇" >> $GITHUB_ENV
echo "WEB_HOOK=${{ secrets.DISCORD_WEB_HOOK }}" >> $GITHUB_ENV
- name: Set Environment Variables for PR Comments
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/noti') }}
run: |
echo "COMMENT_AUTHOR=${{ github.event.comment.user.login }}" >> $GITHUB_ENV
echo "PR_URL=${{ github.event.issue.pull_request.html_url }}" >> $GITHUB_ENV
echo "AUTHOR_URL=${{ github.event.comment.user.avatar_url }}" >> $GITHUB_ENV
echo "CONTENT=$(echo '${{ github.event.comment.body }}' | base64 -w 0)" >> $GITHUB_ENV
echo "COMMENT_BODY=${{ github.event.issue.title }}" >> $GITHUB_ENV
echo "TITLE=Pull Request" >> $GITHUB_ENV
- name: Set Environment Variables for PR Review Comment
if: ${{ contains(github.event.review.body, '/noti') }}
run: |
echo "COMMENT_AUTHOR=${{ github.event.review.user.login }}" >> $GITHUB_ENV
echo "PR_URL=${{ github.event.pull_request.html_url }}" >> $GITHUB_ENV
echo "AUTHOR_URL=${{ github.event.review.user.avatar_url }}" >> $GITHUB_ENV
echo "CONTENT=$(echo '${{ github.event.review.body }}' | base64 -w 0)" >> $GITHUB_ENV
echo "COMMENT_BODY=${{ github.event.pull_request.title }}" >> $GITHUB_ENV
echo "TITLE=Pull Request" >> $GITHUB_ENV
- name: Set Environment Variables for Issue
if: ${{ !github.event.issue.pull_request && contains(github.event.comment.body, '/noti') }}
run: |
echo "COMMENT_AUTHOR=${{ github.event.comment.user.login }}" >> $GITHUB_ENV
echo "PR_URL=${{ github.event.issue.html_url }}" >> $GITHUB_ENV
echo "AUTHOR_URL=${{ github.event.comment.user.avatar_url }}" >> $GITHUB_ENV
echo "CONTENT=$(echo '${{ github.event.comment.body }}' | base64 -w 0)" >> $GITHUB_ENV
echo "COMMENT_BODY=${{ github.event.issue.title }}" >> $GITHUB_ENV
echo "TITLE=Issue" >> $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.COMMENT_AUTHOR && env.PR_URL && env.AUTHOR_URL && env.DISCORD_CONTENT && env.COMMENT_BODY }}
env:
AVATAR_URL: ${{ env.AVATAR_URL }}
COMMENT_BODY: ${{ env.COMMENT_BODY }}
USERNAME: ${{ env.USERNAME }}
WEB_HOOK: ${{ env.WEB_HOOK }}
COMMENT_AUTHOR: ${{ env.COMMENT_AUTHOR }}
PR_URL: ${{ env.PR_URL }}
AUTHOR_URL: ${{ env.AUTHOR_URL }}
DISCORD_CONTENT: ${{ env.DISCORD_CONTENT }}
TITLE: ${{ env.TITLE }}
run: |
CONTENT_DECODED=$(echo "$DISCORD_CONTENT" | base64 --decode)
if [ -n "$WEB_HOOK" ]; then
JSON_PAYLOAD=$(jq -n \
--arg content "$CONTENT_DECODED" \
--arg description "$COMMENT_BODY" \
--arg username "$USERNAME" \
--arg avatar_url "$AVATAR_URL" \
--arg title "$TITLE" \
--arg url "$PR_URL" \
--arg author_url "$AUTHOR_URL" \
--arg author_name "$COMMENT_AUTHOR" \
--arg color "5814783" \
--arg timestamp "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
'{
content: $content,
username: $username,
avatar_url: $avatar_url,
allowed_mentions: {
parse: ["users", "roles"]
},
embeds: [{
title: $title,
url: $url,
author: {
name: $author_name,
icon_url: $author_url
},
description: $description,
color: ($color | tonumber),
timestamp: $timestamp
}]
}')
curl -X POST -H "Content-Type: application/json" -d "$JSON_PAYLOAD" "$WEB_HOOK"
else
echo "No matching title found. Skipping notification."
fi