Skip to content

Commit a73cd13

Browse files
Enable closing pr during resetting and re-downloading (#75)
* Update exercise_config * Enable closing pr during resetting * Enable closing pr during re-downloading * Add info message for closing pr * Update info message for closing pr * Refactor code in download * Address copilot's comment
1 parent 58acd30 commit a73cd13

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

app/commands/download.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from app.utils.git import add_all, commit, empty_commit, init
2525
from app.utils.github_cli import (
2626
clone_with_custom_name,
27+
close_prs,
2728
delete_repo,
2829
fork,
2930
get_username,
@@ -47,8 +48,10 @@ def _download_exercise(
4748
f"Downloading {exercise} to {click.style(exercise + '/', bold=True, italic=True)}"
4849
)
4950

51+
old_config: Optional[ExerciseConfig] = None
5052
if os.path.isdir(exercise):
5153
warn(f"You already have {exercise}, removing it to download again")
54+
old_config = ExerciseConfig.read(Path(exercise), 0)
5255
rmtree(exercise)
5356

5457
os.makedirs(exercise)
@@ -94,6 +97,12 @@ def _download_exercise(
9497
warn("Setup Github and Github CLI before downloading this exercise")
9598
sys.exit(1)
9699

100+
if old_config and old_config.exercise_repo.repo_type == "remote" and old_config.exercise_repo.create_fork:
101+
pr_repo_full_name = old_config.exercise_repo.pr_repo_full_name
102+
if pr_repo_full_name:
103+
info(f"Closing any open PRs in {pr_repo_full_name}...")
104+
close_prs(pr_repo_full_name)
105+
97106
if len(config.base_files) > 0:
98107
info("Downloading base files...")
99108

app/commands/progress/reset.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
warn,
2727
)
2828
from app.utils.git import add_all, commit, push
29-
from app.utils.github_cli import delete_repo, get_prs, get_username, pull_request
29+
from app.utils.github_cli import close_prs, delete_repo, get_prs, get_username, pull_request
3030
from app.utils.gitmastery import ExercisesRepo
3131

3232

@@ -56,6 +56,12 @@ def reset() -> None:
5656
os.chdir(exercise_config.path)
5757
info("Resetting the exercise folder")
5858
if is_remote_type and exercise_config.exercise_repo.create_fork:
59+
pr_repo_full_name = exercise_config.exercise_repo.pr_repo_full_name
60+
if pr_repo_full_name:
61+
info(f"Closing any open PRs in {pr_repo_full_name}...")
62+
close_prs(pr_repo_full_name)
63+
exercise_config.exercise_repo.pr_number = None
64+
exercise_config.exercise_repo.pr_repo_full_name = None
5965
# Remove the fork first
6066
exercise_fork_name = (
6167
f"{username}-gitmastery-{exercise_config.exercise_repo.repo_title}"

app/configs/exercise_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class ExerciseRepoConfig:
1818
create_fork: Optional[bool]
1919
fork_all_branches: Optional[bool]
2020
init: Optional[bool]
21+
pr_number: Optional[int]
22+
pr_repo_full_name: Optional[str]
2123

2224
exercise_name: str
2325
tags: List[str]
@@ -75,6 +77,8 @@ def read(cls: Type[Self], path: Path, cds: int) -> Self:
7577
create_fork=exercise_repo["create_fork"],
7678
fork_all_branches=exercise_repo.get("fork_all_branches", None),
7779
init=exercise_repo["init"],
80+
pr_number=exercise_repo.get("pr_number", None),
81+
pr_repo_full_name=exercise_repo.get("pr_repo_full_name", None),
7882
),
7983
downloaded_at=None,
8084
)

app/utils/github_cli.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,42 @@ def get_user_prs(repo: str, owner: str) -> List[str]:
198198
prs = result.stdout.splitlines()
199199
return prs
200200
return []
201+
202+
203+
def close_prs(repo: str) -> None:
204+
"""Close all open pull requests authored by the current user in `repo`."""
205+
206+
result = run(
207+
[
208+
"gh",
209+
"pr",
210+
"list",
211+
"--repo",
212+
repo,
213+
"--author",
214+
"@me",
215+
"--state",
216+
"open",
217+
"--json",
218+
"number",
219+
"--jq",
220+
".[].number",
221+
],
222+
env={"GH_PAGER": "cat"},
223+
)
224+
225+
if not result.is_success():
226+
return
227+
228+
for pr_number in result.stdout.splitlines():
229+
run(
230+
[
231+
"gh",
232+
"pr",
233+
"close",
234+
pr_number,
235+
"--repo",
236+
repo,
237+
],
238+
env={"GH_PAGER": "cat"},
239+
)

0 commit comments

Comments
 (0)