|
| 1 | +"""CLI for managing the Discord webhook integration. |
| 2 | + |
| 3 | +Subcommands: |
| 4 | +- ``gac discord setup`` — interactively configure (or replace) the webhook URL. |
| 5 | +- ``gac discord remove`` — delete the webhook URL from $HOME/.gac.env. |
| 6 | +- ``gac discord show`` — display whether a webhook is configured (URL masked). |
| 7 | +- ``gac discord test`` — send a test notification to the configured webhook. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import os |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import click |
| 16 | +import questionary |
| 17 | +from dotenv import load_dotenv, set_key, unset_key |
| 18 | + |
| 19 | +from gac.discord_webhook import ENV_KEY, notify_commit |
| 20 | + |
| 21 | +GAC_ENV_PATH = Path.home() / ".gac.env" |
| 22 | + |
| 23 | + |
| 24 | +def _mask_url(url: str) -> str: |
| 25 | + """Return a masked preview of a URL so it's safe to display.""" |
| 26 | + if len(url) <= 30: |
| 27 | + return url |
| 28 | + return f"{url[:30]}…" |
| 29 | + |
| 30 | + |
| 31 | +def _load_existing_url() -> str | None: |
| 32 | + """Read the current webhook URL from $HOME/.gac.env, if any.""" |
| 33 | + if not GAC_ENV_PATH.exists(): |
| 34 | + return None |
| 35 | + load_dotenv(GAC_ENV_PATH, override=True) |
| 36 | + value = os.getenv(ENV_KEY) |
| 37 | + if value is None: |
| 38 | + return None |
| 39 | + value = value.strip() |
| 40 | + return value or None |
| 41 | + |
| 42 | + |
| 43 | +def _prompt_for_url() -> str | None: |
| 44 | + """Prompt the user for a webhook URL. Returns None on cancel / invalid.""" |
| 45 | + response = questionary.text("Discord webhook URL:").ask() |
| 46 | + if response is None: |
| 47 | + click.echo("Cancelled.") |
| 48 | + return None |
| 49 | + url: str = str(response).strip() |
| 50 | + if not url: |
| 51 | + click.echo("No URL provided. Cancelled.") |
| 52 | + return None |
| 53 | + if not url.startswith(("http://", "https://")): |
| 54 | + click.echo("That doesn't look like a URL (must start with http:// or https://).") |
| 55 | + return None |
| 56 | + return url |
| 57 | + |
| 58 | + |
| 59 | +def _save_url(url: str) -> None: |
| 60 | + """Persist the webhook URL to $HOME/.gac.env.""" |
| 61 | + GAC_ENV_PATH.touch(exist_ok=True) |
| 62 | + set_key(str(GAC_ENV_PATH), ENV_KEY, url) |
| 63 | + os.environ[ENV_KEY] = url # keep current process in sync, e.g. for `discord test` |
| 64 | + click.echo("Discord webhook saved.") |
| 65 | + |
| 66 | + |
| 67 | +@click.group() |
| 68 | +def discord() -> None: |
| 69 | + """Manage the Discord webhook integration for commit notifications.""" |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +@discord.command() |
| 74 | +def setup() -> None: |
| 75 | + """Interactively configure a Discord webhook URL.""" |
| 76 | + click.echo("Discord Webhook Setup") |
| 77 | + click.echo( |
| 78 | + "gac can ping a Discord channel every time you make a commit, using a\n" |
| 79 | + "webhook URL from your channel's integration settings.\n" |
| 80 | + ) |
| 81 | + |
| 82 | + existing = _load_existing_url() |
| 83 | + if existing: |
| 84 | + choice = questionary.select( |
| 85 | + f"A Discord webhook is already configured ({_mask_url(existing)}). What now?", |
| 86 | + choices=[ |
| 87 | + "Keep current webhook", |
| 88 | + "Replace with a new webhook URL", |
| 89 | + "Remove the webhook", |
| 90 | + ], |
| 91 | + use_shortcuts=True, |
| 92 | + use_arrow_keys=True, |
| 93 | + use_jk_keys=False, |
| 94 | + ).ask() |
| 95 | + |
| 96 | + if choice is None or choice.startswith("Keep"): |
| 97 | + click.echo("Keeping existing Discord webhook.") |
| 98 | + return |
| 99 | + if choice.startswith("Remove"): |
| 100 | + unset_key(str(GAC_ENV_PATH), ENV_KEY) |
| 101 | + os.environ.pop(ENV_KEY, None) |
| 102 | + click.echo("Removed Discord webhook.") |
| 103 | + return |
| 104 | + # Otherwise fall through to prompt for a new URL. |
| 105 | + |
| 106 | + url = _prompt_for_url() |
| 107 | + if url is None: |
| 108 | + return |
| 109 | + _save_url(url) |
| 110 | + |
| 111 | + |
| 112 | +@discord.command() |
| 113 | +def remove() -> None: |
| 114 | + """Remove the configured Discord webhook URL.""" |
| 115 | + existing = _load_existing_url() |
| 116 | + if existing is None: |
| 117 | + click.echo("No Discord webhook is currently configured.") |
| 118 | + return |
| 119 | + unset_key(str(GAC_ENV_PATH), ENV_KEY) |
| 120 | + os.environ.pop(ENV_KEY, None) |
| 121 | + click.echo("Removed Discord webhook.") |
| 122 | + |
| 123 | + |
| 124 | +@discord.command() |
| 125 | +def show() -> None: |
| 126 | + """Display whether a Discord webhook is configured (URL masked).""" |
| 127 | + existing = _load_existing_url() |
| 128 | + if existing is None: |
| 129 | + click.echo("No Discord webhook configured.") |
| 130 | + click.echo("Run 'uvx gac discord setup' to configure one.") |
| 131 | + return |
| 132 | + click.echo(f"Discord webhook configured: {_mask_url(existing)}") |
| 133 | + |
| 134 | + |
| 135 | +@discord.command() |
| 136 | +def test() -> None: |
| 137 | + """Send a test notification to the configured Discord webhook.""" |
| 138 | + existing = _load_existing_url() |
| 139 | + if existing is None: |
| 140 | + click.echo("No Discord webhook configured.") |
| 141 | + click.echo("Run 'uvx gac discord setup' to configure one first.") |
| 142 | + return |
| 143 | + |
| 144 | + test_message = ( |
| 145 | + "test: 🐶 Biscuit barking from gac discord test\n\n" |
| 146 | + "If you can see this in your Discord channel, the webhook is wired up correctly!" |
| 147 | + ) |
| 148 | + if notify_commit(test_message): |
| 149 | + click.echo("✓ Test notification sent successfully.") |
| 150 | + else: |
| 151 | + click.echo("✗ Failed to send test notification. Check the URL and your network.") |
0 commit comments