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

Updated external tool download path #608

Merged
merged 7 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions superagi/helper/tool_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,13 @@ def register_toolkits(session, organisation):
logger.info(f"Toolkits Registered Successfully for Organisation ID : {organisation.id}!")


def extract_repo_name(repo_link):
def extract_repo_name_with_username(repo_link):
# Extract the repository name from the link
# Assuming the GitHub link format: https://github.com/username/repoName
repo_name = repo_link.rsplit('/', 1)[-1]
username_repo = repo_link.rsplit('/', 2)[-2:]
user_repo_key = "_".join(username_repo)

return repo_name
return user_repo_key


def add_tool_to_json(repo_link):
Expand All @@ -262,7 +263,7 @@ def add_tool_to_json(repo_link):
tools_data = json.load(file)

# Extract the repository name from the link
repo_name = extract_repo_name(repo_link)
repo_name = extract_repo_name_with_username(repo_link)

# Add a new key-value pair to the tools object
tools_data['tools'][repo_name] = repo_link
Expand Down
2 changes: 1 addition & 1 deletion superagi/tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def download_and_extract_tools():
tools_config = load_tools_config()

for tool_name, tool_url in tools_config.items():
tool_folder = os.path.join("", "tools", tool_name)
tool_folder = os.path.join("superagi", "tools", tool_name)
if not os.path.exists(tool_folder):
os.makedirs(tool_folder)
download_tool(tool_url, tool_folder)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/helper/test_tool_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from superagi.helper.tool_helper import (
parse_github_url,
load_module_from_file,
extract_repo_name,
extract_repo_name_with_username,
add_tool_to_json, get_readme_content_from_code_link, download_tool
)

Expand Down Expand Up @@ -73,8 +73,8 @@ def test_get_readme_content_from_code_link(mock_requests_get):

def test_extract_repo_name():
repo_link = 'https://github.com/username/repo'
expected_result = 'repo'
assert extract_repo_name(repo_link) == expected_result
expected_result = 'username_repo'
assert extract_repo_name_with_username(repo_link) == expected_result


@patch('requests.get')
Expand Down
14 changes: 9 additions & 5 deletions tests/unit_tests/test_tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
from unittest.mock import Mock, patch, mock_open, MagicMock
from superagi.tool_manager import parse_github_url, download_tool, load_tools_config, download_and_extract_tools


def test_parse_github_url():
url = 'https://github.com/owner/repo'
assert parse_github_url(url) == 'owner/repo/main'


def setup_function():
os.makedirs('target_folder', exist_ok=True)


# Teardown function to remove the directory
def teardown_function():
shutil.rmtree('target_folder')


@patch('requests.get')
@patch('zipfile.ZipFile')
def test_download_tool(mock_zip, mock_get):
Expand All @@ -31,7 +35,6 @@ def test_download_tool(mock_zip, mock_get):
mock_zip.assert_called_once_with('target_folder/tool.zip', 'r')



@patch('json.load')
def test_load_tools_config(mock_json_load):
mock_json_load.return_value = {"tools": {"tool1": "url1", "tool2": "url2"}}
Expand All @@ -40,12 +43,13 @@ def test_load_tools_config(mock_json_load):
assert config == {"tool1": "url1", "tool2": "url2"}


@patch('superagi.tool_manager.download_tool')
@patch('superagi.tool_manager.load_tools_config')
def test_download_and_extract_tools(mock_load_tools_config, mock_download_tool):
@patch('superagi.tool_manager.download_tool')
def test_download_and_extract_tools(mock_download_tool, mock_load_tools_config):
mock_load_tools_config.return_value = {"tool1": "url1", "tool2": "url2"}

download_and_extract_tools()

mock_load_tools_config.assert_called_once()
mock_download_tool.assert_any_call('url1', 'tools/tool1')
mock_download_tool.assert_any_call('url2', 'tools/tool2')
mock_download_tool.assert_any_call('url1', os.path.join('superagi', 'tools', 'tool1'))
mock_download_tool.assert_any_call('url2', os.path.join('superagi', 'tools', 'tool2'))