@dyno181cm.nexsoft/zentao_mcp 1.2.9 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/tools.js +115 -40
- package/build/zentaoClient.js +28 -0
- package/package.json +1 -1
package/build/tools.js
CHANGED
|
@@ -42,8 +42,39 @@ function formatUser(user) {
|
|
|
42
42
|
}
|
|
43
43
|
return String(user);
|
|
44
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Find all <img src="..."> URLs in an HTML string, download each image
|
|
47
|
+
* to the local tmp directory using the authenticated ZenTao client,
|
|
48
|
+
* and replace the remote src with the local file path.
|
|
49
|
+
* Images already cached on disk are not re-downloaded.
|
|
50
|
+
*/
|
|
51
|
+
async function localizeImages(html) {
|
|
52
|
+
if (!html)
|
|
53
|
+
return html;
|
|
54
|
+
const imgRegex = /<img[^>]+src=["']([^"']+)["']/gi;
|
|
55
|
+
const matches = [...html.matchAll(imgRegex)];
|
|
56
|
+
if (matches.length === 0)
|
|
57
|
+
return html;
|
|
58
|
+
let result = html;
|
|
59
|
+
await Promise.all(matches.map(async (match) => {
|
|
60
|
+
const remoteUrl = match[1];
|
|
61
|
+
// Derive a stable local filename from the last path segment of the URL
|
|
62
|
+
const filename = `zentao_img_${path.basename(remoteUrl.split('?')[0])}`;
|
|
63
|
+
const localPath = path.join(os.tmpdir(), filename);
|
|
64
|
+
try {
|
|
65
|
+
if (!fs.existsSync(localPath)) {
|
|
66
|
+
await client.downloadImageToLocal(remoteUrl, localPath);
|
|
67
|
+
}
|
|
68
|
+
result = result.split(remoteUrl).join(localPath);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// If download fails, keep the original remote URL
|
|
72
|
+
}
|
|
73
|
+
}));
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
45
76
|
/** Format a file size in bytes to a human-readable string (B / KB / MB). */
|
|
46
|
-
|
|
77
|
+
function formatSize(bytes) {
|
|
47
78
|
if (bytes === undefined || bytes === null || bytes === '')
|
|
48
79
|
return 'unknown size';
|
|
49
80
|
const numBytes = typeof bytes === 'string' ? parseInt(bytes, 10) : bytes;
|
|
@@ -59,74 +90,118 @@ export function formatSize(bytes) {
|
|
|
59
90
|
function mcpText(text) {
|
|
60
91
|
return { content: [{ type: "text", text }] };
|
|
61
92
|
}
|
|
62
|
-
/**
|
|
63
|
-
function renderAttachments(files) {
|
|
93
|
+
/** Download all attachments to local tmp dir and render them as a Markdown section with AI hints. */
|
|
94
|
+
async function renderAttachments(files) {
|
|
64
95
|
if (!files || files.length === 0)
|
|
65
96
|
return '';
|
|
66
|
-
const
|
|
67
|
-
|
|
97
|
+
const IMAGE_EXTS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg']);
|
|
98
|
+
const VIDEO_EXTS = new Set(['mp4', 'mov', 'avi', 'mkv', 'webm']);
|
|
99
|
+
const enriched = await Promise.all(files.map(async (f) => {
|
|
100
|
+
const ext = (f.extension || '').toLowerCase();
|
|
101
|
+
const targetPath = path.join(os.tmpdir(), `zentao_file_${f.id}.${ext}`);
|
|
102
|
+
let localPath = null;
|
|
103
|
+
try {
|
|
104
|
+
localPath = fs.existsSync(targetPath)
|
|
105
|
+
? targetPath
|
|
106
|
+
: await client.downloadFile(f.id, targetPath);
|
|
107
|
+
}
|
|
108
|
+
catch { /* keep null, show download failed below */ }
|
|
109
|
+
return { title: f.title, size: f.size, localPath, ext };
|
|
110
|
+
}));
|
|
111
|
+
// Build section header hint based on media types present
|
|
112
|
+
const hasImage = enriched.some(f => f.localPath && IMAGE_EXTS.has(f.ext));
|
|
113
|
+
const hasVideo = enriched.some(f => f.localPath && VIDEO_EXTS.has(f.ext));
|
|
114
|
+
let headerHint = '';
|
|
115
|
+
if (hasImage && hasVideo)
|
|
116
|
+
headerHint = ' *(AI: Please view the images and videos below)*';
|
|
117
|
+
else if (hasImage)
|
|
118
|
+
headerHint = ' *(AI: Please view the images below)*';
|
|
119
|
+
else if (hasVideo)
|
|
120
|
+
headerHint = ' *(AI: Please view the videos below)*';
|
|
121
|
+
const lines = enriched.map((f) => {
|
|
122
|
+
if (!f.localPath)
|
|
123
|
+
return `- 📎 ${f.title} — *(download failed, Size: ${formatSize(f.size)})*`;
|
|
124
|
+
if (IMAGE_EXTS.has(f.ext))
|
|
125
|
+
return `- 📎  *(Size: ${formatSize(f.size)})*`;
|
|
126
|
+
return `- 📎 [${f.title}](${f.localPath}) *(Size: ${formatSize(f.size)})*`;
|
|
127
|
+
});
|
|
128
|
+
return `\n## Files 📎${headerHint}\n${lines.join('\n')}\n`;
|
|
68
129
|
}
|
|
69
130
|
// ─── Public formatters ───────────────────────────────────────────────────────
|
|
70
131
|
/**
|
|
71
132
|
* Format a raw ZenTao task object into a human-readable Markdown document.
|
|
72
133
|
*/
|
|
73
|
-
export function taskToMarkdown(rawTask) {
|
|
134
|
+
export async function taskToMarkdown(rawTask) {
|
|
74
135
|
if (!rawTask)
|
|
75
136
|
return "Task not found.";
|
|
76
137
|
const est = rawTask.estimate ?? 0;
|
|
77
138
|
const cons = rawTask.consumed ?? 0;
|
|
78
139
|
const left = rawTask.left ?? 0;
|
|
79
140
|
const prog = rawTask.progress ?? 0;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
''
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
`- **Estimate / Consumed / Left**: ${est}h / ${cons}h / ${left}h (${prog}%)`,
|
|
141
|
+
// ── Compact metadata row ──
|
|
142
|
+
const meta = [
|
|
143
|
+
`**Status:** ${rawTask.status || 'N/A'}`,
|
|
144
|
+
`**Priority:** ${rawTask.pri || 'N/A'}`,
|
|
145
|
+
`**Estimate/Consumed/Left:** ${est}h / ${cons}h / ${left}h (${prog}%)`,
|
|
86
146
|
];
|
|
87
147
|
if (rawTask.openedBy)
|
|
88
|
-
|
|
148
|
+
meta.push(`**Opened by:** ${formatUser(rawTask.openedBy)}`);
|
|
89
149
|
if (rawTask.assignedTo)
|
|
90
|
-
|
|
150
|
+
meta.push(`**Assigned to:** ${formatUser(rawTask.assignedTo)}`);
|
|
91
151
|
if (rawTask.finishedBy)
|
|
92
|
-
|
|
152
|
+
meta.push(`**Finished by:** ${formatUser(rawTask.finishedBy)}`);
|
|
93
153
|
if (rawTask.closedBy) {
|
|
94
|
-
const reason = rawTask.closedReason ? ` (
|
|
95
|
-
|
|
154
|
+
const reason = rawTask.closedReason ? ` (${rawTask.closedReason})` : '';
|
|
155
|
+
meta.push(`**Closed by:** ${formatUser(rawTask.closedBy)}${reason}`);
|
|
96
156
|
}
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return
|
|
157
|
+
const localizedDesc = await localizeImages(rawTask.desc);
|
|
158
|
+
const desc = htmlToMarkdown(localizedDesc);
|
|
159
|
+
const attachments = await renderAttachments(parseFiles(rawTask.files));
|
|
160
|
+
return [
|
|
161
|
+
`# Task #${rawTask.id}: ${rawTask.name}`,
|
|
162
|
+
'',
|
|
163
|
+
meta.join(' | '),
|
|
164
|
+
'',
|
|
165
|
+
'## Description',
|
|
166
|
+
desc || '*No description provided.*',
|
|
167
|
+
attachments,
|
|
168
|
+
].join('\n');
|
|
101
169
|
}
|
|
102
170
|
/**
|
|
103
171
|
* Format a raw ZenTao bug object into a human-readable Markdown document.
|
|
104
172
|
*/
|
|
105
|
-
export function bugToMarkdown(rawBug) {
|
|
173
|
+
export async function bugToMarkdown(rawBug) {
|
|
106
174
|
if (!rawBug)
|
|
107
175
|
return "Bug not found.";
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
''
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
`- **Type**: ${rawBug.type || 'N/A'}`,
|
|
176
|
+
// ── Compact metadata row ──
|
|
177
|
+
const meta = [
|
|
178
|
+
`**Status:** ${rawBug.status || 'N/A'}`,
|
|
179
|
+
`**Severity:** ${rawBug.severity || 'N/A'}`,
|
|
180
|
+
`**Priority:** ${rawBug.pri || 'N/A'}`,
|
|
181
|
+
`**Type:** ${rawBug.type || 'N/A'}`,
|
|
115
182
|
];
|
|
116
183
|
if (rawBug.openedBy)
|
|
117
|
-
|
|
184
|
+
meta.push(`**Opened by:** ${formatUser(rawBug.openedBy)}`);
|
|
118
185
|
if (rawBug.assignedTo)
|
|
119
|
-
|
|
186
|
+
meta.push(`**Assigned to:** ${formatUser(rawBug.assignedTo)}`);
|
|
120
187
|
if (rawBug.resolvedBy) {
|
|
121
|
-
const resolution = rawBug.resolution ? ` (
|
|
122
|
-
|
|
188
|
+
const resolution = rawBug.resolution ? ` (${rawBug.resolution})` : '';
|
|
189
|
+
meta.push(`**Resolved by:** ${formatUser(rawBug.resolvedBy)}${resolution}`);
|
|
123
190
|
}
|
|
124
191
|
if (rawBug.closedBy)
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return
|
|
192
|
+
meta.push(`**Closed by:** ${formatUser(rawBug.closedBy)}`);
|
|
193
|
+
const localizedSteps = await localizeImages(rawBug.steps);
|
|
194
|
+
const steps = htmlToMarkdown(localizedSteps);
|
|
195
|
+
const attachments = await renderAttachments(parseFiles(rawBug.files));
|
|
196
|
+
return [
|
|
197
|
+
`# Bug #${rawBug.id}: ${rawBug.title}`,
|
|
198
|
+
'',
|
|
199
|
+
meta.join(' | '),
|
|
200
|
+
'',
|
|
201
|
+
'## Repro Steps',
|
|
202
|
+
steps || '*No steps provided.*',
|
|
203
|
+
attachments,
|
|
204
|
+
].join('\n');
|
|
130
205
|
}
|
|
131
206
|
// ─── Download handler (extracted for Single Responsibility) ──────────────────
|
|
132
207
|
/**
|
|
@@ -170,7 +245,7 @@ export function registerTools(server) {
|
|
|
170
245
|
},
|
|
171
246
|
}, async ({ taskId }) => {
|
|
172
247
|
const data = await client.getTaskDetails(taskId);
|
|
173
|
-
return mcpText(taskToMarkdown(data));
|
|
248
|
+
return mcpText(await taskToMarkdown(data));
|
|
174
249
|
});
|
|
175
250
|
server.registerTool("zentao_get_bug_details", {
|
|
176
251
|
description: "Get detailed information of a bug",
|
|
@@ -179,7 +254,7 @@ export function registerTools(server) {
|
|
|
179
254
|
},
|
|
180
255
|
}, async ({ bugId }) => {
|
|
181
256
|
const data = await client.getBugDetails(bugId);
|
|
182
|
-
return mcpText(bugToMarkdown(data));
|
|
257
|
+
return mcpText(await bugToMarkdown(data));
|
|
183
258
|
});
|
|
184
259
|
server.registerTool("zentao_download_attachment", {
|
|
185
260
|
description: "Download a file attachment from ZenTao and save it locally",
|
package/build/zentaoClient.js
CHANGED
|
@@ -145,4 +145,32 @@ export class ZentaoClient {
|
|
|
145
145
|
});
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Downloads an image from a full authenticated URL and saves it to a local path.
|
|
150
|
+
* Uses the same authenticated Axios client so ZenTao session token is injected automatically.
|
|
151
|
+
*
|
|
152
|
+
* @param imageUrl The full image URL to download (e.g. https://zentao.../file-read-xxx.png).
|
|
153
|
+
* @param targetPath The local file path where the image should be saved.
|
|
154
|
+
* @returns A promise that resolves to the targetPath upon success, or rejects with an error.
|
|
155
|
+
*/
|
|
156
|
+
async downloadImageToLocal(imageUrl, targetPath) {
|
|
157
|
+
const writer = fs.createWriteStream(targetPath);
|
|
158
|
+
const res = await this.client.get(imageUrl, {
|
|
159
|
+
baseURL: '', // override baseURL so the full URL is used as-is
|
|
160
|
+
responseType: 'stream',
|
|
161
|
+
});
|
|
162
|
+
return new Promise((resolve, reject) => {
|
|
163
|
+
res.data.pipe(writer);
|
|
164
|
+
let error = null;
|
|
165
|
+
writer.on('error', err => {
|
|
166
|
+
error = err;
|
|
167
|
+
writer.close();
|
|
168
|
+
reject(err);
|
|
169
|
+
});
|
|
170
|
+
writer.on('close', () => {
|
|
171
|
+
if (!error)
|
|
172
|
+
resolve(targetPath);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
148
176
|
}
|