Skip to content

Conversation

@joaomariolago
Copy link
Collaborator

@joaomariolago joaomariolago commented Oct 29, 2025

  • Reduce code complexity of install function by spliting in multiple parts

Summary by Sourcery

Refactor the install function by extracting its substeps into private helper methods to reduce complexity and improve readability

Enhancements:

  • Extract disabling of running extensions into _disable_running_extension
  • Extract extension settings creation into _create_extension_settings
  • Extract Docker authentication preparation into _prepare_docker_auth
  • Extract Docker image pulling logic into _pull_docker_image
  • Extract clearing of remaining extension tags into _clear_remaining_tags
  • Simplify the install method by orchestrating the new helper methods

* Reduce code complexity of install function by spliting in multiple
  parts
@sourcery-ai
Copy link

sourcery-ai bot commented Oct 29, 2025

Reviewer's Guide

Refactor install method into modular helpers to streamline extension installation flow by extracting disabling, settings setup, authentication, image pulling, and cleanup steps into separate functions.

Sequence diagram for refactored install process in Extension

sequenceDiagram
    participant E as Extension
    participant S as ExtensionSettings
    participant D as DockerCtx
    participant Auth as Docker Auth
    participant Other as Other Extension Tags

    E->>E: _disable_running_extension()
    E->>E: _create_extension_settings()
    E->>E: _prepare_docker_auth()
    E->>D: _pull_docker_image(docker_auth)
    D-->>E: progress updates
    E->>E: _clear_remaining_tags()
Loading

File-Level Changes

Change Details Files
Extract disable-running-extension logic
  • Introduce _disable_running_extension helper
  • Move running extension detection and disablement into helper
  • Return optionally disabled extension
core/services/kraken/extension/extension.py
Encapsulate extension settings creation
  • Add _create_extension_settings to build and save ExtensionSettings
  • Invoke helper in install instead of inline creation
core/services/kraken/extension/extension.py
Abstract Docker auth encoding
  • Add _prepare_docker_auth to encode credentials
  • Remove inline base64 logic from install
core/services/kraken/extension/extension.py
Modularize Docker image pull
  • Introduce _pull_docker_image to handle image pull and progress streaming
  • Tag digest-based images within helper
core/services/kraken/extension/extension.py
Isolate cleanup of old tags
  • Add _clear_remaining_tags to uninstall outdated versions
  • Replace inline tag cleanup call in install
core/services/kraken/extension/extension.py
Simplify install orchestration
  • Refactor install to call new helpers sequentially
  • Remove inlined logic and reduce cyclomatic complexity
core/services/kraken/extension/extension.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

@joaomariolago joaomariolago marked this pull request as ready for review October 30, 2025 16:43
Copy link

@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 there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `core/services/kraken/extension/extension.py:173-178` </location>
<code_context>
         self._save_settings(new_extension)
+        return new_extension
+
+    def _prepare_docker_auth(self) -> Optional[str]:
+        """Prepare Docker authentication string from source auth credentials."""
+        if self.source.auth is None:
+            return None
+        docker_auth = f"{self.source.auth.username}:{self.source.auth.password}"
+        return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")
+
+    async def _pull_docker_image(self, docker_auth: Optional[str]) -> AsyncGenerator[bytes, None]:
</code_context>

<issue_to_address>
**suggestion:** Consider handling empty username or password in Docker auth preparation.

Validate that both username and password are non-empty before encoding, and raise an error if either is missing to prevent invalid Docker auth strings.

```suggestion
    def _prepare_docker_auth(self) -> Optional[str]:
        """Prepare Docker authentication string from source auth credentials."""
        if self.source.auth is None:
            return None
        username = getattr(self.source.auth, "username", None)
        password = getattr(self.source.auth, "password", None)
        if not username or not password:
            raise ValueError("Docker authentication requires both non-empty username and password.")
        docker_auth = f"{username}:{password}"
        return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")
```
</issue_to_address>

### Comment 2
<location> `core/services/kraken/extension/extension.py:206` </location>
<code_context>
+        # First we should make sure no other tag is running
+        running_ext = await self._disable_running_extension()

+        self._create_extension_settings()
         try:
             self.lock(self.unique_entry)
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Extension settings are created before acquiring the lock.

Moving the creation and saving of extension settings inside the locked section will prevent race conditions during concurrent installs.
</issue_to_address>

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.

Comment on lines +173 to +178
def _prepare_docker_auth(self) -> Optional[str]:
"""Prepare Docker authentication string from source auth credentials."""
if self.source.auth is None:
return None
docker_auth = f"{self.source.auth.username}:{self.source.auth.password}"
return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider handling empty username or password in Docker auth preparation.

Validate that both username and password are non-empty before encoding, and raise an error if either is missing to prevent invalid Docker auth strings.

Suggested change
def _prepare_docker_auth(self) -> Optional[str]:
"""Prepare Docker authentication string from source auth credentials."""
if self.source.auth is None:
return None
docker_auth = f"{self.source.auth.username}:{self.source.auth.password}"
return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")
def _prepare_docker_auth(self) -> Optional[str]:
"""Prepare Docker authentication string from source auth credentials."""
if self.source.auth is None:
return None
username = getattr(self.source.auth, "username", None)
password = getattr(self.source.auth, "password", None)
if not username or not password:
raise ValueError("Docker authentication requires both non-empty username and password.")
docker_auth = f"{username}:{password}"
return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")

# First we should make sure no other tag is running
running_ext = await self._disable_running_extension()

self._create_extension_settings()
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Extension settings are created before acquiring the lock.

Moving the creation and saving of extension settings inside the locked section will prevent race conditions during concurrent installs.

@patrickelectric patrickelectric merged commit 534a55a into bluerobotics:master Nov 5, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants