name: Discord Notification

on:
  pull_request:
    types: [opened, closed]
  issues:
    types: [opened]

env:
  MAX_TITLE: 100
  MAX_BODY: 250

jobs:
  pull_request:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.repository.default_branch }}
          sparse-checkout: .github/scripts
          sparse-checkout-cone-mode: false
      - name: Notify Discord
        env:
          WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
          ACTION: ${{ github.event.action }}
          MERGED: ${{ github.event.pull_request.merged }}
          PR_NUM: ${{ github.event.pull_request.number }}
          PR_URL: ${{ github.event.pull_request.html_url }}
          PR_TITLE: ${{ github.event.pull_request.title }}
          PR_USER: ${{ github.event.pull_request.user.login }}
          PR_BODY: ${{ github.event.pull_request.body }}
        run: |
          set -o pipefail
          source .github/scripts/discord-helpers.sh
          [ -z "$WEBHOOK" ] && exit 0

          if [ "$ACTION" = "opened" ]; then ICON="🔀"; LABEL="New PR"
          elif [ "$ACTION" = "closed" ] && [ "$MERGED" = "true" ]; then ICON="🎉"; LABEL="Merged"
          elif [ "$ACTION" = "closed" ]; then ICON="❌"; LABEL="Closed"; fi

          TITLE=$(printf '%s' "$PR_TITLE" | trunc $MAX_TITLE | esc)
          [ ${#PR_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
          BODY=$(printf '%s' "$PR_BODY" | trunc $MAX_BODY)
          if [ -n "$PR_BODY" ] && [ ${#PR_BODY} -gt $MAX_BODY ]; then
            BODY=$(printf '%s' "$BODY" | strip_trailing_url)
          fi
          BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
          [ -n "$PR_BODY" ] && [ ${#PR_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
          [ -n "$BODY" ] && BODY=" · $BODY"
          USER=$(printf '%s' "$PR_USER" | esc)

          MSG="$ICON **[$LABEL #$PR_NUM: $TITLE](<$PR_URL>)**"$'\n'"by @$USER$BODY"
          jq -n --arg content "$MSG" '{content: $content}' | curl -sf --retry 2 -X POST "$WEBHOOK" -H "Content-Type: application/json" -d @-

  issues:
    if: github.event_name == 'issues'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.repository.default_branch }}
          sparse-checkout: .github/scripts
          sparse-checkout-cone-mode: false
      - name: Notify Discord
        env:
          WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
          ISSUE_NUM: ${{ github.event.issue.number }}
          ISSUE_URL: ${{ github.event.issue.html_url }}
          ISSUE_TITLE: ${{ github.event.issue.title }}
          ISSUE_USER: ${{ github.event.issue.user.login }}
          ISSUE_BODY: ${{ github.event.issue.body }}
        run: |
          set -o pipefail
          source .github/scripts/discord-helpers.sh
          [ -z "$WEBHOOK" ] && exit 0

          TITLE=$(printf '%s' "$ISSUE_TITLE" | trunc $MAX_TITLE | esc)
          [ ${#ISSUE_TITLE} -gt $MAX_TITLE ] && TITLE="${TITLE}..."
          BODY=$(printf '%s' "$ISSUE_BODY" | trunc $MAX_BODY)
          if [ -n "$ISSUE_BODY" ] && [ ${#ISSUE_BODY} -gt $MAX_BODY ]; then
            BODY=$(printf '%s' "$BODY" | strip_trailing_url)
          fi
          BODY=$(printf '%s' "$BODY" | wrap_urls | esc)
          [ -n "$ISSUE_BODY" ] && [ ${#ISSUE_BODY} -gt $MAX_BODY ] && BODY="${BODY}..."
          [ -n "$BODY" ] && BODY=" · $BODY"
          USER=$(printf '%s' "$ISSUE_USER" | esc)

          MSG="🐛 **[Issue #$ISSUE_NUM: $TITLE](<$ISSUE_URL>)**"$'\n'"by @$USER$BODY"
          jq -n --arg content "$MSG" '{content: $content}' | curl -sf --retry 2 -X POST "$WEBHOOK" -H "Content-Type: application/json" -d @-
