name: Build Performance

on:
  # Note! you can't safely use "pull_request_target" here
  # This workflow is mostly useful for "internal PRs"
  # External PRs won't be able to post a PR comment
  # See https://github.com/preactjs/compressed-size-action/issues/54
  # See https://securitylab.github.com/research/github-actions-preventing-pwn-requests
  pull_request:
    branches:
      - main
      - docusaurus-v**
    paths:
      - package.json
      - yarn.lock
      - packages/**
      - website/**
      - '!website/docs/**'

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  build-size:
    permissions:
      checks: write # for preactjs/compressed-size-action to create and update the checks
      contents: read # for actions/checkout to fetch code
      issues: write # for preactjs/compressed-size-action to create comments
      pull-requests: write # for preactjs/compressed-size-action to write a PR review
    name: Build Size Report
    timeout-minutes: 30
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3
      - name: Set up Node
        uses: actions/setup-node@5b949b50c3461bbcd5a540b150c368278160234a # v3
        with:
          node-version: '16'
          cache: yarn
      - name: Track build size changes
        uses: preactjs/compressed-size-action@8119d3d31b6e57b167e09c81dfa877eada3bcb35 # v2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          build-script: build:website:en
          clean-script: clear:website # see https://github.com/facebook/docusaurus/pull/6838
          pattern: '{website/build/assets/js/main*js,website/build/assets/css/styles*css,website/.docusaurus/globalData.json,website/build/index.html,website/build/blog/index.html,website/build/blog/**/introducing-docusaurus/*,website/build/docs/index.html,website/build/docs/installation/index.html,website/build/tests/docs/index.html,website/build/tests/docs/standalone/index.html}'
          strip-hash: '\.([^;]\w{7})\.'
          minimum-change-threshold: 30
          compression: none
  build-time:
    name: Build Time Perf
    timeout-minutes: 30
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3
      - uses: actions/setup-node@5b949b50c3461bbcd5a540b150c368278160234a # v3
        with:
          cache: yarn
      - name: Installation
        run: yarn

      # Ensure build with a cold cache does not increase too much
      - name: Build (cold cache)
        run: yarn workspace website build --locale en
        timeout-minutes: 8

      # Ensure build with a warm cache does not increase too much
      - name: Build (warm cache)
        run: yarn workspace website build --locale en
        timeout-minutes: 2
    # TODO post a GitHub comment with build with perf warnings?
