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
25- from sentry .plugins .providers .integration_repository import (
26- RepoExistsError ,
27- get_integration_repository_provider ,
28- )
26+ from sentry .plugins .providers .integration_repository import get_integration_repository_provider
2927from sentry .shared_integrations .exceptions import ApiError
3028from sentry .silo .base import SiloMode
3129from sentry .tasks .base import instrumented_task , retry
@@ -171,6 +169,8 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
171169 if dry_run :
172170 return
173171
172+ repo_by_external_id = {r .external_id : r for r in active_repos + disabled_repos }
173+
174174 if new_ids :
175175 integration_repo_provider = get_integration_repository_provider (integration )
176176 repo_configs = [
@@ -179,12 +179,27 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
179179 if str (repo ["id" ]) in new_ids
180180 ]
181181 if repo_configs :
182- try :
183- integration_repo_provider .create_repositories (
184- configs = repo_configs , organization = rpc_org
182+ created_repos , reactivated_repos , _ = integration_repo_provider .create_repositories (
183+ configs = repo_configs , organization = rpc_org
184+ )
185+
186+ for repo in created_repos :
187+ log_repo_change (
188+ event_name = "REPO_ADDED" ,
189+ organization_id = organization_id ,
190+ repo = repo ,
191+ source = "repository sync" ,
192+ provider = integration .provider ,
193+ )
194+
195+ for repo in reactivated_repos :
196+ log_repo_change (
197+ event_name = "REPO_ENABLED" ,
198+ organization_id = organization_id ,
199+ repo = repo ,
200+ source = "repository sync" ,
201+ provider = integration .provider ,
185202 )
186- except RepoExistsError :
187- pass
188203
189204 if removed_ids :
190205 repository_service .disable_repositories_by_external_ids (
@@ -194,13 +209,31 @@ def sync_repos_for_org(organization_integration_id: int) -> None:
194209 external_ids = list (removed_ids ),
195210 )
196211
212+ for eid in removed_ids :
213+ removed_repo = repo_by_external_id .get (eid )
214+ if removed_repo :
215+ log_repo_change (
216+ event_name = "REPO_DISABLED" ,
217+ organization_id = organization_id ,
218+ repo = removed_repo ,
219+ source = "automatic SCM syncing" ,
220+ provider = integration .provider ,
221+ )
222+
197223 if restored_ids :
198224 for repo in disabled_repos :
199225 if repo .external_id in restored_ids :
200226 repo .status = ObjectStatus .ACTIVE
201227 repository_service .update_repository (
202228 organization_id = organization_id , update = repo
203229 )
230+ log_repo_change (
231+ event_name = "REPO_ENABLED" ,
232+ organization_id = organization_id ,
233+ repo = repo ,
234+ source = "automatic SCM syncing" ,
235+ provider = integration .provider ,
236+ )
204237
205238
206239@instrumented_task (
0 commit comments