# name: Performance Benchmark Reporter

on:
  repository_dispatch:
    types: [ benchmark-report ]

jobs:
  report-pull-request:
    name: Report Performance Benchmarks on Pull Request
    runs-on: ubuntu-18.04
    if: ${{ github.event.client_payload.issue_number != -1 }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-west-2
      - name: Get benchmark results
        id: get-results
        run: aws s3api get-object --bucket=performance-benchmarking-github-actions-results --key="$KEY" results.log
        env:
          KEY: ${{ github.event.client_payload.key }}
      - name: Post results to PR
        uses: actions/github-script@v3
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const { ACTOR, ISSUE_NUMBER, GITHUB_WORKSPACE } = process.env;
            const issue_number = parseInt(ISSUE_NUMBER, 10);
            const { owner, repo } = context.repo;
            fs = require('fs');
            fs.readFile(`${GITHUB_WORKSPACE}/results.log`, 'utf8', function (err,data) {
              if (err) {
                return console.log(err);
              }
              return github.issues.createComment({
                issue_number,
                owner,
                repo,
                body: `@${ACTOR}\n ${data}`
              });
            });
        env:
          ACTOR: ${{ github.event.client_payload.actor }}
          ISSUE_NUMBER: ${{ github.event.client_payload.issue_number }}
  report-email:
    name: Report Performance Benchmarks via Email
    runs-on: ubuntu-18.04
    if: ${{ github.event.client_payload.issue_number == -1 }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-west-2
      - name: Get benchmark results
        id: get-results
        run: aws s3api get-object --bucket=performance-benchmarking-github-actions-results --key="$KEY" results.log
        env:
          KEY: ${{ github.event.client_payload.key }}
      - name: Send Email
        if: ${{ github.event.client_payload.actor_email == '' }}
        uses: ./.github/actions/ses-email-action
        with:
          region: us-west-2
          version: ${{ github.event.client_payload.dolt_version }}
          toAddresses: '["${{ secrets.PERF_REPORTS_EMAIL_ADDRESS }}"]'
          dataFile: ${{ format('{0}/results.log', github.workspace) }}
      - name: Send Email
        if: ${{ github.event.client_payload.actor_email != '' }}
        uses: ./.github/actions/ses-email-action
        with:
          region: us-west-2
          version: ${{ github.event.client_payload.dolt_version }}
          toAddresses: '["${{ github.event.client_payload.actor_email }}"]'
          dataFile: ${{ format('{0}/results.log', github.workspace) }}
