|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +from sentry import features |
| 7 | +from sentry.constants import ENABLE_PR_REVIEW_TEST_GENERATION_DEFAULT, HIDE_AI_FEATURES_DEFAULT |
| 8 | +from sentry.models.organization import Organization |
| 9 | +from sentry.models.repository import Repository |
| 10 | +from sentry.models.repositorysettings import CodeReviewSettings, RepositorySettings |
| 11 | + |
| 12 | +DenialReason = str | None |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class CodeReviewPreflightResult: |
| 17 | + allowed: bool |
| 18 | + settings: CodeReviewSettings | None = None |
| 19 | + denial_reason: DenialReason = None |
| 20 | + |
| 21 | + |
| 22 | +class CodeReviewPreflightService: |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + organization: Organization, |
| 26 | + repo: Repository, |
| 27 | + integration_id: int | None = None, |
| 28 | + pr_author_external_id: str | None = None, |
| 29 | + ): |
| 30 | + self.organization = organization |
| 31 | + self.repo = repo |
| 32 | + self.integration_id = integration_id |
| 33 | + self.pr_author_external_id = pr_author_external_id |
| 34 | + |
| 35 | + repo_settings = RepositorySettings.objects.filter(repository=repo).first() |
| 36 | + self._repo_settings = repo_settings.get_code_review_settings() if repo_settings else None |
| 37 | + |
| 38 | + def check(self) -> CodeReviewPreflightResult: |
| 39 | + checks: list[Callable[[], DenialReason]] = [ |
| 40 | + self._check_legal_ai_consent, |
| 41 | + self._check_org_feature_enablement, |
| 42 | + self._check_repo_feature_enablement, |
| 43 | + self._check_billing, |
| 44 | + ] |
| 45 | + |
| 46 | + for check in checks: |
| 47 | + denial_reason = check() |
| 48 | + if denial_reason is not None: |
| 49 | + return CodeReviewPreflightResult(allowed=False, denial_reason=denial_reason) |
| 50 | + |
| 51 | + return CodeReviewPreflightResult(allowed=True, settings=self._repo_settings) |
| 52 | + |
| 53 | + # ------------------------------------------------------------------------- |
| 54 | + # Checks - each returns denial reason (str) or None if valid |
| 55 | + # ------------------------------------------------------------------------- |
| 56 | + |
| 57 | + def _check_legal_ai_consent(self) -> DenialReason: |
| 58 | + has_gen_ai_flag = features.has("organizations:gen-ai-features", self.organization) |
| 59 | + has_hidden_ai = self.organization.get_option( |
| 60 | + "sentry:hide_ai_features", HIDE_AI_FEATURES_DEFAULT |
| 61 | + ) |
| 62 | + |
| 63 | + if not has_gen_ai_flag or has_hidden_ai: |
| 64 | + return "org_legal_ai_consent_not_granted" |
| 65 | + return None |
| 66 | + |
| 67 | + def _check_org_feature_enablement(self) -> DenialReason: |
| 68 | + # Seat-based orgs are always eligible |
| 69 | + if self._is_seat_based_seer_plan_org(): |
| 70 | + return None |
| 71 | + |
| 72 | + # Beta orgs need the legacy toggle enabled |
| 73 | + if self._is_code_review_beta_org(): |
| 74 | + if self._has_legacy_toggle_enabled(): |
| 75 | + return None |
| 76 | + return "org_pr_review_legacy_toggle_disabled" |
| 77 | + |
| 78 | + return "org_not_eligible_for_code_review" |
| 79 | + |
| 80 | + def _check_repo_feature_enablement(self) -> DenialReason: |
| 81 | + if self._is_seat_based_seer_plan_org(): |
| 82 | + if self._repo_settings is None or not self._repo_settings.enabled: |
| 83 | + return "repo_code_review_disabled" |
| 84 | + return None |
| 85 | + elif self._is_code_review_beta_org(): |
| 86 | + # For beta orgs, all repos are considered enabled |
| 87 | + return None |
| 88 | + else: |
| 89 | + return "repo_code_review_disabled" |
| 90 | + |
| 91 | + def _check_billing(self) -> DenialReason: |
| 92 | + # TODO: Once we're ready to actually gate billing (when it's time for GA), uncomment this |
| 93 | + """ |
| 94 | + if self.integration_id is None or self.pr_author_external_id is None: |
| 95 | + return "billing_missing_contributor_info" |
| 96 | +
|
| 97 | + billing_ok = passes_code_review_billing_check( |
| 98 | + organization_id=self.organization.id, |
| 99 | + integration_id=self.integration_id, |
| 100 | + external_identifier=self.pr_author_external_id, |
| 101 | + ) |
| 102 | + if not billing_ok: |
| 103 | + return "billing_quota_exceeded" |
| 104 | + """ |
| 105 | + |
| 106 | + return None |
| 107 | + |
| 108 | + # ------------------------------------------------------------------------- |
| 109 | + # Org type helpers |
| 110 | + # ------------------------------------------------------------------------- |
| 111 | + |
| 112 | + def _is_seat_based_seer_plan_org(self) -> bool: |
| 113 | + return features.has("organizations:seat-based-seer-enabled", self.organization) |
| 114 | + |
| 115 | + def _is_code_review_beta_org(self) -> bool: |
| 116 | + # TODO: Remove the has_legacy_opt_in check once the beta list is frozen |
| 117 | + has_beta_flag = features.has("organizations:code-review-beta", self.organization) |
| 118 | + has_legacy_opt_in = self.organization.get_option( |
| 119 | + "sentry:enable_pr_review_test_generation", False |
| 120 | + ) |
| 121 | + return has_beta_flag or bool(has_legacy_opt_in) |
| 122 | + |
| 123 | + def _has_legacy_toggle_enabled(self) -> bool: |
| 124 | + return bool( |
| 125 | + self.organization.get_option( |
| 126 | + "sentry:enable_pr_review_test_generation", |
| 127 | + ENABLE_PR_REVIEW_TEST_GENERATION_DEFAULT, |
| 128 | + ) |
| 129 | + ) |
0 commit comments