prd-comment-server
Version:
fetch confluence page comments
53 lines (52 loc) • 1.95 kB
JavaScript
/**
* 获取指定页面的评论列表
* @param {string} pageId - Confluence 页面 ID
* @param {string} token - Bearer token
* @param {string} baseUrl - Confluence 基础 URL
* @returns {Promise<Object>} 评论数据
*/
export async function fetchComments(pageId, token, baseUrl) {
try {
const allComments = [];
let start = 0;
const limit = 25; // 每页获取25条评论
let hasMore = true;
baseUrl = baseUrl.replace(/\/$/, "");
while (hasMore) {
const url = `${baseUrl}/rest/api/content/${pageId}/child/comment?expand=body.view&limit=${limit}&start=${start}`;
const response = await fetch(url, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
method: "GET",
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.results.length === 0) {
hasMore = false;
break;
}
const results = data.results?.filter((c) => c.extensions?.location === "footer") ||
[];
// 处理当前页的评论
const pageComments = results.map((item, idx) => `评论 ${start + idx + 1}:\n${item.body.view.value}`);
allComments.push(...pageComments);
// 检查是否还有更多评论
if (data.results.length < limit) {
hasMore = false;
}
else {
start += limit;
}
}
return allComments.join("\n");
}
catch (error) {
// 由于 Node.js 环境下没有 console 对象,改用 process.stderr 输出错误信息
throw error;
}
}