4
4
import argparse
5
5
import os
6
6
import requests
7
+ import re
7
8
8
9
from time import sleep , mktime , gmtime , time , localtime
9
10
from typing import Dict , Optional
@@ -54,7 +55,32 @@ def hit_endpoint(url,token,method='GET'):
54
55
return response_json
55
56
56
57
58
+ def get_repo_owner_and_name (repo_http_url ):
59
+ """ Gets the owner and repo from a url.
57
60
61
+ Args:
62
+ url: Github url
63
+
64
+ Returns:
65
+ Tuple of owner and repo. Or a tuple of None and None if the url is invalid.
66
+ """
67
+
68
+ # Regular expression to parse a GitHub URL into two groups
69
+ # The first group contains the owner of the github repo extracted from the url
70
+ # The second group contains the name of the github repo extracted from the url
71
+ # 'But what is a regular expression?' ----> https://docs.python.org/3/howto/regex.html
72
+ regex = r"https?:\/\/github\.com\/([A-Za-z0-9 \- _]+)\/([A-Za-z0-9 \- _ \.]+)(.git)?\/?$"
73
+ result = re .search (regex , repo_http_url )
74
+
75
+ if not result :
76
+ return None , None
77
+
78
+ capturing_groups = result .groups ()
79
+
80
+ owner = capturing_groups [0 ]
81
+ repo = capturing_groups [1 ]
82
+
83
+ return owner , repo
58
84
59
85
60
86
@@ -71,19 +97,29 @@ def __init__(self, agency: str, version: str, token: Optional[str] = None,):
71
97
"releases" : []
72
98
}
73
99
74
- def get_code_json (self , repo : str ) -> Optional [Dict ]:
100
+ self .token = token
101
+
102
+ def get_code_json_github (self ,repo : str ) -> Optional [Dict ]:
75
103
try :
76
- content = repo .get_contents ("code.json" , ref = repo .default_branch )
77
- except GithubException as e :
104
+ owner ,name = get_repo_owner_and_name (repo )
105
+ code_json_endpoint = f"https://api.github.com/repos/{ owner } /{ name } /contents/code.json"
106
+ content_dict = hit_endpoint (code_json_endpoint ,self .token )#repo.get_contents("code.json", ref = repo.default_branch)
107
+ except Exception as e :
78
108
print (f"GitHub Error: { e .data .get ('message' , 'No message available' )} " )
79
109
return None
80
110
81
111
try :
82
- decoded_content = base64 .b64decode (content . content )
112
+ decoded_content = base64 .b64decode (content_dict [ ' content' ] )
83
113
return json .loads (decoded_content )
84
114
except (json .JSONDecodeError , ValueError ) as e :
85
115
print (f"JSON Error: { str (e )} " )
86
116
return None
117
+
118
+ def get_code_json (self , repo : str ) -> Optional [Dict ]:
119
+ if 'github' in repo :
120
+ return self .get_code_json_github (repo )
121
+ else :
122
+ return None
87
123
88
124
def save_code_json (self , repo : str , output_path : str ) -> Optional [str ]:
89
125
@@ -107,16 +143,19 @@ def update_index(self, index: Dict, code_json: Dict, org_name: str, repo_name: s
107
143
108
144
index ['releases' ].append (baseline )
109
145
110
- def get_org_repos (self , org_name : str ) -> list [Organization ]:
146
+ def get_org_repos (self , org_name : str ) -> list [Dict ]:
111
147
try :
112
- org = self .github .get_organization ( org_name )
148
+ org_endpoint = f"https://api .github.com/orgs/ { org_name } /repos"
113
149
print (f"\n Processing organization: { org_name } " )
114
150
115
- total_repos = org .public_repos
151
+ repo_list = hit_endpoint (org_endpoint ,self .token )
152
+
153
+
154
+ total_repos = len (repo_list )
116
155
print (f"Found { total_repos } public repositories" )
117
156
118
- return total_repos
119
- except GithubException as e :
157
+ return repo_list
158
+ except Exception as e :
120
159
raise e
121
160
122
161
def save_organization_files (self , org_name : str , codeJSONPath ) -> None :
0 commit comments