name: IRC Notify on: push: pull_request: types: [opened, closed, reopened] create: release: types: [published] workflow_dispatch: env: IRC_SERVER: irc.undernet.org IRC_PORT: 6667 IRC_CHANNEL: "#pisg" IRC_NICK: github-pisg jobs: notify: runs-on: ubuntu-latest # Don't run on forks if: github.repository == 'PISG/pisg' steps: - name: build message id: msg env: EVENT: ${{ github.event_name }} REPO: ${{ github.event.repository.name }} ACTOR: ${{ github.actor }} REF_NAME: ${{ github.ref_name }} COMMITS: ${{ toJSON(github.event.commits) }} HEAD_MSG: ${{ github.event.head_commit.message }} COMPARE: ${{ github.event.compare }} PR_ACTION: ${{ github.event.pull_request.merged && 'merged' || github.event.action }} PR_NUMBER: ${{ github.event.pull_request.number }} PR_TITLE: ${{ github.event.pull_request.title }} PR_URL: ${{ github.event.pull_request.html_url }} REF_TYPE: ${{ github.event.ref_type }} RELEASE_TAG: ${{ github.event.release.tag_name }} RELEASE_URL: ${{ github.event.release.html_url }} run: | case "$EVENT" in push) count=$(echo "$COMMITS" | jq length) [ "$count" -gt 0 ] || exit 0 # First line of the last commit only, to avoid flooding the channel title=$(printf '%s\n' "$HEAD_MSG" | head -n1 | cut -c1-200) msg="[$REPO] $ACTOR pushed $count commit(s) to $REF_NAME: $title $COMPARE" ;; pull_request) msg="[$REPO] $ACTOR $PR_ACTION PR #$PR_NUMBER: $PR_TITLE $PR_URL" ;; create) [ "$REF_TYPE" = tag ] || exit 0 msg="[$REPO] $ACTOR tagged $REF_NAME" ;; release) msg="[$REPO] New release $RELEASE_TAG: $RELEASE_URL" ;; workflow_dispatch) msg="[$REPO] test notification triggered by $ACTOR" ;; esac echo "text=$msg" >> "$GITHUB_OUTPUT" # PRIVMSG straight to the channel without joining: needs #pisg to be -n and -r - name: send to IRC if: steps.msg.outputs.text != '' env: MESSAGE: ${{ steps.msg.outputs.text }} shell: python run: | import os, socket, sys chan, msg = os.environ["IRC_CHANNEL"], os.environ["MESSAGE"] s = socket.create_connection((os.environ["IRC_SERVER"], int(os.environ["IRC_PORT"])), timeout=60) f = s.makefile("r", encoding="utf-8", errors="replace") def send(line): s.sendall((line + "\r\n").encode()) send("NICK " + os.environ["IRC_NICK"]) send("USER %s 0 * :pisg GitHub notifier" % os.environ["IRC_NICK"]) ok = False for line in f: line = line.rstrip() p = line.split() if p[0] == "PING": send("PONG " + line[5:]) elif len(p) < 2: continue elif p[1] == "001": send("PRIVMSG %s :%s" % (chan, msg)) # VERSION reply (351) proves the PRIVMSG was processed before we quit send("VERSION") elif p[1] in ("404", "403", "433", "432"): print(line) break elif p[1] == "351": ok = True send("QUIT") break s.close() sys.exit(0 if ok else 1)