From 795334d2c1055b2e1b71d950bd24455c51ac4c0a Mon Sep 17 00:00:00 2001 From: Armen Zambrano G <44410+armenzg@users.noreply.github.com> Date: Wed, 5 Mar 2025 14:29:41 -0500 Subject: [PATCH] A more explicit solution --- .../issues/auto_source_code_config/task.py | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/sentry/issues/auto_source_code_config/task.py b/src/sentry/issues/auto_source_code_config/task.py index cc7f778185c6c3..c8f7a9cb397f18 100644 --- a/src/sentry/issues/auto_source_code_config/task.py +++ b/src/sentry/issues/auto_source_code_config/task.py @@ -193,26 +193,38 @@ def create_repos_and_code_mappings( sample_rate=1.0, ) - created = False + 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 + ) + if existing_code_mappings.exists(): + logger.warning("Investigate.", extra=extra) + continue + if not dry_run: - # The project and stack_root are unique together - _, created = RepositoryProjectPathConfig.objects.get_or_create( + if repository is None: # This is mostly to appease the type checker + logger.warning("Investigate.", extra=extra) + continue + + RepositoryProjectPathConfig.objects.create( project=project, stack_root=code_mapping.stacktrace_root, - defaults={ - "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, - }, + 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, ) - if created or dry_run: - metrics.incr( - key=f"{METRIC_PREFIX}.code_mapping.created", - tags={"platform": platform, "dry_run": dry_run}, - sample_rate=1.0, - ) + metrics.incr( + key=f"{METRIC_PREFIX}.code_mapping.created", + tags={"platform": platform, "dry_run": dry_run}, + sample_rate=1.0, + )