name: CI
on:
  - pull_request
jobs:
  validate:
    name: validate - ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    container: ghcr.io/kedacore/build-tools:1.19.7
    strategy:
      matrix:
        include:
        - runner: ARM64
          name: arm64
        - runner: ubuntu-latest
          name: amd64
    steps:

      - name: Check out code
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Register workspace path
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"

      - name: Check go version
        run: go version

      - name: Set Go paths
        id: go-paths
        run: |
          echo ::set-output name=mod_cache::$(go env GOMODCACHE)
          echo ::set-output name=build_cache::$(go env GOCACHE)

      - name: Go modules cache
        uses: actions/cache@v3.3.1
        with:
          path: ${{ steps.go-paths.outputs.mod_cache }}
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}

      - name: Go build cache
        uses: actions/cache@v3.3.1
        with:
          path: ${{ steps.go-paths.outputs.build_cache }}
          key: ${{ runner.os }}-go-build-cache-${{ hashFiles('**/go.sum') }}

      - name: Go modules sync
        run: go mod tidy -compat=1.19

      - name: Verify Generated clientset is up to date
        run: make clientset-verify

      - name: Build
        run: make build

      - name: Test
        run: make test

      - name: Create test Summary
        uses: test-summary/action@v2
        with:
          paths: "report.xml"
        if: always()

  validate-dockerfiles:
    name: validate-dockerfiles - ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    container: ghcr.io/kedacore/build-tools:1.19.7
    strategy:
       matrix:
        include:
        - runner: ARM64
          name: arm64
        - runner: ubuntu-latest
          name: amd64
    steps:
      - name: Check out code
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Register workspace path
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"

      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            dockerfiles:
              - 'Dockerfile*'

      - name: Build images
        if: steps.filter.outputs.dockerfiles == 'true'
        run: make docker-build

  validate-build-tools:
    name: Validate build-tools - ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    container: ghcr.io/kedacore/build-tools:1.19.7
    strategy:
       matrix:
        include:
        - runner: ARM64
          name: arm64
        - runner: ubuntu-latest
          name: amd64
    steps:
      - uses: actions/checkout@v3

      - name: Register workspace path
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"

      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            tools:
              - 'tools/**'
              - 'Makefile'

      - name: Set up QEMU
        if: steps.filter.outputs.tools == 'true'
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        if: steps.filter.outputs.tools == 'true'
        uses: docker/setup-buildx-action@v2

      - name: Build tools
        if: steps.filter.outputs.tools == 'true'
        run: make docker-build-tools

  validate-dev-container:
    name: Validate dev-container - ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    container: ghcr.io/kedacore/build-tools:1.19.7
    strategy:
       matrix:
        include:
        - runner: ARM64
          name: arm64
        - runner: ubuntu-latest
          name: amd64
    steps:
      - uses: actions/checkout@v3

      - name: Register workspace path
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"

      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            devcontainer:
              - '.devcontainer/Dockerfile'

      - name: Build tools
        if: steps.filter.outputs.devcontainer == 'true'
        run: make docker-build-dev-containers

  statics:
    name: Static Checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4.5.0
        with:
          python-version: 3.x
      - uses: actions/setup-go@v4
        with:
          go-version: 1.19
      - name: Get golangci
        run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0
      - uses: pre-commit/action@v3.0.0

  trivy-scan:
    uses: kedacore/keda/.github/workflows/template-trivy-scan.yml@main
    with:
      runs-on: 'ubuntu-latest'
      scan-type: 'fs'
      format: 'table'
      output: ''
      exit-code: 1
      publish: false
