Skip to content

Commit 9e2405a

Browse files
authored
Cache github user id in memory (bazelbuild#4236)
Following up bazelbuild#4191 This is to prevent the risk of hitting github api limit in presubmit.
1 parent 2c58105 commit 9e2405a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

tools/bcr_validation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class BcrValidationResult(Enum):
8383

8484
GITHUB_REPO_RE = re.compile(r"^(https://github.com/|github:)([^/]+/[^/]+)$")
8585

86+
# Global cache for GitHub user IDs
87+
GITHUB_USER_ID_CACHE = {}
88+
8689

8790
def print_collapsed_group(name):
8891
print("\n\n--- {0}\n\n".format(name))
@@ -210,22 +213,27 @@ def check_github_url(repo_path, source_url):
210213

211214
def get_github_user_id(github_username):
212215
"""
213-
Get the GitHub user ID for a given GitHub username.
216+
Get the GitHub user ID for a given GitHub username, with caching.
214217
215218
Args:
216219
github_username: The GitHub username to look up.
217220
218221
Returns:
219222
The GitHub user ID if found, otherwise None.
220223
"""
224+
if github_username in GITHUB_USER_ID_CACHE:
225+
return GITHUB_USER_ID_CACHE[github_username]
226+
221227
url = f"https://api.github.com/users/{github_username}"
222228
headers = {}
223229
github_token = os.getenv("GITHUB_TOKEN")
224230
if github_token:
225231
headers["Authorization"] = f"token {github_token}"
226232
response = requests.get(url, headers=headers)
227233
if response.status_code == 200:
228-
return response.json().get("id")
234+
user_id = response.json().get("id")
235+
GITHUB_USER_ID_CACHE[github_username] = user_id
236+
return user_id
229237
return None
230238

231239

0 commit comments

Comments
 (0)