name: Badge GORM tests

on:
  workflow_dispatch:
  workflow_run:
    workflows: ["GORM-tests"]
    branches: [master]
    types: [completed]
  
jobs:
  create-gorm-tests-badge:
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: dawidd6/action-download-artifact@v2
        with:
          workflow: gorm-tests.yml
          workflow_conclusion: completed
          path: .
      
      - name: Prepare badge data
        working-directory: Linux-1.19-tests.out
        run: |
          echo "tests_passed=$(cat ./*.out | grep PASS | wc -l)" >> $GITHUB_ENV
          echo "tests_skipped=$(cat ./*.out | grep SKIP | wc -l)" >> $GITHUB_ENV
          echo "tests_failed=$(cat ./*.out | grep FAIL | wc -l)" >> $GITHUB_ENV
      
      - name: Make success badge 
        if: ${{ env.tests_failed == '0' }}
        uses: schneegans/dynamic-badges-action@v1.6.0
        with:
          auth: ${{ secrets.GIST_SECRET }}
          gistID: fb4d23f63d866b3e1e58b26d2f5ed01f
          filename: badge-gorm-tests.json
          label: GORM tests
          message: "Passed: ${{ env.tests_passed }} | Failed: ${{ env.tests_failed }}"
          color: 54a158
          style: for-the-badge
          labelColor: 25292d
          
      - name: Make fail badge
        if: ${{ env.tests_failed != '0' }}
        uses: schneegans/dynamic-badges-action@v1.6.0
        with:
          auth: ${{ secrets.GIST_SECRET }}
          gistID: fb4d23f63d866b3e1e58b26d2f5ed01f
          filename: badge-gorm-tests.json
          label: GORM tests
          message: "Passed: ${{ env.tests_passed }} | Failed: ${{ env.tests_failed }}"
          color: 96232b
          style: for-the-badge
          labelColor: 25292d
