From e9875d7de4f334b31fa750d7a963b382aee7b9e4 Mon Sep 17 00:00:00 2001 From: akea013 Date: Mon, 26 Aug 2019 15:24:26 +1200 Subject: [PATCH] print out original storage request --- snow.py | 9 ++++++++- util/show_ticket.py | 1 + util/ticket_yaml.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 util/ticket_yaml.py diff --git a/snow.py b/snow.py index 4773b2b..9b4fa95 100755 --- a/snow.py +++ b/snow.py @@ -2,7 +2,7 @@ import sys import click -from util import login, list_tasks, show_ticket, patch +from util import login, list_tasks, show_ticket, ticket_yaml, patch class AliasedGroup(click.Group): def get_command(self, ctx, cmd_name): @@ -61,6 +61,13 @@ def show(ctx, number): """Show a ticket""" show_ticket.get_and_print_ticket(ctx.obj, number) +@snow.command(name="extract_yaml") +@click.argument('number') +@click.pass_context +def download_yaml(ctx, number): + """Show a ticket""" + ticket_yaml.extract(ctx.obj, number) + @snow.command(name="comment") @click.argument('number') @click.pass_context diff --git a/util/show_ticket.py b/util/show_ticket.py index 1360001..abef14f 100644 --- a/util/show_ticket.py +++ b/util/show_ticket.py @@ -1,5 +1,6 @@ from bs4 import BeautifulSoup import json +import re fields = [ ("Ticket details", [ diff --git a/util/ticket_yaml.py b/util/ticket_yaml.py new file mode 100644 index 0000000..16d98d4 --- /dev/null +++ b/util/ticket_yaml.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +import json +import re + +def extract(ctx, args): + BASE_URL = ctx["BASE_URL"] + s = ctx["s"] + query = "number=" + args + url = BASE_URL + "/api/now/table/task" + params = { + "sysparm_query": query, + "sysparm_display_value": "true", + } + r = s.get(url, params=params) + r = r.json() + if 'error' in r: + print(r["error"]["message"]) + return + if not r['result']: + print("Ticket not found") + return + ticket = r['result'][0] + p = re.compile(r'^General$', re.M) + ticket_start_position = re.search(p, ticket['comments']).start() + with open('orig_request.yaml','w') as orig_request: + orig_request.write(ticket['comments'][ticket_start_position:]) + print(ticket["comments"]) + print(json.dumps(ticket, indent=4, sort_keys=True)) + +# vim: fenc=utf-8: ft=python:sw=4:et:nu:fdm=indent:fdn=1:syn=python +