|
| 1 | +import logging |
| 2 | +from typing import Literal |
| 3 | + |
| 4 | +from taskbroker_client.retry import Retry |
| 5 | + |
| 6 | +from sentry import features |
| 7 | +from sentry.constants import ObjectStatus |
| 8 | +from sentry.integrations.github.webhook_types import GitHubInstallationRepo |
| 9 | +from sentry.integrations.services.integration import integration_service |
| 10 | +from sentry.integrations.services.integration.model import RpcIntegration |
| 11 | +from sentry.integrations.services.repository.service import repository_service |
| 12 | +from sentry.integrations.source_code_management.metrics import ( |
| 13 | + SCMIntegrationInteractionEvent, |
| 14 | + SCMIntegrationInteractionType, |
| 15 | +) |
| 16 | +from sentry.organizations.services.organization import organization_service |
| 17 | +from sentry.organizations.services.organization.model import RpcOrganization |
| 18 | +from sentry.plugins.providers.integration_repository import ( |
| 19 | + RepoExistsError, |
| 20 | + RepositoryInputConfig, |
| 21 | + get_integration_repository_provider, |
| 22 | +) |
| 23 | +from sentry.silo.base import SiloMode |
| 24 | +from sentry.tasks.base import instrumented_task, retry |
| 25 | +from sentry.taskworker.namespaces import integrations_control_tasks |
| 26 | + |
| 27 | +from .link_all_repos import get_repo_config |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +@instrumented_task( |
| 33 | + name="sentry.integrations.github.tasks.sync_repos_on_install_change", |
| 34 | + namespace=integrations_control_tasks, |
| 35 | + retry=Retry(times=3, delay=120), |
| 36 | + processing_deadline_duration=120, |
| 37 | + silo_mode=SiloMode.CONTROL, |
| 38 | +) |
| 39 | +@retry(exclude=(RepoExistsError, KeyError)) |
| 40 | +def sync_repos_on_install_change( |
| 41 | + integration_id: int, |
| 42 | + action: str, |
| 43 | + repos_added: list[GitHubInstallationRepo], |
| 44 | + repos_removed: list[GitHubInstallationRepo], |
| 45 | + repository_selection: Literal["all", "selected"], |
| 46 | +) -> None: |
| 47 | + """ |
| 48 | + Handle GitHub installation_repositories webhook events. |
| 49 | +
|
| 50 | + Creates Repository records for newly accessible repos and disables |
| 51 | + records for repos that are no longer accessible, across all orgs |
| 52 | + linked to the integration. |
| 53 | + """ |
| 54 | + result = integration_service.organization_contexts(integration_id=integration_id) |
| 55 | + integration = result.integration |
| 56 | + org_integrations = result.organization_integrations |
| 57 | + |
| 58 | + if integration is None or integration.status != ObjectStatus.ACTIVE: |
| 59 | + logger.info( |
| 60 | + "sync_repos_on_install_change.missing_or_inactive_integration", |
| 61 | + extra={"integration_id": integration_id}, |
| 62 | + ) |
| 63 | + return |
| 64 | + |
| 65 | + if not org_integrations: |
| 66 | + logger.info( |
| 67 | + "sync_repos_on_install_change.no_org_integrations", |
| 68 | + extra={"integration_id": integration_id}, |
| 69 | + ) |
| 70 | + return |
| 71 | + |
| 72 | + provider = f"integrations:{integration.provider}" |
| 73 | + |
| 74 | + for oi in org_integrations: |
| 75 | + organization_id = oi.organization_id |
| 76 | + rpc_org = organization_service.get(id=organization_id) |
| 77 | + |
| 78 | + if rpc_org is None: |
| 79 | + logger.info( |
| 80 | + "sync_repos_on_install_change.missing_organization", |
| 81 | + extra={"organization_id": organization_id}, |
| 82 | + ) |
| 83 | + continue |
| 84 | + |
| 85 | + if not features.has("organizations:github-repo-auto-sync", rpc_org): |
| 86 | + continue |
| 87 | + |
| 88 | + with SCMIntegrationInteractionEvent( |
| 89 | + interaction_type=SCMIntegrationInteractionType.SYNC_REPOS_ON_INSTALL_CHANGE, |
| 90 | + integration_id=integration_id, |
| 91 | + organization_id=organization_id, |
| 92 | + provider_key=integration.provider, |
| 93 | + ).capture(): |
| 94 | + _sync_repos_for_org( |
| 95 | + integration=integration, |
| 96 | + rpc_org=rpc_org, |
| 97 | + provider=provider, |
| 98 | + repos_added=repos_added, |
| 99 | + repos_removed=repos_removed, |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def _sync_repos_for_org( |
| 104 | + *, |
| 105 | + integration: RpcIntegration, |
| 106 | + rpc_org: RpcOrganization, |
| 107 | + provider: str, |
| 108 | + repos_added: list[GitHubInstallationRepo], |
| 109 | + repos_removed: list[GitHubInstallationRepo], |
| 110 | +) -> None: |
| 111 | + if repos_added: |
| 112 | + integration_repo_provider = get_integration_repository_provider(integration) |
| 113 | + repo_configs: list[RepositoryInputConfig] = [] |
| 114 | + for repo in repos_added: |
| 115 | + try: |
| 116 | + repo_configs.append(get_repo_config(repo, integration.id)) |
| 117 | + except KeyError: |
| 118 | + logger.exception("Failed to translate repository config") |
| 119 | + continue |
| 120 | + |
| 121 | + if repo_configs: |
| 122 | + try: |
| 123 | + integration_repo_provider.create_repositories( |
| 124 | + configs=repo_configs, organization=rpc_org |
| 125 | + ) |
| 126 | + except RepoExistsError: |
| 127 | + pass |
| 128 | + |
| 129 | + if repos_removed: |
| 130 | + external_ids = [str(repo["id"]) for repo in repos_removed] |
| 131 | + repository_service.disable_repositories_by_external_ids( |
| 132 | + organization_id=rpc_org.id, |
| 133 | + integration_id=integration.id, |
| 134 | + provider=provider, |
| 135 | + external_ids=external_ids, |
| 136 | + ) |
0 commit comments