Created
February 2, 2024 15:23
-
-
Save andrewbolster/6106884f0b27805707c8f53a72245f16 to your computer and use it in GitHub Desktop.
As part of a Atlassian merge+migration where we needed to have all _old_ useraccounts manually migrate their content to _new_ user accounts, to then have the whole instance merged, I didn't want to go through 300 pages of updates. So I wrote this.
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
from tqdm.auto import tqdm | |
from requests.exceptions import HTTPError | |
from getpass import getpass | |
import os | |
from atlassian import Confluence | |
def get(label): | |
if label not in os.environ: | |
os.environ[label]=getpass(f'Enter {label}') | |
return os.environ[label] | |
confluence = Confluence( | |
url=get('url'), #https://example.atlassian.net/' | |
username=get('user_name'), # [email protected] | |
password=get('api_key'), # from https://id.atlassian.com/manage-profile/security/api-tokens | |
cloud=True | |
) | |
user = confluence.get('/rest/api/user/current') | |
ids = [result['user']['accountId'] for result in confluence.get('/rest/api/search/user', params={'cql':f"type=user and user = '{user['displayName']}'"})['results']] | |
assert len(ids) == 2, 'Surprising number of IDs, braking' | |
lookup = {ids[0]:ids[1], ids[1]:ids[0]} # this is a lazy double-mapping; presuming that the 'order' is 'whomever I am, make the owner the other one' | |
new_owner_id = lookup[user['accountId']] | |
def update_page_owner(page, new_owner_id): | |
page_detail = confluence.get(f"api/v2/pages/{page['id']}", params={'body-format':'storage'}) | |
data={ | |
'id':page['id'], | |
'status':page['status'], | |
'title':page['title'], | |
'ownerId': new_owner_id, | |
'body': {k:v for k,v in page_detail['body']['storage'].items() if k in ['value','representation','storage','atlas_doc_format','wiki']}, | |
'version':{'number': confluence.get(f"rest/api/content/{page['id']}")['version']['number']+1, | |
'message': 'Automatic Owner Transformation', | |
'minorEdit': True} | |
} | |
return confluence.put(f"api/v2/pages/{page['id']}", data=data, advanced_mode=True) | |
while (owned_pages:= confluence.get('/rest/api/search', params={'cql':'type=page AND owner=currentUser()'})['results']): | |
for page in (pbar:= tqdm(owned_pages)): | |
try: | |
pbar.set_description(page['title']) | |
update_page_owner(page['content'], id_lookup[page['authorId']]) | |
except HTTPError as e: | |
print(f"Failed with {e} on {page['title']}; {confluence.url}{page['content']['_links']['webui']}") | |
except: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment