Skip to content

Commit

Permalink
Merge pull request #29 from psadi/feature/repos
Browse files Browse the repository at this point in the history
feature/repos
  • Loading branch information
psadi authored Jan 9, 2024
2 parents 073c238 + ddb6387 commit 3e53d43
Show file tree
Hide file tree
Showing 24 changed files with 434 additions and 596 deletions.
55 changes: 18 additions & 37 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-docstring-first
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: fix-encoding-pragma
- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-docstring-first
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: fix-encoding-pragma
- id: requirements-txt-fixer

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: [--profile, black, --filter-files]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.264
hooks:
- id: ruff
args: [bb, --fix, --exit-non-zero-on-fix]


- repo: https://github.com/commitizen-tools/commitizen
rev: 3.2.1
hooks:
- id: commitizen
- id: commitizen-branch
stages:
- post-commit
- push
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.11
hooks:
- id: ruff
args: [bb, --fix, --exit-non-zero-on-fix]
- id: ruff-format
args: [bb]
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.5"
__version__ = "0.5.6"
6 changes: 4 additions & 2 deletions bb/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ def setup() -> None:
)
else:
_setup(
typer.prompt("> bitbucket_host"),
typer.prompt(
"> bitbucket_host",
),
typer.prompt("> username"),
typer.prompt("> token"),
typer.prompt("> token", hide_input=True),
)
typer.echo(
f"Configuration written at '{BB_CONFIG_FILE}',"
Expand Down
12 changes: 9 additions & 3 deletions bb/pr/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
remote and local repository
"""

from typer import prompt
from typing import Optional

from typer import confirm

from bb.pr.diff import show_diff
from bb.utils import api, cmnd, ini, request, richprint
Expand Down Expand Up @@ -69,6 +71,7 @@ def create_pull_request(target: str, yes: bool, diff: bool, rebase: bool) -> Non
It creates a pull request.
"""

_id: Optional[str] = None
username, token, bitbucket_host = ini.parse()
from_branch = cmnd.from_branch()
if target == from_branch:
Expand All @@ -91,7 +94,7 @@ def create_pull_request(target: str, yes: bool, diff: bool, rebase: bool) -> Non
title_and_description,
)

if yes or prompt("Proceed [y/n]").lower().strip() == "y":
if yes or confirm("Proceed"):
with richprint.live_progress("Creating Pull Request ..."):
url = api.pull_request_create(bitbucket_host, project, repository)
body = api.pull_request_body(
Expand Down Expand Up @@ -139,5 +142,8 @@ def create_pull_request(target: str, yes: bool, diff: bool, rebase: bool) -> Non
"dim white",
)

if diff:
if _id and (
diff
or confirm(f"Review diff from '{from_branch}' -> to '{target}' in PR #{_id}?")
):
show_diff(_id)
8 changes: 5 additions & 3 deletions bb/pr/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import json

from typer import prompt
from typer import confirm

from bb.pr.diff import show_diff
from bb.utils import api, cmnd, ini, request, richprint
Expand Down Expand Up @@ -43,10 +43,12 @@ def delete_pull_request(_id: list, yes: bool, diff: bool) -> None:
)
richprint.console.print(table)

if diff:
if diff or confirm(
f"Review diff between '{pull_request_info[1]['fromRef']['displayId']}' & '{pull_request_info[1]['toRef']['displayId']}' in PR #{_no}"
):
show_diff(_no)

if yes or prompt("Proceed [y/n]").lower().strip() == "y":
if yes or confirm("Proceed"):
with richprint.live_progress("Deleting Pull Request ..."):
body = json.dumps({"version": int(pull_request_info[1]["version"])})
pull_request = request.delete(url, username, token, body)
Expand Down
7 changes: 2 additions & 5 deletions bb/pr/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ def show_diff(_id: str) -> None:
)[1]

header = [
("FROM HASH", "bold red"),
("TO HASH", "bold green"),
("HASH", "bold white"),
("FILE", "bold white"),
("TYPE", "bold yellow"),
]

value_args = [
(
f"{response['fromHash'][:11]}",
f"{response['toHash'][:11]}",
f"[bold red]{response['fromHash'][:11]}[/bold red] - [bold green]{response['toHash'][:11]}[/bold green]",
i["path"]["toString"],
f"{i['type']}",
)
Expand Down
26 changes: 6 additions & 20 deletions bb/pr/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from rich import print_json
from typer import prompt
from typer import confirm

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

Expand Down Expand Up @@ -196,27 +196,13 @@ def merge_pull_request(

show_merge_stats(pr_merge_response, from_branch, target_branch)

rebase_condition = (
rebase
or prompt(
f"? Do you want rebase '{from_branch}' branch from '{target_branch}' [y/n]"
).lower()
== "y"
rebase_condition = rebase or confirm(
f"? Do you want rebase '{from_branch}' branch from '{target_branch}'"
)

if (
yes
or prompt(
f"? Proceed with {'rebase and ' if rebase_condition else ''}merge [y/n]"
).lower()
== "y"
):
delete_condition = (
delete_source_branch
or prompt(
f"? Do you want to delete source '{from_branch}' branch [y/n]"
).lower()
== "y"
if yes or confirm("? Proceed with merge"):
delete_condition = delete_source_branch or confirm(
f"? Do you want to delete source '{from_branch}' branch"
)

with richprint.live_progress(
Expand Down
91 changes: 88 additions & 3 deletions bb/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
bb: repo - repository management
"""

import typer
from typer import Argument, Option, Typer

from bb.repo.archive import archive_repository
from bb.repo.create import create_repository
from bb.repo.delete import delete_repository
from bb.utils.cmnd import clone_repo
from bb.utils.ini import parse
from bb.utils.richprint import console, traceback_to_console
from bb.utils.validate import error_tip, state, validate_input

_repo = typer.Typer(add_completion=True, no_args_is_help=True)
_repo = Typer(add_completion=True, no_args_is_help=True)


@_repo.command()
def clone(
name: str = typer.Argument(..., help="repository name, Format: project/repository")
name: str = Argument(..., help="repository name, Format: project/repository"),
) -> None:
"""Clone a BitBucket repository locally"""
try:
Expand All @@ -31,3 +34,85 @@ def clone(
traceback_to_console()
else:
error_tip()


@_repo.command()
def delete(
project: str = Option("", help="project name of the repository"),
repo: str = Option("", help="repository name to delete"),
) -> None:
"Delete a Bitbucket repository"
try:
project = validate_input(project, "Project name", "project can't be none")
repo = validate_input(repo, "Repository Name", "repository can't be none")

delete_repository(project, repo)

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


@_repo.command()
def archive(
project: str = Option("", help="project name of the repository"),
repo: str = Option("", help="repository name to archive"),
) -> None:
"Archive a Bitbucket repository"
try:
project = validate_input(project, "Project name", "project can't be none")
repo = validate_input(repo, "Repository Name", "repository can't be none")

archive_repository(project, repo, True)

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


@_repo.command()
def unarchive(
project: str = Option("", help="project name of the repository"),
repo: str = Option("", help="repository name to unarchive"),
) -> None:
"Unarchive a Bitbucket repository"
try:
project = validate_input(project, "Project name", "project can't be none")
repo = validate_input(repo, "Repository Name", "repository can't be none")

archive_repository(project, repo, False)

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


@_repo.command()
def create(
project: str = Option("", help="project name for the repository"),
repo: str = Option("", help="repository name to create"),
forkable: bool = Option(False, help="Make repository forkable"),
default_branch: str = Option("master", help="Set default branch "),
) -> None:
"""Create a Bitbucket repository"""
try:
project = validate_input(project, "Project name", "project can't be none")
repo = validate_input(repo, "Repository Name", "repository can't be none")

create_repository(project, repo, forkable, default_branch)

except Exception as err:
console.print(f"ERROR: {err}", style="bold red")
if state["verbose"]:
traceback_to_console()
else:
error_tip()
42 changes: 42 additions & 0 deletions bb/repo/archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

"""
bb.repo.delete - deletes a bitbucket repository
"""
import json

from typer import Exit, confirm

from bb.utils.api import delete_repo
from bb.utils.ini import parse
from bb.utils.request import put
from bb.utils.richprint import console, live_progress


def archive_repository(project: str, repo: str, archive: bool) -> None:
if not confirm(
f"Proceed to {'archive' if archive else 'unarchive'} '{project}/{repo}'?"
):
raise Exit(code=1)

username, token, bitbucket_host = parse()
with live_progress(
f"{'Archiving' if archive else 'Unarchiving' } Repository '{project}/{repo}' ... "
) as live:
request = put(
delete_repo(bitbucket_host, project, repo),
username,
token,
json.dumps({"archived": archive}), # type: ignore
)

if request[0] == 200:
live.update(console.print("DONE", style="bold green"))

if request[0] == 409:
live.update(console.print("CONFLICT", style="bold yellow"))
console.print(
f"Message: {request[1]['errors'][0]['message']}",
highlight=True,
style="bold yellow",
)
41 changes: 41 additions & 0 deletions bb/repo/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-

import json

from bb.utils.api import create_repo
from bb.utils.ini import parse
from bb.utils.request import post
from bb.utils.richprint import console, live_progress


def create_repository(
project: str, repo: str, forkable: bool, default_branch: str
) -> None:
username, token, bitbucket_host = parse()
with live_progress(f"Creating '{project}/{repo}' Repository ... ") as live:
request = post(
create_repo(bitbucket_host, project, repo),
username,
token,
json.dumps(
{
"name": repo,
"slug": repo,
"scmId": "git",
"forkable": forkable,
"project": {"key": project},
"defaultBranch": f"refs/heads/{default_branch}",
}
), # type: ignore
)

if request[0] == 200:
live.update(console.print("DONE", style="bold green"))

if request[0] == 409:
live.update(console.print("CONFLICT", style="bold yellow"))
console.print(
f"Message: {request[1]['errors'][0]['message']}",
highlight=True,
style="bold yellow",
)
Loading

0 comments on commit 3e53d43

Please sign in to comment.