Created
July 28, 2023 13:51
-
-
Save juandesant/335e4dabdd55f7f802edc4d7227bf091 to your computer and use it in GitHub Desktop.
Use their Atlassian Python API to set labels for the children of a given page. The script asks for the URL, and the list of tags. It then finds all children for that page, regardless of URL form, and sets the proper tags.
This file contains 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 sys | |
import os | |
from atlassian import Confluence | |
def obtain_confluence_pat(path='~/.ConfluencePAT'): | |
confluence_PAT_filename = os.path.expanduser(path) | |
confluence_PAT = None | |
if os.path.exists(confluence_PAT_filename): | |
with open(confluence_PAT_filename, "r") as pat_file: | |
confluence_PAT = pat_file.readline() | |
else: | |
from getpass import getpass | |
confluence_PAT = getpass(prompt="Confluence Personal Access Token") | |
return confluence_PAT | |
def process_tags(page_id, cf_instance=None, tags=[]): | |
for tag in tags: | |
cf_instance.set_page_label(page_id, tag) | |
def add_tags_to_page_children(page_id, cf_instance=None, tags=[]): | |
page_children = cf_instance.get_child_pages(page_id=page_id) | |
for children in page_children: | |
process_tags(children['id'], cf_instance=cf_instance, tags=tags) | |
def get_id_for_url(url="", cf_instance=None): | |
url_items = url.split("/") | |
try: | |
space_start = url_items.index("display") + 1 | |
space = url_items[space_start] | |
title = url_items[-1] | |
page_id = cf_instance.get_page_id(space, unquote_plus(title)) | |
except ValueError: | |
page_id = url_items[-1].split("=")[-1] | |
return page_id | |
if __name__ == '__main__': | |
alma_cf_url = 'https://confluence.alma.cl/' | |
alma_cf = Confluence(alma_cf_url, token = obtain_confluence_pat()) | |
page_url = input("Provide page URL: ") | |
page_id = get_id_for_url(page_url) | |
tags = [] | |
while True: | |
tag = input("Tag to add (empty to stop): ") | |
if tag != "": | |
tags.append(tag) | |
else: | |
break | |
add_tags_to_page_children(page_id, cf_instance=alma_cf, tags=tags) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment