name: Annotate linting

permissions:
  actions: read # download artifact
  checks: write # annotate

# this is done as a seperate workflow so
# the annotater has access to write to checks (to annotate)
on:
  workflow_run:
    workflows: ["Linting and Testing"]
    types:
      - completed

jobs:
  annotate:
    runs-on: ubuntu-latest

    steps:
      - name: Download linting report
        uses: actions/github-script@v6
        with:
          script: |
            var artifacts = await github.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: ${{github.event.workflow_run.id }},
            });
            var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "eslint_report.json"
            })[0];
            var download = await github.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchArtifact.id,
               archive_format: 'zip',
            });
            var fs = require('fs');
            fs.writeFileSync('${{github.workspace}}/eslint_report.zip', Buffer.from(download.data));
      
      - run: unzip eslint_report.zip
      
      - run: ls -la

      - name: Annotate linting
        uses: ataylorme/eslint-annotate-action@v6
        with:
          repo-token: "${{ secrets.GITHUB_TOKEN }}"
          report-json: "eslint_report.json"