Skip to content

Commit 25d753d

Browse files
committed
Enhance error handling and deduplication logic in Cheatsheets parser
1 parent c1e712e commit 25d753d

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

application/utils/external_project_parsers/parsers/cheatsheets_parser.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ def parse(self, cache: db.Node_collection, ph: prompt_client.PromptHandler):
5151
c_repo = "https://github.com/OWASP/CheatSheetSeries.git"
5252
cheatsheets_path = "cheatsheets/"
5353
cheatsheets = []
54+
repo = None
5455
try:
5556
repo = git.clone(c_repo, sparse_paths=["cheatsheets"], sparse_cone=True)
57+
except Exception as exc:
58+
self.logger.warning(
59+
"Unable to clone OWASP CheatSheetSeries, continuing with supplemental cheat sheets only: %s",
60+
exc,
61+
)
62+
if repo:
5663
cheatsheets = self.register_cheatsheets(
5764
repo=repo,
5865
cache=cache,
5966
cheatsheets_path=cheatsheets_path,
6067
repo_path=c_repo,
6168
)
62-
except Exception as exc:
63-
self.logger.warning(
64-
"Unable to clone OWASP CheatSheetSeries, continuing with supplemental cheat sheets only: %s",
65-
exc,
66-
)
6769
cheatsheets.extend(self.register_supplemental_cheatsheets(cache=cache))
6870
cheatsheets = self.deduplicate_entries(cheatsheets)
6971
results = {self.name: cheatsheets}
@@ -113,6 +115,7 @@ def register_supplemental_cheatsheets(self, cache: db.Node_collection):
113115
hyperlink=entry["hyperlink"],
114116
tags=[],
115117
)
118+
add_link_failures = False
116119
for cre_id in entry.get("cre_ids", []):
117120
cres = cache.get_CREs(external_id=cre_id)
118121
for cre in cres:
@@ -123,14 +126,31 @@ def register_supplemental_cheatsheets(self, cache: db.Node_collection):
123126
ltype=defs.LinkTypes.AutomaticallyLinkedTo,
124127
)
125128
)
126-
except Exception:
127-
continue
128-
if cs.links:
129+
except Exception as exc:
130+
self.logger.warning(
131+
"Failed to add link for cre_id %s to cheatsheet %s: %s",
132+
cre_id,
133+
entry.get("section", "<unknown>"),
134+
exc,
135+
)
136+
add_link_failures = True
137+
if cs.links and not add_link_failures:
129138
standard_entries.append(cs)
130139
return standard_entries
131140

132141
def deduplicate_entries(self, entries: List[defs.Standard]) -> List[defs.Standard]:
133142
deduped = {}
134143
for entry in entries:
135-
deduped[(entry.section, entry.hyperlink)] = entry
144+
key = (entry.section, entry.hyperlink)
145+
if key in deduped:
146+
# Merge duplicates: union links into existing entry
147+
existing_entry = deduped[key]
148+
existing_link_ids = {link.document.id for link in existing_entry.links}
149+
for link in entry.links:
150+
if link.document.id not in existing_link_ids:
151+
existing_entry.add_link(link)
152+
existing_link_ids.add(link.document.id)
153+
else:
154+
# First occurrence: store the entry
155+
deduped[key] = entry
136156
return list(deduped.values())

0 commit comments

Comments
 (0)