Skip to content

Commit

Permalink
fix: staging error in GitHubConnector (#529)
Browse files Browse the repository at this point in the history
* fix: staging error in GitHub connector

Fixes an issue when calling stage on a github connector. We were calling
_checkout_revision before cloining/downloading the repo which caused an
error when trying to interact with the local directory.

The fix is simple, move the call to _checkout_revision to after the
download step.

* add test case covering the bug

* add another assertion for better validation of staging logic
  • Loading branch information
hholb committed Sep 5, 2024
1 parent 4256e17 commit bb8d800
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
9 changes: 5 additions & 4 deletions garden_ai/model_connectors/model_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ def stage(self) -> str:
ConnectorStagingError: If something goes wrong during staging.
"""
try:
self._checkout_revision()

# pull from the repo if we have already downloaded it.
if self._pull_if_downloaded():
return str(self.local_dir)
model_dir = str(self.local_dir)
else:
# otherwise download the repo
return self._download()
model_dir = self._download()

# ensure the correct commit is checked out
self._checkout_revision()
return model_dir
except Exception as e:
raise ConnectorStagingError(str(self.repo_url), None) from e

Expand Down
33 changes: 32 additions & 1 deletion tests/model_connectors/test_github_conn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import base64
from pathlib import Path
import pytest

from garden_ai.model_connectors import GitHubConnector
from garden_ai.model_connectors import GitHubConnector, create_connector
from garden_ai.model_connectors.exceptions import (
ConnectorLFSError,
ConnectorInvalidRepoIdError,
Expand Down Expand Up @@ -141,3 +142,33 @@ def test_init_raises_on_non_github_url(
GitHubConnector(
repo_url="https://some.repo.example.com",
)


@pytest.mark.integration
def test_stage_works_on_empty_local_dir(
tmp_path,
):
"""This tests calling `stage` on a GitHubConnector when the local directory is empty.
This test may fail if there is no internet access or github.com is unreachable.
This was created as part of the resolution of this issue:
https://github.com/Garden-AI/garden/issues/521
"""
c = create_connector(
"https://github.com/WillEngler/model-perovskite-ASR", local_dir=tmp_path
)
assert isinstance(c, GitHubConnector)

# This might still throw errors if the network is down or github is unreachable
model_dir = c.stage()

# Make sure we have cloned the repo
git_dir = Path(model_dir) / ".git"
assert git_dir.exists()

# Ensure we have checked out the correct revision
head = git_dir / "HEAD"
with open(head, "r") as f:
head_rev = f.readline().strip()
assert head_rev == c.revision

0 comments on commit bb8d800

Please sign in to comment.