2024-11-02 22:30:39 +00:00
|
|
|
import logging
|
|
|
|
|
2024-11-02 18:46:53 +00:00
|
|
|
import click
|
|
|
|
import pydantic
|
|
|
|
|
|
|
|
import frg.forgejo.browser as forgejo_browser
|
2024-11-04 02:11:12 +00:00
|
|
|
import frg.git as git
|
2024-11-02 22:30:39 +00:00
|
|
|
from frg.configuration import Config, get_configuration
|
|
|
|
from frg.context import GitContext, get_git_context
|
2024-11-02 22:22:43 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
class CliContext(pydantic.BaseModel):
|
|
|
|
config: Config
|
|
|
|
git: GitContext
|
2024-11-02 22:22:43 +00:00
|
|
|
verbose: bool
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
2024-11-02 22:22:43 +00:00
|
|
|
@click.option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
default=False,
|
|
|
|
type=bool,
|
|
|
|
is_flag=True,
|
|
|
|
help="Exposes extra logging.",
|
|
|
|
)
|
2024-11-02 18:46:53 +00:00
|
|
|
@click.pass_context
|
2024-11-02 22:22:43 +00:00
|
|
|
def cli(ctx, verbose: bool):
|
2024-11-02 19:11:21 +00:00
|
|
|
"""
|
|
|
|
A command-line tool to interact with different kinds of code forges.
|
|
|
|
"""
|
|
|
|
|
2024-11-02 22:22:43 +00:00
|
|
|
if verbose:
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2024-11-02 18:46:53 +00:00
|
|
|
config = get_configuration()
|
2024-11-02 22:22:43 +00:00
|
|
|
logger.info(f"Config: {config}")
|
|
|
|
|
2024-11-02 18:46:53 +00:00
|
|
|
git_context = get_git_context(domain_aliases=config.domain_aliases)
|
2024-11-02 22:22:43 +00:00
|
|
|
logger.info(f"Context: {git_context}")
|
|
|
|
|
|
|
|
ctx.obj = CliContext(git=git_context, config=config, verbose=verbose)
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
|
2024-11-02 19:11:21 +00:00
|
|
|
@cli.group()
|
2024-11-02 18:46:53 +00:00
|
|
|
@click.pass_obj
|
|
|
|
def pr(ctx):
|
2024-11-02 19:11:21 +00:00
|
|
|
"""Interacts with pull requests."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pr.command(name="create")
|
|
|
|
@click.option(
|
|
|
|
"-w",
|
|
|
|
"--web",
|
|
|
|
type=bool,
|
|
|
|
is_flag=True,
|
|
|
|
help="Opens the pull-request page in the default browser.",
|
|
|
|
)
|
|
|
|
@click.pass_obj
|
|
|
|
def create_pr(ctx, web: bool):
|
|
|
|
"""Interacts with pull requests."""
|
2024-11-04 02:11:12 +00:00
|
|
|
|
|
|
|
git.push(branch=ctx.git.current_branch)
|
|
|
|
|
2024-11-02 19:11:21 +00:00
|
|
|
if web:
|
|
|
|
forgejo_browser.create_pull_request_via_web(
|
|
|
|
head=ctx.git.current_branch,
|
|
|
|
host=ctx.git.host,
|
|
|
|
repo=ctx.git.repo_name,
|
|
|
|
owner=ctx.git.repo_author,
|
|
|
|
)
|
2024-11-02 22:22:43 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError("--web is the only mode supported.")
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cli.group()
|
|
|
|
@click.pass_obj
|
|
|
|
def repo(ctx):
|
2024-11-02 19:11:21 +00:00
|
|
|
"""Interacts with repositories"""
|
2024-11-02 18:46:53 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-11-02 19:11:21 +00:00
|
|
|
@repo.command(name="view")
|
|
|
|
@click.option(
|
|
|
|
"-w",
|
|
|
|
"--web",
|
|
|
|
type=bool,
|
|
|
|
is_flag=True,
|
|
|
|
help="Opens the repository page in the default browser.",
|
|
|
|
)
|
2024-11-02 18:46:53 +00:00
|
|
|
@click.pass_obj
|
2024-11-02 19:11:21 +00:00
|
|
|
def view_repo(ctx, web: bool):
|
|
|
|
"""View the current repository."""
|
|
|
|
if web:
|
|
|
|
forgejo_browser.view_repository_via_web(
|
|
|
|
host=ctx.git.host, repo=ctx.git.repo_name, owner=ctx.git.repo_author
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("--web is the only mode supported.")
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-11-02 22:22:43 +00:00
|
|
|
try:
|
|
|
|
cli()
|
|
|
|
except Exception as e:
|
|
|
|
click.secho(f"Error: {e}", fg="red", bold=True)
|
2024-11-02 18:46:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|