name: Verify and Test

defaults:
  run:
    shell: bash

on:
  pull_request:
    branches: [ '*' ]
  push:
    branches: [ 'master','main' ]

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  build:
    name: Run Tests
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'ci skip')"

    steps:
      - name: Checkout current branch (full)
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Build and Test
        id: build
        run: cd build/ && ./assemble_api.sh && ./run_all_tests.sh

      - name: Codecov
        id: codecov0
        uses: codecov/codecov-action@v4
        continue-on-error: true
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
      - name: Codecov Retry
        id: codecov1
        if: steps.codecov0.outcome=='failure'
        uses: codecov/codecov-action@v4
        continue-on-error: true
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
      - name: Codecov Retry 2
        id: codecov2
        if: steps.codecov1.outcome=='failure'
        uses: codecov/codecov-action@v4
        continue-on-error: true
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
      - name: Codecov Retry 3
        id: codecov3
        if: steps.codecov2.outcome=='failure'
        uses: codecov/codecov-action@v4
        continue-on-error: true
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
      - name: set the status              # set the workflow status if command failed
        if: steps.build.outcome=='success'
        run: |
          if ${{ steps.codecov0.outcome=='success' || steps.codecov1.outcome=='success' || steps.codecov2.outcome=='success' || steps.codecov3.outcome=='success' }}; then
             echo "Codecov completed successfully"
          else
            echo "Codecov failed after three retries"
            exit 1
          fi
