confluence-rest-api - 用于在 Cloud Confluence 上创建空间的 Python 脚本
问题描述
我有超过 700 个项目需要在 Confluence Cloud 上创建空间。手动执行此操作是不可行的。必须想出一些脚本或工具来从文本文件中读取信息并创建空间。我有编程背景 - Python 和 Java。有人可以建议实现这一目标的解决方案吗?
解决方案
这是从 excel 中读取空间和页面名称并在 Confluence 云中创建它们的代码片段 -
import requests
import json
from requests.auth import HTTPBasicAuth
from openpyxl import load_workbook
from collections import defaultdict
filename = "path/PjtExport.xlsx"
wb = load_workbook(filename)
sheet = wb["Sheet1"]
confdict = defaultdict(list)
for i1 in range(2,10) :
if(str(sheet.cell(row=i1, column=1).value) != "None") :
spaceName = str(sheet.cell(row=i1, column=1).value)
keyName = spaceName.replace(" ","")
#print(keyName)
if not(confdict.keys().__contains__(spaceName)):
confdict.setdefault(keyName,spaceName)
#print(spaceName)
url = 'https://domain.atlassian.net/wiki/rest/api/space'
auth = HTTPBasicAuth('xxx@yyy.com', 'abcabcabc')
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = json.dumps({
"key": keyName,
"name": spaceName,
"description": {
"plain": {
"value": "Test space created in Python - XL Int",
"representation": "storage"
}
},
})
response = requests.request(
"POST",
url,
data=payload,
headers=headers,
auth=auth
)
res_data = response.json()
spaceID = str((res_data["homepage"]["id"]))
pageName = str(sheet.cell(row=i1, column=2).value)
url = "https://domain.atlassian.net/wiki/rest/api/content/aaaa/pagehierarchy/copy"
auth = HTTPBasicAuth('xxx@yyy.com', 'abcabcabc')
headers = {
"Content-Type": "application/json"
}
payload = json.dumps({
"copyAttachments": True,
"copyPermissions": True,
"copyProperties": True,
"copyLabels": True,
"copyCustomContents": True,
"destinationPageId": spaceID,
"titleOptions": {
"prefix": pageName + " ",
"replace": "",
"search": ""
}
})
response = requests.request(
"POST",
url,
data=payload,
headers=headers,
auth=auth
)
page_data = response.json()
#spaceID = str((res_data["homepage"]["id"]))
print(page_data)
推荐阅读
- css - 无法在 jenking html 报告中看到 css
- python - networkx 的数据结构是否适用于具有邻接依赖性的数值方案?
- java - 如何在运行时执行 jar 文件的同一 shell 中使用 java 执行 shell 脚本?
- sql - 更改表类型后 sp_refreshsqlmodule 期间出错
- java - Java:如何在这个指定的时间只运行一次这段代码?
- java - 低延迟 Flink 流式连接
- ios - 快速编辑 plist 文件(不在捆绑包中)
- selenium - 在量角器中使用 google-chrome-beta 而不是 stable
- python - 如何为我猜测双指数衰减选择正确的初始拟合参数?
- uml - 描述共享用例的事件流和序列图