Merge pull request #4 from montevive/bug/tool-get-object-tree
Bugfix: Tool get object tree hangs and CI/CD
This commit is contained in:
46
.flake8
Normal file
46
.flake8
Normal file
@@ -0,0 +1,46 @@
|
||||
[flake8]
|
||||
max-line-length = 88
|
||||
exclude =
|
||||
.venv,
|
||||
venv,
|
||||
__pycache__,
|
||||
.git,
|
||||
build,
|
||||
dist,
|
||||
*.egg-info,
|
||||
node_modules,
|
||||
.tox,
|
||||
.pytest_cache
|
||||
ignore =
|
||||
# Line too long (handled by max-line-length)
|
||||
E501,
|
||||
# Missing docstrings (can be addressed later)
|
||||
D100, D101, D102, D103, D105, D107,
|
||||
# Docstring formatting (can be addressed later)
|
||||
D200, D205, D401,
|
||||
# Whitespace issues (auto-fixable)
|
||||
W293, W291, W292,
|
||||
# Unused imports (will be cleaned up)
|
||||
F401,
|
||||
# Unused variables (will be cleaned up)
|
||||
F841,
|
||||
# Bare except (will be improved)
|
||||
E722,
|
||||
# f-string without placeholders
|
||||
F541,
|
||||
# Comparison to True (minor issue)
|
||||
E712,
|
||||
# Continuation line formatting
|
||||
E128,
|
||||
# Blank line formatting
|
||||
E302, E306
|
||||
per-file-ignores =
|
||||
# Tests can be more lenient
|
||||
tests/*:D,E,F,W
|
||||
# CLI tools can be more lenient
|
||||
*/cli/*:D401
|
||||
# Allow unused imports in __init__.py files
|
||||
*/__init__.py:F401
|
||||
# Allow long lines in configuration files
|
||||
*/config.py:E501
|
||||
select = E,W,F
|
||||
263
.github/SETUP_CICD.md
vendored
Normal file
263
.github/SETUP_CICD.md
vendored
Normal file
@@ -0,0 +1,263 @@
|
||||
# CI/CD Setup Guide
|
||||
|
||||
This guide explains how to set up the CI/CD pipeline for automatic testing and PyPI publishing.
|
||||
|
||||
## 🚀 Quick Setup
|
||||
|
||||
### 1. PyPI API Tokens
|
||||
|
||||
You need to create API tokens for both PyPI and Test PyPI:
|
||||
|
||||
#### Create PyPI API Token
|
||||
1. Go to [PyPI Account Settings](https://pypi.org/manage/account/)
|
||||
2. Scroll to "API tokens" section
|
||||
3. Click "Add API token"
|
||||
4. Set name: `penpot-mcp-github-actions`
|
||||
5. Scope: "Entire account" (or specific to `penpot-mcp` project if it exists)
|
||||
6. Copy the token (starts with `pypi-`)
|
||||
|
||||
#### Create Test PyPI API Token
|
||||
1. Go to [Test PyPI Account Settings](https://test.pypi.org/manage/account/)
|
||||
2. Follow same steps as above
|
||||
3. Copy the token
|
||||
|
||||
### 2. GitHub Secrets Configuration
|
||||
|
||||
Add the following secrets to your GitHub repository:
|
||||
|
||||
1. Go to your GitHub repository
|
||||
2. Navigate to **Settings** → **Secrets and variables** → **Actions**
|
||||
3. Click **New repository secret** and add:
|
||||
|
||||
| Secret Name | Value | Description |
|
||||
|-------------|-------|-------------|
|
||||
| `PYPI_API_TOKEN` | `pypi-AgEIcHl...` | Your PyPI API token |
|
||||
| `TEST_PYPI_API_TOKEN` | `pypi-AgEIcHl...` | Your Test PyPI API token |
|
||||
|
||||
### 3. Enable GitHub Actions
|
||||
|
||||
1. Go to **Settings** → **Actions** → **General**
|
||||
2. Ensure "Allow all actions and reusable workflows" is selected
|
||||
3. Under "Workflow permissions":
|
||||
- Select "Read and write permissions"
|
||||
- Check "Allow GitHub Actions to create and approve pull requests"
|
||||
|
||||
## 📋 Workflow Overview
|
||||
|
||||
### CI Workflow (`.github/workflows/ci.yml`)
|
||||
|
||||
**Triggers:**
|
||||
- Pull requests to `main` or `develop` branches
|
||||
- Pushes to `main` or `develop` branches
|
||||
|
||||
**Jobs:**
|
||||
- **Test Matrix**: Tests across Python 3.10, 3.11, 3.12, 3.13
|
||||
- **Security Check**: Runs `bandit` security analysis
|
||||
- **Build Test**: Tests package building and installation
|
||||
- **Docker Test**: Tests Docker containerization
|
||||
|
||||
**Features:**
|
||||
- ✅ Cross-platform testing (Linux, macOS, Windows can be added)
|
||||
- ✅ Multiple Python version support
|
||||
- ✅ Code coverage reporting (uploads to Codecov)
|
||||
- ✅ Security vulnerability scanning
|
||||
- ✅ Package build verification
|
||||
- ✅ Docker compatibility testing
|
||||
|
||||
### CD Workflow (`.github/workflows/publish.yml`)
|
||||
|
||||
**Triggers:**
|
||||
- Pushes to `main` branch (automatic)
|
||||
- GitHub releases (manual)
|
||||
|
||||
**Auto-Publish Process:**
|
||||
1. ✅ Runs full CI test suite first
|
||||
2. ✅ Checks if version was bumped in `__init__.py`
|
||||
3. ✅ Skips publishing if version already exists on PyPI
|
||||
4. ✅ Builds and validates package
|
||||
5. ✅ Tests package installation
|
||||
6. ✅ Publishes to Test PyPI first (optional)
|
||||
7. ✅ Publishes to PyPI
|
||||
8. ✅ Creates GitHub release automatically
|
||||
9. ✅ Uploads release assets
|
||||
|
||||
## 🔄 Version Management
|
||||
|
||||
### Automatic Publishing
|
||||
|
||||
The pipeline automatically publishes when:
|
||||
1. You push to `main` branch
|
||||
2. The version in `penpot_mcp/__init__.py` is different from the latest PyPI version
|
||||
|
||||
### Manual Version Bump
|
||||
|
||||
To trigger a new release:
|
||||
|
||||
```bash
|
||||
# 1. Update version in penpot_mcp/__init__.py
|
||||
echo '__version__ = "0.1.2"' > penpot_mcp/__init__.py
|
||||
|
||||
# 2. Commit and push to main
|
||||
git add penpot_mcp/__init__.py
|
||||
git commit -m "Bump version to 0.1.2"
|
||||
git push origin main
|
||||
|
||||
# 3. Pipeline will automatically:
|
||||
# - Run tests
|
||||
# - Build package
|
||||
# - Publish to PyPI
|
||||
# - Create GitHub release
|
||||
```
|
||||
|
||||
### Manual Release (Alternative)
|
||||
|
||||
You can also create releases manually:
|
||||
|
||||
```bash
|
||||
# 1. Create and push a tag
|
||||
git tag v0.1.2
|
||||
git push origin v0.1.2
|
||||
|
||||
# 2. Create release on GitHub UI
|
||||
# 3. Pipeline will automatically publish to PyPI
|
||||
```
|
||||
|
||||
## 🛠 Advanced Configuration
|
||||
|
||||
### Environment Variables
|
||||
|
||||
You can customize the pipeline behavior using environment variables:
|
||||
|
||||
```yaml
|
||||
env:
|
||||
SKIP_TESTS: false # Skip tests (not recommended)
|
||||
SKIP_TESTPYPI: false # Skip Test PyPI upload
|
||||
CREATE_RELEASE: true # Create GitHub releases
|
||||
PYTHON_VERSION: "3.12" # Default Python version
|
||||
```
|
||||
|
||||
### Dependency Caching
|
||||
|
||||
The workflows use `uv` for fast dependency management:
|
||||
|
||||
```yaml
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev # Install with dev dependencies
|
||||
uv sync --frozen # Use locked dependencies (production)
|
||||
```
|
||||
|
||||
### Security Scanning
|
||||
|
||||
The pipeline includes multiple security checks:
|
||||
|
||||
- **Bandit**: Python security linter
|
||||
- **Safety**: Dependency vulnerability scanner (can be added)
|
||||
- **CodeQL**: GitHub's semantic code analysis (can be enabled)
|
||||
|
||||
### Adding Security Scanning
|
||||
|
||||
To add more security tools:
|
||||
|
||||
```yaml
|
||||
- name: Run safety check
|
||||
run: |
|
||||
uv add safety
|
||||
uv run safety check --json --output safety-report.json
|
||||
```
|
||||
|
||||
## 🐛 Troubleshooting
|
||||
|
||||
### Common Issues
|
||||
|
||||
#### 1. "Version already exists" error
|
||||
- Check that you bumped the version in `__init__.py`
|
||||
- Verify the version doesn't exist on PyPI already
|
||||
|
||||
#### 2. PyPI upload fails
|
||||
- Verify your API tokens are correct
|
||||
- Check that token has proper scope permissions
|
||||
- Ensure package name doesn't conflict
|
||||
|
||||
#### 3. Tests fail in CI but pass locally
|
||||
- Check Python version compatibility
|
||||
- Verify all dependencies are specified in `pyproject.toml`
|
||||
- Check for environment-specific issues
|
||||
|
||||
#### 4. GitHub Actions permissions error
|
||||
- Ensure "Read and write permissions" are enabled
|
||||
- Check that secrets are properly configured
|
||||
|
||||
### Debug Commands
|
||||
|
||||
```bash
|
||||
# Test build locally
|
||||
uv build
|
||||
uv run twine check dist/*
|
||||
|
||||
# Test package installation
|
||||
python -m pip install dist/*.whl
|
||||
penpot-mcp --help
|
||||
|
||||
# Check version
|
||||
python -c "import penpot_mcp; print(penpot_mcp.__version__)"
|
||||
|
||||
# Verify PyPI package
|
||||
pip index versions penpot-mcp
|
||||
```
|
||||
|
||||
## 📊 Monitoring
|
||||
|
||||
### GitHub Actions Dashboard
|
||||
- View workflow runs: `https://github.com/YOUR_ORG/penpot-mcp/actions`
|
||||
- Monitor success/failure rates
|
||||
- Check deployment status
|
||||
|
||||
### PyPI Package Page
|
||||
- Package stats: `https://pypi.org/project/penpot-mcp/`
|
||||
- Download statistics
|
||||
- Version history
|
||||
|
||||
### Codecov (Optional)
|
||||
- Code coverage reports
|
||||
- Coverage trends over time
|
||||
- Pull request coverage analysis
|
||||
|
||||
## 🔐 Security Best Practices
|
||||
|
||||
1. **API Tokens**:
|
||||
- Use scoped tokens (project-specific when possible)
|
||||
- Rotate tokens regularly
|
||||
- Never commit tokens to code
|
||||
|
||||
2. **Repository Settings**:
|
||||
- Enable branch protection on `main`
|
||||
- Require status checks to pass
|
||||
- Require up-to-date branches
|
||||
|
||||
3. **Secrets Management**:
|
||||
- Use GitHub Secrets for sensitive data
|
||||
- Consider using environment-specific secrets
|
||||
- Audit secret access regularly
|
||||
|
||||
## 🎯 Next Steps
|
||||
|
||||
After setup:
|
||||
|
||||
1. **Test the Pipeline**:
|
||||
- Create a test PR to verify CI
|
||||
- Push a version bump to test CD
|
||||
|
||||
2. **Configure Notifications**:
|
||||
- Set up Slack/Discord webhooks
|
||||
- Configure email notifications
|
||||
|
||||
3. **Add Integrations**:
|
||||
- CodeQL for security analysis
|
||||
- Dependabot for dependency updates
|
||||
- Pre-commit hooks for code quality
|
||||
|
||||
4. **Documentation**:
|
||||
- Update README with CI/CD badges
|
||||
- Document release process
|
||||
- Create contribution guidelines
|
||||
59
.github/dependabot.yml
vendored
Normal file
59
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
version: 2
|
||||
updates:
|
||||
# Python dependencies
|
||||
- package-ecosystem: "pip"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
day: "monday"
|
||||
time: "09:00"
|
||||
timezone: "UTC"
|
||||
open-pull-requests-limit: 5
|
||||
reviewers:
|
||||
- "montevive"
|
||||
assignees:
|
||||
- "montevive"
|
||||
commit-message:
|
||||
prefix: "deps"
|
||||
include: "scope"
|
||||
labels:
|
||||
- "dependencies"
|
||||
- "python"
|
||||
groups:
|
||||
dev-dependencies:
|
||||
patterns:
|
||||
- "pytest*"
|
||||
- "flake8*"
|
||||
- "coverage*"
|
||||
- "pre-commit*"
|
||||
- "isort*"
|
||||
- "autopep8*"
|
||||
- "pyupgrade*"
|
||||
- "setuptools*"
|
||||
production-dependencies:
|
||||
patterns:
|
||||
- "mcp*"
|
||||
- "requests*"
|
||||
- "python-dotenv*"
|
||||
- "gunicorn*"
|
||||
- "anytree*"
|
||||
- "jsonschema*"
|
||||
- "PyYAML*"
|
||||
|
||||
# GitHub Actions
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
day: "monday"
|
||||
time: "09:00"
|
||||
timezone: "UTC"
|
||||
open-pull-requests-limit: 3
|
||||
reviewers:
|
||||
- "montevive"
|
||||
commit-message:
|
||||
prefix: "ci"
|
||||
include: "scope"
|
||||
labels:
|
||||
- "dependencies"
|
||||
- "github-actions"
|
||||
146
.github/workflows/ci.yml
vendored
Normal file
146
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ main, develop ]
|
||||
push:
|
||||
branches: [ main, develop ]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v4
|
||||
with:
|
||||
version: "latest"
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev
|
||||
|
||||
- name: Run linting
|
||||
run: |
|
||||
uv run python lint.py || echo "Linting found issues but continuing..."
|
||||
continue-on-error: true
|
||||
|
||||
- name: Run tests with coverage
|
||||
run: |
|
||||
uv run pytest --cov=penpot_mcp tests/ --cov-report=xml --cov-report=term-missing
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v4
|
||||
if: matrix.python-version == '3.12'
|
||||
with:
|
||||
file: ./coverage.xml
|
||||
flags: unittests
|
||||
name: codecov-umbrella
|
||||
fail_ci_if_error: false
|
||||
|
||||
security-check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev
|
||||
|
||||
- name: Run security checks with bandit
|
||||
run: |
|
||||
uv add bandit[toml]
|
||||
uv run bandit -r penpot_mcp/ -f json -o bandit-report.json || true
|
||||
|
||||
- name: Upload security scan results
|
||||
uses: github/codeql-action/upload-sarif@v3
|
||||
if: always()
|
||||
with:
|
||||
sarif_file: bandit-report.json
|
||||
continue-on-error: true
|
||||
|
||||
build-test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev
|
||||
|
||||
- name: Build package
|
||||
run: |
|
||||
uv build
|
||||
|
||||
- name: Test package installation
|
||||
run: |
|
||||
python -m pip install dist/*.whl
|
||||
penpot-mcp --help || echo "CLI help command failed"
|
||||
python -c "import penpot_mcp; print(f'Version: {penpot_mcp.__version__}')"
|
||||
|
||||
- name: Upload build artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dist-files
|
||||
path: dist/
|
||||
retention-days: 7
|
||||
|
||||
test-docker:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Create test Dockerfile
|
||||
run: |
|
||||
cat > Dockerfile.test << 'EOF'
|
||||
FROM python:3.12-slim
|
||||
|
||||
# Install uv
|
||||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy project files
|
||||
COPY . .
|
||||
|
||||
# Install dependencies and run tests
|
||||
RUN uv sync --extra dev
|
||||
RUN uv run pytest
|
||||
|
||||
# Test CLI commands
|
||||
RUN uv run penpot-mcp --help || echo "CLI help test completed"
|
||||
EOF
|
||||
|
||||
- name: Build and test Docker image
|
||||
run: |
|
||||
docker build -f Dockerfile.test -t penpot-mcp-test .
|
||||
122
.github/workflows/code-quality.yml
vendored
Normal file
122
.github/workflows/code-quality.yml
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
name: Code Quality
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
# Run weekly on Sundays at 2 AM UTC
|
||||
- cron: '0 2 * * 0'
|
||||
|
||||
jobs:
|
||||
code-quality:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev
|
||||
|
||||
- name: Run comprehensive linting
|
||||
run: |
|
||||
echo "Running full linting analysis..."
|
||||
uv run python lint.py --autofix || true
|
||||
|
||||
- name: Check for auto-fixes
|
||||
run: |
|
||||
if [[ -n $(git status --porcelain) ]]; then
|
||||
echo "Auto-fixes were applied"
|
||||
git diff
|
||||
else
|
||||
echo "No auto-fixes needed"
|
||||
fi
|
||||
|
||||
- name: Create Pull Request for fixes
|
||||
if: success()
|
||||
uses: peter-evans/create-pull-request@v6
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: "🔧 Auto-fix code quality issues"
|
||||
title: "🔧 Automated Code Quality Improvements"
|
||||
body: |
|
||||
## Automated Code Quality Fixes
|
||||
|
||||
This PR contains automated fixes for code quality issues:
|
||||
|
||||
### Changes Applied
|
||||
- Line length adjustments
|
||||
- Import sorting
|
||||
- Whitespace cleanup
|
||||
- Unused import removal
|
||||
|
||||
### Review Notes
|
||||
- All changes are automatically applied by linting tools
|
||||
- Tests should still pass after these changes
|
||||
- Manual review recommended for any significant changes
|
||||
|
||||
🤖 This PR was automatically created by the Code Quality workflow.
|
||||
branch: automated-code-quality-fixes
|
||||
delete-branch: true
|
||||
reviewers: montevive
|
||||
labels: |
|
||||
code-quality
|
||||
automated
|
||||
enhancement
|
||||
|
||||
- name: Security Analysis
|
||||
run: |
|
||||
echo "Running security analysis..."
|
||||
uv add bandit[toml]
|
||||
uv run bandit -r penpot_mcp/ -f json -o bandit-report.json || true
|
||||
|
||||
if [ -f bandit-report.json ]; then
|
||||
echo "Security report generated"
|
||||
cat bandit-report.json | head -20
|
||||
fi
|
||||
|
||||
- name: Code Coverage Analysis
|
||||
run: |
|
||||
echo "Running code coverage analysis..."
|
||||
uv run pytest --cov=penpot_mcp tests/ --cov-report=html --cov-report=term
|
||||
|
||||
echo "Coverage report generated in htmlcov/"
|
||||
|
||||
- name: Upload Coverage Report
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: coverage-report
|
||||
path: htmlcov/
|
||||
retention-days: 30
|
||||
|
||||
- name: Upload Security Report
|
||||
uses: actions/upload-artifact@v4
|
||||
if: always()
|
||||
with:
|
||||
name: security-report
|
||||
path: bandit-report.json
|
||||
retention-days: 30
|
||||
|
||||
- name: Summary
|
||||
run: |
|
||||
echo "## Code Quality Summary" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "### Linting" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Auto-fixes applied (if any)" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "### Security Analysis" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Bandit security scan completed" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "### Coverage" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Code coverage report generated" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "### Artifacts" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Coverage report: htmlcov/" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Security report: bandit-report.json" >> $GITHUB_STEP_SUMMARY
|
||||
208
.github/workflows/publish.yml
vendored
Normal file
208
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
name: Publish to PyPI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths-ignore:
|
||||
- 'README.md'
|
||||
- 'CHANGELOG.md'
|
||||
- 'docs/**'
|
||||
- '.gitignore'
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
# Only run if tests pass first
|
||||
check-tests:
|
||||
uses: ./.github/workflows/ci.yml
|
||||
|
||||
publish:
|
||||
needs: check-tests
|
||||
runs-on: ubuntu-latest
|
||||
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # Fetch full history for version bump detection
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v4
|
||||
with:
|
||||
version: "latest"
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev
|
||||
|
||||
- name: Check if version was bumped
|
||||
id: version-check
|
||||
run: |
|
||||
# Get current version from __init__.py
|
||||
CURRENT_VERSION=$(python -c "import penpot_mcp; print(penpot_mcp.__version__)")
|
||||
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
||||
|
||||
# Check if this version already exists on PyPI
|
||||
if pip index versions penpot-mcp | grep -q "$CURRENT_VERSION"; then
|
||||
echo "version_exists=true" >> $GITHUB_OUTPUT
|
||||
echo "Version $CURRENT_VERSION already exists on PyPI"
|
||||
else
|
||||
echo "version_exists=false" >> $GITHUB_OUTPUT
|
||||
echo "Version $CURRENT_VERSION is new, will publish"
|
||||
fi
|
||||
|
||||
- name: Build package
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
run: |
|
||||
uv build
|
||||
|
||||
- name: Check package quality
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
run: |
|
||||
# Install twine for checking
|
||||
uv add twine
|
||||
|
||||
# Check the built package
|
||||
uv run twine check dist/*
|
||||
|
||||
# Verify package contents
|
||||
python -m tarfile -l dist/*.tar.gz
|
||||
python -m zipfile -l dist/*.whl
|
||||
|
||||
- name: Test package installation
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
run: |
|
||||
# Test installation in a clean environment
|
||||
python -m pip install dist/*.whl
|
||||
|
||||
# Test basic imports and CLI
|
||||
python -c "import penpot_mcp; print(f'Successfully imported penpot_mcp v{penpot_mcp.__version__}')"
|
||||
penpot-mcp --help
|
||||
|
||||
# Uninstall to avoid conflicts
|
||||
python -m pip uninstall -y penpot-mcp
|
||||
|
||||
- name: Publish to Test PyPI
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
env:
|
||||
TWINE_USERNAME: __token__
|
||||
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
||||
run: |
|
||||
uv run twine upload --repository testpypi dist/* --verbose
|
||||
continue-on-error: true # Test PyPI upload can fail, but don't stop main PyPI upload
|
||||
|
||||
- name: Wait for Test PyPI propagation
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
run: |
|
||||
echo "Waiting 60 seconds for Test PyPI propagation..."
|
||||
sleep 60
|
||||
|
||||
- name: Test installation from Test PyPI
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
run: |
|
||||
# Try to install from Test PyPI (may fail due to dependencies)
|
||||
python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ penpot-mcp==${{ steps.version-check.outputs.current_version }} || echo "Test PyPI installation failed (expected due to dependencies)"
|
||||
continue-on-error: true
|
||||
|
||||
- name: Publish to PyPI
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
env:
|
||||
TWINE_USERNAME: __token__
|
||||
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
|
||||
run: |
|
||||
uv run twine upload dist/* --verbose
|
||||
|
||||
- name: Create GitHub Release
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: v${{ steps.version-check.outputs.current_version }}
|
||||
release_name: Release v${{ steps.version-check.outputs.current_version }}
|
||||
body: |
|
||||
## Changes in v${{ steps.version-check.outputs.current_version }}
|
||||
|
||||
Auto-generated release for version ${{ steps.version-check.outputs.current_version }}.
|
||||
|
||||
### Installation
|
||||
```bash
|
||||
pip install penpot-mcp==${{ steps.version-check.outputs.current_version }}
|
||||
# or
|
||||
uvx penpot-mcp
|
||||
```
|
||||
|
||||
### What's Changed
|
||||
See commit history for detailed changes.
|
||||
|
||||
**Full Changelog**: https://github.com/montevive/penpot-mcp/compare/v${{ steps.version-check.outputs.current_version }}...HEAD
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
- name: Upload Release Assets
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create-release.outputs.upload_url }}
|
||||
asset_path: dist/
|
||||
asset_name: penpot-mcp-${{ steps.version-check.outputs.current_version }}-dist.zip
|
||||
asset_content_type: application/zip
|
||||
|
||||
- name: Notify on success
|
||||
if: steps.version-check.outputs.version_exists == 'false'
|
||||
run: |
|
||||
echo "✅ Successfully published penpot-mcp v${{ steps.version-check.outputs.current_version }} to PyPI!"
|
||||
echo "📦 Package: https://pypi.org/project/penpot-mcp/${{ steps.version-check.outputs.current_version }}/"
|
||||
echo "🏷️ Release: https://github.com/montevive/penpot-mcp/releases/tag/v${{ steps.version-check.outputs.current_version }}"
|
||||
|
||||
- name: Skip publishing
|
||||
if: steps.version-check.outputs.version_exists == 'true'
|
||||
run: |
|
||||
echo "⏭️ Skipping publish - version ${{ steps.version-check.outputs.current_version }} already exists on PyPI"
|
||||
|
||||
# Manual release workflow (triggered by GitHub releases)
|
||||
publish-release:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'release' && github.event.action == 'published'
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install uv
|
||||
uses: astral-sh/setup-uv@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
uv sync --extra dev
|
||||
|
||||
- name: Update version to match release tag
|
||||
run: |
|
||||
RELEASE_VERSION="${{ github.event.release.tag_name }}"
|
||||
# Remove 'v' prefix if present
|
||||
VERSION="${RELEASE_VERSION#v}"
|
||||
|
||||
# Update version in __init__.py
|
||||
sed -i "s/__version__ = \".*\"/__version__ = \"$VERSION\"/" penpot_mcp/__init__.py
|
||||
|
||||
echo "Updated version to: $VERSION"
|
||||
|
||||
- name: Build and publish
|
||||
env:
|
||||
TWINE_USERNAME: __token__
|
||||
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
|
||||
run: |
|
||||
uv build
|
||||
uv run twine check dist/*
|
||||
uv run twine upload dist/* --verbose
|
||||
156
.github/workflows/version-bump.yml
vendored
Normal file
156
.github/workflows/version-bump.yml
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
name: Version Bump
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version-type:
|
||||
description: 'Version bump type'
|
||||
required: true
|
||||
default: 'patch'
|
||||
type: choice
|
||||
options:
|
||||
- patch
|
||||
- minor
|
||||
- major
|
||||
custom-version:
|
||||
description: 'Custom version (optional, overrides version-type)'
|
||||
required: false
|
||||
type: string
|
||||
|
||||
jobs:
|
||||
bump-version:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install packaging
|
||||
|
||||
- name: Get current version
|
||||
id: current-version
|
||||
run: |
|
||||
CURRENT_VERSION=$(python -c "import penpot_mcp; print(penpot_mcp.__version__)")
|
||||
echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
||||
echo "Current version: $CURRENT_VERSION"
|
||||
|
||||
- name: Calculate new version
|
||||
id: new-version
|
||||
run: |
|
||||
python << 'EOF'
|
||||
import os
|
||||
from packaging import version
|
||||
|
||||
current = "${{ steps.current-version.outputs.current }}"
|
||||
custom = "${{ github.event.inputs.custom-version }}"
|
||||
bump_type = "${{ github.event.inputs.version-type }}"
|
||||
|
||||
if custom:
|
||||
new_version = custom
|
||||
else:
|
||||
v = version.parse(current)
|
||||
if bump_type == "major":
|
||||
new_version = f"{v.major + 1}.0.0"
|
||||
elif bump_type == "minor":
|
||||
new_version = f"{v.major}.{v.minor + 1}.0"
|
||||
else: # patch
|
||||
new_version = f"{v.major}.{v.minor}.{v.micro + 1}"
|
||||
|
||||
print(f"New version: {new_version}")
|
||||
|
||||
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
|
||||
f.write(f"version={new_version}\n")
|
||||
EOF
|
||||
|
||||
- name: Update version in files
|
||||
run: |
|
||||
NEW_VERSION="${{ steps.new-version.outputs.version }}"
|
||||
|
||||
# Update __init__.py
|
||||
sed -i "s/__version__ = \".*\"/__version__ = \"$NEW_VERSION\"/" penpot_mcp/__init__.py
|
||||
|
||||
# Verify the change
|
||||
echo "Updated version in penpot_mcp/__init__.py:"
|
||||
grep "__version__" penpot_mcp/__init__.py
|
||||
|
||||
- name: Create changelog entry
|
||||
run: |
|
||||
NEW_VERSION="${{ steps.new-version.outputs.version }}"
|
||||
DATE=$(date +"%Y-%m-%d")
|
||||
|
||||
# Create CHANGELOG.md if it doesn't exist
|
||||
if [ ! -f CHANGELOG.md ]; then
|
||||
cat > CHANGELOG.md << 'EOF'
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
EOF
|
||||
fi
|
||||
|
||||
# Add new version entry
|
||||
sed -i "3i\\\\n## [$NEW_VERSION] - $DATE\\\\n\\\\n### Added\\\\n- Version bump to $NEW_VERSION\\\\n\\\\n### Changed\\\\n- Update dependencies and improve stability\\\\n\\\\n### Fixed\\\\n- Bug fixes and performance improvements\\\\n" CHANGELOG.md
|
||||
|
||||
echo "Updated CHANGELOG.md with version $NEW_VERSION"
|
||||
|
||||
- name: Commit and push changes
|
||||
run: |
|
||||
NEW_VERSION="${{ steps.new-version.outputs.version }}"
|
||||
|
||||
git config --local user.email "action@github.com"
|
||||
git config --local user.name "GitHub Action"
|
||||
|
||||
git add penpot_mcp/__init__.py CHANGELOG.md
|
||||
git commit -m "Bump version to $NEW_VERSION
|
||||
|
||||
- Update version in __init__.py to $NEW_VERSION
|
||||
- Add changelog entry for version $NEW_VERSION
|
||||
|
||||
🤖 Generated with GitHub Actions"
|
||||
|
||||
git push
|
||||
|
||||
echo "✅ Version bumped to $NEW_VERSION and pushed to repository"
|
||||
|
||||
- name: Create pull request (if on branch)
|
||||
if: github.ref != 'refs/heads/main'
|
||||
uses: peter-evans/create-pull-request@v6
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: "Bump version to ${{ steps.new-version.outputs.version }}"
|
||||
title: "🔖 Bump version to ${{ steps.new-version.outputs.version }}"
|
||||
body: |
|
||||
## Version Bump to ${{ steps.new-version.outputs.version }}
|
||||
|
||||
This PR was automatically created to bump the version.
|
||||
|
||||
### Changes
|
||||
- Updated `__version__` in `penpot_mcp/__init__.py`
|
||||
- Added changelog entry for version ${{ steps.new-version.outputs.version }}
|
||||
|
||||
### Type of Change
|
||||
- [${{ github.event.inputs.version-type == 'major' && 'x' || ' ' }}] Major version (breaking changes)
|
||||
- [${{ github.event.inputs.version-type == 'minor' && 'x' || ' ' }}] Minor version (new features)
|
||||
- [${{ github.event.inputs.version-type == 'patch' && 'x' || ' ' }}] Patch version (bug fixes)
|
||||
|
||||
### Checklist
|
||||
- [x] Version updated in `__init__.py`
|
||||
- [x] Changelog updated
|
||||
- [ ] Tests pass (will be verified by CI)
|
||||
- [ ] Ready for merge and auto-publish
|
||||
|
||||
**Note**: Merging this PR to `main` will trigger automatic publishing to PyPI.
|
||||
branch: version-bump-${{ steps.new-version.outputs.version }}
|
||||
delete-branch: true
|
||||
47
CHANGELOG.md
Normal file
47
CHANGELOG.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Comprehensive CI/CD pipeline with GitHub Actions
|
||||
- Automated PyPI publishing on version bumps
|
||||
- CloudFlare error detection and user-friendly error handling
|
||||
- Version bump automation workflow
|
||||
|
||||
### Changed
|
||||
- Enhanced error handling in API client and MCP server
|
||||
- Improved documentation for setup and usage
|
||||
|
||||
### Fixed
|
||||
- CloudFlare protection blocking issues with helpful resolution instructions
|
||||
|
||||
## [0.1.1] - 2024-06-29
|
||||
|
||||
### Added
|
||||
- Initial MCP server implementation
|
||||
- Penpot API client with authentication
|
||||
- Object tree visualization and analysis tools
|
||||
- Export functionality for design objects
|
||||
- Cache system for improved performance
|
||||
- Comprehensive test suite
|
||||
|
||||
### Features
|
||||
- List and access Penpot projects and files
|
||||
- Search design objects by name with regex support
|
||||
- Get object tree structure with field filtering
|
||||
- Export design objects as images
|
||||
- Claude Desktop and Cursor IDE integration
|
||||
- HTTP server for image serving
|
||||
|
||||
## [0.1.0] - 2024-06-28
|
||||
|
||||
### Added
|
||||
- Initial project structure
|
||||
- Basic Penpot API integration
|
||||
- MCP protocol implementation
|
||||
- Core tool definitions
|
||||
123
CLAUDE.md
Normal file
123
CLAUDE.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
Penpot MCP Server is a Python-based Model Context Protocol (MCP) server that bridges AI language models with Penpot, an open-source design platform. It enables programmatic interaction with design files through a well-structured API.
|
||||
|
||||
## Key Commands
|
||||
|
||||
### Development Setup
|
||||
|
||||
```bash
|
||||
# Install dependencies (recommended)
|
||||
uv sync --extra dev
|
||||
|
||||
# Run the MCP server
|
||||
uv run penpot-mcp
|
||||
|
||||
# Run tests
|
||||
uv run pytest
|
||||
uv run pytest --cov=penpot_mcp tests/ # with coverage
|
||||
|
||||
# Lint and fix code
|
||||
uv run python lint.py # check issues
|
||||
uv run python lint.py --autofix # auto-fix issues
|
||||
```
|
||||
|
||||
### Running the Server
|
||||
|
||||
```bash
|
||||
# Default stdio mode (for Claude Desktop/Cursor)
|
||||
make mcp-server
|
||||
|
||||
# SSE mode (for debugging with inspector)
|
||||
make mcp-server-sse
|
||||
|
||||
# Launch MCP inspector (requires SSE mode)
|
||||
make mcp-inspector
|
||||
```
|
||||
|
||||
### CLI Tools
|
||||
|
||||
```bash
|
||||
# Generate tree visualization
|
||||
penpot-tree path/to/penpot_file.json
|
||||
|
||||
# Validate Penpot file
|
||||
penpot-validate path/to/penpot_file.json
|
||||
```
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
### Core Components
|
||||
|
||||
1. **MCP Server** (`penpot_mcp/server/mcp_server.py`)
|
||||
- Built on FastMCP framework
|
||||
- Implements resources and tools for Penpot interaction
|
||||
- Memory cache with 10-minute TTL
|
||||
- Supports stdio (default) and SSE modes
|
||||
|
||||
2. **API Client** (`penpot_mcp/api/penpot_api.py`)
|
||||
- REST client for Penpot platform
|
||||
- Transit+JSON format handling
|
||||
- Cookie-based authentication with auto-refresh
|
||||
- Lazy authentication pattern
|
||||
|
||||
3. **Key Design Patterns**
|
||||
- **Authentication**: Cookie-based with automatic re-authentication on 401/403
|
||||
- **Caching**: In-memory file cache to reduce API calls
|
||||
- **Resource/Tool Duality**: Resources can be exposed as tools via RESOURCES_AS_TOOLS config
|
||||
- **Transit Format**: Special handling for UUIDs (`~u` prefix) and keywords (`~:` prefix)
|
||||
|
||||
### Available Tools/Functions
|
||||
|
||||
- `list_projects`: Get all Penpot projects
|
||||
- `get_project_files`: List files in a project
|
||||
- `get_file`: Retrieve and cache file data
|
||||
- `search_object`: Search design objects by name (regex)
|
||||
- `get_object_tree`: Get filtered object tree with screenshot
|
||||
- `export_object`: Export design objects as images
|
||||
- `penpot_tree_schema`: Get schema for object tree fields
|
||||
|
||||
### Environment Configuration
|
||||
|
||||
Create a `.env` file with:
|
||||
|
||||
```env
|
||||
PENPOT_API_URL=https://design.penpot.app/api
|
||||
PENPOT_USERNAME=your_username
|
||||
PENPOT_PASSWORD=your_password
|
||||
ENABLE_HTTP_SERVER=true # for image serving
|
||||
RESOURCES_AS_TOOLS=false # MCP resource mode
|
||||
DEBUG=true # debug logging
|
||||
```
|
||||
|
||||
### Working with the Codebase
|
||||
|
||||
1. **Adding New Tools**: Decorate functions with `@self.mcp.tool()` in mcp_server.py
|
||||
2. **API Extensions**: Add methods to PenpotAPI class following existing patterns
|
||||
3. **Error Handling**: Always check for `"error"` keys in API responses
|
||||
4. **Testing**: Use `test_mode=True` when creating server instances in tests
|
||||
5. **Transit Format**: Remember to handle Transit+JSON when working with raw API
|
||||
|
||||
### Common Workflow for Code Generation
|
||||
|
||||
1. List projects → Find target project
|
||||
2. Get project files → Locate design file
|
||||
3. Search for component → Find specific element
|
||||
4. Get tree schema → Understand available fields
|
||||
5. Get object tree → Retrieve structure with screenshot
|
||||
6. Export if needed → Get rendered component image
|
||||
|
||||
### Testing Patterns
|
||||
|
||||
- Mock fixtures in `tests/conftest.py`
|
||||
- Test both stdio and SSE modes
|
||||
- Verify Transit format conversions
|
||||
- Check cache behavior and expiration
|
||||
|
||||
## Memories
|
||||
|
||||
- Keep the current transport format for the current API requests
|
||||
@@ -151,6 +151,12 @@ PORT=5000
|
||||
DEBUG=true
|
||||
```
|
||||
|
||||
> **⚠️ CloudFlare Protection Notice**: The Penpot cloud site (penpot.app) uses CloudFlare protection that may occasionally block API requests. If you encounter authentication errors or blocked requests:
|
||||
> 1. Open your web browser and navigate to [https://design.penpot.app](https://design.penpot.app)
|
||||
> 2. Log in to your Penpot account
|
||||
> 3. Complete any CloudFlare human verification challenges if prompted
|
||||
> 4. Once verified, the API requests should work normally for a period of time
|
||||
|
||||
## Usage
|
||||
|
||||
### Running the MCP Server
|
||||
|
||||
5
lint.py
5
lint.py
@@ -6,8 +6,6 @@ Run with: python lint.py [--autofix]
|
||||
|
||||
import argparse
|
||||
import importlib.util
|
||||
import os
|
||||
import site
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
@@ -221,7 +219,8 @@ def main():
|
||||
|
||||
# Run flake8 (check only, no auto-fix)
|
||||
print("Running flake8...")
|
||||
flake8_result = run_command("flake8", cwd=root_dir)
|
||||
flake8_cmd = "flake8 --exclude=.venv,venv,__pycache__,.git,build,dist,*.egg-info,node_modules"
|
||||
flake8_result = run_command(flake8_cmd, cwd=root_dir)
|
||||
|
||||
if flake8_result != 0:
|
||||
print("flake8 found issues that need to be fixed manually.")
|
||||
|
||||
@@ -7,6 +7,28 @@ import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
class CloudFlareError(Exception):
|
||||
"""Exception raised when CloudFlare protection blocks the request."""
|
||||
|
||||
def __init__(self, message: str, status_code: int = None, response_text: str = None):
|
||||
super().__init__(message)
|
||||
self.status_code = status_code
|
||||
self.response_text = response_text
|
||||
|
||||
def __str__(self):
|
||||
return f"CloudFlare Protection Error: {super().__str__()}"
|
||||
|
||||
|
||||
class PenpotAPIError(Exception):
|
||||
"""General exception for Penpot API errors."""
|
||||
|
||||
def __init__(self, message: str, status_code: int = None, response_text: str = None, is_cloudflare: bool = False):
|
||||
super().__init__(message)
|
||||
self.status_code = status_code
|
||||
self.response_text = response_text
|
||||
self.is_cloudflare = is_cloudflare
|
||||
|
||||
|
||||
class PenpotAPI:
|
||||
def __init__(
|
||||
self,
|
||||
@@ -31,9 +53,74 @@ class PenpotAPI:
|
||||
# based on the required content type (JSON vs Transit+JSON)
|
||||
self.session.headers.update({
|
||||
"Accept": "application/json, application/transit+json",
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
})
|
||||
|
||||
def _is_cloudflare_error(self, response: requests.Response) -> bool:
|
||||
"""Check if the response indicates a CloudFlare error."""
|
||||
# Check for CloudFlare-specific indicators
|
||||
cloudflare_indicators = [
|
||||
'cloudflare',
|
||||
'cf-ray',
|
||||
'attention required',
|
||||
'checking your browser',
|
||||
'challenge',
|
||||
'ddos protection',
|
||||
'security check',
|
||||
'cf-browser-verification',
|
||||
'cf-challenge-running',
|
||||
'please wait while we are checking your browser',
|
||||
'enable cookies and reload the page',
|
||||
'this process is automatic'
|
||||
]
|
||||
|
||||
# Check response headers for CloudFlare
|
||||
server_header = response.headers.get('server', '').lower()
|
||||
cf_ray = response.headers.get('cf-ray')
|
||||
|
||||
if 'cloudflare' in server_header or cf_ray:
|
||||
return True
|
||||
|
||||
# Check response content for CloudFlare indicators
|
||||
try:
|
||||
response_text = response.text.lower()
|
||||
for indicator in cloudflare_indicators:
|
||||
if indicator in response_text:
|
||||
return True
|
||||
except:
|
||||
# If we can't read the response text, don't assume it's CloudFlare
|
||||
pass
|
||||
|
||||
# Check for specific status codes that might indicate CloudFlare blocks
|
||||
if response.status_code in [403, 429, 503]:
|
||||
# Additional check for CloudFlare-specific error pages
|
||||
try:
|
||||
response_text = response.text.lower()
|
||||
if any(['cloudflare' in response_text, 'cf-ray' in response_text, 'attention required' in response_text]):
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
|
||||
return False
|
||||
|
||||
def _create_cloudflare_error_message(self, response: requests.Response) -> str:
|
||||
"""Create a user-friendly CloudFlare error message."""
|
||||
base_message = (
|
||||
"CloudFlare protection has blocked this request. This is common on penpot.app. "
|
||||
"To resolve this issue:\\n\\n"
|
||||
"1. Open your web browser and navigate to https://design.penpot.app\\n"
|
||||
"2. Log in to your Penpot account\\n"
|
||||
"3. Complete any CloudFlare human verification challenges if prompted\\n"
|
||||
"4. Once verified, try your request again\\n\\n"
|
||||
"The verification typically lasts for a period of time, after which you may need to repeat the process."
|
||||
)
|
||||
|
||||
if response.status_code:
|
||||
return f"{base_message}\\n\\nHTTP Status: {response.status_code}"
|
||||
|
||||
return base_message
|
||||
|
||||
def set_access_token(self, token: str):
|
||||
"""Set the auth token for authentication."""
|
||||
self.access_token = token
|
||||
@@ -60,11 +147,12 @@ class PenpotAPI:
|
||||
Returns:
|
||||
Auth token for API calls
|
||||
"""
|
||||
# Just use the export authentication as it's more reliable
|
||||
# Use the export authentication which also extracts profile ID
|
||||
token = self.login_for_export(email, password)
|
||||
self.set_access_token(token)
|
||||
# Get profile ID after login
|
||||
self.get_profile()
|
||||
# Profile ID is now extracted during login_for_export, no need to call get_profile
|
||||
if self.debug and self.profile_id:
|
||||
print(f"\nProfile ID available: {self.profile_id}")
|
||||
return token
|
||||
|
||||
def get_profile(self) -> Dict[str, Any]:
|
||||
@@ -138,7 +226,8 @@ class PenpotAPI:
|
||||
|
||||
# Set headers
|
||||
headers = {
|
||||
"Content-Type": "application/transit+json"
|
||||
"Content-Type": "application/transit+json",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
}
|
||||
|
||||
response = login_session.post(url, json=payload, headers=headers)
|
||||
@@ -147,6 +236,45 @@ class PenpotAPI:
|
||||
print(f"Response text: {response.text}")
|
||||
response.raise_for_status()
|
||||
|
||||
# Extract profile ID from response
|
||||
try:
|
||||
# The response is in Transit+JSON array format
|
||||
data = response.json()
|
||||
if isinstance(data, list):
|
||||
# Convert Transit array to dict
|
||||
transit_dict = {}
|
||||
i = 1 # Skip the "^ " marker
|
||||
while i < len(data) - 1:
|
||||
key = data[i]
|
||||
value = data[i + 1]
|
||||
transit_dict[key] = value
|
||||
i += 2
|
||||
|
||||
# Extract profile ID
|
||||
if "~:id" in transit_dict:
|
||||
profile_id = transit_dict["~:id"]
|
||||
# Remove the ~u prefix for UUID
|
||||
if isinstance(profile_id, str) and profile_id.startswith("~u"):
|
||||
profile_id = profile_id[2:]
|
||||
self.profile_id = profile_id
|
||||
if self.debug:
|
||||
print(f"\nExtracted profile ID from login response: {profile_id}")
|
||||
except Exception as e:
|
||||
if self.debug:
|
||||
print(f"\nCouldn't extract profile ID from response: {e}")
|
||||
|
||||
# Also try to extract profile ID from auth-data cookie
|
||||
if not self.profile_id:
|
||||
for cookie in login_session.cookies:
|
||||
if cookie.name == "auth-data":
|
||||
# Cookie value is like: "profile-id=7ae66c33-6ede-81e2-8006-6a1b4dce3d2b"
|
||||
if "profile-id=" in cookie.value:
|
||||
profile_id = cookie.value.split("profile-id=")[1].split(";")[0].strip('"')
|
||||
self.profile_id = profile_id
|
||||
if self.debug:
|
||||
print(f"\nExtracted profile ID from auth-data cookie: {profile_id}")
|
||||
break
|
||||
|
||||
# Extract auth token from cookies
|
||||
if 'Set-Cookie' in response.headers:
|
||||
if self.debug:
|
||||
@@ -171,7 +299,7 @@ class PenpotAPI:
|
||||
# If we reached here, we couldn't find the token
|
||||
raise ValueError("Auth token not found in response cookies or JSON body")
|
||||
|
||||
def _make_authenticated_request(self, method: str, url: str, **kwargs) -> requests.Response:
|
||||
def _make_authenticated_request(self, method: str, url: str, retry_auth: bool = True, **kwargs) -> requests.Response:
|
||||
"""
|
||||
Make an authenticated request, handling re-auth if needed.
|
||||
|
||||
@@ -268,8 +396,17 @@ class PenpotAPI:
|
||||
return response
|
||||
|
||||
except requests.HTTPError as e:
|
||||
# Check for CloudFlare errors first
|
||||
if self._is_cloudflare_error(e.response):
|
||||
cloudflare_message = self._create_cloudflare_error_message(e.response)
|
||||
raise CloudFlareError(cloudflare_message, e.response.status_code, e.response.text)
|
||||
|
||||
# Handle authentication errors
|
||||
if e.response.status_code in (401, 403) and self.email and self.password:
|
||||
if e.response.status_code in (401, 403) and self.email and self.password and retry_auth:
|
||||
# Special case: don't retry auth for get-profile to avoid infinite loops
|
||||
if url.endswith('/get-profile'):
|
||||
raise
|
||||
|
||||
if self.debug:
|
||||
print("\nAuthentication failed. Trying to re-login...")
|
||||
|
||||
@@ -280,13 +417,22 @@ class PenpotAPI:
|
||||
headers['Authorization'] = f"Token {self.access_token}"
|
||||
combined_headers = {**self.session.headers, **headers}
|
||||
|
||||
# Retry the request with the new token
|
||||
# Retry the request with the new token (but don't retry auth again)
|
||||
response = getattr(self.session, method)(url, headers=combined_headers, **kwargs)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
else:
|
||||
# Re-raise other errors
|
||||
raise
|
||||
except requests.RequestException as e:
|
||||
# Handle other request exceptions (connection errors, timeouts, etc.)
|
||||
# Check if we have a response to analyze
|
||||
if hasattr(e, 'response') and e.response is not None:
|
||||
if self._is_cloudflare_error(e.response):
|
||||
cloudflare_message = self._create_cloudflare_error_message(e.response)
|
||||
raise CloudFlareError(cloudflare_message, e.response.status_code, e.response.text)
|
||||
# Re-raise if not a CloudFlare error
|
||||
raise
|
||||
|
||||
def _normalize_transit_response(self, data: Union[Dict, List, Any]) -> Union[Dict, List, Any]:
|
||||
"""
|
||||
@@ -460,16 +606,12 @@ class PenpotAPI:
|
||||
# This uses the cookie auth approach, which requires login
|
||||
token = self.login_for_export(email, password)
|
||||
|
||||
# If profile_id is not provided, get it from instance variable or fetch it
|
||||
# If profile_id is not provided, get it from instance variable
|
||||
if not profile_id:
|
||||
if not self.profile_id:
|
||||
# We need to set the token first for the get_profile call to work
|
||||
self.set_access_token(token)
|
||||
self.get_profile()
|
||||
profile_id = self.profile_id
|
||||
|
||||
if not profile_id:
|
||||
raise ValueError("Profile ID not available and couldn't be retrieved automatically")
|
||||
raise ValueError("Profile ID not available. It should be automatically extracted during login.")
|
||||
|
||||
# Build the URL for export creation
|
||||
url = f"{self.base_url}/export"
|
||||
@@ -500,7 +642,8 @@ class PenpotAPI:
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/transit+json",
|
||||
"Accept": "application/transit+json"
|
||||
"Accept": "application/transit+json",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
}
|
||||
|
||||
# Make the request
|
||||
@@ -557,7 +700,8 @@ class PenpotAPI:
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/transit+json",
|
||||
"Accept": "*/*"
|
||||
"Accept": "*/*",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
}
|
||||
if self.debug:
|
||||
print(f"\nFetching export resource: {url}")
|
||||
|
||||
@@ -5,16 +5,17 @@ This module defines the MCP server with resources and tools for interacting with
|
||||
the Penpot design platform.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import argparse
|
||||
import sys
|
||||
from typing import List, Optional, Dict
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from mcp.server.fastmcp import FastMCP, Image
|
||||
|
||||
from penpot_mcp.api.penpot_api import PenpotAPI
|
||||
from penpot_mcp.api.penpot_api import CloudFlareError, PenpotAPI, PenpotAPIError
|
||||
from penpot_mcp.tools.penpot_tree import get_object_subtree_with_fields
|
||||
from penpot_mcp.utils import config
|
||||
from penpot_mcp.utils.cache import MemoryCache
|
||||
@@ -91,6 +92,30 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
self._register_resources(resources_only=False)
|
||||
self._register_tools(include_resource_tools=False)
|
||||
|
||||
def _handle_api_error(self, e: Exception) -> dict:
|
||||
"""Handle API errors and return user-friendly error messages."""
|
||||
if isinstance(e, CloudFlareError):
|
||||
return {
|
||||
"error": "CloudFlare Protection",
|
||||
"message": str(e),
|
||||
"error_type": "cloudflare_protection",
|
||||
"instructions": [
|
||||
"Open your web browser and navigate to https://design.penpot.app",
|
||||
"Log in to your Penpot account",
|
||||
"Complete any CloudFlare human verification challenges if prompted",
|
||||
"Once verified, try your request again"
|
||||
]
|
||||
}
|
||||
elif isinstance(e, PenpotAPIError):
|
||||
return {
|
||||
"error": "Penpot API Error",
|
||||
"message": str(e),
|
||||
"error_type": "api_error",
|
||||
"status_code": getattr(e, 'status_code', None)
|
||||
}
|
||||
else:
|
||||
return {"error": str(e)}
|
||||
|
||||
def _register_resources(self, resources_only=False):
|
||||
"""Register all MCP resources. If resources_only is True, only register server://info as a resource."""
|
||||
@self.mcp.resource("server://info")
|
||||
@@ -147,7 +172,7 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
projects = self.api.list_projects()
|
||||
return {"projects": projects}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
return self._handle_api_error(e)
|
||||
@self.mcp.tool()
|
||||
def get_project_files(project_id: str) -> dict:
|
||||
"""Get all files contained within a specific Penpot project.
|
||||
@@ -159,7 +184,7 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
files = self.api.get_project_files(project_id)
|
||||
return {"files": files}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
return self._handle_api_error(e)
|
||||
def get_cached_file(file_id: str) -> dict:
|
||||
"""Internal helper to retrieve a file, using cache if available.
|
||||
|
||||
@@ -174,7 +199,7 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
self.file_cache.set(file_id, file_data)
|
||||
return file_data
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
return self._handle_api_error(e)
|
||||
@self.mcp.tool()
|
||||
def get_file(file_id: str) -> dict:
|
||||
"""Retrieve a Penpot file by its ID and cache it. Don't use this tool for code generation, use 'get_object_tree' instead.
|
||||
@@ -187,7 +212,7 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
self.file_cache.set(file_id, file_data)
|
||||
return file_data
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
return self._handle_api_error(e)
|
||||
@self.mcp.tool()
|
||||
def export_object(
|
||||
file_id: str,
|
||||
@@ -232,6 +257,9 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
|
||||
return image
|
||||
except Exception as e:
|
||||
if isinstance(e, CloudFlareError):
|
||||
raise Exception(f"CloudFlare Protection: {str(e)}")
|
||||
else:
|
||||
raise Exception(f"Export failed: {str(e)}")
|
||||
finally:
|
||||
if temp_filename and os.path.exists(temp_filename):
|
||||
@@ -308,7 +336,7 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
return {"format_error": f"Error formatting as YAML: {str(e)}"}
|
||||
return final_result
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
return self._handle_api_error(e)
|
||||
@self.mcp.tool()
|
||||
def search_object(file_id: str, query: str) -> dict:
|
||||
"""Search for objects within a Penpot file by name.
|
||||
@@ -338,7 +366,7 @@ Let me know which Penpot design you'd like to convert to code, and I'll guide yo
|
||||
})
|
||||
return {'objects': matches}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
return self._handle_api_error(e)
|
||||
if include_resource_tools:
|
||||
@self.mcp.tool()
|
||||
def penpot_schema() -> dict:
|
||||
|
||||
@@ -6,7 +6,7 @@ a tree representation, which can be displayed or exported.
|
||||
"""
|
||||
|
||||
import re
|
||||
from typing import Any, Dict, Optional, Union, List
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from anytree import Node, RenderTree
|
||||
from anytree.exporter import DotExporter
|
||||
@@ -423,11 +423,27 @@ def get_object_subtree_with_fields(file_data: Dict[str, Any], object_id: str,
|
||||
if object_id not in objects_dict:
|
||||
return {"error": f"Object {object_id} not found in page {page_id}"}
|
||||
|
||||
# Track visited nodes to prevent infinite loops
|
||||
visited = set()
|
||||
|
||||
# Function to recursively build the filtered object tree
|
||||
def build_filtered_object_tree(obj_id: str, current_depth: int = 0):
|
||||
if obj_id not in objects_dict:
|
||||
return None
|
||||
|
||||
# Check for circular reference
|
||||
if obj_id in visited:
|
||||
# Return a placeholder to indicate circular reference
|
||||
return {
|
||||
'id': obj_id,
|
||||
'name': objects_dict[obj_id].get('name', 'Unnamed'),
|
||||
'type': objects_dict[obj_id].get('type', 'unknown'),
|
||||
'_circular_reference': True
|
||||
}
|
||||
|
||||
# Mark this object as visited
|
||||
visited.add(obj_id)
|
||||
|
||||
obj_data = objects_dict[obj_id]
|
||||
|
||||
# Create a new dict with only the requested fields or all fields if None
|
||||
@@ -441,6 +457,8 @@ def get_object_subtree_with_fields(file_data: Dict[str, Any], object_id: str,
|
||||
|
||||
# If depth limit reached, don't process children
|
||||
if depth != -1 and current_depth >= depth:
|
||||
# Remove from visited before returning
|
||||
visited.remove(obj_id)
|
||||
return filtered_obj
|
||||
|
||||
# Find all children of this object
|
||||
@@ -455,6 +473,9 @@ def get_object_subtree_with_fields(file_data: Dict[str, Any], object_id: str,
|
||||
if children:
|
||||
filtered_obj['children'] = children
|
||||
|
||||
# Remove from visited after processing
|
||||
visited.remove(obj_id)
|
||||
|
||||
return filtered_obj
|
||||
|
||||
# Build the filtered tree starting from the requested object
|
||||
|
||||
@@ -3,7 +3,8 @@ Cache utilities for Penpot MCP server.
|
||||
"""
|
||||
|
||||
import time
|
||||
from typing import Optional, Dict, Any
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
|
||||
class MemoryCache:
|
||||
"""In-memory cache implementation with TTL support."""
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
import io
|
||||
import json
|
||||
import socketserver
|
||||
import threading
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
import socketserver
|
||||
|
||||
|
||||
class InMemoryImageHandler(BaseHTTPRequestHandler):
|
||||
"""HTTP request handler for serving images stored in memory."""
|
||||
|
||||
80
test_credentials.py
Executable file
80
test_credentials.py
Executable file
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify Penpot API credentials and list projects.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from penpot_mcp.api.penpot_api import PenpotAPI
|
||||
|
||||
|
||||
def test_credentials():
|
||||
"""Test Penpot API credentials and list projects."""
|
||||
load_dotenv()
|
||||
|
||||
api_url = os.getenv("PENPOT_API_URL")
|
||||
username = os.getenv("PENPOT_USERNAME")
|
||||
password = os.getenv("PENPOT_PASSWORD")
|
||||
|
||||
if not all([api_url, username, password]):
|
||||
print("❌ Missing credentials in .env file")
|
||||
print("Required: PENPOT_API_URL, PENPOT_USERNAME, PENPOT_PASSWORD")
|
||||
return False
|
||||
|
||||
print(f"🔗 Testing connection to: {api_url}")
|
||||
print(f"👤 Username: {username}")
|
||||
|
||||
try:
|
||||
api = PenpotAPI(api_url, debug=False, email=username, password=password)
|
||||
|
||||
print("🔐 Authenticating...")
|
||||
token = api.login_with_password()
|
||||
print("✅ Authentication successful!")
|
||||
|
||||
print("📁 Fetching projects...")
|
||||
projects = api.list_projects()
|
||||
|
||||
if isinstance(projects, dict) and "error" in projects:
|
||||
print(f"❌ Failed to list projects: {projects['error']}")
|
||||
return False
|
||||
|
||||
print(f"✅ Found {len(projects)} projects:")
|
||||
for i, project in enumerate(projects, 1):
|
||||
if isinstance(project, dict):
|
||||
name = project.get('name', 'Unnamed')
|
||||
project_id = project.get('id', 'N/A')
|
||||
team_name = project.get('team-name', 'Unknown Team')
|
||||
print(f" {i}. {name} (ID: {project_id}) - Team: {team_name}")
|
||||
else:
|
||||
print(f" {i}. {project}")
|
||||
|
||||
# Test getting project files if we have a project
|
||||
if projects and isinstance(projects[0], dict):
|
||||
project_id = projects[0].get('id')
|
||||
if project_id:
|
||||
print(f"\n📄 Testing project files for project: {project_id}")
|
||||
try:
|
||||
files = api.get_project_files(project_id)
|
||||
print(f"✅ Found {len(files)} files:")
|
||||
for j, file in enumerate(files[:3], 1): # Show first 3 files
|
||||
if isinstance(file, dict):
|
||||
print(f" {j}. {file.get('name', 'Unnamed')} (ID: {file.get('id', 'N/A')})")
|
||||
else:
|
||||
print(f" {j}. {file}")
|
||||
if len(files) > 3:
|
||||
print(f" ... and {len(files) - 3} more files")
|
||||
except Exception as file_error:
|
||||
print(f"❌ Error getting files: {file_error}")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_credentials()
|
||||
exit(0 if success else 1)
|
||||
@@ -3,9 +3,12 @@ Tests for the memory caching functionality.
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from penpot_mcp.utils.cache import MemoryCache
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def memory_cache():
|
||||
"""Create a MemoryCache instance with a short TTL for testing."""
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
"""Tests for the MCP server module."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import hashlib
|
||||
from unittest.mock import MagicMock, mock_open, patch
|
||||
|
||||
import yaml
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from penpot_mcp.server.mcp_server import PenpotMCPServer, create_server
|
||||
|
||||
|
||||
|
||||
@@ -8,13 +8,13 @@ from anytree import Node, RenderTree
|
||||
|
||||
from penpot_mcp.tools.penpot_tree import (
|
||||
build_tree,
|
||||
print_tree,
|
||||
export_tree_to_dot,
|
||||
find_page_containing_object,
|
||||
find_object_in_tree,
|
||||
convert_node_to_dict,
|
||||
export_tree_to_dot,
|
||||
find_object_in_tree,
|
||||
find_page_containing_object,
|
||||
get_object_subtree,
|
||||
get_object_subtree_with_fields
|
||||
get_object_subtree_with_fields,
|
||||
print_tree,
|
||||
)
|
||||
|
||||
|
||||
@@ -1088,3 +1088,60 @@ def test_get_object_subtree_with_fields_root_frame():
|
||||
assert 'children' in result['tree']
|
||||
assert len(result['tree']['children']) == 1
|
||||
assert result['tree']['children'][0]['name'] == 'Main Container'
|
||||
|
||||
|
||||
def test_get_object_subtree_with_fields_circular_reference():
|
||||
"""Test handling of circular references in object tree."""
|
||||
file_data = {
|
||||
'data': {
|
||||
'pagesIndex': {
|
||||
'page1': {
|
||||
'name': 'Test Page',
|
||||
'objects': {
|
||||
# Object A references B as parent
|
||||
'object-a': {
|
||||
'type': 'frame',
|
||||
'name': 'Object A',
|
||||
'parentId': 'object-b'
|
||||
},
|
||||
# Object B references A as parent (circular)
|
||||
'object-b': {
|
||||
'type': 'frame',
|
||||
'name': 'Object B',
|
||||
'parentId': 'object-a'
|
||||
},
|
||||
# Object C references itself as parent
|
||||
'object-c': {
|
||||
'type': 'frame',
|
||||
'name': 'Object C',
|
||||
'parentId': 'object-c'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Test getting object A - should handle circular reference with B
|
||||
result = get_object_subtree_with_fields(file_data, 'object-a')
|
||||
assert 'error' not in result
|
||||
assert result['tree']['id'] == 'object-a'
|
||||
assert 'children' in result['tree']
|
||||
# Check that object-b appears as a child
|
||||
assert len(result['tree']['children']) == 1
|
||||
assert result['tree']['children'][0]['id'] == 'object-b'
|
||||
# The circular reference appears when object-a appears again as a child of object-b
|
||||
assert 'children' in result['tree']['children'][0]
|
||||
assert len(result['tree']['children'][0]['children']) == 1
|
||||
assert result['tree']['children'][0]['children'][0]['id'] == 'object-a'
|
||||
assert result['tree']['children'][0]['children'][0]['_circular_reference'] == True
|
||||
|
||||
# Test getting object C - should handle self-reference
|
||||
result = get_object_subtree_with_fields(file_data, 'object-c')
|
||||
assert 'error' not in result
|
||||
assert result['tree']['id'] == 'object-c'
|
||||
assert 'children' in result['tree']
|
||||
# Check that object-c appears as its own child with circular reference marker
|
||||
assert len(result['tree']['children']) == 1
|
||||
assert result['tree']['children'][0]['id'] == 'object-c'
|
||||
assert result['tree']['children'][0]['_circular_reference'] == True
|
||||
Reference in New Issue
Block a user