Skip to content
Merged
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_prs,
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)
Comment thread
desmondwong1215 marked this conversation as resolved.
rmtree(exercise)
Comment thread
desmondwong1215 marked this conversation as resolved.

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_prs(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_prs, 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_prs(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,
)
39 changes: 39 additions & 0 deletions app/utils/github_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,42 @@ def get_user_prs(repo: str, owner: str) -> List[str]:
prs = result.stdout.splitlines()
return prs
return []


def close_prs(repo: str) -> None:
"""Close all open pull requests authored by the current user in `repo`."""

result = run(
[
"gh",
"pr",
"list",
"--repo",
repo,
"--author",
"@me",
"--state",
"open",
Comment thread
desmondwong1215 marked this conversation as resolved.
"--json",
"number",
"--jq",
".[].number",
],
Comment on lines +207 to +221
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only fetches the first 30 PRs, and while technically the better option would be to add a flag of the form --limit 9999999 for edge cases, it is extremely unlikely the user will ever open >30 PRs on their repo (unless done deliberately). So it may be fine to leave it this way.

env={"GH_PAGER": "cat"},
)

if not result.is_success():
return

for pr_number in result.stdout.splitlines():
run(
[
"gh",
"pr",
"close",
Comment thread
desmondwong1215 marked this conversation as resolved.
pr_number,
"--repo",
repo,
],
env={"GH_PAGER": "cat"},
)
Loading