# This GitHub workflow will send a notification to Slack when a scheduled test
# workflow completes.

# There needs to be a GitHub secret named "SLACK_HOOK" that is set to the
# incoming webhook URL for Slack. That webhook is tied to a Slack channel, so
# this workflow does not select the channel and relies on that default.
# Instructions to set up Slack for incoming webhooks are here:
# https://api.slack.com/messaging/webhooks.

name: notify

on:
  workflow_run:
    workflows:
      - test
    types:
      - completed

jobs:

  notify:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.event == 'schedule' }}
    steps:

    - name: Post status to Slack
      run: |
        run_id=$(echo "${{ github.event.workflow_run.url }}" | rev | cut -d '/' -f 1 | rev)
        run_url="https://github.com/${{ github.repository }}/actions/runs/$run_id"
        branch="${{ github.event.workflow_run.head_branch }}"
        branch_url="https://github.com/${{ github.repository }}/commits/$branch"
        repo_url="https://github.com/${{ github.repository }}/actions/workflows/test.yml"
        emoji=$(if [[ "${{ github.event.workflow_run.conclusion }}" == "success" ]]; then echo ":white_check_mark:"; else echo ":x:"; fi)
        json="{ \"text\": \"$emoji <$repo_url|${{ github.repository }}>: Scheduled run <$run_url|$run_id> on branch <$branch_url|$branch>: ${{ github.event.workflow_run.conclusion }}\" }"
        curl -s -d "payload=$json" ${{ secrets.SLACK_HOOK }}
