status.yml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. name: status
  2. on:
  3. workflow_run:
  4. workflows: [test]
  5. types: [completed]
  6. defaults:
  7. run:
  8. shell: bash -euv -o pipefail {0}
  9. jobs:
  10. # forward custom statuses
  11. status:
  12. runs-on: ubuntu-latest
  13. steps:
  14. - uses: actions/download-artifact@v4
  15. continue-on-error: true
  16. with:
  17. github-token: ${{secrets.GITHUB_TOKEN}}
  18. run-id: ${{github.event.workflow_run.id}}
  19. pattern: '{status,status-*}'
  20. merge-multiple: true
  21. path: status
  22. - name: update-status
  23. continue-on-error: true
  24. run: |
  25. ls status
  26. for s in $(shopt -s nullglob ; echo status/*.json)
  27. do
  28. # parse requested status
  29. export STATE="$(jq -er '.state' $s)"
  30. export CONTEXT="$(jq -er '.context' $s)"
  31. export DESCRIPTION="$(jq -er '.description' $s)"
  32. # help lookup URL for job/steps because GitHub makes
  33. # it VERY HARD to link to specific jobs
  34. export TARGET_URL="$(
  35. jq -er '.target_url // empty' $s || (
  36. export TARGET_JOB="$(jq -er '.target_job' $s)"
  37. export TARGET_STEP="$(jq -er '.target_step // ""' $s)"
  38. curl -sS -H "authorization: token ${{secrets.BOT_TOKEN}}" \
  39. "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/runs/`
  40. `${{github.event.workflow_run.id}}/jobs" \
  41. | jq -er '.jobs[]
  42. | select(.name == env.TARGET_JOB)
  43. | .html_url
  44. + "?check_suite_focus=true"
  45. + ((.steps[]
  46. | select(.name == env.TARGET_STEP)
  47. | "#step:\(.number):0") // "")'))"
  48. # update status
  49. curl -sS -X POST -H "authorization: token ${{secrets.BOT_TOKEN}}" \
  50. "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/statuses/`
  51. `${{github.event.workflow_run.head_sha}}" \
  52. -d "$(jq -n '{
  53. state: env.STATE,
  54. context: env.CONTEXT,
  55. description: env.DESCRIPTION,
  56. target_url: env.TARGET_URL,
  57. }' | tee /dev/stderr)"
  58. done
  59. # forward custom pr-comments
  60. comment:
  61. runs-on: ubuntu-latest
  62. # only run on success (we don't want garbage comments!)
  63. if: ${{github.event.workflow_run.conclusion == 'success'}}
  64. steps:
  65. # generated comment?
  66. - uses: actions/download-artifact@v4
  67. continue-on-error: true
  68. with:
  69. github-token: ${{secrets.GITHUB_TOKEN}}
  70. run-id: ${{github.event.workflow_run.id}}
  71. pattern: '{comment,comment-*}'
  72. merge-multiple: true
  73. path: comment
  74. - name: update-comment
  75. continue-on-error: true
  76. run: |
  77. ls comment
  78. for s in $(shopt -s nullglob ; echo comment/*.json)
  79. do
  80. export NUMBER="$(jq -er '.number' $s)"
  81. export BODY="$(jq -er '.body' $s)"
  82. # check that the comment was from the most recent commit on the
  83. # pull request
  84. [ "$(curl -sS -H "authorization: token ${{secrets.BOT_TOKEN}}" \
  85. "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$NUMBER" \
  86. | jq -er '.head.sha')" \
  87. == ${{github.event.workflow_run.head_sha}} ] || continue
  88. # update comment
  89. curl -sS -X POST -H "authorization: token ${{secrets.BOT_TOKEN}}" \
  90. "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/`
  91. `$NUMBER/comments" \
  92. -d "$(jq -n '{
  93. body: env.BODY,
  94. }' | tee /dev/stderr)"
  95. done