-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(models): Recreate CodeReviewEvent with updated schema [3/3] #109424
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
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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Generated by Django 5.2.11 on 2026-02-26 00:51 | ||
|
|
||
| import django.db.models.deletion | ||
| import django.utils.timezone | ||
| import sentry.db.models.fields.bounded | ||
| import sentry.db.models.fields.foreignkey | ||
| import sentry.models.code_review_event | ||
| from django.db import migrations, models | ||
|
|
||
| from sentry.new_migrations.migrations import CheckedMigration | ||
|
|
||
|
|
||
| class Migration(CheckedMigration): | ||
| # This flag is used to mark that a migration shouldn't be automatically run in production. | ||
| # This should only be used for operations where it's safe to run the migration after your | ||
| # code has deployed. So this should not be used for most operations that alter the schema | ||
| # of a table. | ||
| # Here are some things that make sense to mark as post deployment: | ||
| # - Large data migrations. Typically we want these to be run manually so that they can be | ||
| # monitored and not block the deploy for a long period of time while they run. | ||
| # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to | ||
| # run this outside deployments so that we don't block them. Note that while adding an index | ||
| # is a schema change, it's completely safe to run the operation after the code has deployed. | ||
| # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment | ||
|
|
||
| is_post_deployment = False | ||
|
|
||
| dependencies = [ | ||
| ("sentry", "1041_projectkeymapping"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="CodeReviewEvent", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| sentry.db.models.fields.bounded.BoundedBigAutoField( | ||
| primary_key=True, serialize=False | ||
| ), | ||
| ), | ||
| ("date_updated", models.DateTimeField(auto_now=True)), | ||
| ("date_added", models.DateTimeField(auto_now_add=True)), | ||
| ("pr_number", models.IntegerField(null=True)), | ||
| ("pr_title", models.TextField(null=True)), | ||
| ("pr_author", models.TextField(null=True)), | ||
| ("pr_url", models.TextField(null=True)), | ||
| ("pr_state", models.CharField(max_length=16, null=True)), | ||
| ("raw_event_type", models.CharField(max_length=64)), | ||
| ("raw_event_action", models.CharField(max_length=64)), | ||
| ("trigger_id", models.CharField(max_length=64, null=True)), | ||
| ("trigger", models.CharField(max_length=64, null=True)), | ||
| ("trigger_user", models.TextField(null=True)), | ||
| ("trigger_at", models.DateTimeField(default=django.utils.timezone.now)), | ||
| ("target_commit_sha", models.CharField(max_length=64, null=True)), | ||
| ( | ||
| "status", | ||
| models.CharField( | ||
| default=sentry.models.code_review_event.CodeReviewEventStatus[ | ||
| "WEBHOOK_RECEIVED" | ||
| ], | ||
| max_length=32, | ||
| ), | ||
| ), | ||
| ("denial_reason", models.TextField(null=True)), | ||
| ("webhook_received_at", models.DateTimeField(null=True)), | ||
| ("preflight_completed_at", models.DateTimeField(null=True)), | ||
| ("task_enqueued_at", models.DateTimeField(null=True)), | ||
| ("sent_to_seer_at", models.DateTimeField(null=True)), | ||
| ("review_started_at", models.DateTimeField(null=True)), | ||
| ("review_completed_at", models.DateTimeField(null=True)), | ||
| ("seer_run_id", models.CharField(max_length=64, null=True)), | ||
| ( | ||
| "comments_posted", | ||
| sentry.db.models.fields.bounded.BoundedPositiveIntegerField(null=True), | ||
| ), | ||
| ("review_result", models.JSONField(null=True)), | ||
| ( | ||
| "organization", | ||
| sentry.db.models.fields.foreignkey.FlexibleForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to="sentry.organization", | ||
| ), | ||
| ), | ||
| ( | ||
| "repository", | ||
| sentry.db.models.fields.foreignkey.FlexibleForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to="sentry.repository", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "db_table": "sentry_code_review_event", | ||
| "indexes": [ | ||
| models.Index(fields=["date_added"], name="sentry_code_date_ad_a2451c_idx"), | ||
| models.Index( | ||
| fields=["organization", "trigger_at"], | ||
| name="sentry_code_organiz_4f4b09_idx", | ||
| ), | ||
| models.Index( | ||
| fields=["organization", "repository", "trigger_at"], | ||
| name="sentry_code_organiz_7ba32c_idx", | ||
| ), | ||
| models.Index( | ||
| fields=["organization", "repository", "pr_number"], | ||
| name="sentry_code_organiz_76bbd1_idx", | ||
| ), | ||
| ], | ||
| "constraints": [ | ||
| models.UniqueConstraint( | ||
| condition=models.Q(("trigger_id__isnull", False)), | ||
| fields=("organization", "repository", "trigger_id"), | ||
| name="unique_org_repo_trigger_id", | ||
| ) | ||
| ], | ||
| }, | ||
| ), | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,18 @@ | |
|
|
||
| from enum import StrEnum | ||
|
|
||
| from django.db import models | ||
| from django.utils import timezone | ||
|
|
||
| from sentry.backup.scopes import RelocationScope | ||
| from sentry.db.models import ( | ||
| BoundedPositiveIntegerField, | ||
| DefaultFieldsModel, | ||
| FlexibleForeignKey, | ||
| region_silo_model, | ||
| sane_repr, | ||
| ) | ||
|
|
||
|
|
||
| class CodeReviewEventStatus(StrEnum): | ||
| WEBHOOK_RECEIVED = "webhook_received" | ||
|
|
@@ -16,3 +28,76 @@ class CodeReviewEventStatus(StrEnum): | |
| @classmethod | ||
| def as_choices(cls) -> tuple[tuple[str, str], ...]: | ||
| return tuple((status.value, status.value) for status in cls) | ||
|
|
||
|
|
||
| @region_silo_model | ||
| class CodeReviewEvent(DefaultFieldsModel): | ||
| """ | ||
| Records every SCM webhook event entering the Seer code review pipeline. | ||
| Tracks the full lifecycle from webhook receipt to review completion. | ||
| """ | ||
|
|
||
| __relocation_scope__ = RelocationScope.Global | ||
|
|
||
| organization = FlexibleForeignKey("sentry.Organization") | ||
| repository = FlexibleForeignKey("sentry.Repository") | ||
|
|
||
| # PR identification | ||
| pr_number = models.IntegerField(null=True) | ||
| pr_title = models.TextField(null=True) | ||
| pr_author = models.TextField(null=True) | ||
| pr_url = models.TextField(null=True) | ||
| pr_state = models.CharField(max_length=16, null=True) # open, closed, merged | ||
|
|
||
| # Raw webhook event metadata (provider-specific values) | ||
| raw_event_type = models.CharField(max_length=64) | ||
| raw_event_action = models.CharField(max_length=64) | ||
| trigger_id = models.CharField(max_length=64, null=True) | ||
|
|
||
| # Provider-agnostic fields (aligns with SeerCodeReviewConfig) | ||
| trigger = models.CharField(max_length=64, null=True) | ||
| trigger_user = models.TextField(null=True) | ||
| trigger_at = models.DateTimeField(default=timezone.now) | ||
|
|
||
| target_commit_sha = models.CharField(max_length=64, null=True) | ||
|
|
||
| # Explicit status column because multiple statuses share the same timestamp | ||
| # field (e.g. PREFLIGHT_DENIED/WEBHOOK_FILTERED both set preflight_completed_at). | ||
| status = models.CharField( | ||
| max_length=32, | ||
| choices=CodeReviewEventStatus.as_choices(), | ||
| default=CodeReviewEventStatus.WEBHOOK_RECEIVED, | ||
| ) | ||
|
Comment on lines
+66
to
+70
Member
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'm wondering if the status can just be derived by looking at the dates, but maybe it's easier to just keep it as a column
Contributor
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. We can't really distinguish based on timestamp fields alone as these are stored for success or failure states both. Plus the stats endpoint (in subsequent PR) filters based on status directly. |
||
| denial_reason = models.TextField(null=True) | ||
|
|
||
| # Timestamps for pipeline stages (region-silo wall clock) | ||
| webhook_received_at = models.DateTimeField(null=True) | ||
|
Member
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. Is this the control timestamp? or when the region receives it?
Contributor
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. region, i'll add a note |
||
| preflight_completed_at = models.DateTimeField(null=True) | ||
| task_enqueued_at = models.DateTimeField(null=True) | ||
| sent_to_seer_at = models.DateTimeField(null=True) | ||
| review_started_at = models.DateTimeField(null=True) | ||
| review_completed_at = models.DateTimeField(null=True) | ||
|
|
||
| # Seer callback data | ||
| seer_run_id = models.CharField(max_length=64, null=True) | ||
| comments_posted = BoundedPositiveIntegerField(null=True) | ||
| review_result = models.JSONField(null=True) # raw Seer response payload | ||
|
|
||
| class Meta: | ||
| app_label = "sentry" | ||
| db_table = "sentry_code_review_event" | ||
| indexes = ( | ||
| models.Index(fields=("date_added",)), # cleanup task | ||
| models.Index(fields=("organization", "trigger_at")), # stats endpoint | ||
| models.Index(fields=("organization", "repository", "trigger_at")), # events list | ||
| models.Index(fields=("organization", "repository", "pr_number")), # PR lookup | ||
| ) | ||
| constraints = [ | ||
| models.UniqueConstraint( | ||
| fields=["organization", "repository", "trigger_id"], | ||
|
Member
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. What will happen when two rows have a trigger_id of null? (e.g. two events for the same PR come in before we have a trigger_id)
Contributor
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. it's a partial unique index with condition=models.Q(trigger_id__isnull=False), meaning it only enforces uniqueness when trigger_id is non-null |
||
| name="unique_org_repo_trigger_id", | ||
| condition=models.Q(trigger_id__isnull=False), | ||
| ), | ||
| ] | ||
|
|
||
| __repr__ = sane_repr("organization_id", "repository_id", "pr_number", "status") | ||
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.
Bug: Migration
1042_create_code_review_eventis missing a dependency on1035_delete_code_review_event, which can cause migration failures due to pre-existing table indexes and constraints.Severity: CRITICAL
Suggested Fix
Update the
dependenciesin1042_create_code_review_event.pyto depend on("sentry", "1035_delete_code_review_event")instead of("sentry", "1041_projectkeymapping"). This ensures the old table is dropped before the new one is created.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.