@@ -153,12 +153,23 @@ def process_advisories(data_source: DataSource) -> None:
153153 existing_ref = get_vuln_pkg_refs (vuln , pkg )
154154 if not existing_ref :
155155 bulk_create_vuln_pkg_refs .add (pkg_vuln_ref )
156+ # A vulnerability-package relationship does not exist already
157+ # if either the vulnerability or the package is just created.
156158
157159 else :
158- # This handles conflicts between existing data and obtained data
159- if existing_ref [0 ].is_vulnerable != pkg_vuln_ref .is_vulnerable :
160- handle_conflicts ([existing_ref [0 ], pkg_vuln_ref .to_model_object ()])
161- existing_ref .delete ()
160+ # insert only if it there is no existing vulnerability-package relationship. # nopep8
161+ existing_ref = get_vuln_pkg_refs (vuln , pkg )
162+ if not existing_ref :
163+ bulk_create_vuln_pkg_refs .add (pkg_vuln_ref )
164+
165+ else :
166+ # This handles conflicts between existing data and obtained data
167+ if existing_ref [0 ].is_vulnerable != pkg_vuln_ref .is_vulnerable :
168+ handle_conflicts (
169+ [existing_ref [0 ], pkg_vuln_ref .to_model_object ()]
170+ )
171+ existing_ref .delete ()
172+
162173 except Exception :
163174 # TODO: store error but continue
164175 logger .error (
@@ -223,26 +234,13 @@ def _get_or_create_vulnerability(
223234 advisory : Advisory ,
224235) -> Tuple [models .Vulnerability , bool ]:
225236
226- if advisory .cve_id :
227- query_kwargs = {"cve_id" : advisory .cve_id }
228- elif advisory .summary :
229- query_kwargs = {"summary" : advisory .summary }
230- else :
231- return models .Vulnerability .objects .create (), True
232-
233- try :
234- vuln , created = models .Vulnerability .objects .get_or_create (** query_kwargs )
235- # Eventually we only want to keep summary from NVD and ignore other descriptions.
236- if advisory .summary and vuln .summary != advisory .summary :
237- vuln .summary = advisory .summary
238- vuln .save ()
239- return vuln , created
240-
241- except Exception :
242- logger .error (
243- f"Failed to _get_or_create_vulnerability: { query_kwargs !r} :\n " + traceback .format_exc ()
244- )
245- raise
237+ vuln , created = models .Vulnerability .objects .get_or_create (vulnerability_id = advisory .vulnerability_id ) # nopep8
238+ # Eventually we only want to keep summary from NVD and ignore other descriptions.
239+ if advisory .summary and vuln .summary != advisory .summary :
240+ vuln .summary = advisory .summary
241+ vuln .save ()
242+
243+ return vuln , created
246244
247245
248246def _get_or_create_package (p : PackageURL ) -> Tuple [models .Package , bool ]:
0 commit comments