diff --git a/.github/workflows/comment-pr.yml b/.github/workflows/comment-pr.yml new file mode 100644 index 0000000000000..fd792f1da59a0 --- /dev/null +++ b/.github/workflows/comment-pr.yml @@ -0,0 +1,56 @@ +name: Comment on the pull request + +on: + workflow_run: + workflows: ["free-programming-books-lint"] + types: + - completed + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + upload: + runs-on: ubuntu-latest + if: > + ${{ github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' }} + steps: + - name: 'Download 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 == "pr" + })[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}/pr.zip`, Buffer.from(download.data)); + + - name: 'Unzip artifact' + run: unzip pr.zip + + - name: 'Comment on PR' + run: | + cat error.log + if [ -s error.log ] + then + gh pr review $(> output.log || echo "Analyzing..." + fpb-lint ./casts/ &>> output.log || echo "Analyzing..." + fpb-lint ./courses/ &>> output.log || echo "Analyzing..." + fpb-lint ./more/ &>> output.log || echo "Analyzing..." + + touch error.log + + - name: Clean output + if: ${{ always() && + github.event_name == 'pull_request' }} + uses: actions/github-script@v6 + with: + script: | + const fs = require('fs'); + const readline = require('readline'); + + const file = readline.createInterface({ + input: fs.createReadStream('output.log'), + output: process.stdout, + terminal: false, + }); + + let lastLine = ''; + file.on('line', (line) => { + if (lastLine) { + fs.appendFile('error.log', lastLine, (err) => { + if (err) { + console.error(err); + } + }); + } + + if (line.includes('/home/runner/work/free-programming-books/')) { + lastLine = line.replace('/home/runner/work/free-programming-books/', '') + "\r\n"; + } else if (line.includes('\u26a0')) { + lastLine = '\r\n\r\n'; + } else if (line.includes('remark-lint')) { + lastLine = line + '\r\n'; + } else { + lastLine = null; + } + }); + + file.on('close', () => { + if (!lastLine || lastLine === '\r\n\r\n') { + return; + } + + fs.appendFile('error.log', lastLine, (err) => { + if (err) { + console.error(err); + } + }); + }); + + - name: Upload artifact + if: ${{ always() && + github.event_name == 'pull_request' }} + run: | + mkdir -p ./pr + echo ${{ github.event.pull_request.html_url }} > ./pr/PRurl + mv error.log ./pr/error.log + - uses: actions/upload-artifact@v3 + with: + name: pr + path: pr/