|
| 1 | +"""Wrapper for Github CLI commands.""" |
| 2 | +# TODO: The following should be built using the builder pattern |
| 3 | + |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +from exercise_utils.cli import run |
| 7 | + |
| 8 | + |
| 9 | +def fork_repo(repository_name: str, fork_name: str, verbose: bool) -> None: |
| 10 | + """Creates a fork of a repository.""" |
| 11 | + run( |
| 12 | + [ |
| 13 | + "gh", |
| 14 | + "repo", |
| 15 | + "fork", |
| 16 | + repository_name, |
| 17 | + "--default-branch-only", |
| 18 | + "--fork-name", |
| 19 | + fork_name, |
| 20 | + ], |
| 21 | + verbose, |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def clone_repo(repository_name: str, verbose: bool, name: Optional[str] = None) -> None: |
| 26 | + """Creates a clone of a repository.""" |
| 27 | + if name is not None: |
| 28 | + run(["gh", "repo", "clone", repository_name, name], verbose) |
| 29 | + else: |
| 30 | + run(["gh", "repo", "clone", repository_name], verbose) |
| 31 | + |
| 32 | + |
| 33 | +def delete_repo(repository_name: str, verbose: bool) -> None: |
| 34 | + """Deletes a repository.""" |
| 35 | + run(["gh", "repo", "delete", repository_name, "--yes"], verbose) |
| 36 | + |
| 37 | + |
| 38 | +def create_repo(repository_name: str, verbose: bool) -> None: |
| 39 | + """Creates a Github repository on the current user's account.""" |
| 40 | + run(["gh", "repo", "create", repository_name, "--public"], verbose) |
| 41 | + |
| 42 | + |
| 43 | +def get_github_username(verbose: bool) -> str: |
| 44 | + """Returns the currently authenticated Github user's username.""" |
| 45 | + result = run(["gh", "api", "user", "-q", ".login"], verbose) |
| 46 | + |
| 47 | + if result.is_success(): |
| 48 | + username = result.stdout.splitlines()[0] |
| 49 | + return username |
| 50 | + return "" |
| 51 | + |
| 52 | + |
| 53 | +def has_repo(repo_name: str, is_fork: bool, verbose: bool) -> bool: |
| 54 | + """Returns if the given repository exists under the current user's repositories.""" |
| 55 | + command = ["gh", "repo", "view", repo_name] |
| 56 | + if is_fork: |
| 57 | + command.extend(["--json", "isFork", "--jq", ".isFork"]) |
| 58 | + result = run( |
| 59 | + command, |
| 60 | + verbose, |
| 61 | + env={"GH_PAGER": "cat"}, |
| 62 | + ) |
| 63 | + return result.is_success() and result.stdout == "true" |
0 commit comments