diff --git a/app/commands/download.py b/app/commands/download.py index 6a07769..854b700 100644 --- a/app/commands/download.py +++ b/app/commands/download.py @@ -24,6 +24,7 @@ from app.utils.git import add_all, commit, empty_commit, init from app.utils.github_cli import ( clone_with_custom_name, + close_pr, delete_repo, fork, get_username, @@ -47,8 +48,10 @@ def _download_exercise( f"Downloading {exercise} to {click.style(exercise + '/', bold=True, italic=True)}" ) + old_config: Optional[ExerciseConfig] = None if os.path.isdir(exercise): warn(f"You already have {exercise}, removing it to download again") + old_config = ExerciseConfig.read(Path(exercise), 0) rmtree(exercise) os.makedirs(exercise) @@ -94,6 +97,12 @@ def _download_exercise( warn("Setup Github and Github CLI before downloading this exercise") sys.exit(1) + if old_config and old_config.exercise_repo.repo_type == "remote" and old_config.exercise_repo.create_fork: + pr_repo_full_name = old_config.exercise_repo.pr_repo_full_name + if pr_repo_full_name: + info(f"Closing any open PRs in {pr_repo_full_name}...") + close_pr(pr_repo_full_name) + if len(config.base_files) > 0: info("Downloading base files...") diff --git a/app/commands/progress/reset.py b/app/commands/progress/reset.py index 748c2a0..7a00a49 100644 --- a/app/commands/progress/reset.py +++ b/app/commands/progress/reset.py @@ -26,7 +26,7 @@ warn, ) from app.utils.git import add_all, commit, push -from app.utils.github_cli import delete_repo, get_prs, get_username, pull_request +from app.utils.github_cli import close_pr, delete_repo, get_prs, get_username, pull_request from app.utils.gitmastery import ExercisesRepo @@ -56,6 +56,12 @@ def reset() -> None: os.chdir(exercise_config.path) info("Resetting the exercise folder") if is_remote_type and exercise_config.exercise_repo.create_fork: + pr_repo_full_name = exercise_config.exercise_repo.pr_repo_full_name + if pr_repo_full_name: + info(f"Closing any open PRs in {pr_repo_full_name}...") + close_pr(pr_repo_full_name) + exercise_config.exercise_repo.pr_number = None + exercise_config.exercise_repo.pr_repo_full_name = None # Remove the fork first exercise_fork_name = ( f"{username}-gitmastery-{exercise_config.exercise_repo.repo_title}" diff --git a/app/configs/exercise_config.py b/app/configs/exercise_config.py index b478e57..75238a3 100644 --- a/app/configs/exercise_config.py +++ b/app/configs/exercise_config.py @@ -18,6 +18,8 @@ class ExerciseRepoConfig: create_fork: Optional[bool] fork_all_branches: Optional[bool] init: Optional[bool] + pr_number: Optional[int] + pr_repo_full_name: Optional[str] exercise_name: str tags: List[str] @@ -75,6 +77,8 @@ def read(cls: Type[Self], path: Path, cds: int) -> Self: create_fork=exercise_repo["create_fork"], fork_all_branches=exercise_repo.get("fork_all_branches", None), init=exercise_repo["init"], + pr_number=exercise_repo.get("pr_number", None), + pr_repo_full_name=exercise_repo.get("pr_repo_full_name", None), ), downloaded_at=None, ) diff --git a/app/utils/github_cli.py b/app/utils/github_cli.py index feb0be1..877550d 100644 --- a/app/utils/github_cli.py +++ b/app/utils/github_cli.py @@ -198,3 +198,41 @@ def get_user_prs(repo: str, owner: str) -> List[str]: prs = result.stdout.splitlines() return prs return [] + + +def close_pr(repo: str) -> None: + username = get_username() + + result = run( + [ + "gh", + "pr", + "list", + "--repo", + repo, + "--author", + username, + "--state", + "open", + "--json", + "number", + "--jq", + ".[].number", + ], + env={"GH_PAGER": "cat"}, + ) + + if not result.is_success(): + return + + for pr_number in result.stdout.splitlines(): + run( + [ + "gh", + "pr", + "close", + pr_number, + "--repo", + repo, + ], + ) \ No newline at end of file