|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from rich.console import Console |
| 7 | + |
| 8 | +from ._gitty import run_gitty, get_gitty_dir |
| 9 | +from ._db import fetch_and_update_issues |
| 10 | + |
| 11 | +console = Console() |
| 12 | + |
| 13 | +def check_openai_key() -> None: |
| 14 | + """Check if OpenAI API key is set in environment variables.""" |
| 15 | + if not os.getenv("OPENAI_API_KEY"): |
| 16 | + print("Error: OPENAI_API_KEY environment variable is not set.") |
| 17 | + print("Please set your OpenAI API key using:") |
| 18 | + print(" export OPENAI_API_KEY='your-api-key'") |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | + |
| 22 | +def check_gh_cli() -> bool: |
| 23 | + """Check if GitHub CLI is installed and accessible.""" |
| 24 | + try: |
| 25 | + subprocess.run(["gh", "--version"], capture_output=True, check=True) |
| 26 | + return True |
| 27 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 28 | + print("[error]Error: GitHub CLI (gh) is not installed or not found in PATH.[/error]") |
| 29 | + print("Please install it from: https://cli.github.com") |
| 30 | + sys.exit(1) |
| 31 | + |
| 32 | + |
| 33 | +def edit_config_file(file_path: str) -> None: |
| 34 | + if not os.path.exists(file_path): |
| 35 | + with open(file_path, "w") as f: |
| 36 | + f.write("# Instructions for gitty agents\n") |
| 37 | + f.write("# Add your configuration below\n") |
| 38 | + editor = os.getenv("EDITOR", "vi") |
| 39 | + subprocess.run([editor, file_path]) |
| 40 | + |
| 41 | + |
| 42 | +def main() -> None: |
| 43 | + parser = argparse.ArgumentParser( |
| 44 | + description="Gitty: A GitHub Issue/PR Assistant.\n\n" |
| 45 | + "This tool fetches GitHub issues or pull requests and uses an AI assistant to generate concise,\n" |
| 46 | + "technical responses to help make progress on your project. You can specify a repository using --repo\n" |
| 47 | + "or let the tool auto-detect the repository based on the current directory.", |
| 48 | + epilog="Subcommands:\n issue - Process and respond to GitHub issues\n pr - Process and respond to GitHub pull requests\n local - Edit repo-specific gitty config\n global- Edit global gitty config\n\n" |
| 49 | + "Usage examples:\n gitty issue 123\n gitty pr 456\n gitty local\n gitty global", |
| 50 | + formatter_class=argparse.RawTextHelpFormatter, |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + "command", choices=["issue", "pr", "fetch", "local", "global"], nargs="?", help="Command to execute" |
| 54 | + ) |
| 55 | + parser.add_argument("number", type=int, nargs="?", help="Issue or PR number (if applicable)") |
| 56 | + |
| 57 | + if len(sys.argv) == 1: |
| 58 | + parser.print_help() |
| 59 | + sys.exit(0) |
| 60 | + |
| 61 | + args = parser.parse_args() |
| 62 | + command = args.command |
| 63 | + |
| 64 | + # Check for gh CLI installation before processing commands that need it |
| 65 | + if command in ["issue", "pr", "fetch"]: |
| 66 | + check_gh_cli() |
| 67 | + |
| 68 | + # Check for OpenAI API key before processing commands that need it |
| 69 | + if command in ["issue", "pr"]: |
| 70 | + check_openai_key() |
| 71 | + |
| 72 | + if command in ["issue", "pr"]: |
| 73 | + # Always auto-detect repository |
| 74 | + pipe = subprocess.run( |
| 75 | + [ |
| 76 | + "gh", |
| 77 | + "repo", |
| 78 | + "view", |
| 79 | + "--json", |
| 80 | + "owner,name", |
| 81 | + "-q", |
| 82 | + '.owner.login + "/" + .name', |
| 83 | + ], |
| 84 | + check=True, |
| 85 | + capture_output=True, |
| 86 | + ) |
| 87 | + owner, repo = pipe.stdout.decode().strip().split("/") |
| 88 | + number = args.number |
| 89 | + if command == "issue": |
| 90 | + asyncio.run(run_gitty(owner, repo, command, number)) |
| 91 | + else: |
| 92 | + print(f"Command '{command}' is not implemented.") |
| 93 | + sys.exit(1) |
| 94 | + elif command == "fetch": |
| 95 | + pipe = subprocess.run( |
| 96 | + [ |
| 97 | + "gh", |
| 98 | + "repo", |
| 99 | + "view", |
| 100 | + "--json", |
| 101 | + "owner,name", |
| 102 | + "-q", |
| 103 | + '.owner.login + "/" + .name', |
| 104 | + ], |
| 105 | + check=True, |
| 106 | + capture_output=True, |
| 107 | + ) |
| 108 | + owner, repo = pipe.stdout.decode().strip().split("/") |
| 109 | + gitty_dir = get_gitty_dir() |
| 110 | + db_path = os.path.join(gitty_dir, "issues.db") |
| 111 | + fetch_and_update_issues(owner, repo, db_path) |
| 112 | + elif command == "local": |
| 113 | + gitty_dir = get_gitty_dir() |
| 114 | + local_config_path = os.path.join(gitty_dir, "config") |
| 115 | + edit_config_file(local_config_path) |
| 116 | + elif command == "global": |
| 117 | + global_config_dir = os.path.expanduser("~/.gitty") |
| 118 | + os.makedirs(global_config_dir, exist_ok=True) |
| 119 | + global_config_path = os.path.join(global_config_dir, "config") |
| 120 | + edit_config_file(global_config_path) |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments