name: Comment Benchmark Result

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

jobs:
  comment-benchmark-result:
    name: Comment Benchmark Result
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    steps:
      - uses: actions/checkout@v3
      - uses: denoland/setup-deno@main
        with:
          deno-version: v1.x
      - name: Download Commit Data Artifact
        uses: actions/github-script@v6
        with:
          script: |
            let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: context.payload.workflow_run.id,
            });
            let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "commitData"
            })[0];
            let download = await github.rest.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchArtifact.id,
               archive_format: 'zip',
            });
            let fs = require('fs');
            fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/commitData.zip`, Buffer.from(download.data));

      - name: Unzip Commit Data Artifact
        run: unzip commitData.zip

      - name: Download Result Artifact
        uses: actions/github-script@v6
        with:
          script: |
            let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: context.payload.workflow_run.id,
            });
            let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "benchmarkResults"
            })[0];
            let download = await github.rest.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchArtifact.id,
               archive_format: 'zip',
            });
            let fs = require('fs');
            fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/benchmarkResults.zip`, Buffer.from(download.data));

      - name: Unzip Result Artifact
        run: unzip benchmarkResults.zip

      - name: Generate Message
        id: genMessage
        run: |
          MESSAGE=$(deno run -A performance/generateMessage.ts)
          echo "MESSAGE<<EOF" >> $GITHUB_ENV
          echo "$MESSAGE" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV

      - name: "Comment on PR"
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const fs = require('fs');
            const commit_sha = fs.readFileSync('./sha', 'utf-8');
            const repo = fs.readFileSync('./repo', 'utf-8');
            if (repo.split('/')[1] === undefined) process.exit(0)
            const pr = await github.rest.repos.listPullRequestsAssociatedWithCommit({
              commit_sha: commit_sha.slice(0,-1),
              owner: repo.split('/')[0],
              repo: repo.split('/')[1].slice(0,-1),
            });
            if (pr.data[0]) {
              github.rest.issues.createComment({
                issue_number: pr.data[0].number,
                owner: "discordeno",
                repo: "discordeno",
                body: `${{ env.MESSAGE }}`
              })
            }
