[fs2zammad] add processed folder, sort input tickets, sleep on failure

This commit is contained in:
fnux 2024-06-24 15:57:37 +02:00
parent a44c5361f4
commit e3eb49427b
No known key found for this signature in database
GPG key ID: 4502C902C00A1E12

View file

@ -7,6 +7,7 @@ import pickle
import sys
import html2text
import traceback
import time
from zammad_py import ZammadAPI
from zammad_py.api import Resource, TagList, TicketArticle
@ -24,7 +25,7 @@ TEMPLATE = """{
"""
COMMENT_TEMPLATE = """
Ticket imported from Request Tracker
Ticket imported from Request Tracker (RT-#{numerical_id})
URL: https://support.ungleich.ch/Ticket/Display.html?id={numerical_id}
Created: {Created}
@ -143,7 +144,7 @@ def create_zammad_ticket(id, zammad, h2t, retries=3):
creator = zammad_creator
zammad_ticket_template = {
"title": rt_ticket["ticket"]["Subject"] or 'RT: Empty Subject',
"title": "[RT-{}] {}".format(id, rt_ticket["ticket"]["Subject"]),
"group": "Users",
"customer": creator,
"note": "RT-import:{}".format(rt_ticket["ticket"]["original_id"]),
@ -177,7 +178,7 @@ def create_zammad_ticket(id, zammad, h2t, retries=3):
# Ignore comments for merged tickets.
if merged:
return
return zammad_ticket["id"]
# Internal note regarding the RT-Zammad import.
TicketArticle(zammad).create(
@ -251,11 +252,18 @@ def create_zammad_ticket(id, zammad, h2t, retries=3):
if restore_creator_to != None:
zammad.ticket.update(zammad_ticket["id"], {"customer_id": restore_creator_to})
# Returns the new Zammad ticket ID.
return zammad_ticket["id"]
except KeyboardInterrupt:
print("Received keyboard interrupt. Exiting.")
sys.exit()
except:
print(f"Failed to import RT-#{id} .. ({retries} retries left)")
print(f"Failed to import RT-{id} .. ({retries} retries left)")
print("Sleeping 5 seconds to give Zammad some air...")
time.sleep(5)
if retries > 0:
create_zammad_ticket(id, zammad, h2t, retries - 1)
else:
@ -280,18 +288,23 @@ os.makedirs("users", exist_ok=True)
os.makedirs("tickets", exist_ok=True)
os.makedirs("attachments", exist_ok=True)
os.makedirs("failed", exist_ok=True)
os.makedirs("processed", exist_ok=True)
ticket_ids = os.listdir("tickets/")
print(f"Found {len(ticket_ids)} tickets on filesystem.")
ticket_ids = list(map(int, ticket_ids))
ticket_ids.sort()
for id in ticket_ids:
try:
create_zammad_ticket(id, zammad, h2t, 3)
zammad_ticket_id = create_zammad_ticket(id, zammad, h2t, 5)
dumpfile = f"processed/{id}"
with open(dumpfile, "w") as handle:
handle.write(f"{zammad_ticket_id}")
except SystemExit:
sys.exit()
except:
print(f"Failed to import RT#{id}")
dumpfile = f"failed/{id}"
with open(dumpfile, "wb") as handle:
pickle.dump(id, handle)
traceback.print_exc(file=handle)