name: ci
on:
  pull_request: {}
  push:
    branches: ["main"]

permissions: write-all

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

jobs:
  tests:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          token: ${{ secrets.BOT_GITHUB_TOKEN }}

      - uses: the-guild-org/shared-config/setup@v1
        name: set up env
        with:
          node-version-file: .node-version

      - name: Install
        run: make install

      - name: Test
        run: npm start -- test --cwd ./gateways/hive-router --run-script ./run.sh  --reporter tap --graphql http://127.0.0.1:4000/graphql --healthcheck http://127.0.0.1:4000/health --exit-on-fail

  gateways:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          token: ${{ secrets.BOT_GITHUB_TOKEN }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}

      - uses: the-guild-org/shared-config/setup@v1
        name: set up env
        with:
          node-version-file: .node-version

      - name: Prepare
        run: make install

      - name: Test Gateways
        run: make test-all

      - name: Upload Logs
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: gateways-logs
          path: gateways/*/logs/*.log

      - name: Summary
        run: |
          EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
          echo "summary<<$EOF" >> "$GITHUB_ENV"
          cat ./REPORT.md >> "$GITHUB_ENV"
          echo "$EOF" >> "$GITHUB_ENV"

      - run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

      - name: publish report
        uses: EndBug/add-and-commit@290ea2c423ad77ca9c62ae0f5b224379612c0321 # v10
        if: github.event_name == 'pull_request' && github.event.pull_request.title != 'Upcoming Release Changes'
        with:
          add: ./REPORT.md ./README.md ./website/index.html ./website/data.json
          message: "Update results"
          pull: "--rebase --autostash"
          author_email: github-actions@github.com
          author_name: github-actions
          github_token: ${{ secrets.BOT_GITHUB_TOKEN }}

      - name: Publish Summary
        if: github.event_name == 'pull_request'
        uses: marocchino/sticky-pull-request-comment@70d2764d1a7d5d9560b100cbea0077fc8f633987 # v3
        with:
          header: "Test Results"
          message: ${{ env.summary }}

      - name: Publish to Cloudflare Pages
        uses: cloudflare/pages-action@f0a1cd58cd66095dee69bfa18fa5efd1dde93bca # v1
        with:
          apiToken: ${{ secrets.CF_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: federation-gateway-compatibility
          directory: ./website
          gitHubToken: ${{ secrets.BOT_GITHUB_TOKEN }}
          branch: ${{ github.event.pull_request.number != '' && format('pr-{0}', github.event.pull_request.number) || 'main' }}
