Skip to content

Commit f982d68

Browse files
authored
feat: auto-inject global_node_id for GitHub usernames during ingestion (#3)
WATonomous/infra-config#4953
1 parent 64f6bc1 commit f982d68

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
compare_line_by_line,
2424
assert_throws,
2525
transform_file,
26+
inject_github_node_ids,
2627
)
2728

2829
# BUILD_INFO is generated by the build pipeline (e.g. docker/metadata-action).
@@ -154,7 +155,12 @@ def ingest(payload: IngestPayload):
154155
for file in payload.files:
155156
transform_file(file)
156157

157-
g = Github(get_github_token())
158+
# Inject global_node_ids for GitHub usernames
159+
token = get_github_token()
160+
for file in payload.files:
161+
inject_github_node_ids(file, token)
162+
163+
g = Github(token)
158164
logger.info(f"GitHub rate limit remaining: {g.rate_limiting[0]} / {g.rate_limiting[1]}")
159165
repo = g.get_repo(payload.repo)
160166

src/utils.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,55 @@ def assert_throws(func, exception_class, message=None):
176176
except exception_class:
177177
pass
178178
else:
179-
raise AssertionError(message or f"{func} did not throw {exception_class}")
179+
raise AssertionError(message or f"{func} did not throw {exception_class}")
180+
181+
182+
def fetch_github_node_id(username: str, token: str) -> str | None:
183+
"""Fetch global_node_id for a GitHub username."""
184+
try:
185+
resp = requests.get(
186+
f"https://api.github.com/users/{username}",
187+
headers={
188+
"Authorization": f"Bearer {token}",
189+
"X-Github-Next-Global-ID": "1"
190+
}
191+
)
192+
if resp.ok:
193+
return resp.json().get('node_id')
194+
logger.warning(f"Failed to fetch node_id for {username}: {resp.status_code}")
195+
return None
196+
except Exception as e:
197+
logger.error(f"Error fetching node_id for {username}: {e}")
198+
return None
199+
200+
201+
def inject_github_node_ids(file: File, token: str) -> File:
202+
"""Inject global_node_id for github and internal_tools if missing."""
203+
if not file.path.startswith("directory/users/data/"):
204+
return file
205+
206+
data = yaml.safe_load(file.content)
207+
modified = False
208+
209+
# github.global_node_id
210+
gh_username = data.get('github', {}).get('username')
211+
if gh_username and not data.get('github', {}).get('global_node_id'):
212+
node_id = fetch_github_node_id(gh_username, token)
213+
if node_id:
214+
data['github']['global_node_id'] = node_id
215+
modified = True
216+
logger.info(f"Injected github.global_node_id for {gh_username}")
217+
218+
# internal_tools.global_node_id
219+
it_username = data.get('internal_tools', {}).get('github_username')
220+
if it_username and not data.get('internal_tools', {}).get('global_node_id'):
221+
node_id = fetch_github_node_id(it_username, token)
222+
if node_id:
223+
data['internal_tools']['global_node_id'] = node_id
224+
modified = True
225+
logger.info(f"Injected internal_tools.global_node_id for {it_username}")
226+
227+
if modified:
228+
file.content = yaml.dump(data, width=float('inf'))
229+
230+
return file

0 commit comments

Comments
 (0)