From e20d2eb1503e8c70665d4b62a2de148dd7872772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Sat, 25 May 2019 11:01:10 +0200 Subject: [PATCH] Download and cache rt data --- .gitignore | 2 ++ migrate.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3e80c5b..92c5d4c 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,5 @@ venv.bak/ # configuration rt2zammad.json +# cache +rt2zammad.cache diff --git a/migrate.py b/migrate.py index 0227fc3..11d98cc 100755 --- a/migrate.py +++ b/migrate.py @@ -3,13 +3,15 @@ Quick and dirty attempt to migrate issues from Request Tracker to Zammad. """ +import pickle import json import os import sys from requests.exceptions import HTTPError from zammad_py import ZammadAPI -from rt import Rt +from zammad_py.api import TagList +from rt import Rt, ALL_QUEUES TEMPLATE = """{ "zammad_host": "", @@ -43,3 +45,42 @@ source = Rt(config['rt_url'], config['rt_user'], config['rt_pass']) if not source.login(): print('Failed to login to RT!') sys.exit(2) + +if os.path.exists('rt2zammad.cache'): + with open('rt2zammad.cache', 'rb') as handle: + data = pickle.load(handle) + users = data['users'] + queues = data['queues'] + tickets = data['tickets'] + +else: + users = {} + tickets = [] + queues = set() + + def ensure_user(username): + if username not in users: + users[username] = source.get_user(username) + + + for i in range(1, 1000): + print('Loading ticket {}'.format(i)) + ticket = source.get_ticket(i) + if ticket is None: + break + queues.add(ticket['Queue']) + ensure_user(ticket['Creator']) + ensure_user(ticket['Owner']) + history = source.get_history(i) + attachments = [] + for a in source.get_attachments_ids(i): + attachment = source.get_attachment(i, a) + attachments.append(attachments) + ensure_user(attachment['Creator']) + tickets.append({ + 'ticket': ticket, + 'history': history, + 'attachments': attachments, + }) + with open('rt2zammad.cache', 'wb') as handle: + data = pickle.dump({'users': users, 'queues': queues, 'tickets': tickets}, handle)