|
| 1 | +"""Post a 'wheel release starting' Slack notification via chat.postMessage. |
| 2 | +
|
| 3 | +Env: SLACK_API_TOKEN, SLACK_CHANNEL_ID (both required or no-op), SOURCE_REPO, |
| 4 | +REF, PACKAGES, RUN_URL. Slack/network errors warn and never fail the job. |
| 5 | +""" |
| 6 | +import json |
| 7 | +import os |
| 8 | +import urllib.error |
| 9 | +import urllib.request |
| 10 | + |
| 11 | +SLACK_URL = "https://slack.com/api/chat.postMessage" |
| 12 | + |
| 13 | + |
| 14 | +def build_text(source_repo: str, ref: str, packages: str, run_url: str) -> str: |
| 15 | + """Return the release notification Slack message body.""" |
| 16 | + # TODO: this fires before the approval gate, so the release is pending. Once |
| 17 | + # the `release` environment gate is removed, reword to "Wheel release starting" |
| 18 | + # with a "View release run" link, since the release will start immediately. |
| 19 | + return ( |
| 20 | + f":hourglass_flowing_sand: *Wheel release pending approval* — `{source_repo}`\n" |
| 21 | + f"• ref: `{ref[:12] or '—'}`\n" |
| 22 | + f"• packages: {packages.strip() or 'auto-detect from tags at HEAD'}\n" |
| 23 | + f"• <{run_url}|Review & approve →>" |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def post(token: str, channel: str, text: str) -> None: |
| 28 | + """Post *text* to *channel*, warning (not failing) on error.""" |
| 29 | + data = json.dumps({"channel": channel, "text": text, "unfurl_links": False}).encode() |
| 30 | + request = urllib.request.Request( |
| 31 | + SLACK_URL, |
| 32 | + data=data, |
| 33 | + headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json; charset=utf-8"}, |
| 34 | + ) |
| 35 | + try: |
| 36 | + with urllib.request.urlopen(request, timeout=15) as response: |
| 37 | + body = json.loads(response.read()) |
| 38 | + except (urllib.error.URLError, TimeoutError, ValueError) as e: |
| 39 | + print(f"::warning::Slack request failed: {e}") |
| 40 | + return |
| 41 | + if not body.get("ok"): |
| 42 | + print(f"::warning::Slack notification failed: {body.get('error', 'unknown error')}") |
| 43 | + |
| 44 | + |
| 45 | +def main() -> None: |
| 46 | + token = os.environ.get("SLACK_API_TOKEN", "").strip() |
| 47 | + channel = os.environ.get("SLACK_CHANNEL_ID", "").strip() |
| 48 | + if not token or not channel: |
| 49 | + print("Slack token or channel not configured; skipping notification.") |
| 50 | + return |
| 51 | + post( |
| 52 | + token, |
| 53 | + channel, |
| 54 | + build_text( |
| 55 | + os.environ.get("SOURCE_REPO", "integrations-core"), |
| 56 | + os.environ.get("REF", ""), |
| 57 | + os.environ.get("PACKAGES", ""), |
| 58 | + os.environ.get("RUN_URL", ""), |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments