status.yml 3.5 KB

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