Skip to content
Open
Changes from 2 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
17 changes: 17 additions & 0 deletions detection_rules/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ def import_rules_into_repo( # noqa: PLR0912, PLR0913, PLR0915
if isinstance(contents["author"], str):
contents["author"] = [contents["author"]]

# Parse created_at and updated_at to creation_date and updated_date if they exist in contents
if "created_at" in contents:
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using these try, except blocks, it would be better to match the style/formatting we use in this function by using .get(). The addition is smaller and does not need to add additional exception handling.

It could look something like this instead for this PR:

        # Parse created_at and updated_at to creation_date and updated_date if they exist in contents
        now = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
        contents["creation_date"] = datetime.strptime(
            contents.get("created_at", now), "%Y-%m-%dT%H:%M:%S.%fZ"
        ).strftime("%Y/%m/%d")
        contents["updated_date"] = datetime.strptime(contents.get("updated_at", now), "%Y-%m-%dT%H:%M:%S.%fZ").strftime(
            "%Y/%m/%d"
        )

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the help, I just updated the functions to use the .get() function using your code.

try:
contents["creation_date"] = datetime.strptime(
contents["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime("%Y/%m/%d")
except ValueError:
pass

if "updated_at" in contents:
try:
contents["updated_date"] = datetime.strptime(
contents["updated_at"], "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime("%Y/%m/%d")
except ValueError:
pass

contents.update(
update_metadata_from_file(
rule_path, {"creation_date": local_creation_date, "updated_date": local_updated_date}
Expand Down
Loading