33from pathlib import Path
44from typing import Literal
55
6- # TODO: Refactor this entirely, maybe use github module or something.
7-
86# ---------------------------------------------------------------------------- #
97# Git #
108# ---------------------------------------------------------------------------- #
@@ -68,7 +66,6 @@ def configure_github_repo(
6866 directory : str | Path ,
6967 repo_name : str ,
7068 protection_type : Literal ["none" , "main" , "main_and_dev" ],
71- no_github : bool = False ,
7269) -> bool :
7370 """
7471 Configure a Git repository locally and optionally on GitHub with specified branch protections.
@@ -86,83 +83,70 @@ def configure_github_repo(
8683 bool: True if configuration was successful, False otherwise
8784 """
8885 try :
89- directory = Path (directory )
90-
91- # Validate inputs
92- if not directory .is_dir ():
93- raise ValueError (f"Directory '{ directory } ' does not exist." )
94-
95- if not repo_name .replace ("-" , "" ).replace ("_" , "" ).isalnum ():
96- raise ValueError (
97- "Invalid repository name. Use only letters, numbers, underscores, and hyphens."
86+ if not _check_gh_cli_installed_authenticated ():
87+ raise RuntimeError (
88+ "gh CLI is required but not installed or not authenticated. "
89+ "Try installing and running `gh auth login`."
9890 )
9991
100- # Check for gh CLI if needed
101- if not no_github :
102- if not _check_gh_cli_installed ():
103- raise RuntimeError (
104- "gh CLI is required but not installed or not authenticated. "
105- "Use no_github=True to skip GitHub operations."
106- )
107-
10892 # Initialize local repository
10993 if not init_local_git_repo (directory ):
11094 return False
11195
112- # Create dev branch if needed
113- if protection_type == "main_and_dev" :
114- if not _branch_exists ("dev" ):
115- _git ("branch dev" )
96+ # # Create dev branch if needed
97+ # if protection_type == "main_and_dev":
98+ # if not _branch_exists("dev"):
99+ # _git("branch dev")
116100
117- # Add semantic versioning tag if it doesn't exist
118- if not _tag_exists ("v0.1.0" ):
119- _git ("tag -a v0.1.0 -m 'Initial version'" )
101+ # # Add semantic versioning tag if it doesn't exist
102+ # if not _tag_exists("v0.1.0"):
103+ # _git("tag -a v0.1.0 -m 'Initial version'")
120104
121- # Create dev branch if needed
122- if protection_type == "main_and_dev" :
123- if not _branch_exists ("dev" ):
124- _git ("branch dev" )
105+ # # Create dev branch if needed
106+ # if protection_type == "main_and_dev":
107+ # if not _branch_exists("dev"):
108+ # _git("branch dev")
125109
126110 # GitHub operations
127- if not no_github :
128- github_username = _get_github_username ()
129-
130- # Create or update GitHub repository
131- if not _github_repo_exists (github_username , repo_name ):
132- _gh (
133- f"repo create { repo_name } --private --source=. --remote=origin --push"
134- )
135- else :
136- remote_url = f"git@github.com:{ github_username } /{ repo_name } .git"
137- try :
138- _git (f"remote set-url origin { remote_url } " )
139- except subprocess .CalledProcessError :
140- _git (f"remote add origin { remote_url } " )
141-
142- # Push branches and tags
143- _git ("push -u origin main" )
144- _git ("push --tags" )
145-
146- if _branch_exists ("dev" ):
147- _git ("push -u origin dev" )
148-
149- # Set branch protections if repository is public
150- is_public = _is_repo_public (github_username , repo_name )
151- if is_public :
152- if protection_type in ["main" , "main_and_dev" ]:
153- _set_branch_protection (github_username , repo_name , "main" )
154- if protection_type == "main_and_dev" :
155- _set_branch_protection (github_username , repo_name , "dev" )
156- print ("Branch protections set successfully." )
157- else :
158- print (
159- "Warning: Branch protections can only be set for public repositories "
160- "or with a GitHub Pro account."
161- )
162-
163- print ("Repository configuration complete on GitHub!" )
111+ github_username = _gh ("api user -q .login" , capture_output = True , text = True ).stdout .strip ()
112+
113+ # Create or update GitHub repository
114+ if not _github_repo_exists (github_username , repo_name ):
115+ _gh (
116+ f"repo create { repo_name } --private --source=. --remote=origin --push"
117+ )
164118 else :
165- print ("Local repository configuration complete!" )
119+ remote_url = _get_gh_remote_url (github_username , repo_name )
120+ raise RuntimeError (f"Github repo already exists at { remote_url } " )
121+ # TODO: Prompt user if they would like to set existing repo as origin.
122+ # remote_url = _get_gh_remote_url(github_username, repo_name)
123+ # try:
124+ # _git(f"remote set-url origin {remote_url}")
125+ # except subprocess.CalledProcessError:
126+ # _git(f"remote add origin {remote_url}")
127+
128+ # Push branches and tags
129+ _git ("push -u origin main" )
130+ # _git("push --tags")
131+
132+ # if _branch_exists("dev"):
133+ # _git("push -u origin dev")
134+
135+ # Set branch protections if repository is public
136+ # is_public = _is_repo_public(github_username, repo_name)
137+ # if is_public:
138+ # if protection_type in ["main", "main_and_dev"]:
139+ # _set_branch_protection(github_username, repo_name, "main")
140+ # if protection_type == "main_and_dev":
141+ # _set_branch_protection(github_username, repo_name, "dev")
142+ # print("Branch protections set successfully.")
143+ # else:
144+ # print(
145+ # "Warning: Branch protections can only be set for public repositories "
146+ # "or with a GitHub Pro account."
147+ # )
148+
149+ print ("Repository configuration complete on GitHub!" )
166150
167151 return True
168152
@@ -176,7 +160,7 @@ def _gh(command: str, **kwargs) -> subprocess.CompletedProcess:
176160 return subprocess .run (f"gh { command } " , shell = True , check = True , ** kwargs )
177161
178162
179- def _check_gh_cli_installed () -> bool :
163+ def _check_gh_cli_installed_authenticated () -> bool :
180164 """Check if gh CLI is installed and authenticated."""
181165 try :
182166 subprocess .run ("gh --version" , shell = True , check = True , capture_output = True )
@@ -185,6 +169,19 @@ def _check_gh_cli_installed() -> bool:
185169 except subprocess .CalledProcessError :
186170 return False
187171
172+ def _get_gh_remote_url (github_username : str , repo_name : str ) -> Literal ["https" , "ssh" ]:
173+ """Returns whether the github protocol is https or ssh from user's config"""
174+ try :
175+ protocol = _gh ("config get git_protocol" , capture_output = True , text = True ).stdout .strip ()
176+ if protocol == "ssh" :
177+ return f"git@github.com:{ github_username } /{ repo_name } .git"
178+ elif protocol == "https" :
179+ return f"https://github.com/{ github_username } /{ repo_name } "
180+ else :
181+ raise ValueError (f"Unexepected GitHub protocol { protocol } " )
182+ except subprocess .CalledProcessError :
183+ # Default to https if not set
184+ return "https"
188185
189186def _tag_exists (tag : str ) -> bool :
190187 """Check if a git tag exists."""
@@ -204,12 +201,6 @@ def _branch_exists(branch: str) -> bool:
204201 return False
205202
206203
207- def _get_github_username () -> str :
208- """Get the authenticated GitHub username."""
209- result = _gh ("api user -q .login" , capture_output = True , text = True )
210- return result .stdout .strip ()
211-
212-
213204def _github_repo_exists (username : str , repo_name : str ) -> bool :
214205 """Check if a GitHub repository exists."""
215206 try :
0 commit comments