-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_renderer.py
More file actions
219 lines (183 loc) · 7.44 KB
/
template_renderer.py
File metadata and controls
219 lines (183 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""Template renderer for WT-TASK.md generation."""
from pathlib import Path
import structlog
from models import IssueData, PRData
class TemplateRenderer:
"""Render WT-TASK.md template with GitHub issue/PR data."""
def __init__(self, template_path: Path) -> None:
"""Initialize template renderer.
Args:
template_path: Path to WT-TASK.md template file
"""
self.logger: structlog.typing.FilteringBoundLogger = structlog.get_logger(__name__)
self.template_path: Path = template_path
def render_for_issue(
self,
issue_data: IssueData,
parent_path: str | Path | None = None,
worktree_name: str | None = None,
branch_name: str | None = None,
) -> str:
"""Render template for GitHub issue.
Args:
issue_data: Issue data from GitHub
parent_path: Parent directory path (for worktree context)
worktree_name: Worktree directory name
branch_name: Branch name
Returns:
Rendered template string
"""
self.logger.info(
"Rendering template for issue",
number=issue_data.number,
title=issue_data.title[:50],
)
template = self._load_template()
replacements = self._build_issue_replacements(issue_data, parent_path, worktree_name, branch_name)
result = self._apply_replacements(template, replacements)
self.logger.debug(
"Template rendered successfully",
number=issue_data.number,
result_length=len(result),
)
return result
def render_for_pr(
self,
pr_data: PRData,
parent_path: str | Path | None = None,
worktree_name: str | None = None,
branch_name: str | None = None,
) -> str:
"""Render template for GitHub PR.
Args:
pr_data: PR data from GitHub
parent_path: Parent directory path (for worktree context)
worktree_name: Worktree directory name
branch_name: Branch name
Returns:
Rendered template string
"""
self.logger.info(
"Rendering template for PR",
number=pr_data.number,
title=pr_data.title[:50],
)
template = self._load_template()
replacements = self._build_pr_replacements(pr_data, parent_path, worktree_name, branch_name)
result = self._apply_replacements(template, replacements)
self.logger.debug(
"Template rendered successfully",
number=pr_data.number,
result_length=len(result),
)
return result
def _load_template(self) -> str:
"""Load template from file.
Returns:
Template content as string
Raises:
RuntimeError: If template file not found or cannot be read
"""
self.logger.debug("Loading template", path=str(self.template_path))
if not self.template_path.exists():
self.logger.error("Template file not found", path=str(self.template_path))
raise RuntimeError(f"Template file not found: {self.template_path}")
try:
template = self.template_path.read_text()
self.logger.debug("Template loaded successfully", template_length=len(template))
return template
except OSError as e:
self.logger.error("Failed to read template file", path=str(self.template_path), error=str(e))
raise RuntimeError(f"Failed to read template file: {self.template_path}") from e
def _build_issue_replacements(
self,
issue_data: IssueData,
parent_path: str | Path | None = None,
worktree_name: str | None = None,
branch_name: str | None = None,
) -> dict[str, str]:
"""Build placeholder replacements for issue.
Args:
issue_data: Issue data from GitHub
parent_path: Parent directory path (for worktree context)
worktree_name: Worktree directory name
branch_name: Branch name
Returns:
Dictionary mapping placeholders to values
"""
from datetime import datetime
# Build comments section
comments_text = ""
if issue_data.comments:
comments_text = "\n".join(
[
f"### Comment by {comment.author} on {comment.created_at}\n{comment.body}\n"
for comment in issue_data.comments
]
)
return {
"{{ISSUE_OR_PR}}": "Issue",
"{{ISSUE_OR_PR_NUMBER}}": str(issue_data.number),
"{{ISSUE_NUMBER}}": str(issue_data.number),
"{{TITLE}}": issue_data.title,
"{{AUTHOR}}": issue_data.author,
"{{GITHUB_URL}}": issue_data.url,
"{{FULL_ISSUE_OR_PR_BODY_CONTENT}}": issue_data.body or "No description provided.",
"{{GITHUB_COMMENTS}}": comments_text,
"{{PARENT_PATH}}": str(parent_path) if parent_path else "PARENT_PATH_PLACEHOLDER",
"{{WORKTREE_NAME}}": worktree_name or "WORKTREE_NAME_PLACEHOLDER",
"{{BRANCH_NAME}}": branch_name or "BRANCH_NAME_PLACEHOLDER",
"{{CREATED_DATE}}": datetime.now().strftime("%Y-%m-%d"),
}
def _build_pr_replacements(
self,
pr_data: PRData,
parent_path: str | Path | None = None,
worktree_name: str | None = None,
branch_name: str | None = None,
) -> dict[str, str]:
"""Build placeholder replacements for PR.
Args:
pr_data: PR data from GitHub
parent_path: Parent directory path (for worktree context)
worktree_name: Worktree directory name
branch_name: Branch name
Returns:
Dictionary mapping placeholders to values
"""
from datetime import datetime
# Build comments section
comments_text = ""
if pr_data.comments:
comments_text = "\n".join(
[
f"### Comment by {comment.author} on {comment.created_at}\n{comment.body}\n"
for comment in pr_data.comments
]
)
return {
"{{ISSUE_OR_PR}}": "Pull Request",
"{{ISSUE_OR_PR_NUMBER}}": str(pr_data.number),
"{{ISSUE_NUMBER}}": str(pr_data.number),
"{{TITLE}}": pr_data.title,
"{{AUTHOR}}": pr_data.author,
"{{GITHUB_URL}}": pr_data.url,
"{{FULL_ISSUE_OR_PR_BODY_CONTENT}}": pr_data.body or "No description provided.",
"{{GITHUB_COMMENTS}}": comments_text,
"{{PARENT_PATH}}": str(parent_path) if parent_path else "PARENT_PATH_PLACEHOLDER",
"{{WORKTREE_NAME}}": worktree_name or "WORKTREE_NAME_PLACEHOLDER",
"{{BRANCH_NAME}}": branch_name or "BRANCH_NAME_PLACEHOLDER",
"{{CREATED_DATE}}": datetime.now().strftime("%Y-%m-%d"),
}
def _apply_replacements(self, template: str, replacements: dict[str, str]) -> str:
"""Apply placeholder replacements to template.
Args:
template: Template string with placeholders
replacements: Dictionary mapping placeholders to values
Returns:
Rendered template string
"""
result = template
for placeholder, value in replacements.items():
result = result.replace(placeholder, value)
return result