name: Pull Request

on:
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      run: yarn build

  lint:
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
          ${{ needs.build.outputs.temp_dir }}
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build

    - name: Lint
      run: yarn lint

  test:
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
          ${{ needs.build.outputs.temp_dir }}
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build

    - name: Test
      run: yarn test

  e2e:
    strategy:
      matrix:
        application: 
          - { "service_name": "api-gateway", "workspace_name": "@standardnotes/api-gateway", "e2e_tag_parameter_name": "api_gateway_image_tag", "package_path": "packages/api-gateway" }
          - { "service_name": "auth", "workspace_name": "@standardnotes/auth-server", "e2e_tag_parameter_name": "auth_image_tag", "package_path": "packages/auth" }  
          - { "service_name": "files", "workspace_name": "@standardnotes/files-server", "e2e_tag_parameter_name": "files_image_tag", "package_path": "packages/files" }
          - { "service_name": "revisions", "workspace_name": "@standardnotes/revisions-server", "e2e_tag_parameter_name": "revisions_image_tag", "package_path": "packages/revisions"}
          - { "service_name": "syncing-server-js",  "workspace_name": "@standardnotes/syncing-server", "e2e_tag_parameter_name": "syncing_server_js_image_tag", "package_path": "packages/syncing-server" }
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Create Bundle Dir
      id: bundle-dir
      run: echo "temp_dir=$(mktemp -d -t ${{ matrix.application.service_name }}-${{ github.sha }}-XXXXXXX)" >> $GITHUB_OUTPUT

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
          ${{ needs.e2e.outputs.temp_dir }}
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build

    - name: Bundle
      run: yarn workspace ${{ matrix.application.workspace_name }} bundle --no-compress --output-directory ${{ steps.bundle-dir.outputs.temp_dir }}

    - name: Login to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Set up QEMU
      uses: docker/setup-qemu-action@master
      with:
        platforms: all

    - name: Set up Docker Buildx
      id: buildx
      uses: docker/setup-buildx-action@master

    - name: Publish Docker image for E2E testing
      uses: docker/build-push-action@v3
      with:
        builder: ${{ steps.buildx.outputs.name }}
        context: ${{ steps.bundle-dir.outputs.temp_dir }}
        file: ${{ steps.bundle-dir.outputs.temp_dir }}/${{ matrix.application.package_path }}/Dockerfile
        platforms: linux/amd64,linux/arm64
        push: true
        tags: standardnotes/${{ matrix.application.service_name }}:${{ github.sha }}

    - name: Run E2E test suite
      uses: convictional/trigger-workflow-and-wait@master
      with:
        owner: standardnotes
        repo: self-hosted
        github_token: ${{ secrets.CI_PAT_TOKEN }}
        workflow_file_name: testing-with-updating-client-and-server.yml
        wait_interval: 30
        client_payload: '{"${{ matrix.application.e2e_tag_parameter_name }}": "${{ github.sha }}"}'
        propagate_failure: true
        trigger_workflow: true
        wait_workflow: true
