Skip to content

[Refactor] GitHub 알림 메시지 포맷 고도화 #48

[Refactor] GitHub 알림 메시지 포맷 고도화

[Refactor] GitHub 알림 메시지 포맷 고도화 #48

name: Slack Notification (Review Requested)
on:
pull_request:
types: [ review_requested ]
permissions:
contents: read
pull-requests: read
concurrency:
group: pr-${{ github.event.pull_request.number }}-slack-review-requested
cancel-in-progress: true
# 재리뷰 요청 알림
jobs:
notify:
runs-on: ubuntu-latest
steps:
# JSON 파싱 도구
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Notify Re-review Requested
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
GITHUB_TOKEN: ${{ github.token }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_URL: ${{ github.event.pull_request.html_url }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_REVIEWER: ${{ github.event.requested_reviewer.login }}
REPOSITORY: ${{ github.repository }}
run: |
REPO_NAME="${GITHUB_REPOSITORY#*/}"
declare -A GITHUB_TO_SLACK
GITHUB_TO_SLACK["soeun2537"]="U09A0LM0CRW"
GITHUB_TO_SLACK["taek2222"]="U099ARRH3D3"
GITHUB_TO_SLACK["changuii"]="U099BR9RNE6"
GITHUB_TO_SLACK["eoehd1ek"]="U0995NANDML"
GITHUB_TO_SLACK["oungsi2000"]="U098U2R57NK"
GITHUB_TO_SLACK["parkjiminnnn"]="U098U8SLXHD"
GITHUB_TO_SLACK["etama123"]="U0995MPSZ62"
if [[ "$PR_REVIEWER" == *"[bot]" ]]; then
echo "Bot 계정 리뷰 요청은 알림 생략"
exit 0
fi
REVIEW_API_URL="https://api.github.com/repos/$REPOSITORY/pulls/$PR_NUMBER/reviews"
REVIEW_COUNT=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" "$REVIEW_API_URL" \
| jq -r --arg reviewer "$PR_REVIEWER" '[.[] | select(.user.login == $reviewer)] | length')
if [ "$REVIEW_COUNT" -eq 0 ]; then
echo "최초 리뷰 요청으로 간주되어 알림 생략"
exit 0
fi
slack_id=${GITHUB_TO_SLACK[$PR_REVIEWER]}
if [[ -z "$slack_id" ]]; then
echo "매핑 없는 사용자(${PR_REVIEWER})는 생략"
exit 0
fi
if [[ -n "${GITHUB_TO_SLACK[$PR_AUTHOR]}" ]]; then
SLACK_AUTHOR_MENTION="<@${GITHUB_TO_SLACK[$PR_AUTHOR]}>"
else
SLACK_AUTHOR_MENTION="@${PR_AUTHOR}" # 평문
fi
SLACK_REVIEWER_MENTION="<@$slack_id>"
PAYLOAD=$(jq -n \
--arg repo "$REPO_NAME" \
--arg title "$PR_TITLE" \
--arg number "$PR_NUMBER" \
--arg author "$SLACK_AUTHOR_MENTION" \
--arg reviewer "$SLACK_REVIEWER_MENTION" \
--arg url "$PR_URL" \
'{
text: "🏸 (\($repo)) 재리뷰 요청 알림",
blocks: [
{type: "header", text: {type: "plain_text", text: "🏸 (\($repo)) 재리뷰 요청 알림", emoji: true}},
{type: "section", text: {type: "mrkdwn", text: "*PR 제목:* \($title) (#\($number))\n*작성자:* \($author)\n*리뷰어:* \($reviewer)\n*링크:* \($url)"}}
]
}')
curl -X POST -H 'Content-type: application/json' --data "$PAYLOAD" $SLACK_WEBHOOK_URL