Blob Blame History Raw
From 7afe09fb5c3a69469b864cf21cacec5b427ae372 Mon Sep 17 00:00:00 2001
From: Matej Habrnal <mhabrnal@redhat.com>
Date: Tue, 29 Mar 2016 15:08:15 +0200
Subject: [PATCH] rhtsupport: use problem report API to create description

Related to: rhbz#1261358

Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
---
 doc/reporter-rhtsupport.txt       |   5 +-
 src/plugins/reporter-rhtsupport.c | 116 +++++++++++++++++++++++++++++---------
 2 files changed, 92 insertions(+), 29 deletions(-)

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