Skip to content

Commit

Permalink
Merge pull request #30 from psadi/feature/maintainance_refactor
Browse files Browse the repository at this point in the history
chore: maintainance refactor
  • Loading branch information
psadi authored Feb 18, 2024
2 parents ec4cecb + 8f36291 commit 27c1c1a
Show file tree
Hide file tree
Showing 30 changed files with 552 additions and 672 deletions.
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"python.autoComplete.extraPaths": ["__pypackages__/3.11/lib"],
"python.analysis.extraPaths": [
"__pypackages__/3.11/lib"
],
"python.analysis.typeCheckingMode": "basic"
"python.analysis.extraPaths": ["__pypackages__/3.11/lib"],
"python.analysis.typeCheckingMode": "off"
}
11 changes: 5 additions & 6 deletions bb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@

import typer

from bb import __version__
from bb.__version__ import __version__ as version
from bb.auth import _auth
from bb.pr import _pr
from bb.repo import _repo
from bb.utils.helper import state
from bb.utils.richprint import console
from bb.utils.validate import state


def version_callback(value: bool) -> None:
"""
returns the docstring of the current module (`__doc__`)
and exits the program.
Prints the version of bb and exits the program.
"""
if value:
console.print(f"bb version: {__version__.__version__}")
console.print(f"bb version: {version}")
raise typer.Exit(code=0)


Expand Down Expand Up @@ -47,7 +46,7 @@ def callback(
version: bool = typer.Option(None, "--version", callback=version_callback),
):
"""
Work seamlessly with Bitbucket from the command line.
Entry point for the bb CLI. Handles global flags like --verbose and --version.
"""
if verbose:
state["verbose"] = True
2 changes: 1 addition & 1 deletion bb/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
Sets the packge version, picked dynamically by
the utility and pyproject
"""
__version__ = "0.5.6"
__version__ = "0.5.7"
46 changes: 20 additions & 26 deletions bb/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import typer

from bb.utils.ini import BB_CONFIG_FILE, _setup, is_config_present, parse
from bb.utils.richprint import console, traceback_to_console
from bb.utils.validate import error_tip, state, validate_config
from bb.utils.helper import error_handler, validate_config
from bb.utils.ini import BB_CONFIG_FILE, auth_setup, is_config_present, parse
from bb.utils.richprint import console

_auth: typer.Typer = typer.Typer(add_completion=False, no_args_is_help=True)
bold_red: str = "bold red"
Expand All @@ -17,13 +17,15 @@
@_auth.command()
def setup() -> None:
"""Configure bbcli to work with bitbucket"""
try:

@error_handler
def _setup() -> None:
if is_config_present():
console.print(
"Configuration file found, Run 'bb auth status' for more information"
)
else:
_setup(
auth_setup(
typer.prompt(
"> bitbucket_host",
),
Expand All @@ -34,33 +36,30 @@ def setup() -> None:
f"Configuration written at '{BB_CONFIG_FILE}',"
+ "Please re-run 'bb auth test' to validate"
)
except Exception as err:
console.print(f"ERROR: {err}", style=bold_red)
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_setup()


@_auth.command()
def test() -> None:
"""Test configuration & connection"""
try:

@error_handler
def _test() -> None:
validate_config()
except Exception as err:
console.print(f"ERROR: {err}", style=bold_red)
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_test()


@_auth.command()
def status(token: bool = typer.Option(False, help="Display auth token")) -> None:
"""View authentication config status"""
try:

@error_handler
def _status(token: bool) -> None:
if not is_config_present():
raise ValueError("Configuration missing, run 'bb auth setup'")

hcm: str = "[bold green]:heavy_check_mark:[/bold green]"
console.print(
f"{hcm} Configuration found at [bold cyan]{BB_CONFIG_FILE}[/bold cyan]"
Expand All @@ -70,10 +69,5 @@ def status(token: bool = typer.Option(False, help="Display auth token")) -> None
f"{hcm} Will connect to [bold]{bitbucket_host}[/bold] as [bold]{username}[/bold]"
)
console.print(f"{hcm} Token: {_token if token else '*' * len(_token)}")
except Exception as err:
console.print(f"ERROR: {err}", style=bold_red)
if state["verbose"]:
traceback_to_console()
else:
error_tip()
typer.Exit(code=1)

_status(token)
98 changes: 42 additions & 56 deletions bb/pr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from bb.pr.review import review_pull_request
from bb.pr.view import view_pull_request
from bb.utils.cmnd import is_git_repo
from bb.utils.richprint import console, traceback_to_console
from bb.utils.validate import error_tip, state, validate_input
from bb.utils.helper import error_handler, validate_input

_pr: typer.Typer = typer.Typer(add_completion=False, no_args_is_help=True)
bold_red: str = "bold red"
Expand All @@ -37,18 +36,17 @@ def create(
),
) -> None:
"""Create a pull request"""
try:

@error_handler
def _create(target: str, yes: bool, diff: bool, rebase: bool) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)

target = validate_input(target, "Target branch", "Target branch cannot be none")

create_pull_request(target, yes, diff, rebase)
except Exception as err:
console.print(f"ERROR: {err}", style=bold_red)
error_tip()
if state["verbose"]:
traceback_to_console()

_create(target, yes, diff, rebase)


@_pr.command()
Expand All @@ -58,7 +56,9 @@ def delete(
diff: bool = typer.Option(False, help="show diff before deleting pull request"),
) -> None:
"""Delete pull requests"""
try:

@error_handler
def _delete(id: str, yes: bool, diff: bool) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)

Expand All @@ -68,11 +68,8 @@ def delete(
"Id's cannot be empty",
).split(",")
delete_pull_request(_id, yes, diff)
except Exception as err:
console.print(f"ERROR: {err}", style=bold_red)
error_tip()
if state["verbose"]:
traceback_to_console()

_delete(id, yes, diff)


class Role(str, Enum):
Expand All @@ -94,17 +91,15 @@ def list(
),
) -> None:
"""List pull requests in a repository"""
try:

@error_handler
def _list(role: str, all: bool) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)

list_pull_request(role, all)
except Exception as err:
console.print(f"ERROR: {err}", style=f"{bold_red}")
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_list(role, all)


class Action(str, Enum):
Expand All @@ -122,9 +117,12 @@ def review(
action: Action = Action.NONE,
) -> None:
"""Add a review to a pull request"""
try:

@error_handler
def _review(id: str, action: Action) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)

_id: str = validate_input(id, "Pull request id to review", id_cannot_be_none)
action_value: str = "none" if action == Action.NONE else action.value
action: str = validate_input(
Expand All @@ -133,12 +131,8 @@ def review(
"action cannot be none",
)
review_pull_request(_id, action)
except Exception as err:
console.print(f"ERROR: {err}", style=f"{bold_red}")
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_review(id, action)


@_pr.command()
Expand All @@ -153,54 +147,48 @@ def merge(
yes: bool = typer.Option(False, help=skip_prompt),
) -> None:
"""Merge a pull request"""
try:

@error_handler
def _merge(id: str, delete_source_branch: bool, rebase: bool, yes: bool) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
_id: str = validate_input(id, "Pull request id to merge", id_cannot_be_none)
merge_pull_request(_id, delete_source_branch, rebase, yes)
except Exception as err:
console.print(f"ERROR: {err}", style=f"{bold_red}")
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_merge(id, delete_source_branch, rebase, yes)


@_pr.command()
def diff(
id: str = typer.Option("", help="pull request number to show diff"),
) -> None:
"""View changes in a pull request"""
try:

@error_handler
def _diff(id: str) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
_id: str = validate_input(
id, "Pull request number to show diff", id_cannot_be_none
)
show_diff(_id)
except Exception as err:
console.print(f"ERROR: {err}", style=f"{bold_red}")
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_diff(id)


@_pr.command()
def copy(id: str = typer.Option("", help="pull request number to copy")) -> None:
"""Copy pull request url to clipboard"""
try:

@error_handler
def _copy(id: str) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)

_id: str = validate_input(id, "Pull request number to copy", id_cannot_be_none)
copy_pull_request(_id)
except Exception as err:
console.print(f"ERROR: {err}", style=f"{bold_red}")
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_copy(id)


@_pr.command()
Expand All @@ -209,14 +197,12 @@ def view(
web: Optional[bool] = typer.Option(False, help="view pull request in browser"),
) -> None:
"""View a pull request"""
try:

@error_handler
def _view(id: str, web: Optional[bool]) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
_id = validate_input(id, "Pull request id to view", id_cannot_be_none)
view_pull_request(_id, web)
except Exception as err:
console.print(f"ERROR: {err}", style=f"{bold_red}")
if state["verbose"]:
traceback_to_console()
else:
error_tip()

_view(id, web)
5 changes: 3 additions & 2 deletions bb/pr/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
clipboard
"""

from bb.utils import api, cmnd, ini, request, richprint
from bb.utils import cmnd, ini, request, richprint
from bb.utils.api import bitbucket_api


def copy_pull_request(_id: str) -> None:
Expand All @@ -21,7 +22,7 @@ def copy_pull_request(_id: str) -> None:
username, token, bitbucket_host = ini.parse()
project, repository = cmnd.base_repo()
url: str = request.get(
api.pull_request_info(bitbucket_host, project, repository, _id),
bitbucket_api.pull_request_info(bitbucket_host, project, repository, _id),
username,
token,
)[1]["links"]["self"][0]["href"]
Expand Down
Loading

0 comments on commit 27c1c1a

Please sign in to comment.