name: Test OCP with OPA

on:
  workflow_call:
    secrets:
      slack_webhook_url:
        required: false

permissions:
  contents: read

jobs:
  test-ocp-with-opa:
    name: Test OCP with OPA
    runs-on: ubuntu-24.04
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.slack_webhook_url }} # zizmor: ignore[secrets-outside-env]
    steps:
      - name: Check out OPA code
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
          path: opa

      - name: Install Go
        uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
        with:
          go-version-file: opa/.go-version
          cache-dependency-path: opa/go.sum

      - name: Get latest OCP release tag
        id: ocp_release
        run: |
          tag=$(gh api repos/open-policy-agent/opa-control-plane/releases/latest --jq '.tag_name')
          echo "tag=$tag" >> $GITHUB_OUTPUT
        env:
          GH_TOKEN: ${{ github.token }}

      - name: Check out OCP ${{ steps.ocp_release.outputs.tag }}
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          repository: open-policy-agent/opa-control-plane
          ref: "61213c778cd937822cfdfed0f8914649a8381954" # ${{ steps.ocp_release.outputs.tag }}
          persist-credentials: false
          path: ocp

      - name: Update OPA dependency
        working-directory: ocp
        run: |
          go mod edit -replace github.com/open-policy-agent/opa=../opa
          go mod tidy

      - name: Build
        working-directory: ocp
        run: make build

      - name: Test
        working-directory: ocp
        run: make test

      - name: e2e tests
        working-directory: ocp
        run: |
          set -euo pipefail
          matches=(opactl_*)
          export OPACTL="$(pwd)/${matches[0]}"
          $OPACTL version
          go test -tags=e2e ./e2e/... -v

      - name: Slack Notification
        uses: slackapi/slack-github-action@dcb1066f776dd043e64d0e8ba94ca15cc7e1875d # v4.0.0
        if: failure() && env.SLACK_WEBHOOK_URL != ''
        with:
          webhook: ${{ env.SLACK_WEBHOOK_URL }} # zizmor: ignore[secrets-outside-env]
          webhook-type: incoming-webhook
          payload: |
            {
              "text": "OCP compatibility check failed: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.workflow }} - ${{ github.job }}>"
            }
