From 28b6695206c2364a27b83a3a22ef47859fb467e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Mon, 6 Jan 2025 21:59:46 +0100 Subject: [PATCH] Move cli app from main to app.py --- nixprstatus/__main__.py | 90 +---------------------------------------- nixprstatus/app.py | 89 ++++++++++++++++++++++++++++++++++++++++ tests/test_cli.py | 3 ++ 3 files changed, 93 insertions(+), 89 deletions(-) create mode 100644 nixprstatus/app.py create mode 100644 tests/test_cli.py diff --git a/nixprstatus/__main__.py b/nixprstatus/__main__.py index d4f74fb..594fa26 100644 --- a/nixprstatus/__main__.py +++ b/nixprstatus/__main__.py @@ -1,92 +1,4 @@ -import typer -import json -from typing import Annotated -from nixprstatus.pr import pr_merge_status -from nixprstatus.pr import commits_since -from nixprstatus.watchlist import Watchlist -from nixprstatus.output import OutputFormat - -app = typer.Typer(rich_markup_mode=None) -watchlist_app = typer.Typer() -app.add_typer(watchlist_app, name="watchlist", help="Manage watchlist.") - -DEFAULT_HEADERS = { - "Accept": "application/vnd.github.text+json", -} - -BRANCHES = ["nixos-unstable-small", "nixos-unstable", "nixos-24.05"] - - -@app.command() -def pr( - pr: list[int], - branches: Annotated[ - list[str] | None, typer.Option(help="Check specific branch") - ] = None, - format: Annotated[ - OutputFormat, typer.Option(help="Output format") - ] = OutputFormat.CONSOLE, -) -> None: - """Get merge status of pull request.""" - if isinstance(pr, int): - status = pr_merge_status(pr, branches) - status.print(format=format) - return - - for pr_ in pr: - status = pr_merge_status(pr_, branches) - status.print(format=format) - print() - - -@app.command() -def since( - commit_sha: str, - target: str = "nixos-unstable", - waybar_format: Annotated[ - bool, typer.Option(help="Output in format expected by waybar") - ] = False, -) -> None: - """ - Return the count of commits that has happened between the two refs. - """ - count = commits_since(target, commit_sha) - - if waybar_format: - output = {"text": str(count), "tooltip": f"{target}: {count}"} - typer.echo(json.dumps(output)) - return - typer.echo(count) - - -@watchlist_app.command(name="list") -def command_list( - watchlist: str | None = None, format: OutputFormat = OutputFormat.CONSOLE -) -> None: - """List PRs in watchlist.""" - wl = Watchlist.from_file(path=watchlist) - wl.print(format=format) - - -@watchlist_app.command() -def add(pr: int, watchlist: str | None = None) -> None: - """Add PR to watchlist.""" - wl = Watchlist.from_file(path=watchlist) - info = wl.add_pr(pr) - wl.to_file(path=watchlist) - print(f"Added #{info.pr}: {info.title} to watchlist.") - - -@watchlist_app.command() -def remove(pr: int) -> None: - """Remove PR from watchlist.""" - wl = Watchlist.from_file() - if pr not in wl: - print(f"#{pr} not in watchlist.") - return - wl.remove(pr) - wl.to_file() - print(f"Removed #{pr} from watchlist.") +from nixprstatus.app import app def main() -> None: diff --git a/nixprstatus/app.py b/nixprstatus/app.py new file mode 100644 index 0000000..0ce9006 --- /dev/null +++ b/nixprstatus/app.py @@ -0,0 +1,89 @@ +import typer +import json +from typing import Annotated +from nixprstatus.pr import pr_merge_status +from nixprstatus.pr import commits_since +from nixprstatus.watchlist import Watchlist +from nixprstatus.output import OutputFormat + +app = typer.Typer(rich_markup_mode=None) +watchlist_app = typer.Typer() +app.add_typer(watchlist_app, name="watchlist", help="Manage watchlist.") + +DEFAULT_HEADERS = { + "Accept": "application/vnd.github.text+json", +} + +BRANCHES = ["nixos-unstable-small", "nixos-unstable", "nixos-24.05"] + + +@app.command() +def pr( + pr: list[int], + branches: Annotated[ + list[str] | None, typer.Option(help="Check specific branch") + ] = None, + format: Annotated[ + OutputFormat, typer.Option(help="Output format") + ] = OutputFormat.CONSOLE, +) -> None: + """Get merge status of pull request(s).""" + if isinstance(pr, int): + status = pr_merge_status(pr, branches) + status.print(format=format) + return + + for pr_ in pr: + status = pr_merge_status(pr_, branches) + status.print(format=format) + print() + + +@app.command() +def since( + commit_sha: str, + target: str = "nixos-unstable", + waybar_format: Annotated[ + bool, typer.Option(help="Output in format expected by waybar") + ] = False, +) -> None: + """ + Return the count of commits that has happened between the two refs. + """ + count = commits_since(target, commit_sha) + + if waybar_format: + output = {"text": str(count), "tooltip": f"{target}: {count}"} + typer.echo(json.dumps(output)) + return + typer.echo(count) + + +@watchlist_app.command(name="list") +def command_list( + watchlist: str | None = None, format: OutputFormat = OutputFormat.CONSOLE +) -> None: + """List PRs in watchlist.""" + wl = Watchlist.from_file(path=watchlist) + wl.print(format=format) + + +@watchlist_app.command() +def add(pr: int, watchlist: str | None = None) -> None: + """Add PR to watchlist.""" + wl = Watchlist.from_file(path=watchlist) + info = wl.add_pr(pr) + wl.to_file(path=watchlist) + print(f"Added #{info.pr}: {info.title} to watchlist.") + + +@watchlist_app.command() +def remove(pr: int) -> None: + """Remove PR from watchlist.""" + wl = Watchlist.from_file() + if pr not in wl: + print(f"#{pr} not in watchlist.") + return + wl.remove(pr) + wl.to_file() + print(f"Removed #{pr} from watchlist.") diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..72bfdf9 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,3 @@ +import unittest + +from nixprstatus.app import app