|
| 1 | +# Post a slack message something like the following for issue and PR actions: |
| 2 | +# <$url|$title> |
| 3 | +# | $repo#$num · issue opened by $user |
| 4 | +# |
| 5 | +# Configuration: |
| 6 | +# 1. Set `SLACK_CHANNEL`. |
| 7 | +# 2. Add a `SLACK_BOT_TOKEN` secret to your repo. This is the "Bot User OAuth |
| 8 | +# Token" from the "OAuth & Permissions" section of your Slack App |
| 9 | +# (https://api.slack.com/apps). The token must have the `chat:write` |
| 10 | +# permission. |
| 11 | +# 3. Optionally tweak the `if:` and `on:` sections below to control which issue |
| 12 | +# and PR events are skipped. |
| 13 | + |
| 14 | +name: slack |
| 15 | + |
| 16 | +env: |
| 17 | + SLACK_CHANNEL: "#apm-agent-node" |
| 18 | + |
| 19 | +on: |
| 20 | + issues: |
| 21 | + types: [opened, reopened, closed] |
| 22 | + pull_request: |
| 23 | + types: [opened, ready_for_review, reopened, closed] |
| 24 | + |
| 25 | +jobs: |
| 26 | + slack: |
| 27 | + # Skip notification if: |
| 28 | + # - dependabot or renovate PRs, too noisy |
| 29 | + # - draft PRs |
| 30 | + if: ${{ !( |
| 31 | + (github.event.action == 'opened' && github.event.pull_request.draft) || |
| 32 | + github.event.pull_request.user.login == 'dependabot[bot]' || |
| 33 | + github.event.pull_request.user.login == 'elastic-renovate-prod[bot]' |
| 34 | + ) }} |
| 35 | + runs-on: ubuntu-24.04 |
| 36 | + steps: |
| 37 | + - name: Prepare Slack message |
| 38 | + id: prepare |
| 39 | + shell: python |
| 40 | + env: |
| 41 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 42 | + run: | |
| 43 | + import os |
| 44 | + from pprint import pprint |
| 45 | + import json |
| 46 | +
|
| 47 | + CLOSED_RED = '#cb2431' |
| 48 | + GITHUB_BLACK = '#24292f' |
| 49 | + MERGED_PURPLE = '#6f42c1' |
| 50 | + OPEN_GREEN = '#36a64f' |
| 51 | + DRAFT_GRAY = '#6a737d' |
| 52 | +
|
| 53 | + ctx = json.loads(os.environ["GITHUB_CONTEXT"]) |
| 54 | + # pprint(ctx) # for dev/debugging |
| 55 | + event = ctx["event"] |
| 56 | + action = event["action"] |
| 57 | + if "issue" in event: |
| 58 | + title = event["issue"]["title"] |
| 59 | + url = event["issue"]["html_url"] |
| 60 | + num = event["issue"]["number"] |
| 61 | + action_str = f"issue {action}" |
| 62 | + color = { |
| 63 | + "opened": OPEN_GREEN, |
| 64 | + "reopened": OPEN_GREEN, |
| 65 | + "closed": CLOSED_RED, |
| 66 | + }.get(action, "#ffffff") |
| 67 | + elif "pull_request" in event: |
| 68 | + title = event["pull_request"]["title"] |
| 69 | + url = event["pull_request"]["html_url"] |
| 70 | + num = event["pull_request"]["number"] |
| 71 | + if action == "closed": |
| 72 | + if event["pull_request"]["merged"]: |
| 73 | + action_str = "PR merged" |
| 74 | + color = MERGED_PURPLE |
| 75 | + else: |
| 76 | + action_str = "PR closed" |
| 77 | + color = CLOSED_RED |
| 78 | + elif event["pull_request"]["draft"]: |
| 79 | + action_str = "PR in draft" |
| 80 | + color = DRAFT_GRAY |
| 81 | + elif action == "ready_for_review": |
| 82 | + action_str = "PR ready for review" |
| 83 | + color = OPEN_GREEN |
| 84 | + else: |
| 85 | + action_str = "PR opened" |
| 86 | + color = OPEN_GREEN |
| 87 | + else: |
| 88 | + pprint(ctx) |
| 89 | + raise ValueError('unexpected event: not an issue or PR event') |
| 90 | +
|
| 91 | + payload = { |
| 92 | + "channel": os.environ["SLACK_CHANNEL"], |
| 93 | +
|
| 94 | + # Note: Omitting the "text" field is intentional, so that it is not |
| 95 | + # rendered by default. Guidelines on accessibility in: |
| 96 | + # https://api.slack.com/methods/chat.postMessage#text-blocks-attachments |
| 97 | + # are unclear for "attachments" usage. This competes with: |
| 98 | + # https://api.slack.com/reference/messaging/attachments#guidelines__message-attachments-as-objects |
| 99 | + # guidelines to group all object data inside the attachment. |
| 100 | + # The downside is that the `chatMessage` below results in warnings |
| 101 | + # from the Slack API about not including the top-level "text". |
| 102 | + #"text": title, |
| 103 | +
|
| 104 | + # Intentionally *not* using Slack's newer blocks, |
| 105 | + # https://api.slack.com/messaging/attachments-to-blocks |
| 106 | + # because styling with the older syntax is slightly nicer, IMHO. |
| 107 | + "attachments": [ |
| 108 | + { |
| 109 | + "color": color, |
| 110 | + "title": title, |
| 111 | + "title_link": url, |
| 112 | + "footer": f"{ctx['repository']}#{num} · *{action_str}* by {event['sender']['login']}", |
| 113 | + "footer_icon": "https://github.githubassets.com/favicon.ico" |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | +
|
| 118 | + with open(os.environ.get("GITHUB_OUTPUT"), "a") as f: |
| 119 | + f.write("payload={}".format(json.dumps(payload))) |
| 120 | +
|
| 121 | + - name: Post Slack message |
| 122 | + |
| 123 | + with: |
| 124 | + method: chat.postMessage |
| 125 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 126 | + payload: ${{ steps.prepare.outputs.payload }} |
0 commit comments