name: Comment PR with Hardware performance tests results

on:
  workflow_run:
    workflows: [Hardware Performance Test]
    types:
      - completed
  
concurrency:
  group: ${{ github.event.workflow_run.event }}-${{ github.event.workflow_run.head_branch }}-${{ github.workflow }} 
  cancel-in-progress: true

permissions:
  pull-requests: write

jobs:
  comment_pr:
    if: |
      github.event.workflow_run.conclusion == 'success' &&
      github.event.workflow_run.event == 'pull_request'

    runs-on: ubuntu-24.04
    name: Comment PR with HW Performance tests results
    steps:
      - name: Download Results from PR
        uses: dawidd6/action-download-artifact@v14
        with:
          workflow: perf_hardware.yml
          path: artifacts
          # The artifact needs to be downloaded from a PR run that comes from a forked repository
          allow_forks: true 

      - name: Move artifacts to  current folder
        if: ${{ github.event.workflow_run.event == 'pull_request' }}
        run: |
          if [ ! -f "artifacts/pr_number/pr_number" ] || [ ! -f "artifacts/comment/comment.md" ]; then
            echo "Required artifact files not found."
            echo "This probably means this run was triggered by a label other than 'Run Benchmark on HW'."
            echo "Exiting workflow."
            echo "results_exist=false" >> $GITHUB_ENV
            exit 0
          fi
          mv artifacts/pr_number/pr_number .
          mv artifacts/comment/comment.md .
          echo "results_exist=true" >> $GITHUB_ENV

      - name: Install Dependencies
        if: ${{env.results_exist == 'true'}}
        run: |
          sudo apt update -y
          sudo apt install -y libfontconfig-dev

      - name: Install EJ dispatcher tool
        if: ${{env.results_exist == 'true'}}
        run: |
          cargo install ejlv

      - name: Comment PR
        if: ${{env.results_exist == 'true'}}
        run: |
          ejlv comment-pr \
            --comment-path comment.md \
            --pr-number $(cat pr_number) \
            --gh-token "${{ secrets.GITHUB_TOKEN }}" \
            --signature "hw_performance_tests"

      - name: Remove trigger label
        if: ${{env.results_exist == 'true'}}
        uses: actions/github-script@v8
        with:
          script: |
            const fs = require('fs');
            const prPath = 'pr_number';

            if (!fs.existsSync(prPath)) {
              throw new Error('Error: pr_number not found! Exiting.');
            }

            const prNumber = Number(fs.readFileSync(prPath, 'utf8').trim());

            try {
              await github.rest.issues.removeLabel({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: prNumber,
                name: 'Run benchmarks on HW'
              });
              console.log('Label removed successfully');
            } catch (error) {
              console.log('Label may have already been removed:', error.message);
            }
