# Build and test all pull requests
name: Pull Requests

on: [pull_request]

jobs:
  build:
    strategy:
      matrix:
        os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2
      - name: Determine platform binaries
        id: pkger-binaries
        uses: actions/github-script@v2
        with:
          result-encoding: string
          script: |
            let platform, binary;
            switch ('${{matrix.os}}') {
              case 'ubuntu-latest':
                platform = 'Linux_x86'
                binary = 'pkger'
                break
              case 'windows-latest':
                platform = 'Windows_x86'
                binary = 'pkger.exe'
                break
              case 'macos-latest':
                platform = 'Darwin_x86'
                binary = 'pkger'
                break
            }
            core.setOutput('platform', platform)
            core.setOutput('binary', binary)
      - name: Determine download URL for latest pkger
        id: pkger-download-url
        uses: actions/github-script@v2
        with:
          result-encoding: string
          script: |
            let platform = "${{ steps.pkger-binaries.outputs.platform }}"
            let binary = "${{ steps.pkger-binaries.outputs.binary }}"
            core.info('PLATFORM: ' + platform)
            core.info('BINARY: ' + binary)
            return github.repos.getReleaseByTag({
                owner: "markbates",
                repo: "pkger",
                tag: "v0.17.1"
            }).then(result => {
                return result.data.assets
                  .filter(a => a.name.includes(platform))
                  .map(a => a.browser_download_url)[0];
            })
      - name: Install pkger
        run: |
          curl -s -L -o pkger.tgz ${{ steps.pkger-download-url.outputs.result }}
          tar xvzf pkger.tgz

      - name: Test
        run:  make test
        env:
          PKGER: "./${{ steps.pkger-binaries.outputs.binary }}"
      - name: Build
        run:  make build
        env:
          PKGER: "./${{ steps.pkger-binaries.outputs.binary }}"
      - name: Lint
        run: make check

  integration-test:
    runs-on: 'ubuntu-latest'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Go
        uses: actions/setup-go@v2
      - name: Provision Cluster
        uses: lkingland/kind-action@v1 # use ./hack/allocate.sh locally
        with:
          version: v0.10.0
          kubectl_version: v1.20.0
          knative_serving: v0.22.0
          knative_kourier: v0.22.0
          knative_eventing: v0.22.0
          config: testdata/cluster.yaml
      - name: Configure Cluster
        run: ./hack/configure.sh
      - name: Integration Test
        run: make test-integration

  e2e-test:
    runs-on: 'ubuntu-latest'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: '^1.16'
      - name: Determine download URL for latest pkger
        id: pkger-download-url
        uses: actions/github-script@v2
        with:
          result-encoding: string
          script: |
            let platform = 'Linux_x86'
            return github.repos.getReleaseByTag({
                owner: "markbates",
                repo: "pkger",
                tag: "v0.17.1"
            }).then(result => {
                return result.data.assets
                  .filter(a => a.name.includes(platform))
                  .map(a => a.browser_download_url)[0];
            })
      - name: Install pkger
        run: |
          curl -s -L -o pkger.tgz ${{ steps.pkger-download-url.outputs.result }}
          tar xvzf pkger.tgz
      - name: Build
        run: make build
        env:
          PKGER: "./pkger"
      - name: Provision Cluster
        uses: container-tools/kind-action@v1 # use ./hack/allocate.sh locally
        with:
          version: v0.10.0
          kubectl_version: v1.20.0
          knative_serving: v0.22.0
          knative_kourier: v0.22.0
          knative_eventing: v0.22.0
          config: testdata/cluster.yaml
      - name: Configure Cluster
        run: ./hack/configure.sh
      - name: Patch Network Cluster
        run: ./test/patch_network.sh

      - name: E2E Test
        run: ./test/run_e2e_test.sh   # by default runs e2e + 'node' specific tests
      #- name: E2E Test Go
      #  run: ./test/run_e2e_test.sh go
      #- name: E2E Test Python
      #  run: ./test/run_e2e_test.sh python
      #- name: E2E Test Quarkus
      #  run: ./test/run_e2e_test.sh quarkus
      #- name: E2E Test Springboot
      #  run: ./test/run_e2e_test.sh springboot
