link-workflow-to-pr.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // This script is used to link a workflow run to a pull request.
  2. // The script retrieves the workflow runs for the repository and finds the workflow run for the branch targeted.
  3. // It then updates the pull request description with the link to the workflow run.
  4. module.exports = async ({github, context, branchTargeted, pullRequestNumber}) => {
  5. console.log(`Link the workflow run to the pull request #${pullRequestNumber} for the branch '${branchTargeted}'`);
  6. // Retrieve the workflow runs for the repository.
  7. const runs = await github.request('GET /repos/{owner}/{repo}/actions/runs', {
  8. owner: context.repo.owner,
  9. repo: context.repo.repo,
  10. headers: {
  11. 'X-GitHub-Api-Version': '2022-11-28'
  12. }
  13. })
  14. // Retrieve the workflow run for the branch targeted.
  15. const workflow = runs.data.workflow_runs.find(run => run.head_branch == branchTargeted)
  16. // Handle the error case.
  17. let message = `No workflow found for the branch ${branchTargeted}`;
  18. if (workflow) {
  19. console.warn(`Workflow run found: ${workflow.html_url}`)
  20. message = `[Workflow result](${workflow.html_url})`;
  21. }
  22. // Update the pull request with the link to the workflow run.
  23. github.rest.pulls.update({
  24. owner: context.repo.owner,
  25. repo: context.repo.repo,
  26. pull_number: pullRequestNumber,
  27. body: message,
  28. });
  29. }