[fs2zammad] remove previously (half) imported zammad ticket

This commit is contained in:
fnux 2024-08-05 11:08:41 +02:00
parent e3c375a29e
commit 2d3526918e
No known key found for this signature in database
GPG key ID: 4502C902C00A1E12

View file

@ -113,6 +113,25 @@ def maybe_create_zammad_user(userdata, zammad_session, attr="login", default=Non
return USERMAP[lowercase_email].get(attr, default)
def remove_existing_zammad_tickets_for(rt_id, zammad, retries=3):
try:
matching_zammad_tickets= zammad.ticket.search(f"title: \"[RT-{rt_id}]*\"")
if len(matching_zammad_tickets) >= 1:
print(f"Found existing ticket:")
for zt in matching_zammad_tickets:
print(f"{zt["id"]} {zt["title"]}")
print(f"Deleting Zammad ticket {zt["id"]}")
zammad.ticket.destroy(zt["id"])
except:
print(f"Failed to cleanup duplicates for RT-{rt_id} .. ({retries} retries left)")
if retries > 0:
print("Sleeping 5 seconds to give Zammad some air...")
time.sleep(5)
remove_existing_zammad_tickets_for(rt_id, zammad, retries - 1)
else:
traceback.print_exc()
raise RuntimeError
# def ensure_user_is_zammad_agent(user, zammad_session):
# print(f"Promoting user {user} to Agent")
# id = maybe_create_zammad_user(user, zammad_session, "id")
@ -297,6 +316,7 @@ ticket_ids.sort()
for id in ticket_ids:
try:
remove_existing_zammad_tickets_for(id, zammad, 5)
zammad_ticket_id = create_zammad_ticket(id, zammad, h2t, 5)
dumpfile = f"processed/{id}"
with open(dumpfile, "w") as handle: