Skip to content

Commit 2ca6de3

Browse files
committed
feat: Add support for creating GitHub repositories under organizations and refine repository name retrieval in get_github_repos.
1 parent 469c506 commit 2ca6de3

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

clearcode/store_scans.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,21 @@ def get_or_init_repo(
185185
and store it in the work dir. Clone if it does not
186186
exist optionally take the latest pull if it does exist.
187187
"""
188-
# TODO: Manage org repo name
188+
# The `repo_namespace` often serves as an organization name or user namespace.
189+
# The `user_name` might be the authenticated user or another target.
190+
namespace = repo_namespace or user_name
191+
repo_url = f"https://github.com/{namespace}/{repo_name}.git"
192+
189193
# MAYBE: CREATE ALL THE REPOS AT A TIME AND CLONE THEM LOCALLY
190194
if repo_name not in get_github_repos(user_name=user_name):
191-
repo_url = create_github_repo(repo_name=repo_name)
195+
# Determine if we should create under an org or the authenticated user
196+
# Note: If namespace is an organization and differs from the authenticated user, it needs to be created under /orgs/
197+
organization = repo_namespace if repo_namespace and repo_namespace != user_name else None
198+
199+
new_repo_url = create_github_repo(repo_name=repo_name, organization=organization)
200+
if new_repo_url:
201+
repo_url = new_repo_url
202+
192203
repo_path = work_dir / repo_name
193204
if repo_path.exists():
194205
repo = Repo(repo_path)
@@ -207,7 +218,7 @@ def get_scan_download_url(
207218
return f"https://raw.githubusercontent.com/{namespace}/{purl_hash}/main/{purl_path}/{scan_file_name}"
208219

209220

210-
def create_github_repo(repo_name, token=os.getenv("GH_TOKEN")):
221+
def create_github_repo(repo_name, organization=None, token=os.getenv("GH_TOKEN")):
211222
headers = {
212223
"Authorization": f"token {token}",
213224
"Accept": "application/vnd.github.v3+json",
@@ -217,21 +228,25 @@ def create_github_repo(repo_name, token=os.getenv("GH_TOKEN")):
217228
"name": repo_name,
218229
}
219230

220-
url = "https://api.github.com/user/repos"
231+
if organization:
232+
url = f"https://api.github.com/orgs/{organization}/repos"
233+
else:
234+
url = "https://api.github.com/user/repos"
221235

222236
response = requests.post(url, headers=headers, json=data)
223237

224238
if response.status_code == 201:
225239
print(f"Repository '{repo_name}' created successfully!")
240+
return response.json().get("clone_url")
226241
else:
227242
print(f"Failed to create repository. Status code: {response.status_code}")
228243
print(response.text)
244+
return None
229245

230246

231247
def get_github_repos(user_name, token=os.getenv("GH_TOKEN")):
232248
"""
233-
Yield full repo names for a user or org name, use the optional ``token`` if provided.
234-
Full repo name is in the form user or org name / repo name
249+
Yield repo names for a user or org name, use the optional ``token`` if provided.
235250
"""
236251
headers = {"Accept": "application/vnd.github.v3+json"}
237252
if token:
@@ -246,6 +261,6 @@ def get_github_repos(user_name, token=os.getenv("GH_TOKEN")):
246261

247262
data = response.json()
248263
for repo_data in data:
249-
full_repo_name = repo_data.get("full_name")
250-
if full_repo_name:
251-
yield full_repo_name
264+
repo_name = repo_data.get("name")
265+
if repo_name:
266+
yield repo_name

0 commit comments

Comments
 (0)