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

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