Skip to content

Commit

Permalink
traceback fix, switched back to requests and minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
psadi committed Sep 23, 2022
1 parent 3b10e25 commit fb5c7c4
Show file tree
Hide file tree
Showing 17 changed files with 659 additions and 985 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Git
* Python3 (3.7 or Higher)
* Pip3 (latest recommended)
* Write access token from bitbucket
* A write access token from bitbucket

---

Expand All @@ -17,8 +17,7 @@
Manual

```text
git clone https://github.com/psadi/bbcli.git
cd bb
git clone https://github.com/psadi/bbcli.git && cd bb
python3 -m build .
pip3 install --user dist/bb-<version>.tar.gz
```
Expand Down Expand Up @@ -51,7 +50,6 @@ bb test
* if all went well, you should get a response like this

```text
➜ bb test
> bb test
⠏ Validating connection with 'https://bitbucket.<company>.com'... OK
```
Expand Down Expand Up @@ -127,7 +125,7 @@ bb test

---

### 💡💡 PRO TIP 💡💡
### 💡 TIP

* Use [Windows Terminal](https://github.com/Microsoft/Terminal) for better visual rendering
* Use Nerd font for better font/icon support, I personally use [DroidSansMono Nerd Font](https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/DroidSansMono.zip)
Expand All @@ -137,13 +135,12 @@ bb test

### CREDITS

[bbcli](https://github.com/psadi/bbcli) wouldn't be possible if not for the open-source tools made avaiable.
[bbcli](https://github.com/psadi/bbcli) wouldn't be possible if not for the awesome open-source tools made avaiable.

A huge thanks to,

* [tiangolo/typer](https://github.com/tiangolo/typer)
* [Textualize/rich](https://github.com/Textualize/rich)
* [encode/httpx](https://github.com/encode/httpx)


---
Expand Down
6 changes: 3 additions & 3 deletions bb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
bb: A command line utility to manage pull requests in bitbucket
"""
__version__ = '0.4.1'
__author__ = 'P S, Adithya (psadi) ([email protected])'
__version__ = "0.4.2"
__author__ = "P S, Adithya (psadi) ([email protected])"
__license__ = "MIT"
__doc__ = f"VERSION: {__version__} \nAUTHOR: {__author__} \nLICENSE: {__license__}"
172 changes: 72 additions & 100 deletions bb/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
bbcli: a comman line utility that can manage pull requests in bitbucket.
"""
Expand All @@ -16,212 +16,184 @@
from bb.pr.merge import merge_pull_request
from bb.pr.diff import show_diff
from bb.utils.cmnd import is_git_repo
from bb.utils.richprint import traceback_to_console, console
from bb.utils.richprint import console, traceback_to_console

# Creating a new Typer app.
app = typer.Typer()
# A global variable that is used to store the state of the application.
state = {"verbose": False}


def version_callback(value: bool):
"""
- It takes a boolean value as input.
- If the value is `True`, it prints the docstring of the current module (`__doc__`) and exits the
program.
- If the value is `False`, it does nothing.
Let's see it in action:
:param value: The value that was passed to the parameter
:type value: bool
"""
if value:
console.print(__doc__)
raise typer.Exit(code=0)


def error_tip():
console.print(
f"💻 Try running 'bb --verbose [OPTIONS] COMMAND [ARGS]' to debug",
style="dim white",
)


@app.callback()
def callback(
verbose: bool = False,
version: Optional[bool] = typer.Option(None, "--version", callback=version_callback)
version: Optional[bool] = typer.Option(
None, "--version", callback=version_callback
),
):
"""
run: "bb --help" for more information
"""
if verbose: state["verbose"] = True
if verbose:
state["verbose"] = True


@app.command()
def create(
target: str = typer.Option("", help="target branch name"),
yes: bool = typer.Option(False, help="skip confirmation prompt"),
diff: bool = typer.Option(False, help="show diff after raising pull request")
yes: bool = typer.Option(False, help="skip confirmation prompt"),
diff: bool = typer.Option(False, help="show diff after raising pull request"),
):
"""- create new pull request"""
"""
It creates a pull request
:param target: str = typer.Option("", help="target branch name")
:type target: str
:param yes: bool = typer.Option(False, help="skip confirmation prompt")
:type yes: bool
:param diff: bool = typer.Option(False, help="show diff after raising pull request")
:type diff: bool
"""
try:
if is_git_repo() is not True:
console.print('Not a git repository', style='red')
console.print("Not a git repository", style="red")
raise typer.Exit(code=1)

if not target:
target = typer.prompt("Target Branch")

create_pull_request(target, yes, diff)
except Exception:
if state['verbose']: traceback_to_console(Exception)
error_tip()
if state["verbose"]:
traceback_to_console(Exception)


@app.command()
def delete(
id: Optional[List[int]] = typer.Option(None, help="pull request number(s) to delete"),
yes: bool = typer.Option(False, help="skip confirmation prompt"),
diff: bool = typer.Option(False, help="show diff before deleting pull request")
id: Optional[List[int]] = typer.Option(
None, help="pull request number(s) to delete"
),
yes: bool = typer.Option(False, help="skip confirmation prompt"),
diff: bool = typer.Option(False, help="show diff before deleting pull request"),
):
"""- delete pull request's by id's"""
"""
It deletes a pull request by id.
:param id: Optional[List[int]] = typer.Option(None, help="pull request number(s) to delete")
:type id: Optional[List[int]]
:param yes: bool = typer.Option(False, help="skip confirmation prompt")
:type yes: bool
:param diff: bool = typer.Option(False, help="show diff before deleting pull request")
:type diff: bool
"""
try:
if is_git_repo() is not True:
console.print('Not a git repository', style='red')
console.print("Not a git repository", style="red")
raise typer.Exit(code=1)
if not id:
id = typer.prompt("Pull request number(s)").split(',')
id = typer.prompt("Pull request number(s)").split(",")
delete_pull_request(id, yes, diff)
except Exception:
if state['verbose']: traceback_to_console(Exception)
error_tip()
if state["verbose"]:
traceback_to_console(Exception)


# `Role` is a subclass of `str` that has a fixed set of values
class Role(str, Enum):
author = "author"
reviewer = "reviewer"
current = "current"


@app.command()
def show(
role: Role = Role.current.value,
all: bool = typer.Option(False, help="show all pull request(s) based on selected role")
all: bool = typer.Option(
False, help="show all pull request(s) based on selected role"
),
):
"""- show pull request's authored & reviewing"""
"""
It shows pull requests authored & reviewing
:param role: Role = Role.current.value,
:type role: Role
:param all: bool = typer.Option(False, help="show all pull request(s) based on selected role")
:type all: bool
"""
try:
if is_git_repo() is not True:
console.print('Not a git repository', style='red')
console.print("Not a git repository", style="red")
raise typer.Exit(code=1)

show_pull_request(role.value, all)
except Exception:
if state['verbose']: traceback_to_console(Exception)
error_tip()
if state["verbose"]:
traceback_to_console(Exception)


@app.command()
def test():
"""- test .alt config (or) prompt for manual config"""
"""
> The function `test()` is used to validate the setup, if any issues are found manual setup will be
prompted
"""
try:
validate()
except (Exception, KeyboardInterrupt):
if state['verbose']: traceback_to_console(Exception)
except Exception:
error_tip()
if state["verbose"]:
traceback_to_console(Exception)


# `Action` is a subclass of `str` that has a fixed set of values
class Action(str, Enum):
"""review enum choices"""

approve = "approve"
unapprove = "unapprove"
needs_work = "needs_work"
none = "none"


@app.command()
def review(
id: int = typer.Option("", help="pull request number to review"),
action: Action = Action.none.value
action: Action = Action.none.value,
):
"""- review Pull Request by ID"""
"""
> review pull request by id
The function has two parameters:
- `id`: pull request number to review
- `action`: action to perform on the pull request
The `action` parameter is an enum of type `Action` which is defined in the `typer_cli.py` file
:param id: int = typer.Option("", help="pull request number to review")
:type id: int
:param action: Action = Action.none.value
:type action: Action
"""
try:
if action.value == 'none':
console.print('Action cannot be none', style='red')
raise(typer.Exit(code=1))
if action.value == "none":
console.print("Action cannot be none", style="red")
raise (typer.Exit(code=1))

review_pull_request(id, action.value)
except Exception:
if state['verbose']: traceback_to_console(Exception)
error_tip()
if state["verbose"]:
traceback_to_console(Exception)


@app.command()
def merge(
id: int = typer.Option("", help="pull request number to merge"),
delete_source_branch: bool = typer.Option(False, help="deletes source branch after merge"),
rebase: bool = typer.Option(False, help="rebase source branch with target before merge"),
yes: bool = typer.Option(False, help="skip confirmation prompt"),
delete_source_branch: bool = typer.Option(
False, help="deletes source branch after merge"
),
rebase: bool = typer.Option(
False, help="rebase source branch with target before merge"
),
yes: bool = typer.Option(False, help="skip confirmation prompt"),
):
"""- merge pull request by id"""
"""
It merges a pull request by id
:param id: int = typer.Option("", help="pull request number to merge")
:type id: int
:param delete_source_branch: bool = typer.Option(False, help="deletes source branch after merge")
:type delete_source_branch: bool
:param rebase: bool = typer.Option(False, help="rebase source branch with target before merge")
:type rebase: bool
:param yes: bool = typer.Option(False, help="skip confirmation prompt")
:type yes: bool
"""
try:
merge_pull_request(id, delete_source_branch, rebase, yes)
except Exception:
if state['verbose']: traceback_to_console(Exception)
error_tip()
if state["verbose"]:
traceback_to_console(Exception)


@app.command()
def diff(
id: int = typer.Option("", help="pull request number to show diff"),
):
"""- view diff in pull request (file only)"""
"""
It shows the diff of the pull request
:param id: int = typer.Option("", help="pull request number to show diff"),
:type id: int
"""
try:
show_diff(id)
except Exception:
if state['verbose']: traceback_to_console(Exception)
error_tip()
if state["verbose"]:
traceback_to_console(Exception)
36 changes: 15 additions & 21 deletions bb/pr/configtest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

# Importing the `echo` and `Exit` functions from the `typer` module, and the `iniparser`,
# `api`, `request`, and `richprint` modules from the `bb.utils` package.
from typer import Exit
from bb.utils import (
iniparser,
api,
request,
richprint
)
from bb.utils import iniparser, api, request, richprint


def validate():
"""
It prints a message, then calls the `api.test` function, which returns a URL. It then calls the
`request.get_response` function, which returns a tuple of the HTTP response code and the response
body. If the response code is not 200, it prints an error message and exits with a non-zero exit
code
alls the `api.test` function, If the response code is not 200,
prints exception and return non-zero exit code
"""
username, token, bitbucket_host = iniparser.parse()
message = f"Validating connection with '{bitbucket_host}'... "
with richprint.live_progress((message)) as live:
reponse = request.get_response(api.test(bitbucket_host), username, token)
if reponse[0] == 200:
live.update(richprint.console.print('OK', style='bold green'))
else:
live.update(richprint.console.print(f"ERROR", style='bold red'))
raise Exit(code=1)
try:
username, token, bitbucket_host = iniparser.parse()
message = f"Validating connection with '{bitbucket_host}'... "
with richprint.live_progress((message)) as live:
response = request.get(api.test(bitbucket_host), username, token)
if response[0] == 200:
live.update(richprint.console.print("OK", style="bold green"))
except Exception as err:
richprint.console.print("ERROR", style="bold red")
raise (err)
Loading

0 comments on commit fb5c7c4

Please sign in to comment.