-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref(derived_code_mappings): Make code clearer #86597
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
Changes from all commits
bcf366f
027649f
3a26e51
cb9c4ed
c712073
d1141cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from collections.abc import Mapping | ||
| from enum import StrEnum | ||
| from typing import Any | ||
|
|
||
|
|
@@ -9,6 +10,7 @@ | |
| from sentry import eventstore | ||
| from sentry.integrations.base import IntegrationInstallation | ||
| from sentry.integrations.models.repository_project_path_config import RepositoryProjectPathConfig | ||
| from sentry.integrations.services.integration import RpcOrganizationIntegration | ||
| from sentry.integrations.source_code_management.metrics import ( | ||
| SCMIntegrationInteractionEvent, | ||
| SCMIntegrationInteractionType, | ||
|
|
@@ -83,7 +85,7 @@ def process_event(project_id: int, group_id: int, event_id: str) -> list[CodeMap | |
| trees_helper = CodeMappingTreesHelper(trees) | ||
| code_mappings = trees_helper.generate_code_mappings(frames_to_process, platform) | ||
| dry_run = platform_config.is_dry_run_platform() | ||
| create_repos_and_code_mappings(code_mappings, installation, project, platform, dry_run) | ||
| create_configurations(code_mappings, installation, project, platform, dry_run) | ||
| except (InstallationNotFoundError, InstallationCannotGetTreesError): | ||
| pass | ||
|
|
||
|
|
@@ -160,7 +162,7 @@ def get_trees_for_org( | |
| return trees | ||
|
|
||
|
|
||
| def create_repos_and_code_mappings( | ||
| def create_configurations( | ||
| code_mappings: list[CodeMapping], | ||
| installation: IntegrationInstallation, | ||
| project: Project, | ||
|
|
@@ -177,57 +179,59 @@ def create_repos_and_code_mappings( | |
|
|
||
| organization_id = organization_integration.organization_id | ||
| for code_mapping in code_mappings: | ||
| repository = ( | ||
| Repository.objects.filter(name=code_mapping.repo.name, organization_id=organization_id) | ||
| .order_by("-date_added") | ||
| .first() | ||
| process_code_mapping( | ||
| organization_id, | ||
| organization_integration, | ||
| project, | ||
| code_mapping, | ||
| platform, | ||
| dry_run, | ||
| ) | ||
|
|
||
| if not repository: | ||
| if not dry_run: | ||
| repository = Repository.objects.create( | ||
| name=code_mapping.repo.name, | ||
| organization_id=organization_id, | ||
| integration_id=organization_integration.integration_id, | ||
| ) | ||
| metrics.incr( | ||
| key=f"{METRIC_PREFIX}.repository.created", | ||
| tags={"platform": platform, "dry_run": dry_run}, | ||
| sample_rate=1.0, | ||
| ) | ||
|
|
||
| extra = { | ||
| "project_id": project.id, | ||
| "stack_root": code_mapping.stacktrace_root, | ||
| "repository_name": code_mapping.repo.name, | ||
| } | ||
| # The project and stack_root are unique together | ||
| existing_code_mappings = RepositoryProjectPathConfig.objects.filter( | ||
| project=project, stack_root=code_mapping.stacktrace_root | ||
|
|
||
| def process_code_mapping( | ||
| organization_id: int, | ||
| organization_integration: RpcOrganizationIntegration, | ||
| project: Project, | ||
| code_mapping: CodeMapping, | ||
| platform: str, | ||
| dry_run: bool, | ||
| ) -> None: | ||
| repository = ( | ||
| Repository.objects.filter(name=code_mapping.repo.name, organization_id=organization_id) | ||
| .order_by("-date_added") | ||
| .first() | ||
| ) | ||
|
|
||
| if not repository and not dry_run: | ||
| repository = Repository.objects.create( | ||
| name=code_mapping.repo.name, | ||
| organization_id=organization_id, | ||
| integration_id=organization_integration.integration_id, | ||
| ) | ||
| if existing_code_mappings.exists(): | ||
| logger.warning("Investigate.", extra=extra) | ||
| continue | ||
|
|
||
| if not dry_run: | ||
| if repository is None: # This is mostly to appease the type checker | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| logger.warning("Investigate.", extra=extra) | ||
| continue | ||
|
|
||
| RepositoryProjectPathConfig.objects.create( | ||
| project=project, | ||
| stack_root=code_mapping.stacktrace_root, | ||
| repository=repository, | ||
| organization_integration_id=organization_integration.id, | ||
| integration_id=organization_integration.integration_id, | ||
| organization_id=organization_integration.organization_id, | ||
| source_root=code_mapping.source_path, | ||
| default_branch=code_mapping.repo.branch, | ||
| automatically_generated=True, | ||
| ) | ||
|
|
||
| metrics.incr( | ||
| key=f"{METRIC_PREFIX}.code_mapping.created", | ||
| tags={"platform": platform, "dry_run": dry_run}, | ||
| sample_rate=1.0, | ||
|
|
||
| create_code_mapping(code_mapping, project, repository, organization_integration, dry_run) | ||
| tags: Mapping[str, str | bool] = {"platform": platform, "dry_run": dry_run} | ||
| metrics.incr(key=f"{METRIC_PREFIX}.code_mapping.created", tags=tags, sample_rate=1.0) | ||
| metrics.incr(key=f"{METRIC_PREFIX}.repository.created", tags=tags, sample_rate=1.0) | ||
|
|
||
|
|
||
| def create_code_mapping( | ||
| code_mapping: CodeMapping, | ||
| project: Project, | ||
| repository: Repository | None, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MichaelSun48 I'm changing this is to |
||
| organization_integration: RpcOrganizationIntegration, | ||
| dry_run: bool, | ||
| ) -> None: | ||
| if not dry_run and repository: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also changed this. |
||
| RepositoryProjectPathConfig.objects.create( | ||
| project=project, | ||
| stack_root=code_mapping.stacktrace_root, | ||
| repository=repository, | ||
| organization_integration_id=organization_integration.id, | ||
| integration_id=organization_integration.integration_id, | ||
| organization_id=organization_integration.organization_id, | ||
| source_root=code_mapping.source_path, | ||
| default_branch=code_mapping.repo.branch, | ||
| automatically_generated=True, | ||
| ) | ||

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.
Moving all of the logic from this loop to
process_code_mapping. It will makecreate_configurationsshorter and easier to read.