# Done this way because of https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
name: Comment on the pull request

# read-write repo token
# access to secrets
on:
  workflow_run:
    workflows: ["PR-WORKFLOW"]
    types:
      - completed

jobs:
  comment:
    runs-on: ubuntu-latest
    if: >
      ${{ github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success' }}
        
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: 18.x
    - name: Download artifact
      uses: dawidd6/action-download-artifact@v2
      with:
        github_token: ${{secrets.GITHUB_TOKEN}}
        workflow: ci.yml
        run_id: ${{github.event.workflow_run.id }}
        name: packets
        path: packets
    - run: npm i minecraft-data
    - run: cd packets && node ../.github/workflows/md-comment-maker.js
    - name: 'Comment on PR'
      uses: actions/github-script@v3
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        script: |
          var fs = require('fs');
          var issue_number = Number(fs.readFileSync('./packets/NR', 'utf-8'));
          await github.issues.createComment({
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: issue_number,
            body: fs.readFileSync('./packets/README.md', 'utf-8')
          });
