Skip to content

Commit 4816dd3

Browse files
committed
fix: markdown rendering in tips and extract constants
- Add missing line breaks in Tip components for markdown rendering - Extract type_mapping as TYPE_MAPPING constant per @elie's review (72703c2#r158071748) - Extract default_templates as DEFAULT_TEMPLATES constant - Convert get_pr_templates loops to list comprehensions per @elie's review (72703c2#r158071738) - Remove unused imports (Dict, List, Any) - Fix unused exception variables
1 parent ad0b5fe commit 4816dd3

File tree

9 files changed

+199
-210
lines changed

9 files changed

+199
-210
lines changed

projects/unit3/build-mcp-server/solution/server.py

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
import os
99
import subprocess
10-
from typing import Dict, List, Any, Optional
10+
from typing import Optional
1111
from pathlib import Path
1212

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

21+
# Default PR templates
22+
DEFAULT_TEMPLATES = {
23+
"bug.md": "Bug Fix",
24+
"feature.md": "Feature",
25+
"docs.md": "Documentation",
26+
"refactor.md": "Refactor",
27+
"test.md": "Test",
28+
"performance.md": "Performance",
29+
"security.md": "Security"
30+
}
31+
32+
# Type mapping for PR templates
33+
TYPE_MAPPING = {
34+
"bug": "bug.md",
35+
"fix": "bug.md",
36+
"feature": "feature.md",
37+
"enhancement": "feature.md",
38+
"docs": "docs.md",
39+
"documentation": "docs.md",
40+
"refactor": "refactor.md",
41+
"cleanup": "refactor.md",
42+
"test": "test.md",
43+
"testing": "test.md",
44+
"performance": "performance.md",
45+
"optimization": "performance.md",
46+
"security": "security.md"
47+
}
48+
2149

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

@@ -143,30 +171,14 @@ async def analyze_file_changes(
143171
@mcp.tool()
144172
async def get_pr_templates() -> str:
145173
"""List available PR templates with their content."""
146-
templates = []
147-
148-
# Define default templates
149-
default_templates = {
150-
"bug.md": "Bug Fix",
151-
"feature.md": "Feature",
152-
"docs.md": "Documentation",
153-
"refactor.md": "Refactor",
154-
"test.md": "Test",
155-
"performance.md": "Performance",
156-
"security.md": "Security"
157-
}
158-
159-
for filename, template_type in default_templates.items():
160-
template_path = TEMPLATES_DIR / filename
161-
162-
# Read template content
163-
content = template_path.read_text()
164-
165-
templates.append({
174+
templates = [
175+
{
166176
"filename": filename,
167177
"type": template_type,
168-
"content": content
169-
})
178+
"content": (TEMPLATES_DIR / filename).read_text()
179+
}
180+
for filename, template_type in DEFAULT_TEMPLATES.items()
181+
]
170182

171183
return json.dumps(templates, indent=2)
172184

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

187-
# Map change types to template files
188-
type_mapping = {
189-
"bug": "bug.md",
190-
"fix": "bug.md",
191-
"feature": "feature.md",
192-
"enhancement": "feature.md",
193-
"docs": "docs.md",
194-
"documentation": "docs.md",
195-
"refactor": "refactor.md",
196-
"cleanup": "refactor.md",
197-
"test": "test.md",
198-
"testing": "test.md",
199-
"performance": "performance.md",
200-
"optimization": "performance.md",
201-
"security": "security.md"
202-
}
203-
204199
# Find matching template
205-
template_file = type_mapping.get(change_type.lower(), "feature.md")
200+
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
206201
selected_template = next(
207202
(t for t in templates if t["filename"] == template_file),
208203
templates[0] # Default to first template if no match

projects/unit3/github-actions-integration/solution/server.py

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
import os
99
import subprocess
10-
from typing import Dict, Any, Optional
10+
from typing import Optional
1111
from pathlib import Path
1212

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

21+
# Default PR templates
22+
DEFAULT_TEMPLATES = {
23+
"bug.md": "Bug Fix",
24+
"feature.md": "Feature",
25+
"docs.md": "Documentation",
26+
"refactor.md": "Refactor",
27+
"test.md": "Test",
28+
"performance.md": "Performance",
29+
"security.md": "Security"
30+
}
31+
2132
# File where webhook server stores events
2233
EVENTS_FILE = Path(__file__).parent / "github_events.json"
2334

35+
# Type mapping for PR templates
36+
TYPE_MAPPING = {
37+
"bug": "bug.md",
38+
"fix": "bug.md",
39+
"feature": "feature.md",
40+
"enhancement": "feature.md",
41+
"docs": "docs.md",
42+
"documentation": "docs.md",
43+
"refactor": "refactor.md",
44+
"cleanup": "refactor.md",
45+
"test": "test.md",
46+
"testing": "test.md",
47+
"performance": "performance.md",
48+
"optimization": "performance.md",
49+
"security": "security.md"
50+
}
51+
2452

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

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

@@ -122,30 +150,14 @@ async def analyze_file_changes(
122150
@mcp.tool()
123151
async def get_pr_templates() -> str:
124152
"""List available PR templates with their content."""
125-
templates = []
126-
127-
# Define default templates
128-
default_templates = {
129-
"bug.md": "Bug Fix",
130-
"feature.md": "Feature",
131-
"docs.md": "Documentation",
132-
"refactor.md": "Refactor",
133-
"test.md": "Test",
134-
"performance.md": "Performance",
135-
"security.md": "Security"
136-
}
137-
138-
for filename, template_type in default_templates.items():
139-
template_path = TEMPLATES_DIR / filename
140-
141-
# Read template content
142-
content = template_path.read_text()
143-
144-
templates.append({
153+
templates = [
154+
{
145155
"filename": filename,
146156
"type": template_type,
147-
"content": content
148-
})
157+
"content": (TEMPLATES_DIR / filename).read_text()
158+
}
159+
for filename, template_type in DEFAULT_TEMPLATES.items()
160+
]
149161

150162
return json.dumps(templates, indent=2)
151163

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

166-
# Map change types to template files
167-
type_mapping = {
168-
"bug": "bug.md",
169-
"fix": "bug.md",
170-
"feature": "feature.md",
171-
"enhancement": "feature.md",
172-
"docs": "docs.md",
173-
"documentation": "docs.md",
174-
"refactor": "refactor.md",
175-
"cleanup": "refactor.md",
176-
"test": "test.md",
177-
"testing": "test.md",
178-
"performance": "performance.md",
179-
"optimization": "performance.md",
180-
"security": "security.md"
181-
}
182-
183178
# Find matching template
184-
template_file = type_mapping.get(change_type.lower(), "feature.md")
179+
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
185180
selected_template = next(
186181
(t for t in templates if t["filename"] == template_file),
187182
templates[0] # Default to first template if no match

projects/unit3/github-actions-integration/starter/server.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
import os
99
import subprocess
10-
from typing import Dict, List, Any, Optional
10+
from typing import Optional
1111
from pathlib import Path
1212
from datetime import datetime
1313

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

22+
# Default PR templates
23+
DEFAULT_TEMPLATES = {
24+
"bug.md": "Bug Fix",
25+
"feature.md": "Feature",
26+
"docs.md": "Documentation",
27+
"refactor.md": "Refactor",
28+
"test.md": "Test",
29+
"performance.md": "Performance",
30+
"security.md": "Security"
31+
}
32+
2233
# TODO: Add path to events file where webhook_server.py stores events
2334
# Hint: EVENTS_FILE = Path(__file__).parent / "github_events.json"
2435

36+
# Type mapping for PR templates
37+
TYPE_MAPPING = {
38+
"bug": "bug.md",
39+
"fix": "bug.md",
40+
"feature": "feature.md",
41+
"enhancement": "feature.md",
42+
"docs": "docs.md",
43+
"documentation": "docs.md",
44+
"refactor": "refactor.md",
45+
"cleanup": "refactor.md",
46+
"test": "test.md",
47+
"testing": "test.md",
48+
"performance": "performance.md",
49+
"optimization": "performance.md",
50+
"security": "security.md"
51+
}
52+
2553

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

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

96124
except subprocess.CalledProcessError as e:
97-
return f"Error analyzing changes: {e.stderr}"
125+
return json.dumps({"error": f"Git error: {e.stderr}"})
98126
except Exception as e:
99-
return f"Error: {str(e)}"
127+
return json.dumps({"error": str(e)})
100128

101129

102130
@mcp.tool()
103131
async def get_pr_templates() -> str:
104132
"""List available PR templates with their content."""
105-
templates = []
106-
107-
# Define default templates
108-
default_templates = {
109-
"bug.md": "Bug Fix",
110-
"feature.md": "Feature",
111-
"docs.md": "Documentation",
112-
"refactor.md": "Refactor",
113-
"test.md": "Test",
114-
"performance.md": "Performance",
115-
"security.md": "Security"
116-
}
117-
118-
for filename, template_type in default_templates.items():
119-
template_path = TEMPLATES_DIR / filename
120-
121-
# Read template content
122-
content = template_path.read_text()
123-
124-
templates.append({
133+
templates = [
134+
{
125135
"filename": filename,
126136
"type": template_type,
127-
"content": content
128-
})
137+
"content": (TEMPLATES_DIR / filename).read_text()
138+
}
139+
for filename, template_type in DEFAULT_TEMPLATES.items()
140+
]
129141

130142
return json.dumps(templates, indent=2)
131143

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

146-
# Map change types to template files
147-
type_mapping = {
148-
"bug": "bug.md",
149-
"fix": "bug.md",
150-
"feature": "feature.md",
151-
"enhancement": "feature.md",
152-
"docs": "docs.md",
153-
"documentation": "docs.md",
154-
"refactor": "refactor.md",
155-
"cleanup": "refactor.md",
156-
"test": "test.md",
157-
"testing": "test.md",
158-
"performance": "performance.md",
159-
"optimization": "performance.md",
160-
"security": "security.md"
161-
}
162-
163158
# Find matching template
164-
template_file = type_mapping.get(change_type.lower(), "feature.md")
159+
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
165160
selected_template = next(
166161
(t for t in templates if t["filename"] == template_file),
167162
templates[0] # Default to first template if no match

0 commit comments

Comments
 (0)