Skip to content

Commit

Permalink
Merge pull request #49 from psadi/feature/functools_optz
Browse files Browse the repository at this point in the history
refactor(bb:pr,repo,auth): functool wraps to error_handler
  • Loading branch information
psadi authored Jul 10, 2024
2 parents f3427e5 + 60dd875 commit 8de2298
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 235 deletions.
33 changes: 25 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
############################################################################
# Bitbucket CLI (bb): Work seamlessly with Bitbucket from the command line
#
# Copyright (C) 2022 P S, Adithya (psadi) ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
############################################################################

name: Continuous Integration

on:
Expand Down Expand Up @@ -106,8 +125,6 @@ jobs:
name: Build, Scan & Push Docker image to Docker Hub
runs-on: ubuntu-latest
needs: "build"
env:
tag:
steps:
- uses: actions/download-artifact@v4
with:
Expand All @@ -127,17 +144,17 @@ jobs:
uses: aquasecurity/[email protected]
with:
image-ref: docker.io/psadi/bbcli:${{ github.event_name == 'release' && steps.meta.outputs.tags || github.sha }}
format: 'sarif'
output: 'trivy-results.sarif'
exit-code: '1'
format: "sarif"
output: "trivy-results.sarif"
exit-code: "1"
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
vuln-type: "os,library"
severity: "CRITICAL,HIGH"

- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
sarif_file: "trivy-results.sarif"

- name: Push Docker Image
if: ${{ github.event_name == 'release' }}
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/update-deps.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
############################################################################
# Bitbucket CLI (bb): Work seamlessly with Bitbucket from the command line
#
# Copyright (C) 2022 P S, Adithya (psadi) ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
############################################################################

name: Update dependencies

on:
Expand Down
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
############################################################################
# Bitbucket CLI (bb): Work seamlessly with Bitbucket from the command line
#
# Copyright (C) 2022 P S, Adithya (psadi) ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
############################################################################

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
############################################################################
# Bitbucket CLI (bb): Work seamlessly with Bitbucket from the command line
#
# Copyright (C) 2022 P S, Adithya (psadi) ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
############################################################################

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand Down
28 changes: 25 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
// ############################################################################
// # Bitbucket CLI (bb): Work seamlessly with Bitbucket from the command line
// #
// # Copyright (C) 2022 P S, Adithya (psadi) ([email protected])
// #
// # This program is free software: you can redistribute it and/or modify
// # it under the terms of the GNU Affero General Public License as
// # published by the Free Software Foundation, either version 3 of the
// # License, or (at your option) any later version.
// #
// # This program is distributed in the hope that it will be useful,
// # but WITHOUT ANY WARRANTY; without even the implied warranty of
// # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// # GNU Affero General Public License for more details.
// #
// # You should have received a copy of the GNU Affero General Public License
// # along with this program. If not, see <https://www.gnu.org/licenses/>.
// ############################################################################
{
"python.autoComplete.extraPaths": ["__pypackages__/3.12/lib"],
"python.analysis.extraPaths": ["__pypackages__/3.12/lib"],
"python.analysis.typeCheckingMode": "off"
"python.autoComplete.extraPaths": [
"__pypackages__/3.12/lib"
],
"python.analysis.extraPaths": [
"__pypackages__/3.12/lib"
],
"python.analysis.typeCheckingMode": "basic"
}
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
############################################################################
# Bitbucket CLI (bb): Work seamlessly with Bitbucket from the command line
#
# Copyright (C) 2022 P S, Adithya (psadi) ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
############################################################################

FROM python:3.13.0b3-alpine

COPY dist/bb-*.tar.gz bb.tar.gz
Expand Down
10 changes: 5 additions & 5 deletions bb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def version_callback(value: bool) -> None:
def setup() -> typer.Typer:
_bb = typer.Typer(
add_completion=False,
epilog="Source Code: https://github.com/psadi/bbcli",
help="Work seamlessly with Bitbucket from the command line.",
epilog="Author: P S, Adithya (psadi) <[email protected]>\n\nProject URL: https://github.com/psadi/bbcli\n\n\nLicense: GNU Affero General Public License v3.0 (AGPLv3) <https://github.com/psadi/bbcli/blob/main/LICENSE>",
help="Work seamlessly with Bitbucket from the command line",
no_args_is_help=True,
)

_bb.add_typer(_pr, name="pr", help="Manage pull requests")
_bb.add_typer(_auth, name="auth", help="Authenticate bb and git with Bitbucket")
_bb.add_typer(_repo, name="repo", help="Work with BitBucket repositories")
_bb.add_typer(_pr, name="pr", help="manage pull requests")
_bb.add_typer(_auth, name="auth", help="authenticate bb and git with bitbucket")
_bb.add_typer(_repo, name="repo", help="work with bitBucket repositories")

return _bb

Expand Down
122 changes: 55 additions & 67 deletions bb/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
_auth: typer.Typer = typer.Typer(add_completion=False, no_args_is_help=True)


@_auth.command(help="Configure bbcli to work with Bitbucket")
@_auth.command(help="configure bbcli to work with bitbucket")
@error_handler
def setup() -> None:
"""
The `setup` function checks for a configuration file and sets up authentication if not found.
Expand All @@ -54,28 +55,25 @@ def setup() -> None:
- :rtype: None
"""

@error_handler
def _setup() -> None:
if is_config_present():
console.print(
"Configuration file found, Run 'bb auth status' for more information"
)
else:
auth_setup(
typer.prompt(
"> bitbucket_host",
),
typer.prompt("> username"),
typer.prompt("> token", hide_input=True),
)
console.print(
f"\nConfiguration written at [yellow]'{BB_CONFIG_FILE}'[/yellow]\nPlease re-run [yellow]`bb auth test`[/yellow] to validate\n"
)

_setup()


@_auth.command(help="Test configuration & connection")
if is_config_present():
console.print(
"Configuration file found, Run 'bb auth status' for more information"
)
else:
auth_setup(
typer.prompt(
"> bitbucket_host",
),
typer.prompt("> username"),
typer.prompt("> token", hide_input=True),
)
console.print(
f"\nConfiguration written at [yellow]'{BB_CONFIG_FILE}'[/yellow]\nPlease re-run [yellow]`bb auth test`[/yellow] to validate\n"
)


@_auth.command(help="test cli configuration & connection")
@error_handler
def test() -> None:
"""
The function `test` contains an inner function `_test` that checks for the presence of configuration
Expand All @@ -89,16 +87,13 @@ def test() -> None:
- :rtype: None
"""

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

_test()


@_auth.command(help="View authentication config status")
@_auth.command(help="view authentication config status")
@error_handler
def status(token: bool = typer.Option(False, help="Display auth token")) -> None:
"""
Displays the status of the authentication token and related configuration
Expand All @@ -115,25 +110,22 @@ def status(token: bool = typer.Option(False, help="Display auth token")) -> None
- :rtype: None
"""

@error_handler
def _status(token: bool) -> None:
if not is_config_present():
raise ValueError("Configuration missing, run 'bb auth setup'")
if not is_config_present():
raise ValueError("vonfiguration 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]"
)
username, _token, bitbucket_host = parse()
console.print(
f"{hcm} Will connect to [bold]{bitbucket_host}[/bold] as [bold]{username}[/bold]"
)
console.print(f"{hcm} Token: {_token if token else '*' * len(_token)}")
hcm: str = "[bold green]:heavy_check_mark:[/bold green]"
console.print(
f"{hcm} Configuration found at [bold cyan]{BB_CONFIG_FILE}[/bold cyan]"
)
username, _token, bitbucket_host = parse()
console.print(
f"{hcm} Will connect to [bold]{bitbucket_host}[/bold] as [bold]{username}[/bold]"
)
console.print(f"{hcm} Token: {_token if token else '*' * len(_token)}")

_status(token)


@_auth.command(help="Reset authentication configuration")
@_auth.command(help="reset authentication configuration")
@error_handler
def reset() -> None:
"""
The function `reset` contains an inner function `_reset` that resets the configuration file.
Expand All @@ -146,27 +138,23 @@ def reset() -> None:
- :rtype: None
"""

@error_handler
def _reset() -> None:
console.print(
f"\nThis will delete,\n\n- File: [yellow]{BB_CONFIG_FILE}[/yellow]\n- Directory: [yellow]{XDG_CONFIG_HOME}[/yellow]\n"
console.print(
f"\nThis will delete,\n\n- File: [yellow]{BB_CONFIG_FILE}[/yellow]\n- Directory: [yellow]{XDG_CONFIG_HOME}[/yellow]\n"
)
if (
typer.prompt(
"Are you sure you want to reset the configuration? (y/n)", default="n"
)
if (
typer.prompt(
"Are you sure you want to reset the configuration? (y/n)", default="n"
)
== "y"
):
import os

if os.path.exists(BB_CONFIG_FILE):
os.remove(BB_CONFIG_FILE)
== "y"
):
import os

if os.path.exists(XDG_CONFIG_HOME):
os.rmdir(XDG_CONFIG_HOME)
if os.path.exists(BB_CONFIG_FILE):
os.remove(BB_CONFIG_FILE)

console.print(
"\nConfiguration reset successfully, please run [yellow]`bb auth setup`[/yellow] to configure again\n"
)
if os.path.exists(XDG_CONFIG_HOME):
os.rmdir(XDG_CONFIG_HOME)

_reset()
console.print(
"\nConfiguration reset successfully, please run [yellow]`bb auth setup`[/yellow] to configure again\n"
)
Loading

0 comments on commit 8de2298

Please sign in to comment.