4
0

irc-notify.yml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. name: IRC Notify
  2. on:
  3. push:
  4. pull_request:
  5. types: [opened, closed, reopened]
  6. create:
  7. release:
  8. types: [published]
  9. workflow_dispatch:
  10. env:
  11. IRC_SERVER: irc.undernet.org
  12. IRC_PORT: 6667
  13. IRC_CHANNEL: "#pisg"
  14. IRC_NICK: github-pisg
  15. jobs:
  16. notify:
  17. runs-on: ubuntu-latest
  18. # Don't run on forks
  19. if: github.repository == 'PISG/pisg'
  20. steps:
  21. - name: build message
  22. id: msg
  23. env:
  24. EVENT: ${{ github.event_name }}
  25. REPO: ${{ github.event.repository.name }}
  26. ACTOR: ${{ github.actor }}
  27. REF_NAME: ${{ github.ref_name }}
  28. COMMITS: ${{ toJSON(github.event.commits) }}
  29. HEAD_MSG: ${{ github.event.head_commit.message }}
  30. COMPARE: ${{ github.event.compare }}
  31. PR_ACTION: ${{ github.event.pull_request.merged && 'merged' || github.event.action }}
  32. PR_NUMBER: ${{ github.event.pull_request.number }}
  33. PR_TITLE: ${{ github.event.pull_request.title }}
  34. PR_URL: ${{ github.event.pull_request.html_url }}
  35. REF_TYPE: ${{ github.event.ref_type }}
  36. RELEASE_TAG: ${{ github.event.release.tag_name }}
  37. RELEASE_URL: ${{ github.event.release.html_url }}
  38. run: |
  39. case "$EVENT" in
  40. push)
  41. count=$(echo "$COMMITS" | jq length)
  42. [ "$count" -gt 0 ] || exit 0
  43. # First line of the last commit only, to avoid flooding the channel
  44. title=$(printf '%s\n' "$HEAD_MSG" | head -n1 | cut -c1-200)
  45. msg="[$REPO] $ACTOR pushed $count commit(s) to $REF_NAME: $title $COMPARE" ;;
  46. pull_request)
  47. msg="[$REPO] $ACTOR $PR_ACTION PR #$PR_NUMBER: $PR_TITLE $PR_URL" ;;
  48. create)
  49. [ "$REF_TYPE" = tag ] || exit 0
  50. msg="[$REPO] $ACTOR tagged $REF_NAME" ;;
  51. release)
  52. msg="[$REPO] New release $RELEASE_TAG: $RELEASE_URL" ;;
  53. workflow_dispatch)
  54. msg="[$REPO] test notification triggered by $ACTOR" ;;
  55. esac
  56. echo "text=$msg" >> "$GITHUB_OUTPUT"
  57. # PRIVMSG straight to the channel without joining: needs #pisg to be -n and -r
  58. - name: send to IRC
  59. if: steps.msg.outputs.text != ''
  60. env:
  61. MESSAGE: ${{ steps.msg.outputs.text }}
  62. shell: python
  63. run: |
  64. import os, socket, sys
  65. chan, msg = os.environ["IRC_CHANNEL"], os.environ["MESSAGE"]
  66. s = socket.create_connection((os.environ["IRC_SERVER"], int(os.environ["IRC_PORT"])), timeout=60)
  67. f = s.makefile("r", encoding="utf-8", errors="replace")
  68. def send(line):
  69. s.sendall((line + "\r\n").encode())
  70. send("NICK " + os.environ["IRC_NICK"])
  71. send("USER %s 0 * :pisg GitHub notifier" % os.environ["IRC_NICK"])
  72. ok = False
  73. for line in f:
  74. line = line.rstrip()
  75. p = line.split()
  76. if p[0] == "PING":
  77. send("PONG " + line[5:])
  78. elif len(p) < 2:
  79. continue
  80. elif p[1] == "001":
  81. send("PRIVMSG %s :%s" % (chan, msg))
  82. # VERSION reply (351) proves the PRIVMSG was processed before we quit
  83. send("VERSION")
  84. elif p[1] in ("404", "403", "433", "432"):
  85. print(line)
  86. break
  87. elif p[1] == "351":
  88. ok = True
  89. send("QUIT")
  90. break
  91. s.close()
  92. sys.exit(0 if ok else 1)