Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use full match in homeassistant backup excludes #5597

Merged
merged 1 commit into from
Feb 3, 2025
Merged

Conversation

mdegat01
Copy link
Contributor

@mdegat01 mdegat01 commented Feb 1, 2025

Proposed change

Use full_match for paths to exclude from Home Assistant backups. This prevents accidental exclusion of things like user config subfolders that happened to be called tts or backups.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to cli pull request:
  • Link to client library pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (ruff format supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints or add-on configuration are added/changed:

Summary by CodeRabbit

  • New Features

    • Enhanced backup exclusion criteria for Home Assistant module
    • Added more specific file matching during backup process
  • Tests

    • Improved test coverage for backup exclusion logic
    • Added new parameterized test for file exclusion scenarios
  • Bug Fixes

    • Updated file matching mechanism to prevent unintended file inclusions during backup

Sorry, something went wrong.

@mdegat01 mdegat01 added the bugfix A bug fix label Feb 1, 2025
@mdegat01 mdegat01 requested a review from agners February 1, 2025 05:30
Copy link
Contributor

coderabbitai bot commented Feb 1, 2025

📝 Walkthrough

Walkthrough

The changes focus on refining the backup exclusion mechanism for Home Assistant in the supervisor module. The modifications update the file exclusion logic in the backup process, adding new exclusion patterns like "**/__pycache__/*", "*.log", and ".DS_Store". The matching method for exclusions has been modified to use full_match instead of match, providing more precise file filtering during backups. A new test function is also introduced to validate the backup exclusion behavior across different scenarios.

Changes

File Change Summary
supervisor/homeassistant/module.py - Updated HOMEASSISTANT_BACKUP_EXCLUDE list with new exclusion patterns
- Modified is_excluded_by_filter method to use path.full_match() for more precise path matching
tests/homeassistant/test_module.py - Added new imports for logging and backup-related types
- Updated test_write_pulse_error method signature
- Added new test_backup_excludes function to test backup exclusion logic

Sequence Diagram

Loading
sequenceDiagram
    participant Backup as Backup Process
    participant Excluder as Exclusion Filter
    participant File as File System

    Backup->>Excluder: Check file for exclusion
    Excluder->>File: Apply full_match exclusion criteria
    File-->>Excluder: Return match result
    Excluder-->>Backup: Determine whether to include/exclude file

The sequence diagram illustrates the updated backup exclusion process, showing how files are now checked against exclusion criteria using a more precise full_match method before being included or excluded from the backup.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sorry, something went wrong.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/homeassistant/test_module.py (1)

150-181: Consider adding assertions for backup content.

While the test verifies that files are correctly logged as excluded, it would be valuable to also assert that excluded files are actually not present in the backup archive.

Here's how you could enhance the test:

 async def test_backup_excludes(
     coresys: CoreSys,
     caplog: pytest.LogCaptureFixture,
     filename: str,
     exclude_db: bool,
     expect_excluded: bool,
     subfolder: str | None,
 ):
     """Test excludes in backup."""
     parent = coresys.config.path_homeassistant
     if subfolder:
         test_path = PurePath(subfolder, filename)
         parent = coresys.config.path_homeassistant / subfolder
         parent.mkdir(parents=True)
     else:
         test_path = PurePath(filename)

     (parent / filename).touch()

     backup = Backup(coresys, coresys.config.path_backup / "test.tar", "test", None)
     backup.new("test", utcnow().isoformat(), BackupType.PARTIAL)
     async with backup.create():
         with (
             patch.object(HomeAssistant, "begin_backup"),
             patch.object(HomeAssistant, "end_backup"),
             caplog.at_level(logging.DEBUG, logger="supervisor.homeassistant.module"),
         ):
             await backup.store_homeassistant(exclude_database=exclude_db)

     assert (
         f"Ignoring data/{test_path.as_posix()} because of " in caplog.text
     ) is expect_excluded
+
+    # Verify file presence/absence in the backup
+    with tarfile.open(backup.tarfile, "r:") as tar:
+        file_list = tar.getnames()
+        backup_path = f"data/{test_path.as_posix()}"
+        assert (backup_path in file_list) is not expect_excluded
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9491b1f and 383295e.

📒 Files selected for processing (2)
  • supervisor/homeassistant/module.py (2 hunks)
  • tests/homeassistant/test_module.py (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Build armv7 supervisor
  • GitHub Check: Build armhf supervisor
  • GitHub Check: Build aarch64 supervisor
  • GitHub Check: Run tests Python 3.13.1
🔇 Additional comments (4)
tests/homeassistant/test_module.py (2)

30-32: LGTM! Good practice adding explicit encoding.

Adding explicit UTF-8 encoding when opening files is a good practice to ensure consistent behavior across different platforms.


120-147: Well-structured test cases covering various backup exclusion scenarios.

The parameterized test cases thoroughly cover:

  • Log files with different extensions
  • Database files with and without exclusion flag
  • Files in backup and tts directories
  • Files in __pycache__ directories
  • .DS_Store files
  • Files in subfolders vs root directory

This provides good coverage of the backup exclusion logic.

supervisor/homeassistant/module.py (2)

63-71: LGTM! Improved backup exclusion patterns.

The updated patterns provide better coverage:

  • Using **/__pycache__/* ensures all pycache directories are excluded
  • Adding **/.DS_Store helps clean up macOS metadata files
  • Adding *.log catches all log files
  • Explicit paths for backup and tts directories prevent accidental exclusions

425-425: Good use of full_match for precise path matching.

Using full_match instead of match ensures that paths are matched exactly, preventing accidental exclusions of similarly named paths. The "data/" prefix correctly aligns with the backup archive structure.

Comment on lines +70 to +71
"backups/*.tar",
"tmp_backups/*.tar",
Copy link
Member

Choose a reason for hiding this comment

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

Technically those two are really only required for Core installations, since in Supervisor we store backups elsewhere. However, they don't really hurt. And maybe if someone just copied the /config directory, it might even be helpful 🤷‍♂️

@agners agners merged commit 4c04f36 into main Feb 3, 2025
21 checks passed
@agners agners deleted the use-full-match-exclude branch February 3, 2025 12:47
@github-actions github-actions bot locked and limited conversation to collaborators Feb 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants