nightly_pr_comment.yml 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. name: Comment PR artifacts links
  2. on:
  3. workflow_run:
  4. workflows: ['Build job']
  5. types: [completed]
  6. jobs:
  7. pr_comment:
  8. if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
  9. runs-on: ubuntu-latest
  10. steps:
  11. - uses: actions/github-script@v3
  12. with:
  13. script: |
  14. const {owner, repo} = context.repo;
  15. const run_id = ${{github.event.workflow_run.id}};
  16. const pull_head_sha = '${{github.event.workflow_run.head_sha}}';
  17. const pull_user_id = ${{github.event.sender.id}};
  18. const issue_number = await (async () => {
  19. const pulls = await github.pulls.list({owner, repo});
  20. for await (const {data} of github.paginate.iterator(pulls)) {
  21. for (const pull of data) {
  22. if (pull.head.sha === pull_head_sha && pull.user.id === pull_user_id) {
  23. return pull.number;
  24. }
  25. }
  26. }
  27. })();
  28. if (issue_number) {
  29. core.info(`Using pull request ${issue_number}`);
  30. } else {
  31. return core.error(`No matching pull request found`);
  32. }
  33. const {data: {artifacts}} = await github.actions.listWorkflowRunArtifacts({owner, repo, run_id});
  34. if (!artifacts.length) {
  35. return core.error(`No artifacts found`);
  36. }
  37. let body = `Download the artifacts for this pull request:\n`;
  38. for (const art of artifacts) {
  39. body += `\n* [${art.name}](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`;
  40. }
  41. const {data: comments} = await github.issues.listComments({repo, owner, issue_number});
  42. const existing_comment = comments.find((c) => c.user.login === 'github-actions[bot]');
  43. if (existing_comment) {
  44. core.info(`Updating comment ${existing_comment.id}`);
  45. await github.issues.updateComment({repo, owner, comment_id: existing_comment.id, body});
  46. } else {
  47. core.info(`Creating a comment`);
  48. await github.issues.createComment({repo, owner, issue_number, body});
  49. }