Allow disabling fetchRelated for compat list

This commit is contained in:
Mustafa Gezen 2023-02-22 02:35:08 +01:00
parent dc804bc06c
commit c267eabf78
Signed by untrusted user who does not match committer: mustafa
GPG Key ID: DCDF010D946438C1

View File

@ -80,6 +80,7 @@ class AdvisoryResponse(BaseModel):
def v3_advisory_to_v2( def v3_advisory_to_v2(
advisory: Advisory, advisory: Advisory,
include_rpms=True, include_rpms=True,
fetch_related=True,
) -> Advisory_Pydantic_V2: ) -> Advisory_Pydantic_V2:
kind = "TYPE_SECURITY" kind = "TYPE_SECURITY"
if advisory.kind == "Bug Fix": if advisory.kind == "Bug Fix":
@ -95,32 +96,34 @@ def v3_advisory_to_v2(
) )
cves = [] cves = []
for cve in advisory.cves: if fetch_related:
cves.append( for cve in advisory.cves:
Advisory_Pydantic_V2_CVE( cves.append(
name=cve.cve, Advisory_Pydantic_V2_CVE(
cvss3ScoringVector=cve.cvss3_scoring_vector, name=cve.cve,
cvss3BaseScore=cve.cvss3_base_score, cvss3ScoringVector=cve.cvss3_scoring_vector,
cwe=cve.cwe, cvss3BaseScore=cve.cvss3_base_score,
sourceBy="MITRE", cwe=cve.cwe,
sourceLink= sourceBy="MITRE",
f"https://cve.mitre.org/cgi-bin/cvename.cgi?name={cve.cve}", sourceLink=
f"https://cve.mitre.org/cgi-bin/cvename.cgi?name={cve.cve}",
)
) )
)
fixes = [] fixes = []
for fix in advisory.fixes: if fetch_related:
fixes.append( for fix in advisory.fixes:
Advisory_Pydantic_V2_Fix( fixes.append(
ticket=fix.ticket_id, Advisory_Pydantic_V2_Fix(
sourceBy="Red Hat", ticket=fix.ticket_id,
sourceLink=fix.source, sourceBy="Red Hat",
description=fix.description, sourceLink=fix.source,
description=fix.description,
)
) )
)
rpms = {} rpms = {}
if include_rpms: if include_rpms and fetch_related:
for pkg in advisory.packages: for pkg in advisory.packages:
name = f"{pkg.supported_product.name} {pkg.supported_products_rh_mirror.match_major_version}" name = f"{pkg.supported_product.name} {pkg.supported_products_rh_mirror.match_major_version}"
if name not in rpms: if name not in rpms:
@ -165,6 +168,7 @@ async def fetch_advisories_compat(
keyword: Optional[str] = None, keyword: Optional[str] = None,
severity: Optional[str] = None, severity: Optional[str] = None,
kind: Optional[str] = None, kind: Optional[str] = None,
fetch_related: bool = True,
): ):
before = None before = None
after = None after = None
@ -208,7 +212,7 @@ async def fetch_advisories_compat(
synopsis, synopsis,
q_severity, q_severity,
q_kind, q_kind,
fetch_related=True, fetch_related=fetch_related,
) )
@ -229,6 +233,7 @@ async def list_advisories_compat_v2(
keyword: str = Query(default=None, alias="filters.keyword"), keyword: str = Query(default=None, alias="filters.keyword"),
severity: str = Query(default=None, alias="filters.severity"), severity: str = Query(default=None, alias="filters.severity"),
kind: str = Query(default=None, alias="filters.type"), kind: str = Query(default=None, alias="filters.type"),
fetch_related: bool = Query(default=True, alias="filters.fetchRelated"),
): ):
state = await RedHatIndexState.first() state = await RedHatIndexState.first()
@ -242,12 +247,17 @@ async def list_advisories_compat_v2(
keyword, keyword,
severity, severity,
kind, kind,
fetch_related,
) )
count = fetch_adv[0] count = fetch_adv[0]
advisories = fetch_adv[1] advisories = fetch_adv[1]
if not fetch_related:
for x in advisories:
await x.fetch_related("affected_products")
v2_advisories: list[Advisory_Pydantic_V2] = [ v2_advisories: list[Advisory_Pydantic_V2] = [
v3_advisory_to_v2(x) for x in advisories v3_advisory_to_v2(x, fetch_related=fetch_related) for x in advisories
] ]
page = create_page(v2_advisories, count, params) page = create_page(v2_advisories, count, params)
@ -281,6 +291,7 @@ async def list_advisories_compat_v2_rss(
keyword, keyword,
severity, severity,
kind, kind,
fetch_related=False,
) )
count = fetch_adv[0] count = fetch_adv[0]
advisories = fetch_adv[1] advisories = fetch_adv[1]