forgejo-notifications-core/integration/cli.py

155 lines
5.2 KiB
Python

import argparse
from requests import Session
def forgejo_from_args(args, c: Session):
from .forgejo import Forgejo
return Forgejo(
host=args.host,
username=args.username,
password=args.password,
email=args.email,
c=c,
)
class Forgejo:
def __init__(self, parser, c: Session):
self.c = c
self.parser = parser
self.subparser = self.parser.add_subparsers()
self.install()
self.register()
self.login()
self.create_repository()
self.create_issue()
def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials")
group.add_argument("username", type=str, help="Forgejo user's username")
group.add_argument("password", type=str, help="Forgejo user's password")
group.add_argument("email", type=str, help="Forgejo user's email")
group.add_argument("host", type=str, help="URI at which Forgejo is running")
def install(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.install()
self.install_parser = self.subparser.add_parser(
name="install", description="Install Forgejo", help="Install Forgejo"
)
self.__add_credentials_parser(self.install_parser)
self.install_parser.set_defaults(func=run)
def register(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.register()
self.register_parser = self.subparser.add_parser(
name="register",
description="Forgejo user registration",
help="Register a user on Forgejo",
)
self.__add_credentials_parser(self.register_parser)
self.register_parser.set_defaults(func=run)
def login(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.login()
self.login_parser = self.subparser.add_parser(
name="login", description="Forgejo user login", help="Login on Forgejo"
)
self.__add_credentials_parser(self.login_parser)
self.login_parser.set_defaults(func=run)
def create_repository(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.login()
forgejo.create_repository(name=args.repo_name)
self.create_repository_parser = self.subparser.add_parser(
name="create_repo",
description="Create repository on Forgejo",
help="Create repository on Forgejo",
)
self.__add_credentials_parser(self.create_repository_parser)
self.create_repository_parser.set_defaults(func=run)
self.create_repository_parser.add_argument(
"repo_name", type=str, help="Name of the repository to be created"
)
def create_issue(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.login()
forgejo.create_issue(owner=args.owner, repo=args.repo)
self.create_issue_parser = self.subparser.add_parser(
name="create_issue",
description="Create issue on a repository owned by someone on Forgejo",
help="Create issue on Forgejo",
)
self.__add_credentials_parser(self.create_issue_parser)
self.create_issue_parser.set_defaults(func=run)
self.create_issue_parser.add_argument(
"owner", type=str, help="Owner of the repo"
)
self.create_issue_parser.add_argument(
"repo", type=str, help="Name of the repository"
)
class Cli:
def __init__(self):
c = Session()
self.c = c
self.parser = argparse.ArgumentParser(
description="Install and Bootstrap Forgejo and Hostea Dashboard"
)
self.subparser = self.parser.add_subparsers()
self.check_env()
self.forgejo()
def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials")
group.add_argument("username", type=str, help="Forgejo user's username")
group.add_argument("password", type=str, help="Forgejo user's password")
group.add_argument("email", type=str, help="Forgejo user's email")
def check_env(self):
def run(args, c: Session):
from .forgejo import Forgejo
Forgejo.check_online(host=args.forgejo_host)
self.check_env_parser = self.subparser.add_parser(
name="check_env",
description="Check and block until environment is ready",
help="Check and block until environment is ready",
)
self.check_env_parser.add_argument(
"forgejo_host", type=str, help="URI at which Forgejo is running"
)
self.check_env_parser.set_defaults(func=run)
def forgejo(self):
self.forgejo = self.subparser.add_parser(
name="forgejo",
description="Forgejo",
help="Forgejo-related functionality",
)
Forgejo(parser=self.forgejo, c=self.c)
def parse(self):
return self.parser.parse_args()