-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfluence.py
More file actions
395 lines (325 loc) · 13.6 KB
/
confluence.py
File metadata and controls
395 lines (325 loc) · 13.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import logging
import json
import requests
import re
import sys
from urllib.parse import urljoin
API_HEADERS = {
"User-Agent": "md2confluence",
}
class MissingArgumentException(Exception):
def __init__(self, arg):
self.message = "Missing required argument: {}".format(arg)
class Confluence:
def __init__(
self,
api_url=None,
username=None,
password=None,
headers=None,
loglevel="INFO",
_client=None,
):
"""Creates a new Confluence API client.
Keyword arguments:
api_url {str} -- The URL to the Confluence API root (e.g. https://my.confluence.local/wiki or https://my.confluence.local/wiki/rest/api)
username {str} -- The Confluence service account username
password {str} -- The Confluence service account password
headers {list(str)} -- The HTTP headers which will be set for all requests
loglevel {Logger Loglevel} -- Loglevel from logger class
"""
self.log = logging.getLogger("confluence")
self.loglevel = loglevel.upper()
self.log.setLevel(getattr(logging, self.loglevel, None))
# A common gotcha will be given a URL that doesn't end with a /, so we
# can account for this
if not api_url.endswith("/"):
api_url = api_url + "/"
if not api_url.endswith("/rest/api/"):
api_url = urljoin(api_url, "rest/api/")
self.api_url = api_url
self.log.info("API URL:\t\t%s" % self.api_url)
self.username = username
self.password = password
if _client is None:
_client = requests.Session()
self._session = _client
self._session.auth = (self.username, self.password)
for header in headers or []:
try:
name, value = header.split(":", 1)
except ValueError:
name, value = header, ""
self._session.headers[name] = value.lstrip()
def _require_kwargs(self, kwargs):
"""Ensures that certain kwargs have been provided
Arguments:
kwargs {dict} -- The dict of required kwargs
"""
missing = []
for k, v in kwargs.items():
if not v:
missing.append(k)
if missing:
raise MissingArgumentException(missing)
def _request(self, method="GET", path="", params=None, data=None, headers=None):
url = urljoin(self.api_url, path)
self.log.debug(
"""API Call:
{method} {url}:
Params: {params}
Data: {data}""".format(
method=method, url=url, params=params, data=data
)
)
if not headers:
headers = {}
headers.update(API_HEADERS)
if data:
headers.update({"Content-Type": "application/json"})
response = self._session.request(
method=method, url=url, params=params, json=data, headers=headers
)
if not response.ok:
try:
response_err = json.loads(response.text)
except Exception as e:
self.log.error("Can not parse response as JSON")
response_err = {'message': response.text}
self.log.error(
"{method} {url}: {status_code} - {reason}".format(
method=method,
url=url,
status_code=response.status_code,
reason=response_err.get("message"),
)
)
self.log.debug(
"""Params: {params}
Data: {data}""".format(
params=params, data=data
)
)
sys.exit(1)
return {}
# Will probably want to be more robust here, but this should work for now
return response.json()
def get(self, path=None, params=None):
return self._request(method="GET", path=path, params=params)
def post(self, path=None, params=None, data=None, files=None):
return self._request(method="POST", path=path, params=params, data=data)
def put(self, path=None, params=None, data=None):
return self._request(method="PUT", path=path, params=params, data=data)
def exists(self, space=None, title=None, ancestor_id=None):
"""Returns the Confluence page that matches the provided metdata, if it exists.
Specifically, this leverages a Confluence Query Language (CQL) query
against the Confluence API. We assume that each slug is unique, at
least to the provided space/ancestor_id.
Keyword arguments:
space {str} -- The Confluence space to use for filtering posts
title {str} -- The page title
ancestor_id {str} -- The ID of the parent page
"""
self._require_kwargs({"title": title})
self.log.debug("Getting page info")
cql_args = []
if title:
cql_args.append("title={!r}".format(title))
if ancestor_id:
cql_args.append("ancestor={}".format(ancestor_id))
if space:
cql_args.append("space={!r}".format(space))
self.log.debug("CQL params: %s" % cql_args)
cql = " and ".join(cql_args)
params = {"expand": "version", "cql": cql}
response = self.get(path="content/search", params=params)
if not response.get("size", None):
self.log.info("We didn't find any page satisfied our query. Assuming this is new page.")
return None
return response["results"][0]
def sls(self, s):
"""Stupidly simple leading white space remover :)
This is required to compare page contents in confluence and
generated one from README.md to make a desicion about page update.
sls abbreviation is for strip_leading_spaces :)
"""
return "\n".join([l.strip() for l in s.splitlines()])
def get_page_contents(self, post_id=None):
"""Get page contents
Arguments:
post_id {str} -- The ID of the Confluence post
"""
self._require_kwargs({"post_id": post_id})
path = "content/{}?expand=body.storage".format(post_id)
response = self.get(path=path)
return response.get("body", {}).get("storage", {}).get("value", "")
def compare_content(self, post_id=None, content=None):
"""Compare our content with content in Confluence
Keyword arguments:
post_id {str} - Existing post ID in Confluence
content {str} - Content to compare to
Return:
{bool} Comparison results, true if different, false is the same
"""
self._require_kwargs({"post_id": post_id, "content": content})
# item to find and cleanup -- 'ac:macro-id="bb96c594-fad4-4efd-86c4-5754db6ff55d"'
"""Parse with Confluence and cleanup our page version"""
html = self.sls(self._convert_html_to_storage(content))
macro_ids = re.findall(r" ac:macro-id=\".*?\"", html, re.DOTALL)
if macro_ids:
for macro_id in macro_ids:
html = html.replace(macro_id, "")
else:
self.log.warning("No macro IDs found in our converted xhtml!")
"""Get page and cleanup page from Confluence.
Note, no page existance checks!
"""
confluence_page = self.sls(
self._convert_html_to_storage(self.get_page_contents(post_id))
)
macro_ids = re.findall(r" ac:macro-id=\".*?\"", confluence_page, re.DOTALL)
if macro_ids:
for macro_id in macro_ids:
confluence_page = confluence_page.replace(macro_id, "")
else:
self.log.warning("No macro IDs found in Confluence page!")
"""This block is only for easier debug"""
if self.loglevel == "DEBUG":
self.log.debug("Writing Confluence page content into 'page_confluence.html'")
f = open("page_confluence.html", "w")
f.write(confluence_page)
f.close()
self.log.debug("Writing Generated content into 'page_generated.html'")
f = open("page_generated.html", "w")
f.write(html)
f.close()
return self.sls(html) != self.sls(confluence_page)
def _convert_html_to_storage(self, html=None):
"""Dummy conversion from generated xhtml to xhtml via Confluence API call for
proper comparison to have same layout and structure as Confluence save post/page contents in database.
So, we're converting from type storage to same type :)
Also, Confluence generates macro IDs on save for any macro used in post body. We'll clean up
them, because they're different each time
Keyword arguments:
html {str} -- The HTML content to parse within Confluence API
Return:
{str} -- Parsed xHTML content
"""
self._require_kwargs({"html": html})
data = {"value": html, "representation": "storage"}
converted = self.post(path="contentbody/convert/storage", data=data)
return converted.get("value", "")
def _create_page_payload(
self, content=None, title=None, ancestor_id=None, space=None, type="page"
):
"""Generate JSON payload for API call
Keyword Arguments:
content {str} -- The HTML content to upload (required)
space {str} -- The Confluence space where the page should reside
title {str} -- The page title
ancestor_id {str} -- The ID of the parent Confluence page
Return
{dict} Combined Dict to use in API call
"""
content = self._convert_html_to_storage(content)
return {
"type": type,
"title": title,
"space": {"key": space},
"body": {"storage": {"representation": "storage", "value": content}},
"ancestors": [{"id": str(ancestor_id)}],
"metadata":{
"properties":{
"content-appearance-draft":{
"value":"full-width"
},
"content-appearance-published":{
"value":"full-width"
}
}
}
}
def create(
self, content=None, space=None, title=None, ancestor_id=None, type="page"
):
"""Creates a new page with the provided content.
If an ancestor_id is specified, then the page will be created as a
child of that ancestor page.
Keyword Arguments:
content {str} -- The HTML content to upload (required)
space {str} -- The Confluence space where the page should reside
title {str} -- The page title
ancestor_id {str} -- The ID of the parent Confluence page
Return:
{bool} Returns boolean representation of result
"""
self._require_kwargs({"content": content, "title": title, "space": space})
page = self._create_page_payload(
content=content,
title=title,
ancestor_id=ancestor_id,
space=space,
type=type,
)
response = self.post(path="content/", data=page)
if not (response.get("_links", None)):
self.log.error("Can't get link to page. See errors above.")
return False
else:
page_url = response["_links"]["base"] + response["_links"]["tinyui"]
self.log.info(
'Page "{title}" (id {page_id}) created successfully at {url}'.format(
title=title, page_id=response.get("id"), url=page_url
)
)
return True
def update(
self,
post_id=None,
content=None,
space=None,
title=None,
ancestor_id=None,
page=None,
type="page",
):
"""Updates an existing page with new content.
This involves updating the attachments stored on Confluence, uploading
the page content, and finally updating the labels.
Keyword Arguments:
post_id {str} -- The ID of the Confluence post
content {str} -- The page represented in Confluence storage format
space {str} -- The Confluence space where the page should reside
title {str} -- The page title
ancestor_id {str} -- The ID of the parent Confluence page
Return:
{bool} Returns boolean representation of result
"""
self._require_kwargs(
{"content": content, "title": title, "post_id": post_id, "space": space}
)
# Next, we can create the updated page structure
new_page = self._create_page_payload(
content=content,
title=title,
ancestor_id=ancestor_id,
space=space,
type=type,
)
# Increment the version number, as required by the Confluence API
# https://docs.atlassian.com/ConfluenceServer/rest/7.1.0/#api/content-update
new_version = page["version"]["number"] + 1
new_page["version"] = {"number": new_version}
path = "content/{}".format(page["id"])
response = self.put(path=path, data=new_page)
if not (response.get("_links", None)):
self.log.error("Can't get link to page. See errors above.")
return False
else:
page_url = response["_links"]["base"] + response["_links"]["tinyui"]
self.log.info(
'Page "{title}" (id {page_id}) updated successfully at {url}'.format(
title=title, page_id=post_id, url=page_url
)
)
return True