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

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