-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate plugins removed from hub before marking them stale (#1301)
* Updating processor to sanity check deletion * Adding Classifier Adapters * Adding back Zulip credentials * Fixing linting * Fixing linting for napari-hub-commons * Installing libffi7 * Updating ubuntu distros * Updating tests * Revert "Installing libffi7" This reverts commit d9e4039. * Revert "Updating ubuntu distros" This reverts commit b9b3320. * Revert "Adding back Zulip credentials" This reverts commit 6597f45. * Fix linting
- Loading branch information
Showing
6 changed files
with
143 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
from functools import cache | ||
|
||
from nhcommons.utils.adapter_helpers import GithubClientHelper | ||
|
||
|
||
REPOSITORY = "https://github.com/napari/npe2api" | ||
FILE_NAME = "public/classifiers.json" | ||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@cache | ||
def _get_recent_query_data() -> dict: | ||
github_client_helper = GithubClientHelper(REPOSITORY) | ||
data = github_client_helper.get_file(FILE_NAME, "json") | ||
if not data: | ||
raise RuntimeError(f"Unable to fetch {FILE_NAME} from {REPOSITORY}") | ||
return data | ||
|
||
|
||
def is_plugin_active(name: str, version: str) -> bool: | ||
try: | ||
recent_query_update = _get_recent_query_data() | ||
active_versions = set(recent_query_update.get("active", {}).get(name, [])) | ||
return version in active_versions | ||
except RuntimeError: | ||
LOGGER.warning(f"Returning {name} {version} is active due to RuntimeError") | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from unittest.mock import Mock, call | ||
|
||
import pytest | ||
|
||
import nhcommons.utils.adapter_helpers | ||
import plugin.classifier_adapter | ||
|
||
REPOSITORY = "https://github.com/napari/npe2api" | ||
FILE_NAME = "public/classifiers.json" | ||
|
||
|
||
class TestClassifierAdapter: | ||
@pytest.fixture(autouse=True) | ||
def clear_cache(self): | ||
plugin.classifier_adapter._get_recent_query_data.cache_clear() | ||
|
||
@pytest.fixture(autouse=True) | ||
def github_client_helper(self, monkeypatch): | ||
self._github_client_helper = Mock() | ||
self._github_client_helper.get_file.side_effect = ( | ||
lambda _, __: self._classifier_json | ||
) | ||
self._github_client_helper_call = Mock( | ||
spec=nhcommons.utils.adapter_helpers.GithubClientHelper, | ||
return_value=self._github_client_helper, | ||
) | ||
monkeypatch.setattr( | ||
plugin.classifier_adapter, | ||
"GithubClientHelper", | ||
self._github_client_helper_call, | ||
) | ||
|
||
def test_handle_valid_query_data(self): | ||
self._classifier_json = {"active": {"foo": ["1.0.0", "1.0.1", "1.0.2"]}} | ||
assert True == plugin.classifier_adapter.is_plugin_active("foo", "1.0.2") | ||
assert False == plugin.classifier_adapter.is_plugin_active("foo", "0.0.9") | ||
assert False == plugin.classifier_adapter.is_plugin_active("bar", "1.0.4") | ||
self._github_client_helper_call.assert_called_once_with(REPOSITORY) | ||
self._github_client_helper.get_file.assert_called_once_with(FILE_NAME, "json") | ||
|
||
def test_handle_invalid_query_data(self): | ||
self._classifier_json = {"inactive": []} | ||
assert False == plugin.classifier_adapter.is_plugin_active("foo", "1.0.2") | ||
assert False == plugin.classifier_adapter.is_plugin_active("bar", "1.0.2") | ||
self._github_client_helper_call.assert_called_once_with(REPOSITORY) | ||
self._github_client_helper.get_file.assert_called_once_with(FILE_NAME, "json") | ||
|
||
@pytest.mark.parametrize("init_classifier_json", [None, {}]) | ||
def test_is_plugin_live_does_not_cache_error(self, init_classifier_json): | ||
# When the classifier json is not a dict with values, _get_recent_query_data() | ||
# should throw a RuntimeError | ||
self._classifier_json = init_classifier_json | ||
assert True == plugin.classifier_adapter.is_plugin_active("foo", "1.0.2") | ||
self._classifier_json = {"active": {"foo": ["1.0.0", "1.0.1", "1.0.2"]}} | ||
assert True == plugin.classifier_adapter.is_plugin_active("foo", "1.0.2") | ||
self._github_client_helper_call.assert_has_calls( | ||
[call(REPOSITORY), call(REPOSITORY)] | ||
) | ||
self._github_client_helper.get_file.assert_has_calls( | ||
[call(FILE_NAME, "json"), call(FILE_NAME, "json")] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters