forked from fnux/rt2zammad
Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
230d8703ff | |||
2d3526918e |
1 changed files with 29 additions and 2 deletions
31
fs2zammad
31
fs2zammad
|
@ -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")
|
||||
|
@ -284,19 +303,27 @@ with open("rt2zammad.json") as handle:
|
|||
h2t = html2text.HTML2Text()
|
||||
zammad = get_zammad_session()
|
||||
|
||||
source_directory = "tickets/"
|
||||
if len(sys.argv) >= 2:
|
||||
source_directory = sys.argv[1]
|
||||
if not os.path.isdir(source_directory):
|
||||
print(f"Could not find source directory {source_directory}. Exiting.")
|
||||
sys.exit(1)
|
||||
|
||||
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 = os.listdir(source_directory)
|
||||
print(f"Found {len(ticket_ids)} tickets on filesystem (source directory: {source_directory})")
|
||||
ticket_ids = list(map(int, ticket_ids))
|
||||
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:
|
||||
|
|
Loading…
Reference in a new issue