1818from vulnerabilities .models import AdvisoryV2
1919from vulnerabilities .models import ToDoRelatedAdvisoryV2
2020from vulnerabilities .pipelines import VulnerableCodePipeline
21- from vulnerabilities .pipes .advisory import advisories_checksum_v2
21+ from vulnerabilities .pipes .advisory import advisories_checksum
2222
2323
2424class ComputeToDo (VulnerableCodePipeline ):
@@ -36,7 +36,9 @@ def steps(cls):
3636 def compute_individual_advisory_todo (self ):
3737 """Create ToDos for missing summary, affected and fixed packages."""
3838
39- advisories = AdvisoryV2 .objects .all ()
39+ advisories = AdvisoryV2 .objects .all ().prefetch_related (
40+ "impacted_packages" ,
41+ )
4042 advisories_count = advisories .count ()
4143 advisory_relation_to_create = {}
4244 todo_to_create = []
@@ -52,7 +54,7 @@ def compute_individual_advisory_todo(self):
5254 progress_step = 1 ,
5355 )
5456 for advisory in progress .iter (advisories .iterator (chunk_size = 5000 )):
55- advisory_todo_id = advisories_checksum_v2 (advisories = advisory )
57+ advisory_todo_id = advisories_checksum (advisories = advisory )
5658 check_missing_summary (
5759 advisory = advisory ,
5860 todo_id = advisory_todo_id ,
@@ -106,9 +108,15 @@ def detect_conflicting_advisories(self):
106108 progress_step = 1 ,
107109 )
108110 for alias in progress .iter (aliases .iterator (chunk_size = 2000 )):
109- advisories = alias .advisories .exclude (
110- advisory_todos__issue_type = "MISSING_AFFECTED_AND_FIXED_BY_PACKAGES"
111- ).distinct ()
111+ advisories = (
112+ alias .advisories .exclude (
113+ advisory_todos__issue_type = "MISSING_AFFECTED_AND_FIXED_BY_PACKAGES"
114+ )
115+ .distinct ()
116+ .prefetch_related (
117+ "impacted_packages" ,
118+ )
119+ )
112120
113121 check_conflicting_affected_and_fixed_by_packages_for_alias (
114122 advisories = advisories ,
@@ -209,11 +217,11 @@ def check_conflicting_affected_and_fixed_by_packages_for_alias(
209217 Add appropriate AdvisoryToDo for conflicting affected/fixed packages.
210218
211219 Compute the comparison matrix for the given set of advisories. Iterate through each advisory
212- and compute and store fixed versions and affected versionrange for each advisory,
220+ and compute and store fixed versionsrange and affected versionrange for each advisory,
213221 keyed by purl.
214222
215223 Use the matrix to determine conflicts in affected/fixed versions for each purl. If for any purl
216- there is more than one set of fixed versions or more than one set of affected versions ,
224+ there is more than one set of fixed versionrange or more than one set of affected versionrange ,
217225 it means the advisories have conflicting opinions on the fixed or affected packages.
218226
219227 Example of comparison matrix:
@@ -224,7 +232,7 @@ def check_conflicting_affected_and_fixed_by_packages_for_alias(
224232 Advisory2: frozenset(...),
225233 },
226234 "fixed": {
227- Advisory1: frozenset(Version1, Version2 ),
235+ Advisory1: frozenset(VersionRange1, VersionRange2 ),
228236 Advisory2: frozenset(...),
229237 },
230238 },
@@ -245,21 +253,19 @@ def check_conflicting_affected_and_fixed_by_packages_for_alias(
245253 for advisory in advisories :
246254 advisory_id = advisory .unique_content_id
247255 for impacted in advisory .impacted_packages .all () or []:
248- if not impacted :
249- continue
250- affected_purl = str (impacted .base_purl )
256+ affected_purl = impacted .base_purl
251257
252258 initialize_sub_matrix (
253259 matrix = matrix ,
254260 affected_purl = affected_purl ,
255261 advisory = advisory ,
256262 )
257263
258- if fixed_version := impacted .fixed_vers :
259- matrix [affected_purl ]["fixed" ][advisory_id ].add (str ( fixed_version ) )
264+ if fixed_version_range := impacted .fixed_vers :
265+ matrix [affected_purl ]["fixed" ][advisory_id ].add (fixed_version_range )
260266
261- if impacted .affecting_vers :
262- matrix [affected_purl ]["affected" ][advisory_id ].add (str ( impacted . affecting_vers ) )
267+ if affecting_version_range := impacted .affecting_vers :
268+ matrix [affected_purl ]["affected" ][advisory_id ].add (affecting_version_range )
263269
264270 has_conflicting_affected_packages = False
265271 has_conflicting_fixed_package = False
@@ -269,17 +275,17 @@ def check_conflicting_affected_and_fixed_by_packages_for_alias(
269275 impacted = board .get ("affected" , {}).values ()
270276
271277 unique_set_of_affected_vers = {frozenset (vers ) for vers in impacted }
272- unique_set_of_fixed_versions = {frozenset (versions ) for versions in fixed }
278+ unique_set_of_fixed_vers = {frozenset (vers ) for vers in fixed }
273279
274280 if len (unique_set_of_affected_vers ) > 1 :
275281 has_conflicting_affected_packages = True
276282 messages .append (
277283 f"{ cve } : { purl } with conflicting affected versions { unique_set_of_affected_vers } "
278284 )
279- if len (unique_set_of_fixed_versions ) > 1 :
285+ if len (unique_set_of_fixed_vers ) > 1 :
280286 has_conflicting_fixed_package = True
281287 messages .append (
282- f"{ cve } : { purl } with conflicting fixed version { unique_set_of_fixed_versions } "
288+ f"{ cve } : { purl } with conflicting fixed version { unique_set_of_fixed_vers } "
283289 )
284290
285291 if not has_conflicting_affected_packages and not has_conflicting_fixed_package :
@@ -296,7 +302,7 @@ def check_conflicting_affected_and_fixed_by_packages_for_alias(
296302 "Conflict matrix" : matrix ,
297303 }
298304
299- todo_id = advisories_checksum_v2 (advisories )
305+ todo_id = advisories_checksum (advisories )
300306 todo = AdvisoryToDoV2 (
301307 related_advisories_id = todo_id ,
302308 issue_type = issue_type ,
0 commit comments