1+ import os
12import subprocess
23from typing import List , Optional
34
89)
910
1011
11- def run_command (command : List [str ]) -> Optional [str ]:
12+ # TODO: We should unify how we call gh from within Python
13+ def get_github_username () -> Optional [str ]:
1214 try :
1315 result = subprocess .run (
14- command ,
16+ [ "gh" , "api" , "user" , "-q" , ".login" ] ,
1517 capture_output = True ,
1618 text = True ,
1719 check = True ,
@@ -21,22 +23,42 @@ def run_command(command: List[str]) -> Optional[str]:
2123 return None
2224
2325
26+ def has_public_repo (username : str ) -> bool :
27+ try :
28+ result = subprocess .run (
29+ [
30+ "gh" ,
31+ "repo" ,
32+ "view" ,
33+ f"{ username } /gitmastery-{ username } -remote-control" ,
34+ "--json" ,
35+ "visibility" ,
36+ "--jq" ,
37+ ".visibility" ,
38+ ],
39+ capture_output = True ,
40+ text = True ,
41+ check = True ,
42+ env = dict (os .environ , ** {"GH_PAGER" : "cat" }),
43+ )
44+ return result .stdout .strip () == "PUBLIC"
45+ except subprocess .CalledProcessError :
46+ return False
47+
48+
2449def verify (exercise : GitAutograderExercise ) -> GitAutograderOutput :
25- username = run_command ([ "gh" , "api" , "user" , "-q" , ".login" ] )
50+ username = get_github_username ( )
2651 if username is None :
2752 raise exercise .wrong_answer (["Your Github CLI is not setup correctly" ])
2853
29- username = username .strip ()
30-
3154 print (f"Create a repo called gitmastery-{ username } -remote-control" )
3255 url = input ("Enter the url of your remote repository: " )
3356 if not url .startswith (
3457 f"https://github.com/{ username } /gitmastery-{ username } -remote-control"
3558 ):
3659 raise exercise .wrong_answer (["That is not the right Github url!" ])
3760
38- code = subprocess .call (["git" , "ls-remote" , url , "--quiet" ])
39- if code == 0 :
61+ if has_public_repo (username ):
4062 return exercise .to_output (
4163 [
4264 "Great work setting up a public remote repository!" ,
0 commit comments