name: E2E Performance Tests (Desktop)

on:
  pull_request:
    branches: [master]
    types:
      - labeled

env:
  RESULTS_PATH: e2e/performance/perf-test-report.json

jobs:
  build:
    if: ${{ github.event.label.name == 'Run E2E Performance Tests' }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        node-version: [16]
    steps:
      - name: Add start comment
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: `E2E Performance Tests started 🏎️`,
            });

      - name: Set env variable for timestamp
        run: echo "NOW=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV

      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      - name: Install packages
        run: sudo apt-get install libxtst-dev libpng++-dev

      - name: Install dependencies 👨🏻‍💻
        run: npm ci

      - name: E2E Performance Tests for Electron 🧪
        run: ELECTRON_DISABLE_SANDBOX=1 xvfb-run --auto-servernum --server-args="-screen 0 1280x960x24" -- cd e2e && npm run test:performance

      - name: Upload artifact to Github
        uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
        with:
          name: perf-test-report.json
          path: ${{ env.RESULTS_PATH }}
          if-no-files-found: error
          retention-days: 14

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_PERFORMANCE_TESTS_PUT_BUCKET }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_PERFORMANCE_TESTS_PUT_BUCKET }}
          aws-region: ${{ secrets.AWS_REGION_PERFORMANCE_TESTS_PUT_BUCKET }}
          mask-aws-account-id: true

      - name: Upload report to S3
        run: aws s3 cp ${{ env.RESULTS_PATH }} s3://${{ secrets.AWS_BUCKET_PERFORMANCE_TESTS }}/${{ github.head_ref }}-${{ github.sha }}-${{ env.NOW }}.json

      - name: Add results in PR comment
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            const fs = require('fs');
            const {generateCommentBodyPerformanceTest} = require('./e2e/utils/pr-e2e-durations-report.js');
            const fileContents = fs.readFileSync('${{ env.RESULTS_PATH }}');
            github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: generateCommentBodyPerformanceTest(fileContents),
            });

      - name: Remove "Run E2E Performance Tests" label
        if: always()
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        continue-on-error: true # Label might have been removed manually
        with:
          script: |
            github.rest.issues.removeLabel({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: 'Run E2E Performance Tests',
            });
