name: test_coverage

on: [pull_request]
#run specific jobs when specific files are updated.
#https://github.community/t/how-to-execute-jobs-if-folder-updated-recursive/117344/5

jobs:
  check:
    name: Check changed files
    outputs:
      database_run_job: ${{ steps.check_files.outputs.database_run_job }}
      functions_run_job: ${{ steps.check_files.outputs.functions_run_job }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: check files
        id: check_files
        env:
          pr_branch: ${{ github.event.pull_request.head.ref }}
        run: ./scripts/code_coverage_report/get_updated_files.sh
  pod-lib-lint-database:
    # Don't run on private repo unless it is a PR.
    needs: check
    if: github.repository == 'Firebase/firebase-ios-sdk' && needs.check.outputs.database_run_job == 'true'
    runs-on: macOS-latest

    strategy:
      matrix:
        target: [ios, tvos, macos]
    steps:
    - uses: actions/checkout@v2
    - name: Setup Bundler
      run: scripts/setup_bundler.sh
    - name: Build and test
      env:
        SDK: database
      run: ./scripts/code_coverage_report/pod_test_code_coverage_report.sh FirebaseDatabase "${{ matrix.target }}"
    - uses: actions/upload-artifact@v2
      with:
        name: database-codecoverage
        path: /Users/runner/*.xcresult

  pod-lib-lint-functions:
    # Don't run on private repo unless it is a PR.
    needs: check
    if: github.repository == 'Firebase/firebase-ios-sdk' && needs.check.outputs.functions_run_job == 'true'
    runs-on: macOS-latest

    strategy:
      matrix:
        target: [ios, tvos, macos]
    steps:
    - uses: actions/checkout@v2
    - name: Setup Bundler
      run: scripts/setup_bundler.sh
    - name: Build and test
      run: ./scripts/code_coverage_report/pod_test_code_coverage_report.sh FirebaseFunctions "${{ matrix.target }}"
    - uses: actions/upload-artifact@v2
      with:
        name: functions-codecoverage
        path: /Users/runner/*.xcresult

  create_report:
    needs: [pod-lib-lint-functions, pod-lib-lint-database]
    if: always()
    runs-on: macOS-latest
    steps:
      - uses: actions/download-artifact@v2
        id: download
        with:
          path: /Users/runner/test
      - name: display results
        run: |
          if [ -d "${{steps.download.outputs.download-path}}" ]; then
          find "/Users/runner/test" -print -regex ".*/.*\.xcresult" -exec xcrun xccov view --report {} \;
          fi
