Merge pull request #32 'Add local staging environment for PR testing' from staging-setup into main
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
"""Configuration loading utilities."""
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from nanobot.config.schema import Config
|
||||
|
||||
|
||||
def get_config_path() -> Path:
|
||||
"""Get the default configuration file path."""
|
||||
"""Get the configuration file path.
|
||||
|
||||
Checks NANOBOT_CONFIG environment variable first, otherwise defaults
|
||||
to ~/.nanobot/config.json
|
||||
"""
|
||||
env_path = os.getenv("NANOBOT_CONFIG")
|
||||
if env_path:
|
||||
return Path(env_path)
|
||||
return Path.home() / ".nanobot" / "config.json"
|
||||
|
||||
|
||||
@@ -84,4 +92,18 @@ def _migrate_config(data: dict) -> dict:
|
||||
exec_cfg = tools.get("exec", {})
|
||||
if "restrictToWorkspace" in exec_cfg and "restrictToWorkspace" not in tools:
|
||||
tools["restrictToWorkspace"] = exec_cfg.pop("restrictToWorkspace")
|
||||
|
||||
# Extract api_key from oauthCredentials if present
|
||||
providers = data.get("providers", {})
|
||||
for _, provider_config in providers.items():
|
||||
if isinstance(provider_config, dict):
|
||||
oauth_creds = provider_config.get("oauthCredentials")
|
||||
if oauth_creds and isinstance(oauth_creds, dict):
|
||||
access_token = oauth_creds.get("access_token", "")
|
||||
# Only set api_key if not already set and access_token exists
|
||||
if access_token and not provider_config.get("api_key"):
|
||||
provider_config["api_key"] = access_token
|
||||
# Clean up migrated data to avoid duplication
|
||||
del provider_config["oauthCredentials"]
|
||||
|
||||
return data
|
||||
|
||||
Executable
+34
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
# test-pr.sh - Quick PR testing script for nanobot staging
|
||||
#
|
||||
# Usage: ./test-pr.sh <pr-number> [test-message]
|
||||
# Example: ./test-pr.sh 31 "test tool use feature"
|
||||
|
||||
set -e
|
||||
|
||||
PR_NUM="$1"
|
||||
TEST_MSG="${2:-Hello, testing PR #$PR_NUM}"
|
||||
REPO_DIR="/config/workspace/nanobot-oauth-port/nanobot-fork"
|
||||
STAGING_CONFIG="/config/workspace/.nanobot-staging/config.json"
|
||||
|
||||
if [ -z "$PR_NUM" ]; then
|
||||
echo "Usage: $0 <pr-number> [test-message]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "==> Fetching PR #$PR_NUM..."
|
||||
cd "$REPO_DIR"
|
||||
git fetch wylab "+pull/$PR_NUM/head:pr-$PR_NUM"
|
||||
|
||||
echo "==> Checking out pr-$PR_NUM..."
|
||||
git checkout "pr-$PR_NUM"
|
||||
|
||||
echo "==> Installing in editable mode..."
|
||||
uv pip install -e . -q
|
||||
|
||||
echo "==> Testing with message: $TEST_MSG"
|
||||
NANOBOT_CONFIG="$STAGING_CONFIG" "$REPO_DIR/.venv/bin/nanobot" agent -m "$TEST_MSG"
|
||||
|
||||
echo ""
|
||||
echo "==> Test complete. Branch pr-$PR_NUM is still checked out."
|
||||
echo " Run 'git checkout main' to return to main branch."
|
||||
@@ -0,0 +1,142 @@
|
||||
"""Tests for config loader (get_config_path and _migrate_config)"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from nanobot.config.loader import get_config_path, _migrate_config
|
||||
|
||||
|
||||
def test_get_config_path_default():
|
||||
"""get_config_path returns ~/.nanobot/config.json by default"""
|
||||
# Ensure NANOBOT_CONFIG is not set
|
||||
env_backup = os.environ.pop("NANOBOT_CONFIG", None)
|
||||
try:
|
||||
path = get_config_path()
|
||||
assert path == Path.home() / ".nanobot" / "config.json"
|
||||
finally:
|
||||
if env_backup:
|
||||
os.environ["NANOBOT_CONFIG"] = env_backup
|
||||
|
||||
|
||||
def test_get_config_path_with_env_var():
|
||||
"""get_config_path uses NANOBOT_CONFIG env var when set"""
|
||||
custom_path = "/tmp/test-nanobot-config.json"
|
||||
env_backup = os.environ.get("NANOBOT_CONFIG")
|
||||
try:
|
||||
os.environ["NANOBOT_CONFIG"] = custom_path
|
||||
path = get_config_path()
|
||||
assert path == Path(custom_path)
|
||||
finally:
|
||||
if env_backup:
|
||||
os.environ["NANOBOT_CONFIG"] = env_backup
|
||||
else:
|
||||
os.environ.pop("NANOBOT_CONFIG", None)
|
||||
|
||||
|
||||
def test_migrate_config_with_oauth_credentials():
|
||||
"""_migrate_config extracts api_key from oauthCredentials"""
|
||||
data = {
|
||||
"providers": {
|
||||
"anthropic": {
|
||||
"oauthCredentials": {
|
||||
"access_token": "sk-ant-test-token",
|
||||
"refresh_token": "",
|
||||
"expires_at": 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = _migrate_config(data)
|
||||
|
||||
# api_key should be extracted
|
||||
assert result["providers"]["anthropic"]["api_key"] == "sk-ant-test-token"
|
||||
# oauthCredentials should be removed after migration
|
||||
assert "oauthCredentials" not in result["providers"]["anthropic"]
|
||||
|
||||
|
||||
def test_migrate_config_without_oauth_credentials():
|
||||
"""_migrate_config leaves config unchanged when no oauthCredentials"""
|
||||
data = {
|
||||
"providers": {
|
||||
"anthropic": {
|
||||
"api_key": "sk-ant-existing-key"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = _migrate_config(data)
|
||||
|
||||
# Should remain unchanged
|
||||
assert result["providers"]["anthropic"]["api_key"] == "sk-ant-existing-key"
|
||||
assert "oauthCredentials" not in result["providers"]["anthropic"]
|
||||
|
||||
|
||||
def test_migrate_config_already_migrated():
|
||||
"""_migrate_config doesn't overwrite existing api_key"""
|
||||
data = {
|
||||
"providers": {
|
||||
"anthropic": {
|
||||
"api_key": "sk-ant-existing-key",
|
||||
"oauthCredentials": {
|
||||
"access_token": "sk-ant-oauth-token",
|
||||
"refresh_token": "",
|
||||
"expires_at": 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = _migrate_config(data)
|
||||
|
||||
# Existing api_key should be preserved
|
||||
assert result["providers"]["anthropic"]["api_key"] == "sk-ant-existing-key"
|
||||
# oauthCredentials should NOT be removed (api_key already existed)
|
||||
assert "oauthCredentials" in result["providers"]["anthropic"]
|
||||
|
||||
|
||||
def test_migrate_config_empty_access_token():
|
||||
"""_migrate_config skips empty access_token"""
|
||||
data = {
|
||||
"providers": {
|
||||
"anthropic": {
|
||||
"oauthCredentials": {
|
||||
"access_token": "",
|
||||
"refresh_token": "",
|
||||
"expires_at": 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = _migrate_config(data)
|
||||
|
||||
# api_key should not be set
|
||||
assert "api_key" not in result["providers"]["anthropic"]
|
||||
# oauthCredentials should remain (no migration happened)
|
||||
assert "oauthCredentials" in result["providers"]["anthropic"]
|
||||
|
||||
|
||||
def test_migrate_config_preserves_other_fields():
|
||||
"""_migrate_config preserves other provider config fields"""
|
||||
data = {
|
||||
"providers": {
|
||||
"anthropic": {
|
||||
"oauthCredentials": {
|
||||
"access_token": "sk-ant-test-token",
|
||||
"refresh_token": "refresh-token",
|
||||
},
|
||||
"customField": "customValue",
|
||||
"anotherField": 123,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = _migrate_config(data)
|
||||
|
||||
# api_key added, oauthCredentials removed
|
||||
assert result["providers"]["anthropic"]["api_key"] == "sk-ant-test-token"
|
||||
assert "oauthCredentials" not in result["providers"]["anthropic"]
|
||||
# Other fields preserved
|
||||
assert result["providers"]["anthropic"]["customField"] == "customValue"
|
||||
assert result["providers"]["anthropic"]["anotherField"] == 123
|
||||
Reference in New Issue
Block a user