-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfluence.py
192 lines (160 loc) · 6.15 KB
/
confluence.py
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
import json
import logging
from mime_types import MimeTypes
import os
import requests
import urllib
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
CONFLUENCE_USER = os.environ.get("CONFLUENCE_USER", "admin")
CONFLUENCE_PASS = os.environ.get("CONFLUENCE_PASS", "admin")
CONFLUENCE_COOKIE = os.environ.get("CONFLUENCE_COOKIE", "")
CONFLUENCE_SERVER_URL = os.environ.get("CONFLUENCE_SERVER_URL", "https://wiki.singular.net")
IMPORT_WORD_URL = "{base}/pages/worddav/importword.action".format(base=CONFLUENCE_SERVER_URL)
DO_IMPORT_URL = "{base}/pages/worddav/doimportword.action".format(base=CONFLUENCE_SERVER_URL)
GOOGLE_SPREADSHEET_TEMPLATE = open("templates/google_spreadsheet.html", 'r').read()
GOOGLE_SLIDES_TEMPLATE = open("templates/google_slides.html", 'r').read()
FOLDER_TEMPLATE = open("templates/folder.html", 'r').read()
ITEM_ID_MACRO = "{item_id}"
def create_page(space_key, title, body="", parent_id=None, google_item=None):
url = "{base}/rest/api/content".format(base=CONFLUENCE_SERVER_URL)
if google_item and body == "":
body = get_body_for_google_item(google_item)
payload = {
"type": "page",
"title": title,
"space": {
"key": space_key
},
"body": {
"storage": {
"value": body,
"representation": "storage"
}
}
}
if parent_id:
payload["ancestors"] = [{"id": parent_id}]
response = requests.request("POST", url, auth=(CONFLUENCE_USER, CONFLUENCE_PASS), json=payload)
print(response.status_code)
return json.loads(response.text)
def update_page(space_key, page_id, title, body=""):
url = "{base}/rest/api/content/{page_id}".format(base=CONFLUENCE_SERVER_URL, page_id=page_id)
payload = {
"id": page_id,
"type": "page",
"title": title,
"space": {
"key": space_key
},
"body": {
"storage": {
"value": body,
"representation": "storage"
}
},
"version": {"number": 2}
}
response = requests.request("PUT", url, auth=(CONFLUENCE_USER, CONFLUENCE_PASS), json=payload)
print(response.status_code)
return json.loads(response.text)
def comment_on_page(page_id, comment_text):
url = "{base}/rest/api/content".format(base=CONFLUENCE_SERVER_URL)
payload = {
"type": "comment",
"container": {
"type": "page",
"id": page_id
},
"body": {
"storage": {
"value": comment_text,
"representation": "storage"
}
}
}
response = requests.request("POST", url, auth=(CONFLUENCE_USER, CONFLUENCE_PASS), json=payload)
print(response.status_code)
return json.loads(response.text)
def upload_attachment(page_id, file_name, file_path, comment):
url = "{base}/rest/api/content/{page_id}/child/attachment".format(base=CONFLUENCE_SERVER_URL, page_id=page_id)
files = {'file': (file_name, open(file_path, 'rb'))}
payload = {
"comment": comment
}
headers = {
'X-Atlassian-Token': "no-check",
}
response = requests.request("POST", url,
headers=headers,
auth=(CONFLUENCE_USER, CONFLUENCE_PASS),
data=payload,
files=files)
print(response.status_code)
return json.loads(response.text)
def import_google_doc(google_item, page_item):
page_id = page_item['id']
_import_word(page_id, google_item['file_name'], google_item['file_content'])
_do_import_word(page_id, 0, google_item['name'])
def get_body_for_google_item(google_item):
body = None
mime_type = google_item['mimeType']
if mime_type == MimeTypes.GOOGLE_APPS_FOLDER:
body = FOLDER_TEMPLATE
elif mime_type == MimeTypes.GOOGLE_SPREADSHEET:
body = GOOGLE_SPREADSHEET_TEMPLATE
elif google_item['mimeType'] == MimeTypes.GOOGLE_SLIDES:
body = GOOGLE_SLIDES_TEMPLATE
if not body:
return None
body = body.format(item_id=google_item['id'])
return body
def embed_google_content(google_item, page_item):
space_key = page_item['space']['key']
page_id = page_item['id']
title = page_item['title']
body = get_body_for_google_item(google_item)
return update_page(space_key, page_id, title, body)
def _import_word(page_id, file_name, file_content):
querystring = {"pageId": page_id}
files = {'filename': (file_name, file_content)}
headers = {
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'X-Atlassian-Token': "no-check",
"Cookie": "JSESSIONID={};".format(CONFLUENCE_COOKIE)
}
response = requests.request("POST", IMPORT_WORD_URL,
files=files,
headers=headers,
params=querystring,
auth=(CONFLUENCE_USER, CONFLUENCE_PASS))
print(response.status_code)
with open('response.html', 'w') as f:
f.write(response.text)
def _do_import_word(page_id, tree_depth, doc_title, conflict=1, import_to_space=False, overwrite_all=False, level=0):
payload = {
"pageId": page_id,
"treeDepth": tree_depth,
"advanced": 'true',
"docTitle": doc_title,
"importSpace": 'true' if import_to_space else 'false',
"overwriteAll": 'true' if overwrite_all else 'false',
"conflict": conflict,
"lvl": level,
"submit": "Import"
}
data = urllib.parse.urlencode(payload)
print(data)
data = data.encode("ascii")
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'X-Atlassian-Token': "no-check",
"Cookie": "JSESSIONID={};".format(CONFLUENCE_COOKIE)
}
response = requests.request("POST", DO_IMPORT_URL,
data=data,
headers=headers,
auth=(CONFLUENCE_USER, CONFLUENCE_PASS))
print(response.status_code)
with open('response.html', 'w') as f:
f.write(response.text)