jira云端插件开发02-使用内容操作来计算 Confluence 页面中的宏
Posted 火腿肠烧烤大赛冠军
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jira云端插件开发02-使用内容操作来计算 Confluence 页面中的宏相关的知识,希望对你有一定的参考价值。
创建项目:
Select the confluence-macro
forge create
更新manifest.yml文件:
modules:
confluence:contentAction:
- key: macro-counter
title: Macro count
function: main
function:
- key: main
handler: index.run
app:
id: '<your-app-id>'
name: macro-counter
发布并重新安装:
forge deploy
forge install
安装@forge/api并开启隧道:
npm install @forge/api
forge tunnel
更新index.jsx文件:
// Import required components from the UI kit
import ForgeUI, { ContentAction, ModalDialog, render, Text, useAction, useProductContext, useState } from "@forge/ui";
import api, { route } from "@forge/api";
// You'll implement getContent and countMacros later
const getContent = async (contentId) => {
return {};
};
const countMacros = (data) => {
return 10;
};
const App = () => {
const [isOpen, setOpen] = useState(true);
if (!isOpen) {
return null;
}
const { contentId } = useProductContext();
const [data] = useAction(
() => null,
async () => await getContent(contentId)
);
const macroCount = countMacros(data);
return (
<ModalDialog header="Macro count" onClose={() => setOpen(false)}>
<Text>{`Number of macros on this page: ${macroCount}`}</Text>
</ModalDialog>
);
};
export const run = render(
<ContentAction>
<App/>
</ContentAction>
);
使用REST API获取宏数目:
const getContent = async (contentId) => {
const response = await api.asUser().requestConfluence(route`/rest/api/content/${contentId}?expand=body.atlas_doc_format`);
if (!response.ok) {
const err = `Error while getContent with contentId ${contentId}: ${response.status} ${response.statusText}`;
console.error(err);
throw new Error(err);
}
return await response.json();
};
const countMacros = (data) => {
if (!data || !data.body || !data.body.atlas_doc_format || !data.body.atlas_doc_format.value) {
return 0;
}
const { body: { atlas_doc_format: { value } } } = data;
const { content: contentList } = JSON.parse(value);
const macros = contentList.filter((content) => {
return content.type === "extension";
});
return macros.length;
};
重新发布程序:
forge deploy
tips:
由于获取使用个数所需要调用RESTAPI需要权限,所以仍需去manifest.yml
文件中配置权限
权限需在REST API 文档中查找
以上是关于jira云端插件开发02-使用内容操作来计算 Confluence 页面中的宏的主要内容,如果未能解决你的问题,请参考以下文章
jira云端插件开发03-在 Confluence 中构建自定义 UI 应用程序