name: contrib-tests
on:
  push:
    branches: [main]
    tags:
      - v[0-9]+.[0-9]+.[0-9]+.*
  pull_request:
    types: [opened, ready_for_review, synchronize, reopened, labeled, unlabeled]
    branches: [main]
  merge_group:
    types: [checks_requested]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}
  cancel-in-progress: true

permissions: read-all

jobs:
  contrib-tests-prepare:
    runs-on: ubuntu-latest
    if: ${{ !contains(github.event.pull_request.labels.*.name, 'Skip Contrib Tests') }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Prepare Contrib Tests
        run: |
          contrib_path=/tmp/opentelemetry-collector-contrib
          git clone --depth=1 https://github.com/open-telemetry/opentelemetry-collector-contrib.git $contrib_path
          make CONTRIB_PATH=$contrib_path prepare-contrib
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: contrib
          path: /tmp/opentelemetry-collector-contrib/
          include-hidden-files: true

  contrib-tests-matrix:
    runs-on: ubuntu-latest
    needs: [contrib-tests-prepare]
    if: ${{ !contains(github.event.pull_request.labels.*.name, 'Skip Contrib Tests') }}
    strategy:
      fail-fast: false
      matrix:
        group:
          - receiver-0
          - receiver-1
          - receiver-2
          - receiver-3
          - processor
          - exporter-0
          - exporter-1
          - extension
          - connector
          - internal
          - pkg
          - cmd-0
          - other
    steps:
      - name: Checkout Repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Download contrib
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: contrib
          path: /tmp/contrib
      - name: Setup Go
        uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          go-version: oldstable
          cache: false

      - name: Cache Go
        id: go-cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/bin
            ~/go/pkg/mod
          key: go-cache-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/go.sum') }}

      - name: Run tests
        run: |
          make CONTRIB_PATH=/tmp/contrib SKIP_RESTORE_CONTRIB=true GROUP=${{ matrix.group }} check-contrib

  contrib_tests:
    runs-on: ubuntu-latest
    if: ${{ !contains(github.event.pull_request.labels.*.name, 'Skip Contrib Tests') }}
    needs: [contrib-tests-matrix]
    steps:
      - name: Print result
        run: echo ${{ needs.contrib-tests-matrix.result }}
      - name: Interpret result
        run: |
          if [[ success == ${{ needs.contrib-tests-matrix.result }} ]]
          then
            echo "All matrix jobs passed!"
          else
            echo "One or more matrix jobs failed."
            false
          fi
