Blame SOURCES/0200-rhtsupport-use-problem-report-API-to-create-descript.patch

2c83a8
From 7afe09fb5c3a69469b864cf21cacec5b427ae372 Mon Sep 17 00:00:00 2001
2c83a8
From: Matej Habrnal <mhabrnal@redhat.com>
2c83a8
Date: Tue, 29 Mar 2016 15:08:15 +0200
2c83a8
Subject: [PATCH] rhtsupport: use problem report API to create description
2c83a8
2c83a8
Related to: rhbz#1261358
2c83a8
2c83a8
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
2c83a8
---
2c83a8
 doc/reporter-rhtsupport.txt       |   5 +-
2c83a8
 src/plugins/reporter-rhtsupport.c | 116 +++++++++++++++++++++++++++++---------
2c83a8
 2 files changed, 92 insertions(+), 29 deletions(-)
2c83a8
2c83a8
diff --git a/doc/reporter-rhtsupport.txt b/doc/reporter-rhtsupport.txt
2c83a8
index c4aa459..66e5bed 100644
2c83a8
--- a/doc/reporter-rhtsupport.txt
2c83a8
+++ b/doc/reporter-rhtsupport.txt
2c83a8
@@ -7,7 +7,7 @@ reporter-rhtsupport - Reports problem to RHTSupport.
2c83a8
 
2c83a8
 SYNOPSIS
2c83a8
 --------
2c83a8
-'reporter-rhtsupport' [-v] [-c CONFFILE] [-u -C UR_CONFFILE] -d DIR
2c83a8
+'reporter-rhtsupport' [-v] [-c CONFFILE] [-F FMTFILE] [-u -C UR_CONFFILE] -d DIR
2c83a8
 
2c83a8
 Or:
2c83a8
 
2c83a8
@@ -87,6 +87,9 @@ OPTIONS
2c83a8
 -C UR_CONFFILE::
2c83a8
    Configuration file for submitting uReports.
2c83a8
 
2c83a8
+-F CONF_FORMAT_FILE::
2c83a8
+   Formatting file for a new case.
2c83a8
+
2c83a8
 FILES
2c83a8
 -----
2c83a8
 /usr/share/libreport/conf.d/plugins/rhtsupport.conf::
2c83a8
diff --git a/src/plugins/reporter-rhtsupport.c b/src/plugins/reporter-rhtsupport.c
2c83a8
index 7b04086..7d45b75 100644
2c83a8
--- a/src/plugins/reporter-rhtsupport.c
2c83a8
+++ b/src/plugins/reporter-rhtsupport.c
2c83a8
@@ -23,6 +23,22 @@
2c83a8
 #include "libreport_curl.h"
2c83a8
 #include "abrt_rh_support.h"
2c83a8
 #include "reporter-rhtsupport.h"
2c83a8
+#include "problem_report.h"
2c83a8
+
2c83a8
+/* problem report format template */
2c83a8
+#define PROBLEM_REPORT_TEMPLATE \
2c83a8
+    "%summary:: [abrt] %pkg_name%[[: %crash_function%()]][[: %reason%]][[: TAINTED %tainted_short%]]\n" \
2c83a8
+    "\n" \
2c83a8
+    "Description of problem:: %bare_comment\n" \
2c83a8
+    "\n" \
2c83a8
+    "Truncated backtrace:: %bare_%short_backtrace\n" \
2c83a8
+    "\n" \
2c83a8
+    "Other report identifiers:: %bare_reported_to\n" \
2c83a8
+    "\n" \
2c83a8
+    "Additional info::" \
2c83a8
+    "    count,reason,package,cmdline,executable,%reporter\n"
2c83a8
+
2c83a8
+#define ABRT_ELEMENTS_KB_ARTICLE "https://access.redhat.com/articles/2134281"
2c83a8
 
2c83a8
 #define QUERY_HINTS_IF_SMALLER_THAN  (8*1024*1024)
2c83a8
 
2c83a8
@@ -429,11 +445,12 @@ int main(int argc, char **argv)
2c83a8
     const char *case_no = NULL;
2c83a8
     GList *conf_file = NULL;
2c83a8
     const char *urconf_file = UREPORT_CONF_FILE_PATH;
2c83a8
+    const char *fmt_file = NULL;
2c83a8
 
2c83a8
     /* Can't keep these strings/structs static: _() doesn't support that */
2c83a8
     const char *program_usage_string = _(
2c83a8
         "\n"
2c83a8
-        "& [-v] [-c CONFFILE] -d DIR\n"
2c83a8
+        "& [-v] [-c CONFFILE] [-F FMTFILE] -d DIR\n"
2c83a8
         "or:\n"
2c83a8
         "& [-v] [-c CONFFILE] [-d DIR] -t[ID] [-u -C UR_CONFFILE] FILE...\n"
2c83a8
         "\n"
2c83a8
@@ -464,6 +481,9 @@ int main(int argc, char **argv)
2c83a8
         OPT_t = 1 << 3,
2c83a8
         OPT_f = 1 << 4,
2c83a8
         OPT_u = 1 << 5,
2c83a8
+        OPT_C = 1 << 6,
2c83a8
+        OPT_F = 1 << 7,
2c83a8
+        OPT_D = 1 << 8,
2c83a8
     };
2c83a8
     /* Keep enum above and order of options below in sync! */
2c83a8
     struct options program_options[] = {
2c83a8
@@ -474,6 +494,8 @@ int main(int argc, char **argv)
2c83a8
         OPT_BOOL(     'f', NULL, NULL          ,         _("Force reporting even if this problem is already reported")),
2c83a8
         OPT_BOOL(     'u', NULL, NULL          ,         _("Submit uReport before creating a new case")),
2c83a8
         OPT_STRING(   'C', NULL, &urconf_file  , "FILE", _("Configuration file for uReport")),
2c83a8
+        OPT_STRING(   'F', NULL, &fmt_file     , "FILE", _("Formatting file for a new case")),
2c83a8
+        OPT_BOOL(     'D', NULL, NULL          ,         _("Debug")),
2c83a8
         OPT_END()
2c83a8
     };
2c83a8
     unsigned opts = parse_opts(argc, argv, program_options, program_usage_string);
2c83a8
@@ -617,20 +639,11 @@ int main(int argc, char **argv)
2c83a8
         }
2c83a8
     }
2c83a8
 
2c83a8
-    /* Gzipping e.g. 0.5gig coredump takes a while. Let user know what we are doing */
2c83a8
-    log(_("Compressing data"));
2c83a8
-
2c83a8
     problem_data_t *problem_data = create_problem_data_for_reporting(dump_dir_name);
2c83a8
     if (!problem_data)
2c83a8
         xfunc_die(); /* create_problem_data_for_reporting already emitted error msg */
2c83a8
 
2c83a8
     const char *errmsg = NULL;
2c83a8
-    char *tempfile = NULL;
2c83a8
-    rhts_result_t *result = NULL;
2c83a8
-    rhts_result_t *result_atch = NULL;
2c83a8
-    char *dsc = NULL;
2c83a8
-    char *summary = NULL;
2c83a8
-
2c83a8
     const char *count = NULL;
2c83a8
     count = problem_data_get_content_or_NULL(problem_data, FILENAME_COUNT);
2c83a8
     if (count != NULL
2c83a8
@@ -678,21 +691,6 @@ int main(int argc, char **argv)
2c83a8
             exit(EXIT_CANCEL_BY_USER);
2c83a8
     }
2c83a8
 
2c83a8
-    const char *function;
2c83a8
-    const char *reason;
2c83a8
-    reason   = problem_data_get_content_or_NULL(problem_data, FILENAME_REASON);
2c83a8
-    function = problem_data_get_content_or_NULL(problem_data, FILENAME_CRASH_FUNCTION);
2c83a8
-    {
2c83a8
-        struct strbuf *buf_summary = strbuf_new();
2c83a8
-        strbuf_append_strf(buf_summary, "[abrt] %s", package);
2c83a8
-        if (function && strlen(function) < 30)
2c83a8
-            strbuf_append_strf(buf_summary, ": %s", function);
2c83a8
-        if (reason)
2c83a8
-            strbuf_append_strf(buf_summary, ": %s", reason);
2c83a8
-        summary = strbuf_free_nobuf(buf_summary);
2c83a8
-        dsc = make_description_bz(problem_data, CD_TEXT_ATT_SIZE_BZ);
2c83a8
-    }
2c83a8
-
2c83a8
     char tmpdir_name[sizeof(LARGE_DATA_TMP_DIR"/rhtsupport-"LIBREPORT_ISO_DATE_STRING_SAMPLE"-XXXXXX")];
2c83a8
     snprintf(tmpdir_name, sizeof(tmpdir_name), LARGE_DATA_TMP_DIR"/rhtsupport-%s-XXXXXX", iso_date_string(NULL));
2c83a8
     /* mkdtemp does mkdir(xxx, 0700), should be safe (is it?) */
2c83a8
@@ -703,8 +701,71 @@ int main(int argc, char **argv)
2c83a8
     /* Starting from here, we must perform cleanup on errors
2c83a8
      * (delete temp dir)
2c83a8
      */
2c83a8
+    char *tempfile = NULL;
2c83a8
     tempfile = concat_path_basename(tmpdir_name, dump_dir_name);
2c83a8
     tempfile = append_to_malloced_string(tempfile, ".tar.gz");
2c83a8
+
2c83a8
+    rhts_result_t *result = NULL;
2c83a8
+    rhts_result_t *result_atch = NULL;
2c83a8
+    package  = problem_data_get_content_or_NULL(problem_data, FILENAME_PACKAGE);
2c83a8
+
2c83a8
+    const char *dsc = NULL;
2c83a8
+    const char *summary = NULL;
2c83a8
+
2c83a8
+    problem_formatter_t *pf = problem_formatter_new();
2c83a8
+
2c83a8
+    /* formatting conf file was set */
2c83a8
+    if (fmt_file)
2c83a8
+    {
2c83a8
+        if (problem_formatter_load_file(pf, fmt_file))
2c83a8
+            error_msg_and_die("Invalid format file: %s", fmt_file);
2c83a8
+    }
2c83a8
+    /* using formatting template */
2c83a8
+    else
2c83a8
+    {
2c83a8
+        if (problem_formatter_load_string(pf, PROBLEM_REPORT_TEMPLATE))
2c83a8
+            error_msg_and_die("Invalid problem report format string");
2c83a8
+    }
2c83a8
+
2c83a8
+    problem_report_t *pr = NULL;
2c83a8
+    if (problem_formatter_generate_report(pf, problem_data, &pr))
2c83a8
+        error_msg_and_die("Failed to format bug report from problem data");
2c83a8
+
2c83a8
+    /* Add information about attachments into the description */
2c83a8
+    problem_report_buffer *dsc_buffer = problem_report_get_buffer(pr, PR_SEC_DESCRIPTION);
2c83a8
+
2c83a8
+    char *tarball_name = basename(tempfile);
2c83a8
+    problem_report_buffer_printf(dsc_buffer,
2c83a8
+            "\n"
2c83a8
+            "sosreport and other files were attached as '%s' to the case.\n"
2c83a8
+            "For more details about elements collected by ABRT see:\n"
2c83a8
+            "%s\n"
2c83a8
+            , tarball_name, ABRT_ELEMENTS_KB_ARTICLE);
2c83a8
+
2c83a8
+
2c83a8
+    summary = problem_report_get_summary(pr);
2c83a8
+    dsc = problem_report_get_description(pr);
2c83a8
+
2c83a8
+    /* debug */
2c83a8
+    if (opts & OPT_D)
2c83a8
+    {
2c83a8
+        printf("summary: %s\n"
2c83a8
+                "\n"
2c83a8
+                "%s"
2c83a8
+                "\n"
2c83a8
+                , summary
2c83a8
+                , dsc
2c83a8
+        );
2c83a8
+
2c83a8
+        problem_report_free(pr);
2c83a8
+        problem_formatter_free(pf);
2c83a8
+
2c83a8
+        exit(0);
2c83a8
+    }
2c83a8
+
2c83a8
+    /* Gzipping e.g. 0.5gig coredump takes a while. Let user know what we are doing */
2c83a8
+    log(_("Compressing data"));
2c83a8
+
2c83a8
     if (create_tarball(tempfile, problem_data) != 0)
2c83a8
     {
2c83a8
         errmsg = _("Can't create temporary file in "LARGE_DATA_TMP_DIR);
2c83a8
@@ -749,6 +810,8 @@ int main(int argc, char **argv)
2c83a8
 
2c83a8
         free(version);
2c83a8
         free(product);
2c83a8
+        problem_report_free(pr);
2c83a8
+        problem_formatter_free(pf);
2c83a8
 
2c83a8
         if (result->error)
2c83a8
         {
2c83a8
@@ -879,9 +942,6 @@ int main(int argc, char **argv)
2c83a8
     if (errmsg)
2c83a8
         error_msg_and_die("%s", errmsg);
2c83a8
 
2c83a8
-    free(summary);
2c83a8
-    free(dsc);
2c83a8
-
2c83a8
     free_rhts_result(result_atch);
2c83a8
     free_rhts_result(result);
2c83a8
 
2c83a8
-- 
2c83a8
1.8.3.1
2c83a8