119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
import markdown
|
|
import requests
|
|
from requests.auth import HTTPBasicAuth
|
|
import json
|
|
import sys
|
|
|
|
input = sys.argv[1]
|
|
company = input.split("/")[6]
|
|
|
|
def readFile(company):
|
|
rootdir = "/Users/normrasmussen/Documents/Northpass/Scripts/API_Notes/SampleNotes/"
|
|
with open(rootdir+company+".md", "r") as companyfile:
|
|
notes = companyfile.read()
|
|
notes = markdown.markdown(notes)
|
|
getContent(company, notes)
|
|
|
|
def createNewPage(company, notes):
|
|
url = "https://northpass.atlassian.net/wiki/rest/api/content/"
|
|
auth = HTTPBasicAuth("nrasmussen@northpass.com", "qf9Il7X4wkthgQKBOIly5737")
|
|
headers = {
|
|
"X-Atlassian-Token": "no-check",
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json"
|
|
}
|
|
payload = json.dumps( {
|
|
"type":"page",
|
|
"title": company,
|
|
"ancestors":[{"id":2210463745}],
|
|
"space":
|
|
{"key":"~350535240"},
|
|
"body":
|
|
{"storage":
|
|
{"value": notes,
|
|
"representation":"storage"}}
|
|
} )
|
|
response = requests.request(
|
|
"POST",
|
|
url,
|
|
data=payload,
|
|
headers=headers,
|
|
auth=auth
|
|
)
|
|
#print("createNewPage function has run")
|
|
response = json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
|
|
#print(response)
|
|
exists = "already exists"
|
|
# This if statement checks if the response from the call includes that the page already exists.
|
|
# If the page does exist, it will get the ID and Version of the page and then run a PUT call to update the page.
|
|
if exists in response:
|
|
#print("This page exists! Updating page instead.")
|
|
getContent(company, notes)
|
|
|
|
def getContent(company, notes):
|
|
url = "https://northpass.atlassian.net/wiki/rest/api/content/search?cql=parent=2210463745&expand=body.storage,version"
|
|
# Found the answer to this URL here: https://community.atlassian.com/t5/Confluence-questions/How-can-i-get-the-page-version-using-a-specific-page-id/qaq-p/898721
|
|
auth = HTTPBasicAuth("nrasmussen@northpass.com", "qf9Il7X4wkthgQKBOIly5737")
|
|
headers = {
|
|
"Accept": "application/json",
|
|
}
|
|
response = requests.request("GET", url, headers=headers, auth=auth)
|
|
jsonResponse = response.json()
|
|
text = json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",",": "))
|
|
#print(text)
|
|
listCompanies = []
|
|
for response in jsonResponse['results']:
|
|
if response['title'] == company:
|
|
#print(f"{company} Found.")
|
|
version = int(response["version"]["_links"]["self"][-1])
|
|
if version == "":
|
|
version = 1
|
|
else:
|
|
pass
|
|
id = response['id']
|
|
#print(id)
|
|
#print(version)
|
|
updatePage(company, notes, id, version)
|
|
else:
|
|
listCompanies.append(response["title"])
|
|
pass
|
|
#print("Other Companies, not pertinent right now. List of companies:")
|
|
#print(listCompanies)
|
|
|
|
def updatePage(company, notes, id, version):
|
|
url = f"https://northpass.atlassian.net/wiki/rest/api/content/{id}"
|
|
newVersion = version+1
|
|
auth = HTTPBasicAuth("nrasmussen@northpass.com", "qf9Il7X4wkthgQKBOIly5737")
|
|
headers = {
|
|
"X-Atlassian-Token": "no-check",
|
|
"Accept": "application/json",
|
|
"Content-Type": "application/json",
|
|
"User-Agent" : "python-requests/2.28.1",
|
|
}
|
|
payload = json.dumps( {
|
|
"version": {
|
|
"number": newVersion
|
|
},
|
|
"type":"page",
|
|
"title": company,
|
|
"status": "curent",
|
|
"ancestors":[{"id": 2210463745}],
|
|
"body":
|
|
{"storage":
|
|
{"value": notes,
|
|
"representation":"storage"}}
|
|
} )
|
|
response = requests.request(
|
|
"PUT",
|
|
url,
|
|
data=payload,
|
|
headers=headers,
|
|
auth=auth
|
|
)
|
|
response = json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
|
|
#print(response)
|
|
#print("updatePage function has run")
|
|
|
|
if __name__ == "__main__":
|
|
readFile(company)
|