Skip to content

Conversation

@electron271
Copy link
Member

@electron271 electron271 commented Jun 4, 2025

Summary by Sourcery

Load and merge default settings from settings.yml.example into the user config to prevent crashes when settings.yml is missing keys and simplify retrieval of ALLOW_SYSADMINS_EVAL.

Bug Fixes:

  • Prevent crashes when settings.yml lacks expected keys by merging in defaults from settings.yml.example.

Enhancements:

  • Introduce merge_defaults to recursively fill missing config entries and log added keys.
  • Remove manual fallback and warning logic for ALLOW_SYSADMINS_EVAL in favor of default from the merged config.

@electron271 electron271 requested a review from Copilot June 4, 2025 22:09
@electron271 electron271 self-assigned this Jun 4, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 4, 2025

Reviewer's Guide

Auto-merges missing config keys from settings.yml.example to prevent crashes and replaces manual ALLOW_SYSADMINS_EVAL fallback with a direct merged config lookup.

Sequence Diagram for Configuration Loading and Merging

sequenceDiagram
    participant App as Application
    participant CPY as "config.py module"
    participant UserConfig as "settings.yml (user)"
    participant DefaultConfig as "settings.yml.example (default)"
    participant LoggerSvc as "Logger"

    App->>CPY: Starts (imports config.py)
    CPY->>UserConfig: Reads data (config_file.read_text())
    UserConfig-->>CPY: Returns user_data
    CPY->>DefaultConfig: Reads data (config_file_example.read_text())
    DefaultConfig-->>CPY: Returns default_data

    CPY->>CPY: Calls merge_defaults(user_data, default_data)
    activate CPY
    loop For each key, default_value in default_data
        alt key not in user_data
            Note over CPY: user_data[key] = default_value
            CPY->>LoggerSvc: logger.warning(f"Added missing config key: {key}")
        else key is in user_data AND user_data[key] is dict AND default_value is dict
            CPY->>CPY: merge_defaults(user_data[key], default_value) // Recursive call
        end
    end
    deactivate CPY

    Note over CPY: Module-level 'config' dictionary is now merged

    App->>CPY: Access Config.ALLOW_SYSADMINS_EVAL
    CPY-->>App: Returns value from merged 'config' dictionary
Loading

File-Level Changes

Change Details Files
Merge missing settings with defaults to prevent crashes at runtime
  • Load settings.yml.example alongside settings.yml
  • Define recursive merge_defaults to fill missing keys with defaults and warn
  • Invoke merge_defaults(config, config_example) before using config
tux/utils/config.py
Simplify ALLOW_SYSADMINS_EVAL retrieval to use merged config
  • Remove manual fallback logic and associated warning for ALLOW_SYSADMINS_EVAL
  • Assign ALLOW_SYSADMINS_EVAL directly from merged config
tux/utils/config.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures the application does not crash when settings.yml is missing newly introduced keys by loading defaults from settings.yml.example and auto-adding any missing keys. It also simplifies the ALLOW_SYSADMINS_EVAL lookup by relying on the patched config.

  • Load settings.yml.example and compare keys
  • Log warnings and patch missing keys in memory
  • Remove manual defaulting logic for ALLOW_SYSADMINS_EVAL
Comments suppressed due to low confidence (3)

tux/utils/config.py:39

  • [nitpick] Rename config_example to example_config or default_config to more clearly indicate its role as the default settings reference.
config_example = yaml.safe_load(config_file_example.read_text())

tux/utils/config.py:41

  • [nitpick] Add a comment explaining that this patch only updates the in-memory config and does not write changes back to settings.yml, or implement persistence if needed.
missing_keys = set(config_example.keys()) - set(config.keys())

tux/utils/config.py:41

  • Add unit tests to verify that missing keys from the example config are correctly added to the loaded config at runtime.
missing_keys = set(config_example.keys()) - set(config.keys())

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @electron271 - I've reviewed your changes - here's some feedback:

  • Consider doing a recursive (deep) merge of config_example into config so that missing nested keys are also handled, not just top-level ones.
  • Wrap the loading of settings.yml.example in a try/except block to avoid introducing a new crash if the example file is missing or malformed.
  • If you’re automatically adding missing keys, you may want to persist the updated config back to settings.yml or clearly document that the fix is in-memory only.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jun 4, 2025

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8fdf5d7
Status: ✅  Deploy successful!
Preview URL: https://d13cb682.tux-afh.pages.dev
Branch Preview URL: https://config-autogen.tux-afh.pages.dev

View logs

@electron271
Copy link
Member Author

@sourcery-ai review

@electron271 electron271 requested a review from Copilot June 4, 2025 22:24
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @electron271 - I've reviewed your changes - here's some feedback:

  • Consider lowering the log level (e.g. to debug or info) or batching warnings for added keys to avoid flooding your logs on startup.
  • Since merge_defaults mutates the config dict in place and reuses the example values directly, consider deep-copying defaults or moving the merge logic into a dedicated loader function to avoid unintended side effects.
  • Wrap the YAML loading in try/except blocks to provide clearer errors if either settings.yml or the example is missing or malformed instead of relying solely on safe_load to raise exceptions.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@electron271 electron271 merged commit d2f1951 into main Jun 4, 2025
10 checks passed
@electron271 electron271 deleted the config-autogen branch June 4, 2025 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants