| name | pr-documentation-uml |
| description | Generate PR documentation with UML diagrams based on Git branch differences. Explains modification reasons, content, and implementation. Uses Mermaid to draw architecture, class, sequence diagrams with exact code element names matching actual code. |
| license | Apache-2.0 |
| metadata | [object Object] |
| allowed-tools | Shell(git:*) ReadFile Grep Glob |
PR Documentation with UML
This skill helps you generate high-quality Pull Request documentation by combining code diff analysis with UML visualization to clearly explain why changes were made, what was changed, and how they work.
When to Use
- Preparing to submit a Pull Request
- Need to explain complex code changes
- Before Code Review, need detailed documentation
- Need to visualize architecture or process changes
Workflow
1. Get Code Differences
First, use Git commands to get the diff between two commits or branches:
# Compare two commits
git diff <commit1> <commit2>
# Compare current branch with main branch
git diff main...HEAD
# Find branch point
git merge-base main feature-branch
2. Analyze Code Changes with Static Analysis
This is the MOST CRITICAL step - use static analysis tools to extract accurate code element information:
# Use the provided Python static analysis tool
python scripts/analyze_code.py <file_path>
The tool outputs structured information about:
- Module imports and dependencies
- Class definitions (names, bases, methods, attributes)
- Function definitions (names, parameters, return types)
- Type annotations
- Decorators
See examples/analysis/ for detailed usage examples of how to analyze different code patterns.
3. Choose Appropriate UML Diagram Types
Select the most suitable UML diagram based on change type:
| Change Type | Recommended UML | Mermaid Type |
|---|---|---|
| Architecture refactor, module dependencies | Package/Component diagram | graph |
| Add/modify class relationships | Class diagram | classDiagram |
| API interaction flow | Sequence diagram | sequenceDiagram |
| State change logic | State diagram | stateDiagram-v2 |
| Business process changes | Flowchart | flowchart |
| Data model changes | ER diagram | erDiagram |
4. Generate PR Documentation
Use the provided document template (references/PR_TEMPLATE.md) with these sections:
- Change Overview - Brief description of PR purpose
- Reason for Changes - Explain why these modifications are needed
- Change Details - List modified files and code
- Architecture/Flow Diagrams - Visualize with Mermaid UML
- Implementation Details - Explain key implementation logic
- Testing - How to verify the changes
- Impact Scope - List affected modules
Critical Rules
⚠️ Ensure Accurate Code Element Names
This is the most critical requirement! All code elements (class names, function names, parameter names, module names) in PR docs and UML diagrams must exactly match the actual code.
Correct Approach:
- Always use
scripts/analyze_code.pyfirst to extract actual code structure - Use
ReadFileto read relevant files and verify names - Use
Grepto search and confirm class/function definitions - Copy-paste the extracted names directly into UML diagrams
Example Workflow:
# 1. Analyze the modified file
python scripts/analyze_code.py src/auth/authenticator.py
# Output will show:
# Classes:
# - JWTAuthenticator
# Methods: authenticate(token: str) -> User
# generate_token(user: User) -> str
# verify_token(token: str) -> bool
# 2. Use these EXACT names in your Mermaid class diagram
Wrong Approach:
- ❌ Guess class or function names based on understanding
- ❌ Use simplified or generalized names
- ❌ Write UML code without verification
Writing Mermaid Syntax
Check references/MERMAID_SYNTAX.md for complete Mermaid syntax reference including:
- Class diagram syntax and relationships
- Sequence diagram with interactions
- State diagram with transitions
- Flowchart with decision logic
- ER diagram with relationships
- Styling and annotations
Static Analysis Tool Usage
The scripts/analyze_code.py tool is your primary interface for extracting accurate code information.
Basic Usage
# Analyze a single file
python scripts/analyze_code.py path/to/file.py
# Analyze multiple files
python scripts/analyze_code.py src/module1.py src/module2.py
# Output as JSON for programmatic use
python scripts/analyze_code.py --format json path/to/file.py
Output Format
The tool provides structured output including:
File: src/auth/authenticator.py
Imports:
- from typing import Optional
- from .models import User
- import jwt
Classes:
JWTAuthenticator(BaseAuthenticator)
Attributes:
- secret_key: str
- algorithm: str
Methods:
+ authenticate(token: str) -> Optional[User]
+ generate_token(user: User, expires_in: int = 3600) -> str
+ verify_token(token: str) -> bool
- _decode_token(token: str) -> dict
Decorators: []
Functions:
create_authenticator(config: dict) -> JWTAuthenticator
Parameters: config: dict
Returns: JWTAuthenticator
Analysis Examples
See examples/ directory for executable examples:
01_basic_class_analysis.py- Analyzing simple classes02_inheritance_analysis.py- Analyzing class hierarchies03_decorator_analysis.py- Handling decorators and properties
Each example can be run directly:
python3 examples/01_basic_class_analysis.py
Or imported as a module to use the analysis functions in your own code.
All examples have corresponding tests in tests/ directory to verify correctness.
Example Workflow
Suppose you're documenting a PR that adds user authentication:
# 1. View the diff
git diff main...feature/auth
# 2. Analyze the new authentication module
python scripts/analyze_code.py src/auth/authenticator.py
# Output shows:
# Classes:
# JWTAuthenticator(BaseAuthenticator)
# Methods: authenticate(token: str) -> Optional[User]
# 3. Create sequence diagram using EXACT names
In your PR documentation:
## Authentication Flow
The new `JWTAuthenticator` class handles token-based authentication:
```mermaid
sequenceDiagram
participant Client
participant API
participant JWTAuthenticator
participant UserRepository
Client->>API: POST /login
API->>JWTAuthenticator: authenticate(token)
JWTAuthenticator->>UserRepository: find_user()
UserRepository-->>JWTAuthenticator: User
JWTAuthenticator-->>API: Optional[User]
API-->>Client: Response
```
Best Practices
- Analyze Before Drawing - Always run static analysis first
- Verify Names - Cross-check extracted names with
ReadFileorGrep - Choose Appropriate Granularity - Don't show too many details in one diagram
- Use Multiple Diagrams - Show changes from different angles (architecture → class → flow)
- Add Annotations - Include key explanations in UML diagrams
- Maintain Consistency - Keep naming consistent across diagrams
- Test Rendering - Ensure Mermaid code renders correctly
Reference Materials
references/PR_TEMPLATE.md- PR documentation templatereferences/MERMAID_SYNTAX.md- Complete Mermaid syntax referencescripts/analyze_code.py- Python code static analysis toolexamples/- Executable Python examples demonstrating analysis usagetests/- Comprehensive test suite validating analysis accuracy
Output Format
The generated PR documentation should be a Markdown file containing:
- Clear section structure
- Renderable Mermaid code blocks (using ```mermaid)
- Accurate code references (wrapped in backticks)
- Code snippets when necessary
- Well-illustrated explanations
The document should allow reviewers to:
- Quickly understand the change intent
- Understand architecture/flow changes through UML diagrams
- Verify implementation is reasonable
- Identify potential issues
- Trust that all code element names are accurate