Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Generate snippet message only in case of bad packages are found #236

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/codegate/pipeline/extract_snippets/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ async def _snippet_comment(self, snippet: CodeSnippet, secrets: PipelineSensitiv
base_url=secrets.api_base,
)

# Check if any of the snippet libraries is a bad package
storage_engine = StorageEngine()
libobjects = await storage_engine.search_by_property("name", snippet.libraries)
logger.info(f"Found {len(libobjects)} libraries in the storage engine")

libraries_text = ""
# If no bad packages are found, just return empty comment
if len(libobjects) == 0:
return ""

# Otherwise, generate codegate warning message
warnings = []

# Use snippet.libraries to generate a CSV list of libraries
if snippet.libraries:
libraries_text = ", ".join([f"`{lib}`" for lib in snippet.libraries])
# Use libobjects to generate a CSV list of bad libraries
libobjects_text = ", ".join([f"""`{lib.properties["name"]}`""" for lib in libobjects])

for lib in libobjects:
lib_name = lib.properties["name"]
Expand All @@ -70,12 +74,10 @@ async def _snippet_comment(self, snippet: CodeSnippet, secrets: PipelineSensitiv
f"- More information: [{lib_url}]({lib_url})\n"
)

comment = ""
if libraries_text != "":
comment += f"\n\nCodegate detected the following libraries: {libraries_text}\n"

if warnings:
comment += "\n### 🚨 Warnings\n" + "\n".join(warnings) + "\n"
# Add a codegate warning for the bad packages found in the snippet
comment = f"\n\nWarning: CodeGate detected one or more potentially malicious or \
archived packages: {libobjects_text}\n"
comment += "\n### 🚨 Warnings\n" + "\n".join(warnings) + "\n"

return comment

Expand Down
Loading