name: Contrib Repo Tests

on:
  push:
    branches-ignore:
    - 'release/*'
  pull_request:
env:
  CORE_REPO_SHA: 2ac9e646af8d6420f13e7e3b2508586f3efb435a

jobs:
  build:
    env:
      # We use these variables to convert between tox and GHA version literals
      py37: 3.7
      py38: 3.8
      py39: 3.9
      py310: "3.10"
      py311: "3.11"
      pypy3: "pypy3.7"
      RUN_MATRIX_COMBINATION: ${{ matrix.python-version }}-${{ matrix.package }}-${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false  # ensures the entire test matrix is run, even if one permutation fails
      matrix:
        python-version: [ py37, py38, py39, py310, py311, pypy3 ]
        package: ["instrumentation", "distro", "exporter", "sdkextension", "propagator"]
        os: [ ubuntu-20.04 ]
    steps:
      - name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
        uses: actions/checkout@v2
      - name: Set up Python ${{ env[matrix.python-version] }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env[matrix.python-version] }}
      - name: Install tox
        run: pip install -U tox-factor
      - name: Cache tox environment
        # Preserves .tox directory between runs for faster installs
        uses: actions/cache@v1
        with:
          path: |
            .tox
            ~/.cache/pip
          key: v7-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }}
      - name: run tox
        run: tox -f ${{ matrix.python-version }}-${{ matrix.package }} -- --benchmark-json=${{ env.RUN_MATRIX_COMBINATION }}-benchmark.json
  #     - name: Find and merge ${{ matrix.package }} benchmarks
  #       # TODO: Add at least one benchmark to every package type to remove this (#249)
  #       if: matrix.package == 'sdkextension' || matrix.package == 'propagator'
  #       run: >-
  #         mkdir -p benchmarks;
  #         jq -s '.[0].benchmarks = ([.[].benchmarks] | add)
  #         | if .[0].benchmarks == null then null else .[0] end'
  #         **/**/tests/*${{ matrix.package }}*-benchmark.json > benchmarks/output_${{ matrix.package }}.json
  #     - name: Upload all benchmarks under same key as an artifact
  #       if: ${{ success() }}
  #       uses: actions/upload-artifact@v2
  #       with:
  #         name: benchmarks
  #         path: benchmarks/output_${{ matrix.package }}.json
  # combine-benchmarks:
  #   runs-on: ubuntu-latest
  #   needs: build
  #   if: ${{ always() }}
  #   name: Combine benchmarks from previous build job
  #   steps:
  #     - name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
  #       uses: actions/checkout@v2
  #     - name: Download all benchmarks as artifact using key
  #       uses: actions/download-artifact@v2
  #       with:
  #         name: benchmarks
  #         path: benchmarks
  #     - name: Find and merge all benchmarks
  #       run: >-
  #         jq -s '.[0].benchmarks = ([.[].benchmarks] | add)
  #         | if .[0].benchmarks == null then null else .[0] end'
  #         benchmarks/output_*.json > output.json;
  #     - name: Report on benchmark results
  #       uses: benchmark-action/github-action-benchmark@v1
  #       with:
  #         name: OpenTelemetry Python Benchmarks - Python ${{ env[matrix.python-version ]}} - ${{ matrix.package }}
  #         tool: pytest
  #         output-file-path: output.json
  #         github-token: ${{ secrets.GITHUB_TOKEN }}
  #         max-items-in-chart: 100
  #         # Alert with a commit comment on possible performance regression
  #         alert-threshold: 200%
  #         fail-on-alert: true
  #         # Make a commit on `gh-pages` with benchmarks from previous step
  #         auto-push: ${{ github.ref == 'refs/heads/main' }}
  #         gh-pages-branch: gh-pages
  #         benchmark-data-dir-path: benchmarks
  misc:
    strategy:
      fail-fast: false
      matrix:
        tox-environment: [ "docker-tests", "spellcheck", "lint", "docs", "generate" ]
    name: ${{ matrix.tox-environment }}
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
        uses: actions/checkout@v2
      - name: Set up Python 3.9
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install tox
        run: pip install -U tox
      - name: Install libsnappy-dev
        if: ${{ matrix.tox-environment == 'lint' }}
        run: sudo apt-get install -y libsnappy-dev
      - name: Cache tox environment
        # Preserves .tox directory between runs for faster installs
        uses: actions/cache@v1
        with:
          path: |
            .tox
            ~/.cache/pip
          key: v7-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }}
      - name: run tox
        run: tox -e ${{ matrix.tox-environment }}
      - name: Ensure generated code is up to date
        if: matrix.tox-environment == 'generate'
        run: git diff --exit-code || (echo 'Generated code is out of date, please run "tox -e generate" and commit the changes in this PR.' && exit 1)
