-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfluence.py
49 lines (39 loc) · 1.51 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
import base64
import requests
import json
from requests.auth import HTTPBasicAuth
import os
# Request URL - API for creating a new page as a child of another page
URL = 'https://novaspikes.atlassian.net/wiki/rest/api/content/'
# Request Headers
HEADERS = {
'Content-Type': 'application/json;charset=iso-8859-1',
}
def create_confluence_page_if_not_exists(page_title: str, server_name: str):
# todo: Check if page exists
# Set the title and content of the page to create
page_html = f'<p>{page_title} is a member of the {server_name} discord server.</p>'
# Request body
data = {
'type': 'page',
'title': page_title,
'ancestors': [{'id':os.getenv('CONFLUENCE_PARENT_PAGE_ID')}],
'space': {'key':os.getenv('CONFLUENCE_SPACE_KEY')},
'body': {
'storage':{
'value': page_html,
'representation':'storage',
}
}
}
# We're ready to call the api
try:
r = requests.post(url=URL, data=json.dumps(data), headers=HEADERS, auth=(os.getenv('CONFLUENCE_EMAIL'), os.getenv('CONFLUENCE_TOKEN')))
# Consider any status other than 2xx an error
if not r.status_code // 100 == 2:
print("Error: Unexpected response {}, {}".format(r.status_code, r.reason))
else:
print(f'Page created for {page_title}')
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
print("Error: {}".format(e))