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 ,
@@ -127,8 +128,9 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
127128 r for r in all_repos if r .status == ObjectStatus .DISABLED and r .external_id
128129 ]
129130
130- sentry_active_ids = {r .external_id for r in active_repos }
131- sentry_disabled_ids = {r .external_id for r in disabled_repos }
131+ # external_id is guaranteed non-None by the filter above
132+ sentry_active_ids : set [str ] = {r .external_id for r in active_repos } # type: ignore[misc]
133+ sentry_disabled_ids : set [str ] = {r .external_id for r in disabled_repos } # type: ignore[misc]
132134
133135 new_ids = github_external_ids - sentry_active_ids - sentry_disabled_ids
134136 removed_ids = sentry_active_ids - github_external_ids
@@ -171,6 +173,8 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
171173 if dry_run :
172174 return
173175
176+ repo_by_external_id = {r .external_id : r for r in active_repos + disabled_repos }
177+
174178 if new_ids :
175179 integration_repo_provider = get_integration_repository_provider (integration )
176180 repo_configs = [
@@ -179,13 +183,23 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
179183 if str (repo ["id" ]) in new_ids
180184 ]
181185 if repo_configs :
186+ created_repos = []
182187 try :
183- integration_repo_provider .create_repositories (
188+ created_repos = integration_repo_provider .create_repositories (
184189 configs = repo_configs , organization = rpc_org
185190 )
186191 except RepoExistsError :
187192 pass
188193
194+ for repo in created_repos :
195+ log_repo_change (
196+ event_name = "REPO_ADDED" ,
197+ organization_id = organization_id ,
198+ repo = repo ,
199+ source = "repository sync" ,
200+ provider = integration .provider ,
201+ )
202+
189203 if removed_ids :
190204 repository_service .disable_repositories_by_external_ids (
191205 organization_id = organization_id ,
@@ -194,13 +208,31 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
194208 external_ids = list (removed_ids ),
195209 )
196210
211+ for eid in removed_ids :
212+ repo = repo_by_external_id .get (eid )
213+ if repo :
214+ log_repo_change (
215+ event_name = "REPO_DISABLED" ,
216+ organization_id = organization_id ,
217+ repo = repo ,
218+ source = "repository sync" ,
219+ provider = integration .provider ,
220+ )
221+
197222 if restored_ids :
198223 for repo in disabled_repos :
199224 if repo .external_id in restored_ids :
200225 repo .status = ObjectStatus .ACTIVE
201226 repository_service .update_repository (
202227 organization_id = organization_id , update = repo
203228 )
229+ log_repo_change (
230+ event_name = "REPO_ENABLED" ,
231+ organization_id = organization_id ,
232+ repo = repo ,
233+ source = "repository sync" ,
234+ provider = integration .provider ,
235+ )
204236
205237
206238@instrumented_task (
0 commit comments