Skip to content

Commit 96bca57

Browse files
fix: test_output_json2 failures in test_output_engine.py (#4903)
1 parent 9be9127 commit 96bca57

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

cve_bin_tool/output_engine/json_output.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import IO
99

1010
from cve_bin_tool.cvedb import CVEDB
11+
from cve_bin_tool.log import LOGGER
1112
from cve_bin_tool.util import CVEData, ProductInfo, VersionInfo
1213
from cve_bin_tool.version import VERSION
1314

@@ -45,20 +46,50 @@ def db_entries_count():
4546
"""
4647
Retrieves the count of CVE entries from the database grouped by data source.
4748
49+
The function fetches all data sources from the database and ensures that
50+
schema-required sources (NVD, OSV, GAD, REDHAT) are always included in the result,
51+
even if they don't exist in the database yet, while also including all other
52+
sources with counts initialized to 0.
53+
4854
Returns:
4955
dict: A dictionary containing the count of CVE entries for each data source.
5056
"""
5157
instance = CVEDB()
5258
cursor = instance.db_open_and_get_cursor()
53-
cve_entries_check = "SELECT data_source, COUNT(*) as number FROM cve_severity GROUP BY data_source ORDER BY number DESC"
54-
cursor.execute(cve_entries_check)
5559
data_entries = {}
56-
rows = cursor.fetchall()
57-
for row in rows:
58-
source = row[0]
59-
entries = row[1]
60-
data_entries[source] = entries
61-
instance.db_close()
60+
61+
# Always include the schema-required sources
62+
required_sources = ["NVD", "OSV", "GAD", "REDHAT"]
63+
for source in required_sources:
64+
data_entries[source] = 0
65+
66+
try:
67+
# Get all possible sources from the database
68+
sources_query = "SELECT DISTINCT data_source FROM cve_severity"
69+
cursor.execute(sources_query)
70+
all_sources = [row[0] for row in cursor.fetchall()]
71+
72+
# Initialize counts to 0 for all sources not in required_sources
73+
for source in all_sources:
74+
if source not in data_entries:
75+
data_entries[source] = 0
76+
77+
# Get actual counts
78+
cve_entries_check = (
79+
"SELECT data_source, COUNT(*) as number FROM cve_severity "
80+
"GROUP BY data_source ORDER BY number DESC"
81+
)
82+
cursor.execute(cve_entries_check)
83+
rows = cursor.fetchall()
84+
for row in rows:
85+
source = row[0]
86+
entries = row[1]
87+
data_entries[source] = entries
88+
89+
except Exception as e:
90+
LOGGER.error(f"Error in db_entries_count or cve_severity in db: {e}")
91+
finally:
92+
instance.db_close()
6293
return data_entries
6394

6495

0 commit comments

Comments
 (0)