20
20
LEVEL_ERROR ,
21
21
Diff ,
22
22
Issue ,
23
+ IssueLink ,
23
24
Repository ,
24
25
Revision ,
25
26
)
@@ -121,19 +122,31 @@ def get_queryset(self):
121
122
# Because of the perf. hit filter issues that are not older than today - 3 months.
122
123
.filter (created__gte = date .today () - timedelta (days = 90 ))
123
124
.prefetch_related (
124
- "issues" ,
125
+ Prefetch (
126
+ "issue_links" ,
127
+ queryset = IssueLink .objects .select_related ("issue" ),
128
+ ),
125
129
"revision" ,
126
130
"revision__base_repository" ,
127
131
"revision__head_repository" ,
128
132
"repository" ,
129
133
)
130
- .annotate (nb_issues = Count ("issues" ))
131
- .annotate (nb_errors = Count ("issues" , filter = Q (issues__level = "error" )))
132
- .annotate (nb_warnings = Count ("issues" , filter = Q (issues__level = "warning" )))
134
+ .annotate (nb_issues = Count ("issue_links" ))
135
+ .annotate (
136
+ nb_errors = Count (
137
+ "issue_links" , filter = Q (issue_links__issue__level = LEVEL_ERROR )
138
+ )
139
+ )
140
+ .annotate (
141
+ nb_warnings = Count (
142
+ "issue_links" , filter = Q (issue_links__issue__level = "warning" )
143
+ )
144
+ )
133
145
.annotate (
134
146
nb_issues_publishable = Count (
135
- "issues" ,
136
- filter = Q (issues__in_patch = True ) | Q (issues__level = LEVEL_ERROR ),
147
+ "issue_links" ,
148
+ filter = Q (issue_links__in_patch = True )
149
+ | Q (issue_links__issue__level = LEVEL_ERROR ),
137
150
)
138
151
)
139
152
.order_by ("-id" )
@@ -190,9 +203,25 @@ def get_queryset(self):
190
203
if not self .kwargs .get ("diff_id" ):
191
204
return Issue .objects .none ()
192
205
diff = get_object_or_404 (Diff , id = self .kwargs ["diff_id" ])
193
- # No multiple revision should be linked to a single diff
194
- # but we use the distinct clause to match the DB state.
195
- return Issue .objects .filter (diffs = diff ).distinct ()
206
+ return (
207
+ Issue .objects .filter (issue_links__diff = diff )
208
+ .annotate (publishable = Q (issue_links__in_patch = True ) & Q (level = LEVEL_ERROR ))
209
+ .values (
210
+ "id" ,
211
+ "hash" ,
212
+ "analyzer" ,
213
+ "analyzer_check" ,
214
+ "path" ,
215
+ "issue_links__line" ,
216
+ "issue_links__nb_lines" ,
217
+ "issue_links__char" ,
218
+ "level" ,
219
+ "message" ,
220
+ "publishable" ,
221
+ "issue_links__in_patch" ,
222
+ "issue_links__new_for_revision" ,
223
+ )
224
+ )
196
225
197
226
198
227
class IssueBulkCreate (generics .CreateAPIView ):
@@ -212,7 +241,7 @@ def get_serializer_context(self):
212
241
return context
213
242
214
243
215
- class IssueCheckDetails (CachedView , generics .ListAPIView ):
244
+ class IssueCheckDetails (generics .ListAPIView ):
216
245
"""
217
246
List all the issues found by a specific analyzer check in a repository
218
247
"""
@@ -223,24 +252,27 @@ def get_queryset(self):
223
252
repo = self .kwargs ["repository" ]
224
253
225
254
queryset = (
226
- Issue .objects .filter (revisions__head_repository__slug = repo )
255
+ Issue .objects .filter (issue_links__revision__head_repository__slug = repo )
227
256
.filter (analyzer = self .kwargs ["analyzer" ])
228
257
.filter (analyzer_check = self .kwargs ["check" ])
258
+ .annotate (publishable = Q (issue_links__in_patch = True ) & Q (level = LEVEL_ERROR ))
229
259
.prefetch_related (
230
- "diffs__repository " ,
260
+ "issue_links__diff__repository " ,
231
261
Prefetch (
232
- "diffs__revision " ,
262
+ "issue_links__diff__revision " ,
233
263
queryset = Revision .objects .select_related (
234
264
"base_repository" , "head_repository"
235
265
),
236
266
),
237
267
)
268
+ # List of diffs for each link of this issue
269
+ .prefetch_related ("diffs" )
238
270
.order_by ("-created" )
239
271
)
240
272
241
273
# Display only publishable issues by default
242
274
publishable = self .request .query_params .get ("publishable" , "true" ).lower ()
243
- _filter = Q (in_patch = True ) | Q (level = LEVEL_ERROR )
275
+ _filter = Q (issue_links__in_patch = True ) | Q (level = LEVEL_ERROR )
244
276
if publishable == "true" :
245
277
queryset = queryset .filter (_filter )
246
278
elif publishable == "false" :
@@ -271,14 +303,22 @@ class IssueCheckStats(CachedView, generics.ListAPIView):
271
303
def get_queryset (self ):
272
304
queryset = (
273
305
Issue .objects .values (
274
- "revisions__head_repository__slug" , "analyzer" , "analyzer_check"
306
+ "issue_links__revision__head_repository__slug" ,
307
+ "analyzer" ,
308
+ "analyzer_check" ,
275
309
)
276
310
# We want to count distinct issues because they can be referenced on multiple diffs
277
311
.annotate (total = Count ("id" , distinct = True ))
278
312
.annotate (
279
- publishable = Count ("id" , filter = Q (in_patch = True ) | Q (level = LEVEL_ERROR ))
313
+ publishable = Count (
314
+ "id" , filter = Q (issue_links__in_patch = True ) | Q (level = LEVEL_ERROR )
315
+ )
316
+ )
317
+ .distinct (
318
+ "issue_links__revision__head_repository__slug" ,
319
+ "analyzer" ,
320
+ "analyzer_check" ,
280
321
)
281
- .distinct ("revisions__head_repository__slug" , "analyzer" , "analyzer_check" )
282
322
)
283
323
284
324
# Filter issues by date
@@ -292,10 +332,21 @@ def get_queryset(self):
292
332
# Because of the perf. hit filter, issues that are not older than today - 3 months.
293
333
since = date .today () - timedelta (days = 90 )
294
334
295
- queryset = queryset .filter (revisions__created__gte = since ).distinct ()
335
+ queryset = queryset .filter (issue_links__revision__created__gte = since ).distinct ()
296
336
297
337
return queryset .order_by (
298
- "-total" , "revisions__head_repository__slug" , "analyzer" , "analyzer_check"
338
+ "-total" ,
339
+ "issue_links__revision__head_repository__slug" ,
340
+ "analyzer" ,
341
+ "analyzer_check" ,
342
+ ).values (
343
+ "issue_links__revision__head_repository__slug" ,
344
+ "analyzer" ,
345
+ "analyzer_check" ,
346
+ "total" ,
347
+ "publishable" ,
348
+ "issue_links__in_patch" ,
349
+ "issue_links__new_for_revision" ,
299
350
)
300
351
301
352
0 commit comments