-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
perf(releases): Batch CommitAuthor queries in set_commits #106318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,8 +283,8 @@ def update_group_resolutions(release, commit_author_by_commit): | |
|
|
||
|
|
||
| def create_commit_authors(commit_list, release): | ||
| authors = {} | ||
|
|
||
| # Collect unique emails and their author data | ||
| author_data_by_email = {} | ||
| for data in commit_list: | ||
| author_email = data.get("author_email") | ||
| if author_email is None and data.get("author_name"): | ||
|
|
@@ -293,23 +293,70 @@ def create_commit_authors(commit_list, release): | |
| ) | ||
|
|
||
| author_email = truncatechars(author_email, 75) | ||
| if author_email: | ||
| # Lowercase to match CommitAuthorManager.get_or_create behavior | ||
| author_email = author_email.lower() | ||
|
|
||
| # Store normalized email back in data for later lookup | ||
| data["_normalized_email"] = author_email | ||
|
|
||
| if author_email and author_email not in author_data_by_email: | ||
| author_data_by_email[author_email] = {"name": data.get("author_name")} | ||
|
||
|
|
||
| if not author_data_by_email: | ||
| # No authors to process, set all to None | ||
| for data in commit_list: | ||
| data["author_model"] = None | ||
| return | ||
|
|
||
| # Batch fetch existing authors | ||
| existing_authors = { | ||
| author.email: author | ||
| for author in CommitAuthor.objects.filter( | ||
| organization_id=release.organization_id, | ||
| email__in=author_data_by_email.keys(), | ||
| ) | ||
| } | ||
|
|
||
| if not author_email: | ||
| author = None | ||
| elif author_email not in authors: | ||
| author_data = {"name": data.get("author_name")} | ||
| author, created = CommitAuthor.objects.get_or_create( | ||
| # Identify authors needing creation | ||
| emails_to_create = set(author_data_by_email.keys()) - set(existing_authors.keys()) | ||
|
|
||
| if emails_to_create: | ||
| # Batch create missing authors | ||
| authors_to_create = [ | ||
| CommitAuthor( | ||
| organization_id=release.organization_id, | ||
| email=author_email, | ||
| defaults=author_data, | ||
| email=email, | ||
| name=author_data_by_email[email]["name"], | ||
| ) | ||
| if author.name != author_data["name"]: | ||
| author.update(name=author_data["name"]) | ||
| authors[author_email] = author | ||
| else: | ||
| author = authors[author_email] | ||
| for email in emails_to_create | ||
| ] | ||
| CommitAuthor.objects.bulk_create(authors_to_create, ignore_conflicts=True) | ||
|
|
||
| data["author_model"] = author | ||
| # Re-fetch to get IDs (needed because bulk_create with ignore_conflicts | ||
| # doesn't populate IDs on PostgreSQL for conflicting rows) | ||
| newly_created = CommitAuthor.objects.filter( | ||
| organization_id=release.organization_id, | ||
| email__in=emails_to_create, | ||
| ) | ||
| for author in newly_created: | ||
| existing_authors[author.email] = author | ||
|
|
||
| # Batch update names where needed | ||
| authors_to_update = [] | ||
| for email, author in existing_authors.items(): | ||
| expected_name = author_data_by_email[email]["name"] | ||
| if author.name != expected_name: | ||
| author.name = expected_name | ||
| authors_to_update.append(author) | ||
|
|
||
| if authors_to_update: | ||
| CommitAuthor.objects.bulk_update(authors_to_update, ["name"]) | ||
|
|
||
| # Assign author models to commit data | ||
| for data in commit_list: | ||
| author_email = data.pop("_normalized_email", None) | ||
| data["author_model"] = existing_authors.get(author_email) if author_email else None | ||
|
|
||
|
|
||
| def create_repositories(commit_list, release): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Up to you but worth adding a test to verify the batching behavior or anything else related to commit authors?