comment-pr.yml 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. name: Comment on the pull request
  2. on:
  3. workflow_run:
  4. workflows: ["free-programming-books-lint"]
  5. types:
  6. - completed
  7. jobs:
  8. upload:
  9. permissions:
  10. pull-requests: write
  11. runs-on: ubuntu-latest
  12. if: >
  13. ${{ github.event.workflow_run.event == 'pull_request' &&
  14. github.event.workflow_run.conclusion == 'success' }}
  15. steps:
  16. - name: 'Download artifact'
  17. uses: actions/github-script@v6
  18. with:
  19. script: |
  20. let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
  21. owner: context.repo.owner,
  22. repo: context.repo.repo,
  23. run_id: context.payload.workflow_run.id,
  24. });
  25. let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
  26. return artifact.name == "pr"
  27. })[0];
  28. let download = await github.rest.actions.downloadArtifact({
  29. owner: context.repo.owner,
  30. repo: context.repo.repo,
  31. artifact_id: matchArtifact.id,
  32. archive_format: 'zip',
  33. });
  34. let fs = require('fs');
  35. fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr.zip`, Buffer.from(download.data));
  36. - name: 'Unzip artifact'
  37. run: unzip pr.zip
  38. - name: 'Comment on PR'
  39. env:
  40. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  41. run: |
  42. if [ -s error.log ]
  43. then
  44. gh pr comment $(<PRurl) -b "Linter failed, fix the error(s):
  45. \`\`\`
  46. $(cat error.log)
  47. \`\`\`"
  48. gh pr edit $(<PRurl) --add-label "linter error"
  49. else
  50. gh pr edit $(<PRurl) --remove-label "linter error"
  51. fi