Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
79 changes: 37 additions & 42 deletions projects/unit3/build-mcp-server/solution/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import os
import subprocess
from typing import Dict, List, Any, Optional
from typing import Optional
from pathlib import Path

from mcp.server.fastmcp import FastMCP
Expand All @@ -18,6 +18,34 @@
# PR template directory (shared between starter and solution)
TEMPLATES_DIR = Path(__file__).parent.parent.parent / "templates"

# Default PR templates
DEFAULT_TEMPLATES = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}

# Type mapping for PR templates
TYPE_MAPPING = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}


@mcp.tool()
async def analyze_file_changes(
Expand All @@ -44,7 +72,7 @@ async def analyze_file_changes(
root = roots_result.roots[0]
# FileUrl object has a .path property that gives us the path directly
working_directory = root.uri.path
except Exception as e:
except Exception:
# If we can't get roots, fall back to current directory
pass

Expand Down Expand Up @@ -143,30 +171,14 @@ async def analyze_file_changes(
@mcp.tool()
async def get_pr_templates() -> str:
"""List available PR templates with their content."""
templates = []

# Define default templates
default_templates = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}

for filename, template_type in default_templates.items():
template_path = TEMPLATES_DIR / filename

# Read template content
content = template_path.read_text()

templates.append({
templates = [
{
"filename": filename,
"type": template_type,
"content": content
})
"content": (TEMPLATES_DIR / filename).read_text()
}
for filename, template_type in DEFAULT_TEMPLATES.items()
]

return json.dumps(templates, indent=2)

Expand All @@ -184,25 +196,8 @@ async def suggest_template(changes_summary: str, change_type: str) -> str:
templates_response = await get_pr_templates()
templates = json.loads(templates_response)

# Map change types to template files
type_mapping = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}

# Find matching template
template_file = type_mapping.get(change_type.lower(), "feature.md")
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
selected_template = next(
(t for t in templates if t["filename"] == template_file),
templates[0] # Default to first template if no match
Expand Down
79 changes: 37 additions & 42 deletions projects/unit3/github-actions-integration/solution/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import os
import subprocess
from typing import Dict, Any, Optional
from typing import Optional
from pathlib import Path

from mcp.server.fastmcp import FastMCP
Expand All @@ -18,9 +18,37 @@
# PR template directory (shared between starter and solution)
TEMPLATES_DIR = Path(__file__).parent.parent.parent / "templates"

# Default PR templates
DEFAULT_TEMPLATES = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}

# File where webhook server stores events
EVENTS_FILE = Path(__file__).parent / "github_events.json"

# Type mapping for PR templates
TYPE_MAPPING = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}


# ===== Original Tools from Module 1 (with output limiting) =====

Expand Down Expand Up @@ -49,7 +77,7 @@ async def analyze_file_changes(
root = roots_result.roots[0]
# FileUrl object has a .path property that gives us the path directly
working_directory = root.uri.path
except Exception as e:
except Exception:
# If we can't get roots, fall back to current directory
pass

Expand Down Expand Up @@ -122,30 +150,14 @@ async def analyze_file_changes(
@mcp.tool()
async def get_pr_templates() -> str:
"""List available PR templates with their content."""
templates = []

# Define default templates
default_templates = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}

for filename, template_type in default_templates.items():
template_path = TEMPLATES_DIR / filename

# Read template content
content = template_path.read_text()

templates.append({
templates = [
{
"filename": filename,
"type": template_type,
"content": content
})
"content": (TEMPLATES_DIR / filename).read_text()
}
for filename, template_type in DEFAULT_TEMPLATES.items()
]

return json.dumps(templates, indent=2)

Expand All @@ -163,25 +175,8 @@ async def suggest_template(changes_summary: str, change_type: str) -> str:
templates_response = await get_pr_templates()
templates = json.loads(templates_response)

# Map change types to template files
type_mapping = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}

# Find matching template
template_file = type_mapping.get(change_type.lower(), "feature.md")
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
selected_template = next(
(t for t in templates if t["filename"] == template_file),
templates[0] # Default to first template if no match
Expand Down
81 changes: 38 additions & 43 deletions projects/unit3/github-actions-integration/starter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import os
import subprocess
from typing import Dict, List, Any, Optional
from typing import Optional
from pathlib import Path
from datetime import datetime

Expand All @@ -19,9 +19,37 @@
# PR template directory (shared between starter and solution)
TEMPLATES_DIR = Path(__file__).parent.parent.parent / "templates"

# Default PR templates
DEFAULT_TEMPLATES = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}

# TODO: Add path to events file where webhook_server.py stores events
# Hint: EVENTS_FILE = Path(__file__).parent / "github_events.json"

# Type mapping for PR templates
TYPE_MAPPING = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}


# ===== Module 1 Tools (Already includes output limiting fix from Module 1) =====

Expand Down Expand Up @@ -94,38 +122,22 @@ async def analyze_file_changes(
return json.dumps(analysis, indent=2)

except subprocess.CalledProcessError as e:
return f"Error analyzing changes: {e.stderr}"
return json.dumps({"error": f"Git error: {e.stderr}"})
except Exception as e:
return f"Error: {str(e)}"
return json.dumps({"error": str(e)})


@mcp.tool()
async def get_pr_templates() -> str:
"""List available PR templates with their content."""
templates = []

# Define default templates
default_templates = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}

for filename, template_type in default_templates.items():
template_path = TEMPLATES_DIR / filename

# Read template content
content = template_path.read_text()

templates.append({
templates = [
{
"filename": filename,
"type": template_type,
"content": content
})
"content": (TEMPLATES_DIR / filename).read_text()
}
for filename, template_type in DEFAULT_TEMPLATES.items()
]

return json.dumps(templates, indent=2)

Expand All @@ -143,25 +155,8 @@ async def suggest_template(changes_summary: str, change_type: str) -> str:
templates_response = await get_pr_templates()
templates = json.loads(templates_response)

# Map change types to template files
type_mapping = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}

# Find matching template
template_file = type_mapping.get(change_type.lower(), "feature.md")
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
selected_template = next(
(t for t in templates if t["filename"] == template_file),
templates[0] # Default to first template if no match
Expand Down
Loading