Skip to content

Commit

Permalink
refactor(constants): added constants module to maintains all globally…
Browse files Browse the repository at this point in the history
… hardcoded vars into a single class for maintainance
  • Loading branch information
psadi committed Mar 11, 2024
1 parent 12d3aee commit 3d8939d
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 94 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"python.autoComplete.extraPaths": ["__pypackages__/3.11/lib"],
"python.analysis.extraPaths": ["__pypackages__/3.11/lib"],
"python.autoComplete.extraPaths": ["__pypackages__/3.12/lib"],
"python.analysis.extraPaths": ["__pypackages__/3.12/lib"],
"python.analysis.typeCheckingMode": "off"
}
Empty file modified LICENSE
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions bb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from bb.auth import _auth
from bb.pr import _pr
from bb.repo import _repo
from bb.utils.helper import state
from bb.utils.constants import vars
from bb.utils.richprint import console


Expand Down Expand Up @@ -49,4 +49,4 @@ def callback(
Entry point for the bb CLI. Handles global flags like --verbose and --version.
"""
if verbose:
state["verbose"] = True
vars.state["verbose"] = True
1 change: 0 additions & 1 deletion bb/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from bb.utils.richprint import console

_auth: typer.Typer = typer.Typer(add_completion=False, no_args_is_help=True)
bold_red: str = "bold red"


@_auth.command()
Expand Down
43 changes: 23 additions & 20 deletions bb/pr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
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.constants import vars
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"
id_cannot_be_none: str = "id cannot be none"
not_a_git_repo: str = "Not a git repository"
skip_prompt: str = "skip confirmation prompt"


@_pr.command()
def create(
target: str = typer.Option("", help="target branch name"),
yes: bool = typer.Option(False, help=skip_prompt),
yes: bool = typer.Option(False, help=vars.skip_prompt),
diff: bool = typer.Option(False, help="show diff after raising pull request"),
rebase: bool = typer.Option(
False, help="rebase source branch with target before creation"
Expand All @@ -40,7 +37,7 @@ def create(
@error_handler
def _create(target: str, yes: bool, diff: bool, rebase: bool) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
raise ValueError(vars.not_a_git_repo)

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

Expand All @@ -52,15 +49,15 @@ def _create(target: str, yes: bool, diff: bool, rebase: bool) -> None:
@_pr.command()
def delete(
id: str = typer.Option("", help="pull request number(s) to delete"),
yes: bool = typer.Option(False, help=skip_prompt),
yes: bool = typer.Option(False, help=vars.skip_prompt),
diff: bool = typer.Option(False, help="show diff before deleting pull request"),
) -> None:
"""Delete pull requests"""

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

_id = validate_input(
id,
Expand Down Expand Up @@ -95,7 +92,7 @@ def list(
@error_handler
def _list(role: str, all: bool) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
raise ValueError(vars.not_a_git_repo)

list_pull_request(role, all)

Expand All @@ -121,9 +118,11 @@ def review(
@error_handler
def _review(id: str, action: Action) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
raise ValueError(vars.not_a_git_repo)

_id: str = validate_input(id, "Pull request id to review", id_cannot_be_none)
_id: str = validate_input(
id, "Pull request id to review", vars.id_cannot_be_none
)
action_value: str = "none" if action == Action.NONE else action.value
action: str = validate_input(
action_value,
Expand All @@ -144,15 +143,17 @@ def merge(
rebase: bool = typer.Option(
False, help="rebase source branch with target before merge"
),
yes: bool = typer.Option(False, help=skip_prompt),
yes: bool = typer.Option(False, help=vars.skip_prompt),
) -> None:
"""Merge a pull request"""

@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)
raise ValueError(vars.not_a_git_repo)
_id: str = validate_input(
id, "Pull request id to merge", vars.id_cannot_be_none
)
merge_pull_request(_id, delete_source_branch, rebase, yes)

_merge(id, delete_source_branch, rebase, yes)
Expand All @@ -167,9 +168,9 @@ def diff(
@error_handler
def _diff(id: str) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
raise ValueError(vars.not_a_git_repo)
_id: str = validate_input(
id, "Pull request number to show diff", id_cannot_be_none
id, "Pull request number to show diff", vars.id_cannot_be_none
)
show_diff(_id)

Expand All @@ -183,9 +184,11 @@ def copy(id: str = typer.Option("", help="pull request number to copy")) -> None
@error_handler
def _copy(id: str) -> None:
if not is_git_repo():
raise ValueError(not_a_git_repo)
raise ValueError(vars.not_a_git_repo)

_id: str = validate_input(id, "Pull request number to copy", id_cannot_be_none)
_id: str = validate_input(
id, "Pull request number to copy", vars.id_cannot_be_none
)
copy_pull_request(_id)

_copy(id)
Expand All @@ -201,8 +204,8 @@ def view(
@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)
raise ValueError(vars.not_a_git_repo)
_id = validate_input(id, "Pull request id to view", vars.id_cannot_be_none)
view_pull_request(_id, web)

_view(id, web)
8 changes: 5 additions & 3 deletions bb/pr/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ def delete_pull_request(_id: list, yes: bool, diff: bool) -> None:
("Title", pull_request_info[1]["title"]),
(
"Description",
pull_request_info[1]["description"]
if "description" in pull_request_info[1].keys()
else "-",
(
pull_request_info[1]["description"]
if "description" in pull_request_info[1].keys()
else "-"
),
),
],
True,
Expand Down
8 changes: 5 additions & 3 deletions bb/pr/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ def outcome(_pr: dict) -> tuple:
show the current status of the pr clean/conflicted
"""
return (
"[bold green]CLEAN"
if "mergeResult" not in _pr["properties"]
else f"{state_check(_pr['properties']['mergeResult']['outcome'])}",
(
"[bold green]CLEAN"
if "mergeResult" not in _pr["properties"]
else f"{state_check(_pr['properties']['mergeResult']['outcome'])}"
),
)


Expand Down
13 changes: 6 additions & 7 deletions bb/pr/merge.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-

"""
bb.pr.merge - merges a pull request given a id.
validates automerge conditions & prompts for optional
rebase and source branch deletion
bb.pr.merge - merges a pull request given a id.
validates automerge conditions & prompts for optional
rebase and source branch deletion
"""

from rich import print_json
from typer import confirm

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

BOLD_RED = "bold red"
from bb.utils.constants import vars


def pr_source_branch_delete_check(
Expand Down Expand Up @@ -101,7 +100,7 @@ def show_merge_stats(pr_merge_response, from_branch, target_branch) -> None:
]

richprint.str_print(
f"> '{from_branch}' with merge to '{','.join(automerge_branches).replace(',',' and ')}'",
f"> '{from_branch}' with merge to '{','.join(automerge_branches).replace(',', ' and ')}'",
"bold cyan",
)
else:
Expand Down Expand Up @@ -151,7 +150,7 @@ def merge_pr(
richprint.console.print(
pr_merge_response[1]["errors"][0]["message"],
highlight=True,
style=BOLD_RED,
style=vars.bold_red,
)
return pr_merge_response[0]

Expand Down
8 changes: 5 additions & 3 deletions bb/pr/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ def view_pull_request(_id: str, web: bool) -> None:
("Title", url[1]["title"]),
(
"Description",
url[1]["description"]
if "description" in url[1].keys()
else "-",
(
url[1]["description"]
if "description" in url[1].keys()
else "-"
),
),
("State", url[1]["state"]),
],
Expand Down
Empty file modified bb/repo/archive.py
100644 → 100755
Empty file.
Empty file modified bb/repo/create.py
100644 → 100755
Empty file.
Empty file modified bb/repo/delete.py
100644 → 100755
Empty file.
39 changes: 13 additions & 26 deletions bb/utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ def get_repo_info(self, project: str) -> str:
def default_reviewers(
self, project: str, repo_id: str, from_branch: str, target: str
) -> str:
reviewer_query = f"avatarSize=32&sourceRepoId={repo_id}&sourceRefId=refs%2Fheads%2F{from_branch.replace(
'/', '%2F')}&targetRepoId={repo_id}&targetRefId=refs%2Fheads%2F{target.replace('/', '%2F')}"
reviewer_query = f"avatarSize=32&sourceRepoId={repo_id}&sourceRefId=refs%2Fheads%2F{from_branch.replace('/', '%2F')}&targetRepoId={repo_id}&targetRefId=refs%2Fheads%2F{target.replace('/', '%2F')}"
return self._api_url(
f"/rest/default-reviewers/latest/projects/{
project}/repos/repository/reviewers?{reviewer_query}"
f"/rest/default-reviewers/latest/projects/{project}/repos/repository/reviewers?{reviewer_query}"
)

def pull_request_body(
Expand Down Expand Up @@ -79,14 +77,12 @@ def pull_request_difference(
self, project: str, repository: str, pr_number: str
) -> str:
return self._api_url(
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests/{
pr_number}/changes?start=0&limit=1000&changeScope=unreviewed"
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests/{pr_number}/changes?start=0&limit=1000&changeScope=unreviewed"
)

def pull_request_info(self, project: str, repository: str, _id: str) -> str:
return self._api_url(
f"/rest/api/latest/projects/{project}/repos/{
repository}/pull-requests/{_id}"
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests/{_id}"
)

def pull_request_viewer(self, role: str) -> str:
Expand All @@ -96,8 +92,7 @@ def pull_request_viewer(self, role: str) -> str:

def current_pull_request(self, project: str, repository: str) -> str:
return self._api_url(
f"/rest/api/latest/projects/{project}/repos/{
repository}/pull-requests"
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests"
)

def whoami(self) -> str:
Expand All @@ -107,34 +102,29 @@ def action_pull_request(
self, project: str, repository: str, target: int, user_id: str
) -> str:
return self._api_url(
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests/{
target}/participants/{user_id}?avatarSize=32"
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests/{target}/participants/{user_id}?avatarSize=32"
)

def pr_source_branch_delete_check(
self, project: str, repository: str, _id: str, delete_source_branch: str
) -> str:
return self._api_url(
f"/rest/pull-request-cleanup/latest/projects/{project}/repos/{repository}/pull-requests/{
_id}?deleteSourceRef={delete_source_branch}&retargetDependents={delete_source_branch}"
f"/rest/pull-request-cleanup/latest/projects/{project}/repos/{repository}/pull-requests/{_id}?deleteSourceRef={delete_source_branch}&retargetDependents={delete_source_branch}"
)

def validate_merge(self, project: str, repository: str, _id: str) -> str:
return self._api_url(
f"/rest/api/latest/projects/{project}/repos/{
repository}/pull-requests/{_id}/merge"
f"/rest/api/latest/projects/{project}/repos/{repository}/pull-requests/{_id}/merge"
)

def merge_config(self, project: str, repository: str) -> str:
return self._api_url(
f"/rest/api/latest/projects/{project}/repos/{
repository}/settings/pull-requests"
f"/rest/api/latest/projects/{project}/repos/{repository}/settings/pull-requests"
)

def get_merge_info(self, project: str, repository: str, target_branch: str) -> str:
return self._api_url(
f"/rest/branch-utils/latest/projects/{project}/repos/{
repository}/automerge/path/refs/heads/{target_branch}"
f"/rest/branch-utils/latest/projects/{project}/repos/{repository}/automerge/path/refs/heads/{target_branch}"
)

def pr_merge_body(
Expand All @@ -154,8 +144,7 @@ def pr_merge_body(

def pr_cleanup(self, project: str, repository: str, _id: str) -> str:
return self._api_url(
f"/rest/pull-request-cleanup/latest/projects/{
project}/repos/{repository}/pull-requests/{_id}"
f"/rest/pull-request-cleanup/latest/projects/{project}/repos/{repository}/pull-requests/{_id}"
)

def pr_cleanup_body(self, delete_retarget: bool) -> str:
Expand All @@ -170,17 +159,15 @@ def pr_rebase(self, project: str, repository: str, _id: str, version: int) -> li
return [
json.dumps({"version": version}),
self._api_url(
f"/rest/git/latest/projects/{project}/repos/{
repository}/pull-requests/{_id}/rebase"
f"/rest/git/latest/projects/{project}/repos/{repository}/pull-requests/{_id}/rebase"
),
]

def delete_branch(self, project: str, repository: str, source_branch: str) -> list:
return [
json.dumps({"name": f"{source_branch}"}),
self._api_url(
f"/rest/branch-utils/latest/projects/{
project}/repos/{repository}/branches"
f"/rest/branch-utils/latest/projects/{project}/repos/{repository}/branches"
),
]

Expand Down
Loading

0 comments on commit 3d8939d

Please sign in to comment.