Skip to content

Commit 632b3a4

Browse files
committed
Update backend/apps/github/management/commands/github_sync_user.py
1 parent 742629c commit 632b3a4

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

backend/apps/github/management/commands/github_sync_user.py

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,53 @@ def handle(self, *args, **options):
266266
total_orgs,
267267
)
268268

269+
# Fetch commits
270+
commit_query = f"author:{username} org:{org.login} committer-date:{date_range}"
271+
try:
272+
gh_commits = gh.search_commits(query=commit_query)
273+
274+
self.stdout.write(f" Found {gh_commits.totalCount} commits")
275+
logger.info("Found %s commits in %s", gh_commits.totalCount, org.login)
276+
277+
processed_commits = 0
278+
for gh_commit in gh_commits:
279+
processed_commits += 1
280+
repo_full_name = gh_commit.repository.full_name
281+
cache_key = repo_full_name.lower()
282+
283+
repo = repositories_cache.get(cache_key)
284+
if not repo:
285+
logger.warning(
286+
"Repository %s not in database, skipping commit", repo_full_name
287+
)
288+
continue
289+
290+
commit_committer = None
291+
if gh_commit.committer:
292+
commit_committer = User.update_data(gh_commit.committer, save=False)
293+
committers_data.append(commit_committer)
294+
295+
commit = Commit.update_data(
296+
gh_commit,
297+
repository=repo,
298+
author=user,
299+
committer=commit_committer,
300+
save=False,
301+
)
302+
303+
commits_data.append(commit)
304+
logger.info(
305+
"Processed commit %s/%s: %s in %s",
306+
processed_commits,
307+
gh_commits.totalCount,
308+
gh_commit.sha[:7],
309+
repo.name,
310+
)
311+
312+
except GithubException as e:
313+
error_msg = f" Error searching commits in {org.login}: {e}"
314+
self.stderr.write(self.style.WARNING(error_msg))
315+
269316
# Fetch pull requests
270317
pr_query = f"author:{username} org:{org.login} type:pr created:{date_range}"
271318
try:
@@ -363,53 +410,6 @@ def handle(self, *args, **options):
363410
error_msg = f" Error searching issues in {org.login}: {e}"
364411
self.stderr.write(self.style.WARNING(error_msg))
365412

366-
# Fetch commits
367-
commit_query = f"author:{username} org:{org.login} committer-date:{date_range}"
368-
try:
369-
gh_commits = gh.search_commits(query=commit_query)
370-
371-
self.stdout.write(f" Found {gh_commits.totalCount} commits")
372-
logger.info("Found %s commits in %s", gh_commits.totalCount, org.login)
373-
374-
processed_commits = 0
375-
for gh_commit in gh_commits:
376-
processed_commits += 1
377-
repo_full_name = gh_commit.repository.full_name
378-
cache_key = repo_full_name.lower()
379-
380-
repo = repositories_cache.get(cache_key)
381-
if not repo:
382-
logger.warning(
383-
"Repository %s not in database, skipping commit", repo_full_name
384-
)
385-
continue
386-
387-
commit_committer = None
388-
if gh_commit.committer:
389-
commit_committer = User.update_data(gh_commit.committer, save=False)
390-
committers_data.append(commit_committer)
391-
392-
commit = Commit.update_data(
393-
gh_commit,
394-
repository=repo,
395-
author=user,
396-
committer=commit_committer,
397-
save=False,
398-
)
399-
400-
commits_data.append(commit)
401-
logger.info(
402-
"Processed commit %s/%s: %s in %s",
403-
processed_commits,
404-
gh_commits.totalCount,
405-
gh_commit.sha[:7],
406-
repo.name,
407-
)
408-
409-
except GithubException as e:
410-
error_msg = f" Error searching commits in {org.login}: {e}"
411-
self.stderr.write(self.style.WARNING(error_msg))
412-
413413
total_synced = 0
414414

415415
if committers_data:
@@ -424,6 +424,15 @@ def handle(self, *args, **options):
424424
User.bulk_save(committers_list)
425425
self.stdout.write(f"\nSaved {len(committers_list)} unique committer(s)")
426426

427+
# Reload saved committers and update commit references
428+
saved_committers = {
429+
c.node_id: c
430+
for c in User.objects.filter(node_id__in=[c.node_id for c in committers_list])
431+
}
432+
for commit in commits_data:
433+
if commit.committer and commit.committer.node_id in saved_committers:
434+
commit.committer = saved_committers[commit.committer.node_id]
435+
427436
if commits_data:
428437
logger.info("Bulk saving %s commits", len(commits_data))
429438
Commit.bulk_save(commits_data)

0 commit comments

Comments
 (0)