66from pathlib import Path
77from sys import exit
88from typing import Any , Dict , List , Optional , Tuple
9+ from urllib .parse import quote
910
1011import click
1112import requests
@@ -100,7 +101,7 @@ def get_user_orgs(verbose: bool = False) -> List[str]:
100101 return []
101102
102103
103- def get_user_prs (repo : str , verbose : bool = False ) -> List [str ]:
104+ def get_user_prs (repo : str , owner : str , verbose : bool = False ) -> List [str ]:
104105 try :
105106 result = subprocess .run (
106107 [
@@ -114,9 +115,11 @@ def get_user_prs(repo: str, verbose: bool = False) -> List[str]:
114115 "--head" ,
115116 "submission" ,
116117 "--json" ,
118+ "headRepositoryOwner" ,
119+ "--json" ,
117120 "url" ,
118121 "-q" ,
119- ".[0].url" ,
122+ f'.[] | select ( .headRepositoryOwner.login == " { owner } " ) | .url' ,
120123 ],
121124 capture_output = True ,
122125 text = True ,
@@ -226,7 +229,7 @@ def setup(ctx: click.Context) -> None:
226229 )
227230 else :
228231 confirm (
229- f"Please confirm that you wish to:\n \t 1. Create the Git-Mastery folder under { click .style (directory_path , italic = True , bold = True )} " ,
232+ f"Please confirm that you wish to:\n \t 1. Create the Git-Mastery folder under { click .style (directory_path , italic = True , bold = True )} \n " ,
230233 abort = True ,
231234 )
232235
@@ -357,20 +360,20 @@ def submit(ctx: click.Context) -> None:
357360
358361 gitmastery_config = read_gitmastery_config (gitmastery_root_path )
359362 org_name = gitmastery_config .get ("org_name" , "" )
360- head = (
361- f"{ username } :submission" if org_name .strip () == "" else f"{ org_name } :submission"
362- )
363+ has_org = org_name .strip () != ""
364+ owner = org_name if has_org else username
363365
364366 gitmastery_exercise_root = find_gitmastery_exercise_root ()
365367 if gitmastery_exercise_root is None :
366368 error ("You are not inside a Git-Mastery exercise folder." )
367369
368370 assert gitmastery_exercise_root is not None
369- gitmastery_exercise_root_path , steps_to_cd = gitmastery_exercise_root
371+ gitmastery_exercise_root_path , _ = gitmastery_exercise_root
370372 gitmastery_exercise_config = read_gitmastery_exercise_config (
371373 gitmastery_exercise_root_path
372374 )
373375 exercise_name = gitmastery_exercise_config .get ("exercise_name" )
376+ exercise_repo_name = f"git-mastery/{ exercise_name } "
374377
375378 if (
376379 subprocess .call (
@@ -381,29 +384,8 @@ def submit(ctx: click.Context) -> None:
381384 info (f"Creating { click .style ('submission' , bold = True , italic = True )} branch" )
382385 subprocess .run (["git" , "branch" , "submission" ], stdout = stdout , stderr = stderr )
383386
384- pr_exists_result = subprocess .run (
385- [
386- "gh" ,
387- "pr" ,
388- "list" ,
389- "--repo" ,
390- f"git-mastery/{ exercise_name } " ,
391- "--state" ,
392- "open" ,
393- "--author" ,
394- "@me" ,
395- "--head" ,
396- head ,
397- "--json" ,
398- "number" ,
399- "--jq" ,
400- ".[].number" ,
401- ],
402- capture_output = True ,
403- text = True ,
404- env = dict (os .environ , ** {"GH_PAGER" : "cat" }),
405- )
406- pr_exists = len (pr_exists_result .stdout .splitlines ()) > 0
387+ user_prs = get_user_prs (exercise_repo_name , owner , verbose )
388+ pr_exists = len (user_prs ) > 0
407389
408390 current_branch_result = subprocess .run (
409391 ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ], capture_output = True , text = True
@@ -428,32 +410,49 @@ def submit(ctx: click.Context) -> None:
428410 subprocess .run (["git" , "checkout" , current_branch ], stdout = stdout , stderr = stderr )
429411
430412 if not pr_exists :
431- info ("PR does not exist yet, creating a new one!" )
432- subprocess .run (
433- [
434- "gh" ,
435- "pr" ,
436- "create" ,
437- "--repo" ,
438- f"git-mastery/{ exercise_name } " ,
439- "--base" ,
440- "main" ,
441- "--head" ,
442- head ,
443- "--title" ,
444- f"[{ username } ] [{ exercise_name } ] Submission" ,
445- "--body" ,
446- "Automated submission" ,
447- ],
448- stdout = stdout ,
449- stderr = stderr ,
450- )
413+ if has_org :
414+ info ("You are using a Github Organization to attempt this problem set." )
415+ warn (
416+ "The Github CLI currently does not support creating PRs for an organization yet."
417+ )
418+ info (
419+ f"Create the PR via the following link and click { click .style ('Create Pull Request' , bold = True )} :"
420+ )
421+ info (
422+ click .style (
423+ f"https://github.com/git-mastery/diagnostic/compare/main...{ org_name } :{ exercise_name } :submission?expand=1&body={ quote ('Automated Submission' )} &title={ quote (f'[{ org_name } ] [{ exercise_name } ] Submission' )} " ,
424+ bold = True ,
425+ italic = True ,
426+ )
427+ )
428+ confirm ("Have you created the pull request?" )
429+ else :
430+ info ("PR does not exist yet, creating a new one from your branch!" )
431+ subprocess .run (
432+ [
433+ "gh" ,
434+ "pr" ,
435+ "create" ,
436+ "--repo" ,
437+ f"git-mastery/{ exercise_name } " ,
438+ "--base" ,
439+ "main" ,
440+ "--head" ,
441+ f"{ username } :submission" ,
442+ "--title" ,
443+ f"[{ username } ] [{ exercise_name } ] Submission" ,
444+ "--body" ,
445+ "Automated submission" ,
446+ ],
447+ stdout = stdout ,
448+ stderr = stderr ,
449+ )
451450 info ("Pull request created!" )
452451 time .sleep (5 )
453452 else :
454453 info ("A PR already exists, the latest push should update it." )
455454
456- user_prs = get_user_prs (f"git-mastery/ { exercise_name } " , verbose )
455+ user_prs = get_user_prs (exercise_repo_name , owner , verbose )
457456 if len (user_prs ) == 0 :
458457 warn (
459458 f"You should have one PR, but we could not detect it yet. Try visiting { click .style (f'https://github.com/git-mastery/{ exercise_name } /pulls' , bold = True , italic = True )} to find it"
0 commit comments