From e3eb49427b2e4b18c955c933dfcb8e5734c30fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Mon, 24 Jun 2024 15:57:37 +0200 Subject: [PATCH] [fs2zammad] add processed folder, sort input tickets, sleep on failure --- fs2zammad | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/fs2zammad b/fs2zammad index 30963b5..3f65a7a 100755 --- a/fs2zammad +++ b/fs2zammad @@ -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)