Download and cache rt data

This commit is contained in:
Michal Čihař 2019-05-25 11:01:10 +02:00
parent 560911282c
commit e20d2eb150
2 changed files with 44 additions and 1 deletions

2
.gitignore vendored
View file

@ -105,3 +105,5 @@ venv.bak/
# configuration
rt2zammad.json
# cache
rt2zammad.cache

View file

@ -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)