From 8e8ff86f9f0645c0f2145f1e7039f6f02ed3aa15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Mon, 22 Apr 2024 17:25:43 +0200 Subject: [PATCH] [fs2zammad] do not crash on failed ticket import --- fs2zammad | 59 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/fs2zammad b/fs2zammad index baf921a..3586ed6 100755 --- a/fs2zammad +++ b/fs2zammad @@ -110,28 +110,7 @@ def maybe_create_zammad_user(userdata, zammad_session, attr="login", default=Non # id, {"roles": roles.append("Agent")} # ) -### main logic ### - -if not os.path.exists("rt2zammad.json"): - print("Missing rt2zammad.json!") - print("Create one based on following template:") - print(TEMPLATE) - sys.exit(1) - -with open("rt2zammad.json") as handle: - config = json.load(handle) - -h2t = html2text.HTML2Text() -zammad = get_zammad_session() - -os.makedirs("users", exist_ok=True) -os.makedirs("tickets", exist_ok=True) -os.makedirs("attachments", exist_ok=True) - -ticket_ids = os.listdir("tickets/") -print(f"Found {len(ticket_ids)} tickets on filesystem.") - -for id in ticket_ids: +def create_zammad_ticket(id, zammad, h2t): with open(f"tickets/{id}", "rb") as handle: rt_ticket = pickle.load(handle) @@ -186,7 +165,7 @@ for id in ticket_ids: # Ignore comments for merged tickets. if merged: - continue + return # Internal note regarding the RT-Zammad import. TicketArticle(zammad).create( @@ -258,3 +237,37 @@ for id in ticket_ids: if restore_creator_to != None: zammad.ticket.update(zammad_ticket["id"], {"customer_id": restore_creator_to}) + +### main logic ### + +if not os.path.exists("rt2zammad.json"): + print("Missing rt2zammad.json!") + print("Create one based on following template:") + print(TEMPLATE) + sys.exit(1) + +with open("rt2zammad.json") as handle: + config = json.load(handle) + +h2t = html2text.HTML2Text() +zammad = get_zammad_session() + +os.makedirs("users", exist_ok=True) +os.makedirs("tickets", exist_ok=True) +os.makedirs("attachments", exist_ok=True) +os.makedirs("failed", exist_ok=True) + +ticket_ids = os.listdir("tickets/") +print(f"Found {len(ticket_ids)} tickets on filesystem.") + +for id in ticket_ids: + try: + create_zammad_ticket(id, zammad, h2t) + except KeyboardInterrupt: + print("Received keyboard interrupt. Exiting.") + sys.exit() + except: + print(f"Failed to import RT#{id}") + dumpfile = f"failed/{id}" + with open(dumpfile, "wb") as handle: + pickle.dump(id, handle)