name: Send Notifications

on:
  push:
    branches: [ main ]

jobs:
  send-notifications:
    timeout-minutes: 30
    name: send commit notifications
    runs-on: ubuntu-latest

    steps:

      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true
          fetch-depth: 2

      - name: Form variables for notification message
        id: git_info_grab
        run: |
          git_stat=$(git show --stat=50)
          git_stat="${git_stat//'%'/'%25'}"
          git_stat="${git_stat//$'\n'/'%0A'}"
          git_stat="${git_stat//$'\r'/'%0D'}"
          git_stat="${git_stat// / }" # space -> 'Space En', as github tends to eat ordinary spaces
          echo "::set-output name=git_stat::$git_stat"
          echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
          echo "##[set-output name=git_branch;]$(echo ${GITHUB_REF#refs/heads/})"

      - name: Send notification
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          format: markdown
          args: |
            *@${{ github.actor }} pushed to* [${{ github.repository }}:${{steps.git_info_grab.outputs.git_branch}}](github.com/${{ github.repository }}/commit/${{steps.git_info_grab.outputs.sha_short }})

            ```
            ${{ steps.git_info_grab.outputs.git_stat }}
            ```

