-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-renderer.ts
More file actions
225 lines (187 loc) · 5.95 KB
/
template-renderer.ts
File metadata and controls
225 lines (187 loc) · 5.95 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
220
221
222
223
224
225
import * as core from '@actions/core';
import * as fs from 'fs';
import * as path from 'path';
import Handlebars from 'handlebars';
import { ChangedFile } from './file-detector';
import { FileDiff, formatDiffAsCodeBlock } from './diff-generator';
export interface TemplateContext {
// File information
filename: string;
file_path: string;
file_link: string;
// Diff information
diff: string;
diff_raw: string;
// Commit information
commit_sha: string;
commit_sha_short: string;
commit_link: string;
commit_message: string;
commit_date: string;
author: string;
// Pull request information
pull_request: boolean;
pr_number: string;
pr_link: string;
pr_title: string;
// Branch information
branch: string;
// Change information
change_type: string;
previous_path: string;
}
export interface RenderOptions {
titleTemplate: string;
bodyTemplate: string;
includeDiff: boolean;
includeFileLink: boolean;
includeCommitLink: boolean;
includePrLink: boolean;
sanitizeDiff: boolean;
}
export interface RenderedIssue {
title: string;
body: string;
}
// Default issue body template
const DEFAULT_TEMPLATE = `## Specification Changed
A specification file has been modified and may require implementation changes.
**File:** {{ file_path }}
**Changed in:** {{ commit_sha_short }} ({{ commit_link }})
{{#if pull_request}}
**Pull Request:** {{ pr_link }}
{{/if}}
**Author:** @{{ author }}
**Date:** {{ commit_date }}
## Changes
{{{ diff }}}
## Checklist
- [ ] Reviewed specification change
- [ ] Determined if code changes are required
- [ ] Implementation complete (or confirmed no changes needed)
---
*This issue was automatically created by [spec-ops-action](https://github.com/spec-ops-method/spec-ops-action)*`;
/**
* Renders issue title and body from templates
*/
export function renderIssue(
fileDiff: FileDiff,
options: RenderOptions,
context: Partial<TemplateContext>
): RenderedIssue {
// Build full template context
const fullContext = buildTemplateContext(fileDiff, context, options);
// Compile and render title
const titleCompiled = Handlebars.compile(options.titleTemplate);
const title = titleCompiled(fullContext);
// Get body template
const bodyTemplateContent = resolveBodyTemplate(options.bodyTemplate);
// Compile and render body
const bodyCompiled = Handlebars.compile(bodyTemplateContent);
const body = bodyCompiled(fullContext);
return { title, body };
}
/**
* Builds the full template context with all available variables
*/
function buildTemplateContext(
fileDiff: FileDiff,
baseContext: Partial<TemplateContext>,
options: RenderOptions
): TemplateContext {
const { file } = fileDiff;
// Get filename from path
const filename = path.basename(file.path);
// Format diff if included
const formattedDiff = options.includeDiff ? formatDiffAsCodeBlock(fileDiff.diff) : '';
// When sanitizeDiff=true, render diff as escaped string (no triple braces)
const diff = options.sanitizeDiff ? formattedDiff : formattedDiff;
const diff_raw = options.includeDiff ? fileDiff.diff : '';
// Build file link
const repoUrl = getRepoUrl();
const commitSha = baseContext.commit_sha || process.env.GITHUB_SHA || '';
const file_link = options.includeFileLink
? `${repoUrl}/blob/${commitSha}/${file.path}`
: '';
// Build commit link
const commit_link = options.includeCommitLink
? `${repoUrl}/commit/${commitSha}`
: '';
// Build PR link
const prNumber = baseContext.pr_number || '';
const pr_link = options.includePrLink && prNumber
? `${repoUrl}/pull/${prNumber}`
: '';
return {
// File information
filename,
file_path: file.path,
file_link,
// Diff information
diff,
diff_raw,
// Commit information
commit_sha: commitSha,
commit_sha_short: commitSha.substring(0, 7),
commit_link,
commit_message: baseContext.commit_message || '',
commit_date: baseContext.commit_date || new Date().toISOString(),
author: baseContext.author || '',
// Pull request information
pull_request: !!prNumber,
pr_number: prNumber,
pr_link,
pr_title: baseContext.pr_title || '',
// Branch information
branch: baseContext.branch || process.env.GITHUB_REF_NAME || '',
// Change information
change_type: file.changeType,
previous_path: file.previousPath || '',
};
}
/**
* Resolves the body template - either from a file path or returns inline template
*/
function resolveBodyTemplate(templateInput: string): string {
if (!templateInput || templateInput.trim() === '') {
return DEFAULT_TEMPLATE;
}
// Check if it's a file path
if (templateInput.endsWith('.md') || templateInput.includes('/')) {
try {
const templatePath = path.resolve(process.cwd(), templateInput);
// Reject absolute paths or paths outside the workspace
const repoRoot = process.cwd();
if (!templatePath.startsWith(repoRoot)) {
core.warning(`Template path resolves outside repo root: ${templatePath}. Using default template.`);
return DEFAULT_TEMPLATE;
}
if (fs.existsSync(templatePath)) {
core.debug(`Loading template from file: ${templatePath}`);
return fs.readFileSync(templatePath, 'utf-8');
} else {
core.warning(`Template file not found: ${templatePath}, using default template`);
return DEFAULT_TEMPLATE;
}
} catch (error) {
core.warning(`Error reading template file: ${error}, using default template`);
return DEFAULT_TEMPLATE;
}
}
// Treat as inline template
return templateInput;
}
/**
* Gets the repository URL from environment
*/
function getRepoUrl(): string {
const serverUrl = process.env.GITHUB_SERVER_URL || 'https://github.com';
const repository = process.env.GITHUB_REPOSITORY || '';
return `${serverUrl}/${repository}`;
}
/**
* Gets the default template content
*/
export function getDefaultTemplate(): string {
return DEFAULT_TEMPLATE;
}