Skip to content

Commit 9d97ccf

Browse files
Enable closing pr during resetting
1 parent 406c60a commit 9d97ccf

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

app/commands/progress/reset.py

Lines changed: 6 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_pr, delete_repo, get_prs, get_username, pull_request
3030
from app.utils.gitmastery import ExercisesRepo
3131

3232

@@ -56,6 +56,11 @@ 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+
close_pr(pr_repo_full_name)
62+
exercise_config.exercise_repo.pr_number = None
63+
exercise_config.exercise_repo.pr_repo_full_name = None
5964
# Remove the fork first
6065
exercise_fork_name = (
6166
f"{username}-gitmastery-{exercise_config.exercise_repo.repo_title}"

app/utils/github_cli.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,41 @@ 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_pr(repo: str) -> None:
204+
username = get_username()
205+
206+
result = run(
207+
[
208+
"gh",
209+
"pr",
210+
"list",
211+
"--repo",
212+
repo,
213+
"--author",
214+
username,
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+
)

0 commit comments

Comments
 (0)