Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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...")

Expand Down
8 changes: 7 additions & 1 deletion app/commands/progress/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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}"
Expand Down
4 changes: 4 additions & 0 deletions app/configs/exercise_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
)
38 changes: 38 additions & 0 deletions app/utils/github_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
)
Loading