name: Discord Commit Notification

on:
  push:
    branches:
      - '**'

jobs:
  notify:
    runs-on: ubuntu-latest

    steps:
      - name: Send Discord notification
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
          COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
          AUTHOR: ${{ github.event.head_commit.author.name }}
        run: |
          COMMIT_SHA="${{ github.sha }}"
          SHORT_SHA="${COMMIT_SHA:0:7}"
          BRANCH="${{ github.ref_name }}"
          REPO="${{ github.repository }}"
          COMMIT_URL="https://github.com/${REPO}/commit/${COMMIT_SHA}"

          PAYLOAD=$(jq -n \
            --arg title "New Commit in \`${REPO}\`" \
            --arg url "$COMMIT_URL" \
            --arg author "$AUTHOR" \
            --arg branch "\`${BRANCH}\`" \
            --arg commit "[\`${SHORT_SHA}\`](${COMMIT_URL})" \
            --arg message "$COMMIT_MESSAGE" \
            '{
              embeds: [{
                title: $title,
                url: $url,
                color: 5814783,
                fields: [
                  {name: "Author",  value: $author,  inline: true},
                  {name: "Branch",  value: $branch,  inline: true},
                  {name: "Commit",  value: $commit,  inline: true},
                  {name: "Message", value: $message}
                ]
              }]
            }')

          curl -sf -X POST "$DISCORD_WEBHOOK" \
            -H "Content-Type: application/json" \
            -d "$PAYLOAD"
