2121 SCMIntegrationInteractionEvent ,
2222 SCMIntegrationInteractionType ,
2323)
24+ from sentry .integrations .source_code_management .repo_audit import log_repo_change
2425from sentry .organizations .services .organization import organization_service
2526from sentry .plugins .providers .integration_repository import (
2627 RepoExistsError ,
@@ -124,8 +125,9 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
124125 r for r in all_repos if r .status == ObjectStatus .DISABLED and r .external_id
125126 ]
126127
127- sentry_active_ids = {r .external_id for r in active_repos }
128- sentry_disabled_ids = {r .external_id for r in disabled_repos }
128+ # external_id is guaranteed non-None by the filter above
129+ sentry_active_ids : set [str ] = {r .external_id for r in active_repos } # type: ignore[misc]
130+ sentry_disabled_ids : set [str ] = {r .external_id for r in disabled_repos } # type: ignore[misc]
129131
130132 new_ids = github_external_ids - sentry_active_ids - sentry_disabled_ids
131133 removed_ids = sentry_active_ids - github_external_ids
@@ -168,6 +170,8 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
168170 if dry_run :
169171 return
170172
173+ repo_by_external_id = {r .external_id : r for r in active_repos + disabled_repos }
174+
171175 if new_ids :
172176 integration_repo_provider = get_integration_repository_provider (integration )
173177 repo_configs = [
@@ -176,13 +180,23 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
176180 if str (repo ["id" ]) in new_ids
177181 ]
178182 if repo_configs :
183+ created_repos = []
179184 try :
180- integration_repo_provider .create_repositories (
185+ created_repos = integration_repo_provider .create_repositories (
181186 configs = repo_configs , organization = rpc_org
182187 )
183188 except RepoExistsError :
184189 pass
185190
191+ for repo in created_repos :
192+ log_repo_change (
193+ event_name = "REPO_ADDED" ,
194+ organization_id = organization_id ,
195+ repo = repo ,
196+ source = "repository sync" ,
197+ provider = integration .provider ,
198+ )
199+
186200 if removed_ids :
187201 repository_service .disable_repositories_by_external_ids (
188202 organization_id = organization_id ,
@@ -191,13 +205,31 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
191205 external_ids = list (removed_ids ),
192206 )
193207
208+ for eid in removed_ids :
209+ repo = repo_by_external_id .get (eid )
210+ if repo :
211+ log_repo_change (
212+ event_name = "REPO_DISABLED" ,
213+ organization_id = organization_id ,
214+ repo = repo ,
215+ source = "repository sync" ,
216+ provider = integration .provider ,
217+ )
218+
194219 if restored_ids :
195220 for repo in disabled_repos :
196221 if repo .external_id in restored_ids :
197222 repo .status = ObjectStatus .ACTIVE
198223 repository_service .update_repository (
199224 organization_id = organization_id , update = repo
200225 )
226+ log_repo_change (
227+ event_name = "REPO_ENABLED" ,
228+ organization_id = organization_id ,
229+ repo = repo ,
230+ source = "repository sync" ,
231+ provider = integration .provider ,
232+ )
201233
202234
203235@instrumented_task (
0 commit comments