forked from RittmanMead/md_to_conf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
md2conf.py
executable file
·929 lines (738 loc) · 31 KB
/
md2conf.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#!/usr/bin/env python3
"""
# --------------------------------------------------------------------------------------------------
# Rittman Mead Markdown to Confluence Tool
# --------------------------------------------------------------------------------------------------
# Create or Update Atlas pages remotely using markdown files.
#
# --------------------------------------------------------------------------------------------------
# Usage: rest_md2conf.py markdown spacekey
# --------------------------------------------------------------------------------------------------
"""
import logging
import sys
import os
import re
import json
import collections
import mimetypes
import codecs
import argparse
import urllib
import webbrowser
import requests
import markdown
logging.basicConfig(level=logging.INFO, format='%(asctime)s - \
%(levelname)s - %(funcName)s [%(lineno)d] - \
%(message)s')
LOGGER = logging.getLogger(__name__)
# ArgumentParser to parse arguments and options
PARSER = argparse.ArgumentParser()
PARSER.add_argument("markdownFile", help="Full path of the markdown file to convert and upload.")
PARSER.add_argument('spacekey',
help="Confluence Space key for the page. If omitted, will use user space.")
PARSER.add_argument('-u', '--username', help='Confluence username if $CONFLUENCE_USERNAME not set.')
PARSER.add_argument('-p', '--apikey', help='Confluence API key if $CONFLUENCE_API_KEY not set.')
PARSER.add_argument('-o', '--orgname',
help='Confluence organisation if $CONFLUENCE_ORGNAME not set. '
'e.g. https://XXX.atlassian.net/wiki'
'If orgname contains a dot, considered as the fully qualified domain name.'
'e.g. https://XXX')
PARSER.add_argument('-a', '--ancestor',
help='Parent page under which page will be created or moved.')
PARSER.add_argument('-t', '--attachment', nargs='+',
help='Attachment(s) to upload to page. Paths relative to the markdown file.')
PARSER.add_argument('-c', '--contents', action='store_true', default=False,
help='Use this option to generate a contents page.')
PARSER.add_argument('-g', '--nogo', action='store_true', default=False,
help='Use this option to skip navigation after upload.')
PARSER.add_argument('-n', '--nossl', action='store_true', default=False,
help='Use this option if NOT using SSL. Will use HTTP instead of HTTPS.')
PARSER.add_argument('-d', '--delete', action='store_true', default=False,
help='Use this option to delete the page instead of create it.')
PARSER.add_argument('-l', '--loglevel', default='INFO',
help='Use this option to set the log verbosity.')
PARSER.add_argument('-s', '--simulate', action='store_true', default=False,
help='Use this option to only show conversion result.')
PARSER.add_argument('-v', '--version', type=int, action='store', default=1,
help='Version of confluence page (default is 1).')
PARSER.add_argument('-mds', '--markdownsrc', action='store', default='default',
choices=['default', 'bitbucket'],
help='Use this option to specify a markdown source (i.e. what processor this markdown was targeting). '
'Possible values: bitbucket.')
PARSER.add_argument('--label', action='append', dest='labels', default=[],
help='A list of labels to set on the page.')
PARSER.add_argument('--property', action='append', dest='properties', default=[],
type=lambda kv: kv.split("="),
help='A list of content properties to set on the page.')
PARSER.add_argument('--title', action='store', dest='title', default=None,
help='Set the title for the page, otherwise the title is going to be the first line in the markdown file')
PARSER.add_argument('--remove-emojies', action='store_true', dest='remove_emojies', default=False,
help='Remove emojies if there are any. This may be need if the database doesn\'t support emojies')
ARGS = PARSER.parse_args()
# Assign global variables
try:
# Set log level
LOGGER.setLevel(getattr(logging, ARGS.loglevel.upper(), None))
MARKDOWN_FILE = ARGS.markdownFile
SPACE_KEY = ARGS.spacekey
USERNAME = os.getenv('CONFLUENCE_USERNAME', ARGS.username)
API_KEY = os.getenv('CONFLUENCE_API_KEY', ARGS.apikey)
ORGNAME = os.getenv('CONFLUENCE_ORGNAME', ARGS.orgname)
ANCESTOR = ARGS.ancestor
NOSSL = ARGS.nossl
DELETE = ARGS.delete
SIMULATE = ARGS.simulate
VERSION = ARGS.version
MARKDOWN_SOURCE = ARGS.markdownsrc
LABELS = ARGS.labels
PROPERTIES = dict(ARGS.properties)
ATTACHMENTS = ARGS.attachment
GO_TO_PAGE = not ARGS.nogo
CONTENTS = ARGS.contents
TITLE = ARGS.title
REMOVE_EMOJIES = ARGS.remove_emojies
if USERNAME is None:
LOGGER.error('Error: Username not specified by environment variable or option.')
sys.exit(1)
if API_KEY is None:
LOGGER.error('Error: API key not specified by environment variable or option.')
sys.exit(1)
if not os.path.exists(MARKDOWN_FILE):
LOGGER.error('Error: Markdown file: %s does not exist.', MARKDOWN_FILE)
sys.exit(1)
if SPACE_KEY is None:
SPACE_KEY = '~%s' % (USERNAME)
if ORGNAME is not None:
if ORGNAME.find('.') != -1:
CONFLUENCE_API_URL = 'https://%s' % ORGNAME
else:
CONFLUENCE_API_URL = 'https://%s.atlassian.net/wiki' % ORGNAME
else:
LOGGER.error('Error: Org Name not specified by environment variable or option.')
sys.exit(1)
if NOSSL:
CONFLUENCE_API_URL.replace('https://', 'http://')
except Exception as err:
LOGGER.error('\n\nException caught:\n%s ', err)
LOGGER.error('\nFailed to process command line arguments. Exiting.')
sys.exit(1)
def convert_comment_block(html):
"""
Convert markdown code bloc to Confluence hidden comment
:param html: string
:return: modified html string
"""
open_tag = '<ac:placeholder>'
close_tag = '</ac:placeholder>'
html = html.replace('<!--', open_tag).replace('-->', close_tag)
return html
def create_table_of_content(html):
"""
Check for the string '[TOC]' and replaces it the Confluence "Table of Content" macro
:param html: string
:return: modified html string
"""
html = re.sub(
r'<p>\[TOC\]</p>',
'<p><ac:structured-macro ac:name="toc" ac:schema-version="1"/></p>',
html,
1)
return html
def convert_code_block(html):
"""
Convert html code blocks to Confluence macros
:param html: string
:return: modified html string
"""
code_blocks = re.findall(r'<pre><code.*?>.*?</code></pre>', html, re.DOTALL)
if code_blocks:
for tag in code_blocks:
conf_ml = '<ac:structured-macro ac:name="code">'
conf_ml = conf_ml + '<ac:parameter ac:name="theme">Midnight</ac:parameter>'
conf_ml = conf_ml + '<ac:parameter ac:name="linenumbers">true</ac:parameter>'
lang = re.search('code class="(.*)"', tag)
if lang:
lang = lang.group(1)
else:
lang = 'none'
conf_ml = conf_ml + '<ac:parameter ac:name="language">' + lang + '</ac:parameter>'
content = re.search(r'<pre><code.*?>(.*?)</code></pre>', tag, re.DOTALL).group(1)
content = '<ac:plain-text-body><![CDATA[' + content + ']]></ac:plain-text-body>'
conf_ml = conf_ml + content + '</ac:structured-macro>'
conf_ml = conf_ml.replace('<', '<').replace('>', '>')
conf_ml = conf_ml.replace('"', '"').replace('&', '&')
html = html.replace(tag, conf_ml)
return html
def remove_emojies(html):
"""
Remove emojies if there are any
:param html: string
:return: modified html string
"""
regrex_pattern = re.compile(pattern = "["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]+", flags = re.UNICODE)
return regrex_pattern.sub(r'',html)
def convert_info_macros(html):
"""
Converts html for info, note or warning macros
:param html: html string
:return: modified html string
"""
info_tag = '<p><ac:structured-macro ac:name="info"><ac:rich-text-body><p>'
note_tag = info_tag.replace('info', 'note')
warning_tag = info_tag.replace('info', 'warning')
close_tag = '</p></ac:rich-text-body></ac:structured-macro></p>'
# Custom tags converted into macros
html = html.replace('<p>~?', info_tag).replace('?~</p>', close_tag)
html = html.replace('<p>~!', note_tag).replace('!~</p>', close_tag)
html = html.replace('<p>~%', warning_tag).replace('%~</p>', close_tag)
# Convert block quotes into macros
quotes = re.findall('<blockquote>(.*?)</blockquote>', html, re.DOTALL)
if quotes:
for quote in quotes:
note = re.search('^<.*>Note', quote.strip(), re.IGNORECASE)
warning = re.search('^<.*>Warning', quote.strip(), re.IGNORECASE)
if note:
clean_tag = strip_type(quote, 'Note')
macro_tag = clean_tag.replace('<p>', note_tag).replace('</p>', close_tag).strip()
elif warning:
clean_tag = strip_type(quote, 'Warning')
macro_tag = clean_tag.replace('<p>', warning_tag).replace('</p>', close_tag).strip()
else:
macro_tag = quote.replace('<p>', info_tag).replace('</p>', close_tag).strip()
html = html.replace('<blockquote>%s</blockquote>' % quote, macro_tag)
# Convert doctoc to toc confluence macro
html = convert_doctoc(html)
return html
def convert_doctoc(html):
"""
Convert doctoc to confluence macro
:param html: html string
:return: modified html string
"""
toc_tag = '''<p>
<ac:structured-macro ac:name="toc">
<ac:parameter ac:name="printable">true</ac:parameter>
<ac:parameter ac:name="style">disc</ac:parameter>
<ac:parameter ac:name="maxLevel">7</ac:parameter>
<ac:parameter ac:name="minLevel">1</ac:parameter>
<ac:parameter ac:name="type">list</ac:parameter>
<ac:parameter ac:name="outline">clear</ac:parameter>
<ac:parameter ac:name="include">.*</ac:parameter>
</ac:structured-macro>
</p>'''
html = re.sub('\<\!\-\- START doctoc.*END doctoc \-\-\>', toc_tag, html, flags=re.DOTALL)
return html
def strip_type(tag, tagtype):
"""
Strips Note or Warning tags from html in various formats
:param tag: tag name
:param tagtype: tag type
:return: modified tag
"""
tag = re.sub('%s:\s' % tagtype, '', tag.strip(), re.IGNORECASE)
tag = re.sub('%s\s:\s' % tagtype, '', tag.strip(), re.IGNORECASE)
tag = re.sub('<.*?>%s:\s<.*?>' % tagtype, '', tag, re.IGNORECASE)
tag = re.sub('<.*?>%s\s:\s<.*?>' % tagtype, '', tag, re.IGNORECASE)
tag = re.sub('<(em|strong)>%s:<.*?>\s' % tagtype, '', tag, re.IGNORECASE)
tag = re.sub('<(em|strong)>%s\s:<.*?>\s' % tagtype, '', tag, re.IGNORECASE)
tag = re.sub('<(em|strong)>%s<.*?>:\s' % tagtype, '', tag, re.IGNORECASE)
tag = re.sub('<(em|strong)>%s\s<.*?>:\s' % tagtype, '', tag, re.IGNORECASE)
string_start = re.search('<.*?>', tag)
tag = upper_chars(tag, [string_start.end()])
return tag
def upper_chars(string, indices):
"""
Make characters uppercase in string
:param string: string to modify
:param indices: character indice to change to uppercase
:return: uppercased string
"""
upper_string = "".join(c.upper() if i in indices else c for i, c in enumerate(string))
return upper_string
def slug(string, lowercase):
"""
Creates a slug string
:param string: string to modify
:param lowercase: bool indicating whether string has to be lowercased
:return: slug string
"""
slug_string = string
if lowercase:
slug_string = string.lower()
# Remove all html code tags
slug_string = re.sub(r'<[^>]+>', '', slug_string)
# Remove html code like '&'
slug_string = re.sub(r'&[a-z]+;', '', slug_string)
# Replace all spaces ( ) with dash (-)
slug_string = re.sub(r'[ ]', '-', slug_string)
# Remove all special chars, except for dash (-)
slug_string = re.sub(r'[^a-zA-Z0-9-]', '', slug_string)
return slug_string
def process_refs(html):
"""
Process references
:param html: html string
:return: modified html string
"""
refs = re.findall('\n(\[\^(\d)\].*)|<p>(\[\^(\d)\].*)', html)
if refs:
for ref in refs:
if ref[0]:
full_ref = ref[0].replace('</p>', '').replace('<p>', '')
ref_id = ref[1]
else:
full_ref = ref[2]
ref_id = ref[3]
full_ref = full_ref.replace('</p>', '').replace('<p>', '')
html = html.replace(full_ref, '')
href = re.search('href="(.*?)"', full_ref).group(1)
superscript = '<a id="test" href="%s"><sup>%s</sup></a>' % (href, ref_id)
html = html.replace('[^%s]' % ref_id, superscript)
return html
def get_page(title):
"""
Retrieve page details by title
:param title: page tile
:return: Confluence page info
"""
LOGGER.info('\tRetrieving page information: %s', title)
url = '%s/rest/api/content?title=%s&spaceKey=%s&expand=version,ancestors' % (
CONFLUENCE_API_URL, urllib.parse.quote_plus(title), SPACE_KEY)
# We retrieve content property values as part of page content
# to make sure we are able to update them later
if PROPERTIES:
url = '%s,%s' % (url, ','.join("metadata.properties.%s" % v for v in PROPERTIES.keys()))
session = requests.Session()
retry_max_requests=5
retry_backoff_factor=0.1
retry_status_forcelist=(404, 500, 501, 502, 503, 504)
retry = requests.adapters.Retry(
total=retry_max_requests,
connect=retry_max_requests,
read=retry_max_requests,
backoff_factor=retry_backoff_factor,
status_forcelist=retry_status_forcelist,
)
adapter = requests.adapters.HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.auth = (USERNAME, API_KEY)
response = session.get(url)
# Check for errors
try:
response.raise_for_status()
except requests.RequestException as err:
LOGGER.error('err.response: %s', err)
if response.status_code == 404:
LOGGER.error('Error: Page not found. Check the following are correct:')
LOGGER.error('\tSpace Key : %s', SPACE_KEY)
LOGGER.error('\tOrganisation Name: %s', ORGNAME)
else:
LOGGER.error('Error: %d - %s', response.status_code, response.content)
sys.exit(1)
data = response.json()
LOGGER.debug("data: %s", str(data))
if len(data[u'results']) >= 1:
page_id = data[u'results'][0][u'id']
version_num = data[u'results'][0][u'version'][u'number']
link = '%s%s' % (CONFLUENCE_API_URL, data[u'results'][0][u'_links'][u'webui'])
try:
LOGGER.info(str(data))
properties = data[u'results'][0][u'metadata'][u'properties']
except KeyError:
# In case when page has no content properties we can simply ignore them
properties = {}
pass
page_info = collections.namedtuple('PageInfo', ['id', 'version', 'link', 'properties'])
page = page_info(page_id, version_num, link, properties)
return page
return False
# Scan for images and upload as attachments if found
def add_images(page_id, html):
"""
Scan for images and upload as attachments if found
:param page_id: Confluence page id
:param html: html string
:return: html with modified image reference
"""
source_folder = os.path.dirname(os.path.abspath(MARKDOWN_FILE))
for tag in re.findall('<img(.*?)\/>', html):
rel_path = re.search('src="(.*?)"', tag).group(1)
alt_text = re.search('alt="(.*?)"', tag).group(1)
abs_path = os.path.join(source_folder, rel_path)
basename = os.path.basename(rel_path)
upload_attachment(page_id, abs_path, alt_text)
if re.search('http.*', rel_path) is None:
if CONFLUENCE_API_URL.endswith('/wiki'):
html = html.replace('%s' % (rel_path),
'/wiki/download/attachments/%s/%s' % (page_id, basename))
else:
html = html.replace('%s' % (rel_path),
'/download/attachments/%s/%s' % (page_id, basename))
return html
def add_contents(html):
"""
Add contents page
:param html: html string
:return: modified html string
"""
contents_markup = '<ac:structured-macro ac:name="toc">\n<ac:parameter ac:name="printable">' \
'true</ac:parameter>\n<ac:parameter ac:name="style">disc</ac:parameter>'
contents_markup = contents_markup + '<ac:parameter ac:name="maxLevel">5</ac:parameter>\n' \
'<ac:parameter ac:name="minLevel">1</ac:parameter>'
contents_markup = contents_markup + '<ac:parameter ac:name="class">rm-contents</ac:parameter>\n' \
'<ac:parameter ac:name="exclude"></ac:parameter>\n' \
'<ac:parameter ac:name="type">list</ac:parameter>'
contents_markup = contents_markup + '<ac:parameter ac:name="outline">false</ac:parameter>\n' \
'<ac:parameter ac:name="include"></ac:parameter>\n' \
'</ac:structured-macro>'
html = contents_markup + '\n' + html
return html
def add_attachments(page_id, files):
"""
Add attachments for an array of files
:param page_id: Confluence page id
:param files: list of files to attach to the given Confluence page
:return: None
"""
source_folder = os.path.dirname(os.path.abspath(MARKDOWN_FILE))
if files:
for file in files:
upload_attachment(page_id, os.path.join(source_folder, file), '')
def add_local_refs(page_id, title, html):
"""
Convert local links to correct confluence local links
:param page_title: string
:param page_id: integer
:param html: string
:return: modified html string
"""
ref_prefixes = {
"default": "#",
"bitbucket": "#markdown-header-"
}
ref_postfixes = {
"default": "_%d",
"bitbucket": "_%d"
}
# We ignore local references in case of unknown or unspecified markdown source
if not MARKDOWN_SOURCE in ref_prefixes or \
not MARKDOWN_SOURCE in ref_postfixes:
LOGGER.warning('Local references weren''t processed because '
'--markdownsrc wasn''t set or specified source isn''t supported')
return html
ref_prefix = ref_prefixes[MARKDOWN_SOURCE]
ref_postfix = ref_postfixes[MARKDOWN_SOURCE]
LOGGER.info('Converting confluence local links...')
headers = re.findall(r'<h\d+>(.*?)</h\d+>', html, re.DOTALL)
if headers:
headers_map = {}
headers_count = {}
for header in headers:
key = ref_prefix + slug(header, True)
if VERSION == 1:
value = re.sub(r'(<.+?>|[ ])', '', header)
if VERSION == 2:
value = slug(header, False)
if key in headers_map:
alt_count = headers_count[key]
alt_key = key + (ref_postfix % alt_count)
alt_value = value + ('.%s' % alt_count)
headers_map[alt_key] = alt_value
headers_count[key] = alt_count + 1
else:
headers_map[key] = value
headers_count[key] = 1
links = re.findall(r'<a href="#.+?">.+?</a>', html)
if links:
for link in links:
matches = re.search(r'<a href="(#.+?)">(.+?)</a>', link)
ref = matches.group(1)
alt = matches.group(2)
result_ref = headers_map.get(ref)
if result_ref:
base_uri = '%s/spaces/%s/pages/%s/%s' % (CONFLUENCE_API_URL, SPACE_KEY, page_id, '+'.join(title.split()))
if VERSION == 1:
replacement_uri = '%s#%s-%s' % (base_uri, ''.join(title.split()), result_ref)
replacement = '<ac:link ac:anchor="%s"><ac:plain-text-link-body><![CDATA[%s]]></ac:plain-text-link-body></ac:link>' % (result_ref, re.sub(r'( *<.+?> *)', ' ', alt))
if VERSION == 2:
replacement_uri = '%s#%s' % (base_uri, result_ref)
replacement = '<a href="%s" title="%s">%s</a>' % (replacement_uri, alt, alt)
html = html.replace(link, replacement)
return html
def create_page(title, body, ancestors):
"""
Create a new page
:param title: confluence page title
:param body: confluence page content
:param ancestors: confluence page ancestor
:return:
"""
LOGGER.info('Creating page...')
url = '%s/rest/api/content/' % CONFLUENCE_API_URL
session = requests.Session()
session.auth = (USERNAME, API_KEY)
session.headers.update({'Content-Type': 'application/json'})
new_page = {
'type': 'page',
'title': title,
'space': {'key': SPACE_KEY},
'body': {
'storage': {
'value': body,
'representation': 'storage'
}
},
'ancestors': ancestors,
'metadata': {
'properties': {
'editor': {
'value': 'v%d' % VERSION
}
}
}
}
LOGGER.debug("data: %s", json.dumps(new_page))
response = session.post(url, data=json.dumps(new_page))
try:
response.raise_for_status()
except requests.exceptions.HTTPError as excpt:
LOGGER.error("error: %s - %s", excpt, response.content)
exit(1)
if response.status_code == 200:
data = response.json()
space_name = data[u'space'][u'name']
page_id = data[u'id']
version = data[u'version'][u'number']
link = '%s%s' % (CONFLUENCE_API_URL, data[u'_links'][u'webui'])
LOGGER.info('Page created in %s with ID: %s.', space_name, page_id)
LOGGER.info('URL: %s', link)
# Populate properties dictionary with initial property values
properties = {}
if PROPERTIES:
for key in PROPERTIES:
properties[key] = {"key": key, "version": 1, "value": PROPERTIES[key]}
img_check = re.search('<img(.*?)\/>', body)
local_ref_check = re.search('<a href="(#.+?)">(.+?)</a>', body)
if img_check or local_ref_check or properties or ATTACHMENTS or LABELS:
LOGGER.info('\tAttachments, local references, content properties or labels found, update procedure called.')
update_page(page_id, title, body, version, ancestors, properties, ATTACHMENTS)
else:
if GO_TO_PAGE:
webbrowser.open(link)
else:
LOGGER.error('Could not create page.')
sys.exit(1)
def delete_page(page_id):
"""
Delete a page
:param page_id: confluence page id
:return: None
"""
LOGGER.info('Deleting page...')
url = '%s/rest/api/content/%s' % (CONFLUENCE_API_URL, page_id)
session = requests.Session()
session.auth = (USERNAME, API_KEY)
session.headers.update({'Content-Type': 'application/json'})
response = session.delete(url)
response.raise_for_status()
if response.status_code == 204:
LOGGER.info('Page %s deleted successfully.', page_id)
else:
LOGGER.error('Page %s could not be deleted.', page_id)
def update_page(page_id, title, body, version, ancestors, properties, attachments):
"""
Update a page
:param page_id: confluence page id
:param title: confluence page title
:param body: confluence page content
:param version: confluence page version
:param ancestors: confluence page ancestor
:param attachments: confluence page attachments
:return: None
"""
LOGGER.info('Updating page...')
# Add images and attachments
body = add_images(page_id, body)
add_attachments(page_id, attachments)
# Add local references
body = add_local_refs(page_id, title, body)
url = '%s/rest/api/content/%s' % (CONFLUENCE_API_URL, page_id)
session = requests.Session()
session.auth = (USERNAME, API_KEY)
session.headers.update({'Content-Type': 'application/json'})
page_json = {
"id": page_id,
"type": "page",
"title": title,
"space": {"key": SPACE_KEY},
"body": {
"storage": {
"value": body,
"representation": "storage"
}
},
"version": {
"number": version + 1,
"minorEdit" : True
},
'ancestors': ancestors
}
if LABELS:
if 'metadata' not in page_json:
page_json['metadata'] = {}
labels = []
for value in LABELS:
labels.append({"name": value})
page_json['metadata']['labels'] = labels
response = session.put(url, data=json.dumps(page_json))
# Check for errors
try:
response.raise_for_status()
except requests.RequestException as err:
LOGGER.error('err.response: %s', err)
if response.status_code == 404:
LOGGER.error('Error: Page not found. Check the following are correct:')
LOGGER.error('\tSpace Key : %s', SPACE_KEY)
LOGGER.error('\tOrganisation Name: %s', ORGNAME)
else:
LOGGER.error('Error: %d - %s', response.status_code, response.content)
sys.exit(1)
if response.status_code == 200:
data = response.json()
link = '%s%s' % (CONFLUENCE_API_URL, data[u'_links'][u'webui'])
LOGGER.info("Page updated successfully.")
LOGGER.info('URL: %s', link)
if properties:
LOGGER.info("Updating page content properties...")
for key in properties:
prop_url = '%s/property/%s' % (url, key)
prop_json = {"key": key, "version": {"number": properties[key][u"version"]}, "value": properties[key][u"value"]}
response = session.put(prop_url, data=json.dumps(prop_json))
response.raise_for_status()
if response.status_code == 200:
LOGGER.info("\tUpdated property %s", key)
if GO_TO_PAGE:
webbrowser.open(link)
else:
LOGGER.error("Page could not be updated.")
def get_attachment(page_id, filename):
"""
Get page attachment
:param page_id: confluence page id
:param filename: attachment filename
:return: attachment info in case of success, False otherwise
"""
url = '%s/rest/api/content/%s/child/attachment?filename=%s' % (CONFLUENCE_API_URL, page_id, filename)
session = requests.Session()
session.auth = (USERNAME, API_KEY)
response = session.get(url)
response.raise_for_status()
data = response.json()
if len(data[u'results']) >= 1:
att_id = data[u'results'][0]['id']
att_info = collections.namedtuple('AttachmentInfo', ['id'])
attr_info = att_info(att_id)
return attr_info
return False
def upload_attachment(page_id, file, comment):
"""
Upload an attachement
:param page_id: confluence page id
:param file: attachment file
:param comment: attachment comment
:return: boolean
"""
if re.search('http.*', file):
return False
content_type = mimetypes.guess_type(file)[0]
filename = os.path.basename(file)
if not os.path.isfile(file):
LOGGER.error('File %s cannot be found --> skip ', file)
return False
file_to_upload = {
'comment': comment,
'file': (filename, open(file, 'rb'), content_type, {'Expires': '0'})
}
attachment = get_attachment(page_id, filename)
if attachment:
url = '%s/rest/api/content/%s/child/attachment/%s/data' % (CONFLUENCE_API_URL, page_id, attachment.id)
else:
url = '%s/rest/api/content/%s/child/attachment/' % (CONFLUENCE_API_URL, page_id)
session = requests.Session()
session.auth = (USERNAME, API_KEY)
session.headers.update({'X-Atlassian-Token': 'no-check'})
LOGGER.info('\tUploading attachment %s...', filename)
response = session.post(url, files=file_to_upload)
response.raise_for_status()
return True
def main():
"""
Main program
:return:
"""
LOGGER.info('\t\t----------------------------------')
LOGGER.info('\t\tMarkdown to Confluence Upload Tool')
LOGGER.info('\t\t----------------------------------\n\n')
LOGGER.info('Markdown file:\t%s', MARKDOWN_FILE)
LOGGER.info('Space Key:\t%s', SPACE_KEY)
if TITLE:
title = TITLE
else:
with open(MARKDOWN_FILE, 'r') as mdfile:
title = mdfile.readline().lstrip('#').strip()
mdfile.seek(0)
LOGGER.info('Title:\t\t%s', title)
with codecs.open(MARKDOWN_FILE, 'r', 'utf-8') as mdfile:
html = mdfile.read()
html = markdown.markdown(html, extensions=['tables', 'fenced_code', 'footnotes'])
if not TITLE:
html = '\n'.join(html.split('\n')[1:])
html = create_table_of_content(html)
html = convert_info_macros(html)
html = convert_comment_block(html)
html = convert_code_block(html)
if REMOVE_EMOJIES:
html = remove_emojies(html)
if CONTENTS:
html = add_contents(html)
html = process_refs(html)
LOGGER.debug('html: %s', html)
if SIMULATE:
LOGGER.info("Simulate mode is active - stop processing here.")
sys.exit(0)
LOGGER.info('Checking if Atlas page exists...')
page = get_page(title)
if DELETE and page:
delete_page(page.id)
sys.exit(1)
if ANCESTOR:
parent_page = get_page(ANCESTOR)
if parent_page:
ancestors = [{'type': 'page', 'id': parent_page.id}]
else:
LOGGER.error('Error: Parent page does not exist: %s', ANCESTOR)
sys.exit(1)
else:
ancestors = []
if page:
# Populate properties dictionary with updated property values
properties = {}
if PROPERTIES:
for key in PROPERTIES:
if key in page.properties:
properties[key] = {"key": key, "version": page.properties[key][u'version'][u'number'] + 1, "value": PROPERTIES[key]}
else:
properties[key] = {"key": key, "version": 1, "value": PROPERTIES[key]}
update_page(page.id, title, html, page.version, ancestors, properties, ATTACHMENTS)
else:
create_page(title, html, ancestors)
LOGGER.info('Markdown Converter completed successfully.')
if __name__ == "__main__":
main()