Blame SOURCES/0175-ureport-enable-attaching-of-arbitrary-values.patch

562801
From 9a37ec67fc849a8fd9862d5e03c3066b7a37ba26 Mon Sep 17 00:00:00 2001
562801
From: Matej Habrnal <mhabrnal@redhat.com>
562801
Date: Fri, 12 Feb 2016 17:23:32 +0100
562801
Subject: [PATCH] ureport: enable attaching of arbitrary values
562801
562801
Whenever we introduce a new report type we need to teach
562801
reporter-ureport to attach its report results to micro-reports.
562801
562801
This commit adds support for attaching arbitrary values, so we will not
562801
need to modify reporter-ureport when a new reporter is added.
562801
562801
Two operating modes are being introduced.
562801
The mode for attaching random values:
562801
$ reporter-ureport -l "foo" -T "blah" -A
562801
562801
and the mode for attaching the report results
562801
(data from reported_to file):
562801
$ reporter-ureport -L "URL" -T "url" -r "upload" -A
562801
562801
'-v', '--value' is taken by '--verbose'.
562801
'-d', '--data' is taken by '--directory'.
562801
Hence I used '-l', '--value'.
562801
562801
'-t', '--type' is taken by '--auth'.
562801
Hence I used '-T', '--type'.
562801
562801
Signed-off-by: Jakub Filak <jfilak@redhat.com>
562801
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
562801
---
562801
 doc/reporter-ureport.txt       | 18 +++++++++++-
562801
 src/plugins/reporter-ureport.c | 66 ++++++++++++++++++++++++++++++++++++++++--
562801
 2 files changed, 81 insertions(+), 3 deletions(-)
562801
562801
diff --git a/doc/reporter-ureport.txt b/doc/reporter-ureport.txt
562801
index 420adcf..e896d9d 100644
562801
--- a/doc/reporter-ureport.txt
562801
+++ b/doc/reporter-ureport.txt
562801
@@ -7,7 +7,7 @@ reporter-ureport - Reports ABRT problems in format of micro report
562801
 
562801
 SYNOPSIS
562801
 --------
562801
-'reporter-ureport' [-v] [-c CONFFILE] [-u URL] [-k] [-A -a bthash -B -b bug-id -E -e email] [-r] [-d DIR]
562801
+'reporter-ureport' [-v] [-c CONFFILE] [-u URL] [-k] [-A -a bthash -B -b bug-id -E -e email -l DATA -L FIELD -T TYPE -r RESULT_TYPE] [-d DIR]
562801
 
562801
 DESCRIPTION
562801
 -----------
562801
@@ -119,6 +119,22 @@ OPTIONS
562801
 -i AUTH_DATA_ITEMS::
562801
    List of dump dir files included in the 'auth' uReport object.
562801
 
562801
+-l DATA::
562801
+   Attach DATA (requires -T and -a|-A)
562801
+
562801
+-L REPORT_RESULT_FILED::
562801
+   Attach the value of REPORT_RESULT_FILED member of the last report result
562801
+   indentified by REPORT_RESULT_TYPE passed with -r option
562801
+   (requires -r, -T and -a|-A).
562801
+
562801
+-T ATTACHMENT_TYPE::
562801
+   Specifies the attachment type when attaching an arbitrary data to BTHASH
562801
+   (takes effect only with -l or -L)
562801
+
562801
+-r REPORT_RESULT_TYP::
562801
+   Used to single out report results ('reported_to' file lines) when attaching
562801
+   an arbitrary data to BTHASH (takes effect only with -L)
562801
+
562801
 ENVIRONMENT VARIABLES
562801
 ---------------------
562801
 Environment variables take precedence over values provided in
562801
diff --git a/src/plugins/reporter-ureport.c b/src/plugins/reporter-ureport.c
562801
index e0c2281..6dcce81 100644
562801
--- a/src/plugins/reporter-ureport.c
562801
+++ b/src/plugins/reporter-ureport.c
562801
@@ -61,6 +61,11 @@ int main(int argc, char **argv)
562801
     int rhbz_bug_from_rt = 0;
562801
     const char *email_address = NULL;
562801
     int email_address_from_env = 0;
562801
+    char *attach_value = NULL;
562801
+    char *attach_value_from_rt = NULL;
562801
+    char *attach_value_from_rt_data = NULL;
562801
+    char *report_result_type = NULL;
562801
+    char *attach_type = NULL;
562801
     struct dump_dir *dd = NULL;
562801
     struct options program_options[] = {
562801
         OPT__VERBOSE(&g_verbose),
562801
@@ -84,11 +89,24 @@ int main(int argc, char **argv)
562801
                           _("attach RHBZ bug (requires -a|-A, conflicts with -B)")),
562801
         OPT_BOOL('B', "bug-id-rt", &rhbz_bug_from_rt,
562801
                           _("attach last RHBZ bug from reported_to (requires -a|-A, conflicts with -b)")),
562801
+
562801
+        /* va l ue */
562801
+        OPT_STRING('l', "value", &attach_value, "DATA",
562801
+                          _("attach value (requires -a|-A and -T, conflicts with -L)")),
562801
+        OPT_STRING('L', "value-rt", &attach_value_from_rt, "FIELD",
562801
+                          _("attach data of FIELD [URL] of the last report result (requires -a|-A, -r and -T, conflicts with -l)")),
562801
+
562801
+        OPT_STRING('r', "report-result-type", &report_result_type, "REPORT_RESULT_TYPE",
562801
+                          _("use REPORT_RESULT_TYPE when looking for FIELD in reported_to (used only with -L)")),
562801
+        OPT_STRING('T', "type", &attach_type, "ATTACHMENT_TYPE",
562801
+                          _("attach DATA as ureporte attachment ATTACHMENT_TYPE (used only with -l|-L)")),
562801
         OPT_END(),
562801
     };
562801
 
562801
     const char *program_usage_string = _(
562801
         "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-h CREDENTIALS] [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
562801
+        "  [-A -a bthash -T ATTACHMENT_TYPE -r REPORT_RESULT_TYPE -L RESULT_FIELD] [-d DIR]\n"
562801
+        "  [-A -a bthash -T ATTACHMENT_TYPE -l DATA] [-d DIR]\n"
562801
         "& [-v] [-c FILE] [-u URL] [-k] [-t SOURCE] [-h CREDENTIALS] [-i AUTH_ITEMS]\\\n"
562801
         "  [-A -a bthash -B -b bug-id -E -e email] [-d DIR]\n"
562801
         "\n"
562801
@@ -130,7 +148,24 @@ int main(int argc, char **argv)
562801
     if (email_address && email_address_from_env)
562801
         error_msg_and_die("You need to pass either -e bthash or -E");
562801
 
562801
-    if (ureport_hash_from_rt || rhbz_bug_from_rt)
562801
+    if (attach_value && attach_value_from_rt)
562801
+        error_msg_and_die("You need to pass either -l url or -L");
562801
+
562801
+    if ((attach_value || attach_value_from_rt) && attach_type == NULL)
562801
+        error_msg_and_die("You need to pass -T together with -l and -L");
562801
+
562801
+    if (attach_value_from_rt)
562801
+    {
562801
+        if (report_result_type == NULL)
562801
+            error_msg_and_die("You need to pass -r together with -L");
562801
+
562801
+        /* If you introduce a new recognized value, don't forget to update
562801
+         * the documentation and the conditions below. */
562801
+        if (strcmp(attach_value_from_rt, "URL") != 0)
562801
+            error_msg_and_die("-L accepts only 'URL'");
562801
+    }
562801
+
562801
+    if (ureport_hash_from_rt || rhbz_bug_from_rt || attach_value_from_rt)
562801
     {
562801
         dd = dd_opendir(dump_dir_path, DD_OPEN_READONLY);
562801
         if (!dd)
562801
@@ -168,6 +203,25 @@ int main(int argc, char **argv)
562801
             free_report_result(bz_result);
562801
         }
562801
 
562801
+        if (attach_value_from_rt)
562801
+        {
562801
+            report_result_t *result = find_in_reported_to(dd, report_result_type);
562801
+
562801
+            if (!result)
562801
+                error_msg_and_die(_("This problem has not been reported to '%s'."), report_result_type);
562801
+
562801
+            /* If you introduce a new attach_value_from_rt recognized value,
562801
+             * this condition will become invalid. */
562801
+            if (!result->url)
562801
+                error_msg_and_die(_("The report result '%s' is missing URL."), report_result_type);
562801
+
562801
+            /* Avoid the need to duplicate the string. */
562801
+            attach_value = attach_value_from_rt_data = result->url;
562801
+            result->url = NULL;
562801
+
562801
+            free_report_result(result);
562801
+        }
562801
+
562801
         dd_close(dd);
562801
     }
562801
 
562801
@@ -181,7 +235,7 @@ int main(int argc, char **argv)
562801
 
562801
     if (ureport_hash)
562801
     {
562801
-        if (rhbz_bug < 0 && !email_address)
562801
+        if (rhbz_bug < 0 && !email_address && !attach_value)
562801
             error_msg_and_die(_("You need to specify bug ID, contact email or both"));
562801
 
562801
         if (rhbz_bug >= 0)
562801
@@ -196,6 +250,12 @@ int main(int argc, char **argv)
562801
                 goto finalize;
562801
         }
562801
 
562801
+        if (attach_value)
562801
+        {
562801
+            if (ureport_attach_string(ureport_hash, attach_type, attach_value, &config))
562801
+                goto finalize;
562801
+        }
562801
+
562801
         ret = 0;
562801
         goto finalize;
562801
     }
562801
@@ -239,6 +299,8 @@ int main(int argc, char **argv)
562801
     ureport_server_response_free(response);
562801
 
562801
 finalize:
562801
+    free(attach_value_from_rt_data);
562801
+
562801
     if (config.ur_prefs.urp_auth_items == auth_items)
562801
         config.ur_prefs.urp_auth_items = NULL;
562801
 
562801
-- 
562801
1.8.3.1
562801