13
13
ProjectArtifactBundle ,
14
14
ReleaseArtifactBundle ,
15
15
)
16
+ from sentry .models .debugfile import ProguardArtifactRelease , ProjectDebugFile
16
17
from sentry .models .files import FileBlobOwner
17
18
from sentry .models .organization import Organization
18
19
from sentry .models .project import Project
23
24
24
25
25
26
@instrumented_task (
26
- name = "sentry.demo_mode.tasks.sync_artifact_bundles " ,
27
+ name = "sentry.demo_mode.tasks.sync_debug_artifacts " ,
27
28
queue = "demo_mode" ,
28
29
taskworker_config = TaskworkerConfig (namespace = demomode_tasks ),
29
30
)
30
- def sync_artifact_bundles ():
31
+ def sync_debug_artifacts ():
31
32
32
33
if (
33
34
not options .get ("sentry.demo_mode.sync_artifact_bundles.enable" )
@@ -43,6 +44,8 @@ def sync_artifact_bundles():
43
44
lookback_days = options .get ("sentry.demo_mode.sync_artifact_bundles.lookback_days" )
44
45
45
46
_sync_artifact_bundles (source_org , target_org , lookback_days )
47
+ _sync_project_debug_files (source_org , target_org , lookback_days )
48
+ _sync_proguard_artifact_releases (source_org , target_org , lookback_days )
46
49
47
50
48
51
def _sync_artifact_bundles (source_org : Organization , target_org : Organization , lookback_days = 1 ):
@@ -67,6 +70,62 @@ def _sync_artifact_bundles(source_org: Organization, target_org: Organization, l
67
70
_sync_artifact_bundle (source_artifact_bundle , target_org )
68
71
69
72
73
+ def _sync_project_debug_files (source_org : Organization , target_org : Organization , lookback_days = 1 ):
74
+ if not source_org or not target_org :
75
+ return
76
+
77
+ source_project_ids = Project .objects .filter (
78
+ organization_id = source_org .id ,
79
+ ).values_list ("id" , flat = True )
80
+
81
+ target_project_ids = Project .objects .filter (
82
+ organization_id = target_org .id ,
83
+ ).values_list ("id" , flat = True )
84
+
85
+ source_project_debug_files = ProjectDebugFile .objects .filter (
86
+ project_id__in = source_project_ids ,
87
+ )
88
+
89
+ target_project_debug_files = ProjectDebugFile .objects .filter (
90
+ project_id__in = target_project_ids ,
91
+ )
92
+
93
+ different_project_debug_files = source_project_debug_files .exclude (
94
+ debug_id__in = target_project_debug_files .values_list ("debug_id" , flat = True )
95
+ )
96
+
97
+ for source_project_debug_file in different_project_debug_files :
98
+ _sync_project_debug_file (source_project_debug_file , target_org )
99
+
100
+
101
+ def _sync_proguard_artifact_releases (
102
+ source_org : Organization , target_org : Organization , lookback_days = 1
103
+ ):
104
+ if not source_org or not target_org :
105
+ return
106
+
107
+ cutoff_date = timezone .now () - timedelta (days = lookback_days )
108
+
109
+ proguard_artifact_releases = ProguardArtifactRelease .objects .filter (
110
+ Q (organization_id = source_org .id ) | Q (organization_id = target_org .id ),
111
+ date_added__gte = cutoff_date ,
112
+ )
113
+
114
+ source_proguard_artifact_releases = proguard_artifact_releases .filter (
115
+ organization_id = source_org .id ,
116
+ )
117
+ target_proguard_artifact_releases = proguard_artifact_releases .filter (
118
+ organization_id = target_org .id ,
119
+ )
120
+
121
+ different_proguard_artifact_releases = source_proguard_artifact_releases .exclude (
122
+ proguard_uuid__in = target_proguard_artifact_releases .values_list ("proguard_uuid" , flat = True )
123
+ )
124
+
125
+ for source_proguard_artifact_release in different_proguard_artifact_releases :
126
+ _sync_proguard_artifact_release (source_proguard_artifact_release , target_org )
127
+
128
+
70
129
def _sync_artifact_bundle (source_artifact_bundle : ArtifactBundle , target_org : Organization ):
71
130
try :
72
131
with atomic_transaction (
@@ -144,6 +203,75 @@ def _sync_release_artifact_bundle(
144
203
)
145
204
146
205
206
+ def _sync_project_debug_file (
207
+ source_project_debug_file : ProjectDebugFile , target_org : Organization
208
+ ) -> ProjectDebugFile | None :
209
+ try :
210
+ with atomic_transaction (using = (router .db_for_write (ProjectDebugFile ))):
211
+ target_project = _find_matching_project (
212
+ source_project_debug_file .project_id ,
213
+ target_org .id ,
214
+ )
215
+
216
+ if not target_project :
217
+ return None
218
+
219
+ return ProjectDebugFile .objects .create (
220
+ project_id = target_project .id ,
221
+ file = source_project_debug_file .file ,
222
+ checksum = source_project_debug_file .checksum ,
223
+ object_name = source_project_debug_file .object_name ,
224
+ cpu_name = source_project_debug_file .cpu_name ,
225
+ debug_id = source_project_debug_file .debug_id ,
226
+ code_id = source_project_debug_file .code_id ,
227
+ data = source_project_debug_file .data ,
228
+ date_accessed = source_project_debug_file .date_accessed ,
229
+ )
230
+ except IntegrityError as e :
231
+ sentry_sdk .capture_exception (e )
232
+ return None
233
+
234
+
235
+ def _sync_proguard_artifact_release (
236
+ source_proguard_artifact_release : ProguardArtifactRelease , target_org : Organization
237
+ ):
238
+ try :
239
+ with atomic_transaction (using = (router .db_for_write (ProguardArtifactRelease ))):
240
+ target_project = _find_matching_project (
241
+ source_proguard_artifact_release .project_id ,
242
+ target_org .id ,
243
+ )
244
+
245
+ if not target_project :
246
+ return
247
+
248
+ # project_debug_file _should_ already be synced, but we'll make sure it is
249
+ project_debug_file = ProjectDebugFile .objects .filter (
250
+ project_id = target_project .id ,
251
+ debug_id = source_proguard_artifact_release .project_debug_file .debug_id ,
252
+ ).first ()
253
+
254
+ if not project_debug_file :
255
+ project_debug_file = _sync_project_debug_file (
256
+ source_proguard_artifact_release .project_debug_file , target_org
257
+ )
258
+
259
+ if not project_debug_file :
260
+ # we require a project debug file
261
+ return
262
+
263
+ ProguardArtifactRelease .objects .create (
264
+ organization_id = target_org .id ,
265
+ project_id = target_project .id ,
266
+ release_name = source_proguard_artifact_release .release_name ,
267
+ proguard_uuid = source_proguard_artifact_release .proguard_uuid ,
268
+ project_debug_file = project_debug_file ,
269
+ date_added = source_proguard_artifact_release .date_added ,
270
+ )
271
+ except IntegrityError as e :
272
+ sentry_sdk .capture_exception (e )
273
+
274
+
147
275
def _find_matching_project (project_id , organization_id ):
148
276
try :
149
277
source_project = Project .objects .get (id = project_id )
0 commit comments