name: Core Repo Tests

on:
  push:
    branches-ignore:
    - 'release/*'
  pull_request:
env:
  # Set variable to 'main' if your change will not affect Contrib.
  # Otherwise, set variable to the commit of your branch on
  # opentelemetry-python-contrib which is compatible with these Core repo
  # changes.
  CONTRIB_REPO_SHA: 481972cf87c1506e789f586843ba82d93633eac7
  # This is needed because we do not clone the core repo in contrib builds anymore.
  # When running contrib builds as part of core builds, we use actions/checkout@v2 which
  # does not set an environment variable (simply just runs tox), which is different when
  # contrib builds are run directly from contrib (since test.yml is executed, which sets CORE_REPO_SHA)
  # The solution is to include CORE_REPO_SHA as part of THIS environment so it can be accessed
  # from within the contrib build.
  CORE_REPO_SHA: ${{ github.sha }}

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: pypy-3.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: ["api", "sdk", "semantic", "getting", "shim", "exporter", "protobuf",
          "propagator"]
        os: [ubuntu-20.04, windows-2019]
    steps:
    - name: Checkout Core Repo @ SHA - ${{ github.sha }}
      uses: actions/checkout@v2
    - name: Set up Python ${{ env[matrix.python-version] }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ env[matrix.python-version] }}
        architecture: 'x64'
    - name: Install tox
      run: pip install tox==3.27.1 -U tox-factor
    - name: Cache tox environment
        # Preserves .tox directory between runs for faster installs
      uses: actions/cache@v2
      with:
        path: |
          .tox
          ~/.cache/pip
        key: v3-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini',
          'dev-requirements.txt') }}-core
    - name: Windows does not let git check out files with long names
      if: ${{ matrix.os == 'windows-2019'}}
      run: git config --system core.longpaths true
    - name: run tox
      run: tox -f ${{ matrix.python-version }}-${{ matrix.package }} -- -ra --benchmark-json=${{
        env.RUN_MATRIX_COMBINATION }}-benchmark.json
#       - name: Find and merge benchmarks
#         id: find_and_merge_benchmarks
#         run: >-
#           jq -s '.[0].benchmarks = ([.[].benchmarks] | add)
#           | if .[0].benchmarks == null then null else .[0] end'
#           $(find . -name '*${{ matrix.package }}*-benchmark.json') > output.json
#           && echo "json_plaintext=$(cat output.json)" >> $GITHUB_OUTPUT
#       - name: Report on benchmark results
#         if: steps.find_and_merge_benchmarks.outputs.json_plaintext != 'null'
#         uses: rhysd/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-proto3", "docker-tests-proto4", "lint", "spellcheck",
          "docs", "mypy", "mypyinstalled", "tracecontext"]
    name: ${{ matrix.tox-environment }}
    runs-on: ubuntu-20.04
    steps:
    - name: Checkout Core Repo @ SHA - ${{ github.sha }}
      uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.10'
        architecture: 'x64'
    - name: Install tox
      run: pip install tox==3.27.1
    - name: Cache tox environment
        # Preserves .tox directory between runs for faster installs
      uses: actions/cache@v2
      with:
        path: |
          .tox
          ~/.cache/pip
        key: v3-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt')
          }}-core
    - name: run tox
      run: tox -e ${{ matrix.tox-environment }}

  # Contrib unit test suite in order to ensure changes in core do not break anything in contrib.
  # We only run contrib unit tests on the oldest supported Python version (3.7) as running the same tests
  # on all versions is somewhat redundant.
  contrib-build:
    env:
      # We use these variables to convert between tox and GHA version literals
      py37: 3.7
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false  # ensures the entire test matrix is run, even if one permutation fails
      matrix:
        python-version: [py37]
        package: ["instrumentation", "exporter"]
        os: [ubuntu-20.04]
    steps:
    - name: Checkout Contrib Repo @ SHA - ${{ env.CONTRIB_REPO_SHA }}
      uses: actions/checkout@v2
      with:
        repository: open-telemetry/opentelemetry-python-contrib
        ref: ${{ env.CONTRIB_REPO_SHA }}
    - name: Checkout Core Repo @ SHA ${{ github.sha }}
      uses: actions/checkout@v2
      with:
        repository: open-telemetry/opentelemetry-python
        path: opentelemetry-python-core
    - name: Set up Python ${{ env[matrix.python-version] }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ env[matrix.python-version] }}
        architecture: 'x64'
    - name: Install tox
      run: pip install tox==3.27.1 -U tox-factor
    - name: Cache tox environment
        # Preserves .tox directory between runs for faster installs
      uses: actions/cache@v2
      with:
        path: |
          .tox
          ~/.cache/pip
        key: v3-tox-cache-${{ matrix.python-version }}-${{ matrix.package }}-${{ matrix.os
          }}-${{ hashFiles('tox.ini', 'dev-requirements.txt') }}-contrib
    - name: run tox
      run: tox -f ${{ matrix.python-version }}-${{ matrix.package }} -- -ra