Skip to content
Open
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
39 changes: 29 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import os
import json
import sys
from typing import Optional, Any


def load_github_event(event_path: str) -> Optional[Any]:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Type Annotation

The function uses Any type which is too generic. This reduces type safety and prevents proper static type checking for the return value.

Suggested change
def load_github_event(event_path: str) -> Optional[Any]:
def load_github_event(event_path: str) -> Optional[dict]:
Standards
  • Type Safety
  • PEP 484

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Docstring

The load_github_event function lacks a docstring. This reduces code maintainability as other developers won't understand the function's purpose and behavior without reading the implementation.

Suggested change
def load_github_event(event_path: str) -> Optional[Any]:
def load_github_event(event_path: str) -> Optional[dict]:
"""Load and parse GitHub event data from the specified JSON file path.
Args:
event_path: Path to the GitHub event JSON file
Returns:
Parsed event data as dictionary or None if loading fails
"""
try:
with open(event_path, "r") as file:
return json.load(file)
Standards
  • PEP 257
  • Documentation Completeness

try:
with open(event_path, "r") as file:
return json.load(file)
except FileNotFoundError:
print(f"❌ Event file not found: {event_path}")
except json.JSONDecodeError as e:
print(f"❌ Failed to parse JSON: {e}")
except Exception as e:
print(f"❌ Unexpected error reading event file: {e}")
return None
Comment on lines +7 to +17

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function is well-structured for loading the event data. For better adherence to command-line tool conventions, error messages should be printed to standard error (sys.stderr) rather than standard output. This allows users to separate normal output from error logs. You can do this by adding the file=sys.stderr argument to your print calls within the except blocks.

Suggested change
def load_github_event(event_path: str) -> Optional[Any]:
try:
with open(event_path, "r") as file:
return json.load(file)
except FileNotFoundError:
print(f"❌ Event file not found: {event_path}")
except json.JSONDecodeError as e:
print(f"❌ Failed to parse JSON: {e}")
except Exception as e:
print(f"❌ Unexpected error reading event file: {e}")
return None
def load_github_event(event_path: str) -> Optional[Any]:
try:
with open(event_path, "r") as file:
return json.load(file)
except FileNotFoundError:
print(f"❌ Event file not found: {event_path}", file=sys.stderr)
except json.JSONDecodeError as e:
print(f"❌ Failed to parse JSON: {e}", file=sys.stderr)
except Exception as e:
print(f"❌ Unexpected error reading event file: {e}", file=sys.stderr)
return None



def main():
github_event_name = os.getenv("GITHUB_EVENT_NAME")
github_event_path = os.getenv("GITHUB_EVENT_PATH")

print(f"Received GitHub event: {github_event_name}")
if not github_event_name:
print("⚠️ GITHUB_EVENT_NAME not set.")
else:
print(f"📦 Received GitHub event: {github_event_name}")

if not github_event_path:
print("GITHUB_EVENT_PATH not set, cannot read event data.")
return
print("GITHUB_EVENT_PATH not set. Cannot read event data.")
sys.exit(1)
Comment on lines +24 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the error handling in load_github_event, this warning and error message should also be directed to standard error (sys.stderr). This is a standard practice for command-line applications to distinguish between informational output and diagnostics.

Suggested change
if not github_event_name:
print("⚠️ GITHUB_EVENT_NAME not set.")
else:
print(f"📦 Received GitHub event: {github_event_name}")
if not github_event_path:
print("GITHUB_EVENT_PATH not set, cannot read event data.")
return
print("❌ GITHUB_EVENT_PATH not set. Cannot read event data.")
sys.exit(1)
if not github_event_name:
print("⚠️ GITHUB_EVENT_NAME not set.", file=sys.stderr)
else:
print(f"📦 Received GitHub event: {github_event_name}")
if not github_event_path:
print("❌ GITHUB_EVENT_PATH not set. Cannot read event data.", file=sys.stderr)
sys.exit(1)

Comment on lines +24 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent Error Handling

Inconsistent error handling between github_event_name and github_event_path. Missing event name only prints a warning while missing event path exits the program, creating unpredictable behavior.

Suggested change
if not github_event_name:
print("⚠️ GITHUB_EVENT_NAME not set.")
else:
print(f"📦 Received GitHub event: {github_event_name}")
if not github_event_path:
print("GITHUB_EVENT_PATH not set, cannot read event data.")
return
print("❌ GITHUB_EVENT_PATH not set. Cannot read event data.")
sys.exit(1)
if not github_event_name:
print("❌ GITHUB_EVENT_NAME not set. Cannot process event.")
sys.exit(1)
print(f"📦 Received GitHub event: {github_event_name}")
if not github_event_path:
print("❌ GITHUB_EVENT_PATH not set. Cannot read event data.")
sys.exit(1)
Standards
  • Consistent Error Handling
  • Fail Fast Principle


try:
with open(github_event_path, "r") as file:
event_data = json.load(file)
print("Event JSON Payload:")
event_data = load_github_event(github_event_path)
if event_data is not None:
print("📄 Event JSON Payload:")
print(json.dumps(event_data, indent=2))
except Exception as e:
print(f"Error reading event data: {e}")
else:
sys.exit(1)


if __name__ == "__main__":
main()
main()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Newline

The file is missing a newline at the end, which is causing the SECTION 1 text to be appended to the code. This could cause syntax errors or unexpected behavior.

Suggested change
main()
main()
Standards
  • PEP 8
  • File Format Standards