name: "Post benchmark results"

on:
  workflow_run:
    workflows: ["Benchmark"]
    types:
      - completed

jobs:
  post-benchmark-results:
    if: github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-latest

    steps:
      # Checkout repo for the github-action-benchmark action
      - uses: actions/checkout@v4

      - name: Download result artifacts
        uses: actions/download-artifact@v4
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
          run-id: ${{ github.event.workflow_run.id }}
          name: results
          path: ./artifacts

      - name: Test for PR
        uses: mathiasvr/command-output@v1
        id: test-pr
        with:
          run: cat artifacts/.PR_INFO

      # gh-pages branch is updated and pushed automatically with extracted benchmark data
      - name: Store benchmark result
        uses: benchmark-action/github-action-benchmark@v1
        with:
          name: "Yew master branch benchmarks (Lower is better)"
          tool: "customSmallerIsBetter"
          output-file-path: artifacts/results.json
          gh-pages-branch: "gh-pages"
          # Access token to deploy GitHub Pages branch
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # Push and deploy GitHub pages branch automatically
          alert-threshold: "200%"
          alert-comment-cc-users: "@yewstack/yew"
          # Only push when this is a non-pr commit that has been benchmarked, i.e. master
          auto-push: ${{ fromJSON(steps.test-pr.outputs.stdout).number == '' }}
          save-data-file: ${{ fromJSON(steps.test-pr.outputs.stdout).number == '' }}
          # Enable job summary
          summary-always: true
          comment-always: true
          ref: ${{ github.event.workflow_run.head_sha }}
