Add bug statistic report generator

Go into the TestRail test plan and create a report
   with the bugs which fails(blocks)  the tests

Example:
    export TESTRAIL_USER_EMAIL='user@domain.com'
    export TESTRAIL_USER_KEY='USERKEYSTRING'
    export TESTRAIL_URL='https://domain.testrail.com'
    export TESTRAIL_PROJECT='Project Name'
    export TESTRAIL_PLAN='Plan Name'
    export JIRA_USER=username
    export JIRA_PASSWORD=usernamepass
    export JIRA_HOST='https://domain.jira.com'

   python report.py create-report \
           --testrail-host $TESTRAIL_URL \
           --testrail-user $TESTRAIL_USER \
           --testrail-key $TESTRAIL_USER_KEY \
           --testrail-project $TESTRAIL_PROJECT \
           --testrail-plan $TESTRAIL_PLAN \
           --jira-user $JIRA_USER \
           --jira-password $JIRA_PASSWORD \
           --jira-host $JIRA_HOST \
           --push-to-testrail
diff --git a/tcp_tests/report.py b/tcp_tests/report.py
new file mode 100644
index 0000000..a1058f8
--- /dev/null
+++ b/tcp_tests/report.py
@@ -0,0 +1,371 @@
+#!/usr/bin/env python
+import datetime
+import sys
+import logging
+from collections import defaultdict, OrderedDict
+
+import jira
+import argparse
+
+from testrail import TestRail
+from testrail.test import Test
+from functools32 import lru_cache
+
+logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
+LOG = logging.getLogger(__name__)
+
+
+def run_cli():
+    cli = argparse.ArgumentParser(
+        prog="Report generator",
+        description="Command line tool for generate summary report")
+
+    commands = cli.add_subparsers(
+        title="Operation commands",
+        dest="command")
+
+    cli_process = commands.add_parser(
+        "create-report",
+        help="Create summary report",
+        description="Create summary report")
+    cli_process.add_argument(
+        "-T", "--testrail-host", dest="testrail_host",
+        required=True,
+        help="TestRail hostname")
+    cli_process.add_argument(
+        "-U", "--testrail-user", dest="testrail_user",
+        required=True,
+        help="TestRail user email")
+    cli_process.add_argument(
+        "-K", "--testrail-user-key", dest="testrail_user_key",
+        required=True,
+        help="TestRail user key")
+    cli_process.add_argument(
+        "-R", "--testrail-plan", dest="testrail_plan",
+        required=True,
+        help="TestRail test plan for analize")
+    cli_process.add_argument(
+        "-P", "--testrail-project", dest="testrail_project",
+        required=True,
+        help="TestRail project name")
+    cli_process.add_argument(
+        "--testrail-only-run", dest="testrail_only_run",
+        help="Analize only one run in selected plan")
+    cli_process.add_argument(
+        "--out-type", dest="out_type", choices=["text", "html", 'md', 'none'],
+        default='none',
+        help="Select output format for report table. "
+             "By default print nothing (none).")
+    cli_process.add_argument(
+        "--sort-by", dest="sort_by", default='fails',
+        choices=["fails", "blocks", 'project', 'priority', 'status'],
+        help="Select sorting column. By deafult table sort by fails")
+    cli_process.add_argument(
+        "--push-to-testrail", dest="push_report_flag", action="store_true",
+        default=False,
+        help="Save report in plan description")
+    cli_process.add_argument(
+        "-j", "--jira-host", dest="jira_host",
+        required=True,
+        help="JIRA hostname")
+    cli_process.add_argument(
+        "-u", "--jira-user", dest="jira_user_id",
+        required=True,
+        help="JIRA username")
+    cli_process.add_argument(
+        "-p", "--jira-password", dest="jira_user_password",
+        required=True,
+        help="JIRA user password")
+
+    if len(sys.argv) == 1:
+        cli.print_help()
+        sys.exit(1)
+
+    return cli.parse_args()
+
+
+def get_runs(t_client, plan_name, run_name):
+    LOG.info("Get runs from plan - {}".format(plan_name))
+    ret = []
+    plan = t_client.plan(plan_name)
+    for e in plan.entries:
+        for r in e.runs:
+            LOG.info("Run {} #{}".format(r.name, r.id))
+            if run_name is not None and r.name != run_name:
+                continue
+            ret.append(r)
+    return ret
+
+
+def get_all_results(t_client, list_of_runs):
+    ret = []
+    for run in list_of_runs:
+        ret.extend(get_results(t_client, run))
+    return ret
+
+
+@lru_cache()
+def fetch_test(api, test_id, run_id):
+    return Test(api.test_with_id(test_id, run_id))
+
+
+def get_results(t_client, run):
+    _statuses = ('product_failed', 'failed',
+                 'prodfailed', 'blocked')
+    LOG.info("Get results for run - {}".format(run.name))
+    results = t_client.results(run)
+    ret = [(run.id, r) for r in results
+           if r.raw_data()['status_id'] is not None and
+           r.raw_data()['defects'] is not None and
+           r.status.name.lower() in _statuses]
+    for r in ret:
+        run_id, result = r
+        test = fetch_test(result.api, result.raw_data()['test_id'], run_id)
+        LOG.info("Test {} - {} - {}".format(test.title, result.status.name,
+                                            ','.join(result.defects)))
+    return ret
+
+
+@lru_cache()
+def get_defect_info(j_client, defect):
+    LOG.info("Get info about issue {}".format(defect))
+    try:
+        issue = j_client.issue(defect)
+    except jira.exceptions.JIRAError as e:
+        if e.status_code == 404:
+            LOG.error("Defect {} wasn't found in Jira".format(defect))
+            return {
+                'id': defect,
+                'title': "Title for #{} not found".format(defect),
+                'project': "Not found",
+                'priority': "Not found",
+                'status': "Not found",
+                'url': "Not found"
+            }
+        else:
+            raise
+    return {
+        'id': issue.key,
+        'title': issue.fields.summary,
+        'project': issue.fields.project.key,
+        'priority': issue.fields.priority.name,
+        'status': issue.fields.status.name,
+        'url': issue.permalink()
+    }
+
+
+def get_defects_table(jira_client, list_of_results, sort_by):
+    LOG.info("Collect report table")
+    table = defaultdict(dict)
+    for run_id, result in list_of_results:
+        for defect in result.defects:
+            if defect not in table:
+                info = get_defect_info(jira_client, defect)
+
+                table[defect].update(info)
+                table[defect]['results'] = set([(run_id, result)])
+                if result.status.name.lower() == 'blocked':
+                    table[defect]['blocks'] = 1
+                    table[defect]['fails'] = 0
+                else:
+                    table[defect]['fails'] = 1
+                    table[defect]['blocks'] = 0
+            else:
+                table[defect]['results'].add((run_id, result))
+                if result.status.name.lower() == 'blocked':
+                    table[defect]['blocks'] += 1
+                else:
+                    table[defect]['fails'] += 1
+    return OrderedDict(
+        sorted(table.items(), key=lambda i: i[1][sort_by], reverse=True))
+
+
+def get_text_table(table):
+    LOG.info("Generation text table")
+    lines = []
+    line = ("{fails:^5} | {blocks:^5} | {project:^10} | {priority:^15} | "
+            "{status:^15} | {bug:^100} | {tests} ")
+
+    def title_uid(r):
+        run_id, result = r
+        test = fetch_test(result.api, result.raw_data()['test_id'], run_id)
+        return {
+            "title": test.title,
+            "uid": test.id}
+
+    def list_of_defect_tests(results):
+        ret = ["[{title} #{uid}]".format(**title_uid(r)) for
+               r in results]
+        return ' '.join(ret)
+
+    lines.append(line.format(fails='FAILS', blocks='BLOCKS', project="PROJECT",
+                             priority="PRIORITY", status="STATUS", bug="BUG",
+                             tests="TESTS"))
+    for k in table:
+        one = table[k]
+        data = {
+            "fails": one['fails'],
+            "blocks": one['blocks'],
+            "project": one['project'],
+            "priority": one['priority'],
+            "status": one['status'],
+            "bug": "{uid} {title}".format(uid=one['id'], title=one['title']),
+            "tests": list_of_defect_tests(one['results'])
+        }
+        lines.append(line.format(**data))
+    return '\n'.join(lines)
+
+
+def get_md_table(table):
+    LOG.info("Generation MD table")
+    lines = []
+    line = ("||{fails} | {blocks} | {project} | {priority} | "
+            "{status} | {bug} | {tests} ")
+
+    def title_uid_link(r):
+        run_id, result = r
+        test = fetch_test(result.api, result.raw_data()['test_id'], run_id)
+
+        return {
+            "title": test.title.replace('[', '{').replace(']', '}'),
+            "uid": test.id,
+            "link": "{url}/index.php?/tests/view/{uid}".format(
+                    url=test.api._conf()['url'], uid=test.id)
+        }
+
+    def list_of_defect_tests(results):
+        ret = ["<[{title} #{uid}]({link})>".format(**title_uid_link(r))
+               for r in results]
+        return ' '.join(ret)
+
+    lines.append(line.format(fails='|:FAILS', blocks=':BLOCKS',
+                             project=":PROJECT", priority=":PRIORITY",
+                             status=":STATUS", bug=":BUG", tests=":TESTS"))
+    for k in table:
+        one = table[k]
+        data = {
+            "fails": one['fails'],
+            "blocks": one['blocks'],
+            "project": one['project'],
+            "priority": one['priority'],
+            "status": one['status'],
+            "bug": "[{uid} {title}]({url})".format(
+                uid=one['id'],
+                title=one['title'].replace('[', '{').replace(']', '}'),
+                url=one['url']),
+            "tests": list_of_defect_tests(one['results'])
+        }
+        lines.append(line.format(**data))
+    return '\n'.join(lines)
+
+
+def get_html_table(table):
+    LOG.info("Generation HTML table")
+    html = "<table>{lines}</table>"
+    lines = []
+    line = ("<tr><th>{fails:^5}</th><th>{blocks:^5}</th><th>{project:^10}</th>"
+            "<th>{priority:^15}</th>"
+            "<th>{status:^15}</th><th>{bug:^100}</th><th>{tests}</th></tr>")
+    lines.append(line.format(fails='FAILS', blocks='BLOCKS', project="PROJECT",
+                             priority="PRIORITY", status="STATUS", bug="BUG",
+                             tests="TESTS"))
+
+    def title_uid_link(r):
+        run_id, result = r
+        test = fetch_test(result.api, result.raw_data()['test_id'], run_id)
+
+        return {
+            "title": test.title,
+            "uid": test.id,
+            "link": "{url}/index.php?/tests/view/{uid}".format(
+                    url=test.api._conf()['url'], uid=test.id)
+        }
+
+    def list_of_defect_tests(results):
+        ret = ["<a href='{link}'>{title} #{uid}</a>".format(
+               **title_uid_link(r)) for r in results]
+        return ' '.join(ret)
+
+    for k in table:
+        one = table[k]
+        data = {
+            "fails": one['fails'],
+            "blocks": one['blocks'],
+            "project": one['project'],
+            "priority": one['priority'],
+            "status": one['status'],
+            "bug": "<a href='{url}'>{uid} {title}</a>".format(
+                uid=one['id'], title=one['title'], url=one['url']),
+            "tests": list_of_defect_tests(one['results'])
+        }
+        lines.append(line.format(**data))
+    return html.format(lines=''.join(lines))
+
+
+def out_table(out_type, table):
+    if out_type == 'none':
+        return
+    elif out_type == 'html':
+        print(get_html_table(table))
+    elif out_type == 'md':
+        print(get_md_table(table))
+    else:
+        print(get_text_table(table))
+
+
+def push_report(t_client, plan_name, table):
+    LOG.info("Push report table into plan - {}".format(plan_name))
+    text = "Bugs Statistics (generated on {date})\n" \
+           "=======================================================\n" \
+           "{table}".format(
+               date=datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y"),
+               table=get_md_table(table))
+    plan = t_client.plan(plan_name)
+    plan.description = text
+    plan.api._post(
+        'update_plan/{}'.format(plan.id),
+        {
+            'name': plan.name,
+            'description': plan.description,
+            'milestone_id': plan.milestone.id
+        })
+
+
+def create_report(**kwargs):
+    j_host = kwargs.get('jira_host')
+    j_user = kwargs.get('jira_user_id')
+    j_user_pwd = kwargs.get('jira_user_password')
+    t_host = kwargs.get('testrail_host')
+    t_user = kwargs.get('testrail_user')
+    t_user_key = kwargs.get('testrail_user_key')
+    t_plan = kwargs.get('testrail_plan')
+    t_project = kwargs.get('testrail_project')
+    t_a_run = kwargs.get('testrail_only_run')
+    o_type = kwargs.get('out_type')
+    push_report_flag = kwargs.get('push_report_flag')
+    sort_by = kwargs.get('sort_by')
+
+    t_client = TestRail(email=t_user, key=t_user_key, url=t_host)
+    t_client.set_project_id(t_client.project(t_project).id)
+
+    j_client = jira.JIRA(j_host, basic_auth=(j_user, j_user_pwd))
+
+    runs = get_runs(t_client, t_plan, t_a_run)
+    results = get_all_results(t_client, runs)
+    table = get_defects_table(j_client, results, sort_by)
+    out_table(o_type, table)
+    if push_report_flag:
+        push_report(t_client, t_plan, table)
+
+
+COMMAND_MAP = {
+    'create-report': create_report
+}
+
+
+def main():
+    args = run_cli()
+    COMMAND_MAP[args.command](**vars(args))
+
+if __name__ == '__main__':
+    main()