Merge "Add analize report on one day scheduler"
diff --git a/testplan_summary/setup.py b/testplan_summary/setup.py
deleted file mode 100644
index a92906a..0000000
--- a/testplan_summary/setup.py
+++ /dev/null
@@ -1,20 +0,0 @@
-from setuptools import setup
-
-setup(
-    name='testplan-summary',
-    version='0.0.1',
-    packages=[],
-    url='',
-    license='',
-    author='oscore-qa',
-    author_email='',
-    description='',
-    requires_python=">=3.8",
-    install_requires=[
-        'jira==3.5.2',
-        'testrail-api==1.8.0'
-    ],
-    scripts=[
-        'report.py',
-    ]
-)
diff --git a/testrail_bot/control/celery_tasks/schedules_pipeline.py b/testrail_bot/control/celery_tasks/schedules_pipeline.py
index 8539357..5a87374 100644
--- a/testrail_bot/control/celery_tasks/schedules_pipeline.py
+++ b/testrail_bot/control/celery_tasks/schedules_pipeline.py
@@ -1,6 +1,7 @@
 import os
 import logging
 from datetime import datetime, timedelta
+import subprocess
 
 from .. import models
 from . import tasks
@@ -121,3 +122,52 @@
         if result.updated_at.strftime("%Y-%m-%d") < border_date:
             LOG.info(f"Deleting {result=}")
             result.delete()
+
+
+def task_to_analize_testrail_reports(plan_name: str):
+    """
+    Analise reports for testrail
+    on one day or plan name
+    """
+    today = datetime.today().strftime("%Y-%m-%d")
+    _plan_name = plan_name or f"[MCP2.0]OSCORE-{today}"
+
+    _url = "https://mirantis.testrail.com"
+    _email = os.environ.get("TESTRAIL_EMAIL")
+    _password = os.environ.get("TESTRAIL_PASSWORD")
+    _project_name = "'Mirantis Cloud Platform'"
+    _jira_user = os.environ.get("JIRA_USER")
+    _jira_password = os.environ.get("JIRA_PASSWORD")
+    _sumReportOptions = [
+        "--testrail-host",
+        _url,
+        "--testrail-user",
+        _email,
+        "--testrail-user-key",
+        _password,
+        "--testrail-plan",
+        _plan_name,
+        "--testrail-project",
+        _project_name,
+        "--out-type html",
+        "--push-to-testrail",
+        "--sort-by fails",
+        "--jira-host https://mirantis.jira.com/",
+        "--jira-user",
+        _jira_user,
+        "--jira-password",
+        _jira_password,
+    ]
+    _sumReportOptionsList = map(str, _sumReportOptions)
+    report_script = f"""\
+              export TESTRAIL_URL={_url};
+              export TESTRAIL_USER_EMAIL={_email};
+              export TESTRAIL_USER_KEY={_password};
+              pip install testplan_summary/.;
+              python testplan_summary/report.py \
+              create-report {" ".join(_sumReportOptionsList)}
+              """
+    p = subprocess.Popen(
+        report_script, shell=True, stdout=subprocess.PIPE
+    ).stdout.read()
+    return p
diff --git a/testrail_bot/control/celery_tasks/tasks.py b/testrail_bot/control/celery_tasks/tasks.py
index 3d090c2..dbfb37a 100644
--- a/testrail_bot/control/celery_tasks/tasks.py
+++ b/testrail_bot/control/celery_tasks/tasks.py
@@ -90,3 +90,15 @@
     :return:
     """
     schedules_pipeline.task_to_delete_old_2m_reports()
+
+
+@shared_task
+def analize_testrail_reports(plan_name, *args, **kwargs):
+    """
+    Finds today testplan
+    Creates TestRun with this id
+    Creates Periodic task to analyze created TestRun
+    :String plan_name
+    :return:
+    """
+    schedules_pipeline.task_to_analize_testrail_reports(plan_name)
diff --git a/testrail_bot/control/migrations/0015_alter_cronperiodictask_task_name.py b/testrail_bot/control/migrations/0015_alter_cronperiodictask_task_name.py
new file mode 100644
index 0000000..5feb92e
--- /dev/null
+++ b/testrail_bot/control/migrations/0015_alter_cronperiodictask_task_name.py
@@ -0,0 +1,43 @@
+# Generated by Django 4.2.7 on 2024-07-19 09:35
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("control", "0014_testresult_testrailreport_test_results"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="cronperiodictask",
+            name="task_name",
+            field=models.CharField(
+                choices=[
+                    (
+                        "control.celery_tasks.tasks.check_today_testplan",
+                        "Check today testplan",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.check_specific_testplan",
+                        "Check specific testplan",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.delete_old_2m_testruns",
+                        "Delete previous 2-month TestRuns(for bot view)",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.delete_old_2m_reports",
+                        "Delete previous 2-month Reports(for bot view)",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.analize_testrail_reports",
+                        "Summary report on one day",
+                    ),
+                ],
+                default="control.celery_tasks.tasks.check_today_testplan",
+                max_length=300,
+            ),
+        ),
+    ]
diff --git a/testrail_bot/control/models.py b/testrail_bot/control/models.py
index b3e84fb..eb5c7cc 100644
--- a/testrail_bot/control/models.py
+++ b/testrail_bot/control/models.py
@@ -174,6 +174,11 @@
         "Delete previous 2-month Reports(for bot view)",
         [],
     ),
+    (
+        "control.celery_tasks.tasks.analize_testrail_reports",
+        "Summary report on one day",
+        ["plan_name"],
+    ),
 ]
 
 TASK_CHOICES = list(map(lambda x: x[:-1], TASKS))
diff --git a/testplan_summary/report.py b/testrail_bot/testplan_summary/report.py
similarity index 94%
rename from testplan_summary/report.py
rename to testrail_bot/testplan_summary/report.py
index fe1322b..4f86ea4 100644
--- a/testplan_summary/report.py
+++ b/testrail_bot/testplan_summary/report.py
@@ -29,7 +29,7 @@
         "mark-fails",
         help="Extract linked bugs from previous reports",
         description="Extract linked bugs from previous reports"
-                    " and mark current",
+        " and mark current",
     )
     cli_process.add_argument(
         "-T",
@@ -94,12 +94,18 @@
         help="Save report in plan description",
     )
     cli_process.add_argument(
-        "-j", "--jira-host", dest="jira_host", required=True,
-        help="JIRA hostname"
+        "-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"
+        "-u",
+        "--jira-user",
+        dest="jira_user_id",
+        required=True,
+        help="JIRA username",
     )
     cli_process.add_argument(
         "-p",
@@ -282,8 +288,8 @@
 
     def generate_result(t_c, tst, m_r, m_t):
         link_comment = "{url}/index.php?/tests/view/{uid}".format(
-            url=t_h,
-            uid=m_t.id)
+            url=t_h, uid=m_t.id
+        )
         tmp_result = t_c.result()
         tmp_result.test = tst
         tmp_result.status = m_r.status
@@ -302,11 +308,9 @@
                         run.id, test.title[-72:], m_result.defects[0]
                     )
                 )
-                ret.append([generate_result(t_cl,
-                                            test,
-                                            m_result,
-                                            m_test),
-                            run.id])
+                ret.append(
+                    [generate_result(t_cl, test, m_result, m_test), run.id]
+                )
     return ret
 
 
@@ -359,9 +363,9 @@
                     table[defect]["blocks"] += 1
                 else:
                     table[defect]["fails"] += 1
-    return OrderedDict(sorted(table.items(),
-                              key=lambda i: i[1][sort_by],
-                              reverse=True))
+    return OrderedDict(
+        sorted(table.items(), key=lambda i: i[1][sort_by], reverse=True)
+    )
 
 
 def get_text_table(table):
@@ -396,7 +400,6 @@
         one = table[k]
         data = {
             "fails": one["fails"],
-            "blocks": one["blocks"],
             "project": one["project"],
             "priority": one["priority"],
             "status": one["status"],
@@ -410,10 +413,7 @@
 def get_md_table(table):
     LOG.info("Generation MD table")
     lines = []
-    line = (
-        "||{fails} | {blocks} | {priority} | "
-        "{status} | <div style='width:200px'>{bug}</div> | {tests} |"
-    )
+    line = "||{fails} | {priority} | " "{status} | {bug} | {tests} |"
 
     def title_uid_link(r):
         run_id, result = r
@@ -428,7 +428,7 @@
 
     def list_of_defect_tests(results):
         ret = [
-            "<[{title} #{uid}]({link})>".format(**title_uid_link(r))
+            "[{title} #{uid}]({link})".format(**title_uid_link(r))
             for r in results
         ]
         return " ".join(ret)
@@ -436,7 +436,6 @@
     lines.append(
         line.format(
             fails="|:FAILS",
-            blocks=":BLOCKS",
             project=":PROJECT",
             priority=":PRIORITY",
             status=":STATUS",
@@ -448,7 +447,6 @@
         one = table[k]
         data = {
             "fails": one["fails"],
-            "blocks": one["blocks"],
             "project": one["project"],
             "priority": one["priority"],
             "status": one["status"],
@@ -605,10 +603,12 @@
         email=testrail_user, key=testrail_user_key, url=testrail_host
     )
     testrail_client.set_project_id(
-        testrail_client.project(testrail_project).id)
+        testrail_client.project(testrail_project).id
+    )
     # Get list runs with marked results
-    marked_runs = get_runs(testrail_client, testrail_m_plan,
-                           testrail_active_run)
+    marked_runs = get_runs(
+        testrail_client, testrail_m_plan, testrail_active_run
+    )
     # Get list runs to update
     runs = get_runs(testrail_client, testrail_plan, testrail_active_run)
     # Get list (failed, prod_failed, test_failed,skipped..) tests with defects
diff --git a/testrail_bot/testplan_summary/setup.py b/testrail_bot/testplan_summary/setup.py
new file mode 100644
index 0000000..33fb003
--- /dev/null
+++ b/testrail_bot/testplan_summary/setup.py
@@ -0,0 +1,22 @@
+from setuptools import setup
+
+setup(
+    name="testplan-summary",
+    version="0.0.1a",
+    packages=[],
+    url="",
+    license="",
+    author="oscore-qa",
+    author_email="",
+    description="",
+    requires_python=">=3.8",
+    install_requires=[
+        "jira==3.5.2",
+        "testrail-api==1.8.0",
+        "ipdb",
+        "testrail",
+    ],
+    scripts=[
+        "report.py",
+    ],
+)