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

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