name: Contrib Repo Tests

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

jobs:
  build:
    env:
      # We use these variables to convert between tox and GHA version literals
      py35: 3.5
      py36: 3.6
      py37: 3.7
      py38: 3.8
      pypy3: pypy3
      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: [ py35, py36, py37, py38, pypy3 ]
        package: ["instrumentation", "exporter", "sdkextension"]
        os: [ ubuntu-latest ]
        include:
          # py35-instrumentation segfaults on 18.04 so we instead run on 20.04
          - python-version: py35
            package: instrumentation
            os: ubuntu-20.04
        exclude:
          - os: ubuntu-latest
            python-version: py35
            package: instrumentation
    steps:
      - name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
        uses: actions/checkout@v2
      - name: Checkout Core Repo @ SHA - ${{ env.CORE_REPO_SHA }}
        uses: actions/checkout@v2
        with:
          repository: open-telemetry/opentelemetry-python
          ref: ${{ env.CORE_REPO_SHA }}
          path: opentelemetry-python-core
      - name: Set up Python ${{ env[matrix.python-version] }}
        uses: actions/setup-python@v2
        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@v2
        with:
          path: .tox
          key: tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', '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 benchmarks
        # TODO: Add at least one benchmark to every package type to remove this (#249)
        if: matrix.package == 'sdkextension'
        run: >-
          jq -s '.[0].benchmarks = ([.[].benchmarks] | add)
          | if .[0].benchmarks == null then null else .[0] end'
          **/**/tests/*${{ matrix.package }}*-benchmark.json > output.json
      - name: Report on benchmark results
        # TODO: Add at least one benchmark to every package type to remove this (#249)
        if: matrix.package == 'sdkextension'
        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/master' }}
          gh-pages-branch: gh-pages
          benchmark-data-dir-path: benchmarks
  misc:
    strategy:
      fail-fast: false
      matrix:
        tox-environment: [ "docker-tests", "lint" ]
    name: ${{ matrix.tox-environment }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Contrib Repo @ SHA - ${{ github.sha }}
        uses: actions/checkout@v2
      - name: Checkout Core Repo @ SHA ${{ env.CORE_REPO_SHA }}
        uses: actions/checkout@v2
        with:
          repository: open-telemetry/opentelemetry-python
          ref: ${{ env.CORE_REPO_SHA }}
          path: opentelemetry-python-core
      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - name: Install tox
        run: pip install -U tox
      - name: Cache tox environment
        # Preserves .tox directory between runs for faster installs
        uses: actions/cache@v2
        with:
          path: .tox
          key: tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt') }}
      - name: run tox
        run: tox -e ${{ matrix.tox-environment }}
