Created
July 2, 2024 12:49
-
-
Save dudebowski/eb879bc3558522caa71e832218f51dbb to your computer and use it in GitHub Desktop.
uploading MD document to confluence with plantuml diagramms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from os.path import join, splitext, basename | |
| import requests | |
| from markdown import markdown | |
| from mdx_gfm import GithubFlavoredMarkdownExtension | |
| from atlassian import Confluence | |
| from plantuml_markdown import PlantUMLMarkdownExtension | |
| import glob | |
| # Set generic parametersvi | |
| cloud = 'YOUR_CONFLUENCE_CLOUD_NAME' | |
| user = 'CONFLUENCE_USER' | |
| token = 'API_TOKEN' | |
| space = '~SPACE_NAME' | |
| parent_page_id = PAGE_ID | |
| def open_markdown_file(file_path): | |
| full_path = file_path | |
| print(f"Opening file: {full_path}") | |
| with open(full_path) as f: | |
| md = f.read() | |
| return md | |
| def create_confluence_page(title, space, parent_page_id, user, token): | |
| url = f"https://{cloud}.atlassian.net/wiki/rest/api/content" | |
| content = { | |
| 'type': 'page', | |
| 'title': title, | |
| 'ancestors': [{'id': parent_page_id}], | |
| 'space': {'key': space}, | |
| 'body': {'storage': {'value': '<p>Placeholder</p>', 'representation': 'storage'}} | |
| } | |
| response = requests.post(url, json=content, auth=(user, token)).json() | |
| return response['id'] | |
| def upload_confluence_content(file_path, page_id, user, token): | |
| url = f"https://{cloud}.atlassian.net/wiki/rest/api/content/{page_id}" | |
| current = requests.get(url, auth=(user, token)).json() | |
| html = markdown(open_markdown_file(file_path), extensions=[GithubFlavoredMarkdownExtension(), PlantUMLMarkdownExtension() ]) | |
| content = { | |
| 'id': current['id'], | |
| 'type': current['type'], | |
| 'title': current['title'], | |
| 'version': {'number': current['version']['number'] + 1}, | |
| 'body': { | |
| 'storage': { | |
| 'value': html, | |
| 'representation': 'storage' | |
| } | |
| } | |
| } | |
| updated = requests.put(url, json=content, auth=(user, token)).json() | |
| link = updated['_links']['base'] + updated['_links']['webui'] | |
| print(f'Uploaded content successfully to page {link}') | |
| return link | |
| def get_or_create_page(file_name, space, parent_page_id, user, token): | |
| # Convert filename to title and check for existing page | |
| title = splitext(basename(file_name))[0].replace('_', ' ').title() | |
| url = f"https://{cloud}.atlassian.net/wiki/rest/api/content?title={title}&spaceKey={space}&expand=version,ancestors" | |
| response = requests.get(url, auth=(user, token)).json() | |
| if response['size'] == 0: | |
| # Page does not exist, create it | |
| page_id = create_confluence_page(title, space, parent_page_id, user, token) | |
| print(f'Created new page with title: {title} and ID: {page_id}') | |
| else: | |
| # Page exists | |
| page_id = response['results'][0]['id'] | |
| print(f'Page with title: {title} already exists with ID: {page_id}') | |
| return page_id | |
| # Find all markdown files in the docs directory | |
| markdown_files = glob.glob(join( '/home/amutter/repos/evc-infra-doc/', 'docs/**/*.md'), recursive=True) | |
| for file_path in markdown_files: | |
| print(f"Processing file: {file_path}") | |
| page_id = get_or_create_page(file_path, space, parent_page_id, user, token) | |
| upload_confluence_content(file_path, page_id, user, token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment