|
| 1 | +name: Slack Notification |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: ["**"] |
| 5 | + pull_request: |
| 6 | + types: [opened, closed, reopened] |
| 7 | + issue_comment: |
| 8 | + types: [created] |
| 9 | + |
| 10 | +jobs: |
| 11 | + notify: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout |
| 15 | + uses: actions/checkout@v3 |
| 16 | + |
| 17 | + - name: Send Notification to Slack |
| 18 | + env: |
| 19 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 20 | + EVENT_NAME: ${{ github.event_name }} |
| 21 | + REPO: ${{ github.repository }} |
| 22 | + ACTOR: ${{ github.actor }} |
| 23 | + EVENT_JSON: ${{ toJson(github.event) }} |
| 24 | + run: | |
| 25 | + if [ -z "$SLACK_WEBHOOK_URL" ]; then |
| 26 | + echo "::warning::SLACK_WEBHOOK_URL is not set." |
| 27 | + exit 0 |
| 28 | + fi |
| 29 | +
|
| 30 | + # イベント情報を一時ファイルへ保存 (クォート問題回避のため) |
| 31 | + printf '%s' "$EVENT_JSON" > event.json |
| 32 | +
|
| 33 | + # 共通変数の設定 |
| 34 | + TS=$(date +%s) |
| 35 | + COLOR="#999999" |
| 36 | +
|
| 37 | + # イベントごとの処理 |
| 38 | + case "$EVENT_NAME" in |
| 39 | + push) |
| 40 | + EMOJI="🚀" |
| 41 | + REF_NAME="${GITHUB_REF_NAME}" |
| 42 | + COMMIT_MSG=$(jq -r '.head_commit.message' event.json) |
| 43 | + COMMIT_URL=$(jq -r '.head_commit.url' event.json) |
| 44 | + SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7) |
| 45 | + |
| 46 | + TITLE="Push to \`$REF_NAME\`" |
| 47 | + MESSAGE="$COMMIT_MSG" |
| 48 | + COLOR="good" |
| 49 | + URL="$COMMIT_URL" |
| 50 | + |
| 51 | + FIELDS=$(jq -n \ |
| 52 | + --arg sha "$SHORT_SHA" \ |
| 53 | + --arg url "$COMMIT_URL" \ |
| 54 | + --arg actor "$ACTOR" \ |
| 55 | + '[ |
| 56 | + {"title":"Commit", "value":"<\($url)|`\($sha)`>", "short":true}, |
| 57 | + {"title":"Author", "value":$actor, "short":true} |
| 58 | + ]') |
| 59 | + ;; |
| 60 | +
|
| 61 | + pull_request) |
| 62 | + ACTION=$(jq -r '.action' event.json) |
| 63 | + PR_NUM=$(jq -r '.pull_request.number' event.json) |
| 64 | + PR_TITLE=$(jq -r '.pull_request.title' event.json) |
| 65 | + PR_BODY=$(jq -r '.pull_request.body // ""' event.json | head -c 200) |
| 66 | + MERGED=$(jq -r '.pull_request.merged' event.json) |
| 67 | + |
| 68 | + URL=$(jq -r '.pull_request.html_url' event.json) |
| 69 | + MESSAGE="*$PR_TITLE*\n$PR_BODY" |
| 70 | + |
| 71 | + if [ "$MERGED" == "true" ]; then |
| 72 | + EMOJI="🎉"; TITLE="Merged PR #$PR_NUM"; COLOR="#6f42c1" |
| 73 | + elif [ "$ACTION" == "opened" ]; then |
| 74 | + EMOJI="📬"; TITLE="New PR #$PR_NUM"; COLOR="warning" |
| 75 | + elif [ "$ACTION" == "closed" ]; then |
| 76 | + EMOJI="🚫"; TITLE="Closed PR #$PR_NUM"; COLOR="#999999" |
| 77 | + else |
| 78 | + EMOJI="🔄"; TITLE="PR $ACTION #$PR_NUM"; COLOR="warning" |
| 79 | + fi |
| 80 | + |
| 81 | + FIELDS=$(jq -n \ |
| 82 | + --arg head "$(jq -r '.pull_request.head.ref' event.json)" \ |
| 83 | + --arg base "$(jq -r '.pull_request.base.ref' event.json)" \ |
| 84 | + '[ |
| 85 | + {"title":"Branch", "value":"`\($head)` → `\($base)`", "short":true} |
| 86 | + ]') |
| 87 | + ;; |
| 88 | +
|
| 89 | + issue_comment) |
| 90 | + EMOJI="💬" |
| 91 | + ISSUE_NUM=$(jq -r '.issue.number' event.json) |
| 92 | + COMMENT_BODY=$(jq -r '.comment.body' event.json) |
| 93 | + URL=$(jq -r '.comment.html_url' event.json) |
| 94 | + COLOR="#007bff" |
| 95 | + |
| 96 | + if [ "$(jq -r '.issue.pull_request != null' event.json)" == "true" ]; then |
| 97 | + TITLE="Comment on PR #$ISSUE_NUM" |
| 98 | + else |
| 99 | + TITLE="Comment on Issue #$ISSUE_NUM" |
| 100 | + fi |
| 101 | + |
| 102 | + MESSAGE="$COMMENT_BODY" |
| 103 | + FIELDS="[]" |
| 104 | + ;; |
| 105 | + |
| 106 | + *) |
| 107 | + EMOJI="ℹ️" |
| 108 | + TITLE="Event: $EVENT_NAME" |
| 109 | + MESSAGE="Triggered by $ACTOR" |
| 110 | + URL="https://github.com/$REPO" |
| 111 | + FIELDS="[]" |
| 112 | + ;; |
| 113 | + esac |
| 114 | +
|
| 115 | + # Payload 生成 (jqで安全に構築) |
| 116 | + PAYLOAD=$(jq -nc \ |
| 117 | + --arg username "GitHub Notifications" \ |
| 118 | + --arg icon ":github:" \ |
| 119 | + --arg text "$EMOJI Notification from $REPO" \ |
| 120 | + --arg color "$COLOR" \ |
| 121 | + --arg title "$TITLE" \ |
| 122 | + --arg url "$URL" \ |
| 123 | + --arg message "$MESSAGE" \ |
| 124 | + --arg footer "<https://github.com/$REPO|$REPO>" \ |
| 125 | + --argjson fields "${FIELDS:-[]}" \ |
| 126 | + --argjson ts "$TS" \ |
| 127 | + '{ |
| 128 | + username: $username, |
| 129 | + icon_emoji: $icon, |
| 130 | + text: $text, |
| 131 | + attachments: [{ |
| 132 | + color: $color, |
| 133 | + title: $title, |
| 134 | + title_link: $url, |
| 135 | + text: $message, |
| 136 | + fields: $fields, |
| 137 | + footer: $footer, |
| 138 | + ts: $ts |
| 139 | + }] |
| 140 | + }') |
| 141 | +
|
| 142 | + # 送信処理(エラーハンドリング付き) |
| 143 | + HTTP_CODE=$(echo "$PAYLOAD" | curl -X POST -H 'Content-type: application/json' \ |
| 144 | + -d @- "$SLACK_WEBHOOK_URL" \ |
| 145 | + -w '%{http_code}' \ |
| 146 | + -s -o /tmp/slack_response.txt) |
| 147 | +
|
| 148 | + if [ "$HTTP_CODE" -eq 200 ]; then |
| 149 | + echo "✅ Notification sent (HTTP 200)" |
| 150 | + else |
| 151 | + echo "::error::Failed to send notification. HTTP Code: $HTTP_CODE" |
| 152 | + cat /tmp/slack_response.txt |
| 153 | + exit 1 |
| 154 | + fi |
0 commit comments