Blob Blame History Raw
From 9786e890de57260d2975a605512147779383c137 Mon Sep 17 00:00:00 2001
From: Nikola Pajkovsky <npajkovs@redhat.com>
Date: Thu, 1 Dec 2011 14:48:48 +0100
Subject: [PATCH 5/6] do not insert duplicate comment to bugzilla

Signed-off-by: Nikola Pajkovsky <npajkovs@redhat.com>
---
 src/plugins/reporter-bugzilla.c |   11 +++++--
 src/plugins/rhbz.c              |   60 +++++++++++++++++++++++++++++++++++----
 src/plugins/rhbz.h              |    3 ++
 3 files changed, 65 insertions(+), 9 deletions(-)

diff --git a/src/plugins/reporter-bugzilla.c b/src/plugins/reporter-bugzilla.c
index e150944..9bdadba 100644
--- a/src/plugins/reporter-bugzilla.c
+++ b/src/plugins/reporter-bugzilla.c
@@ -397,17 +397,22 @@ int main(int argc, char **argv)
             strbuf_append_strf(full_desc, "Architecture: %s\n", arch);
             strbuf_append_strf(full_desc, "OS Release: %s\n", release);
 
-            log(_("Adding new comment to bug %d"), bz->bi_id);
             /* unused code, enable it when gui/cli will be ready
             int is_priv = is_private && string_to_bool(is_private);
             const char *is_private = get_problem_item_content_or_NULL(problem_data,
                                                                       "is_private");
             */
-            rhbz_add_comment(client, bz->bi_id, full_desc->buf, 0);
+
+            int allow_comment = is_comment_dup(bz->bi_comments, full_desc->buf);
+            if (!allow_comment)
+            {
+                log(_("Adding new comment to bug %d"), bz->bi_id);
+                rhbz_add_comment(client, bz->bi_id, full_desc->buf, 0);
+            }
             strbuf_free(full_desc);
 
             unsigned rating = xatou(rating_str);
-            if (bz->bi_best_bt_rating < rating)
+            if (!allow_comment && (bz->bi_best_bt_rating < rating))
             {
                 char bug_id_str[sizeof(int)*3 + 2];
                 sprintf(bug_id_str, "%i", bz->bi_id);
diff --git a/src/plugins/rhbz.c b/src/plugins/rhbz.c
index 3662816..4c3c91f 100644
--- a/src/plugins/rhbz.c
+++ b/src/plugins/rhbz.c
@@ -45,17 +45,17 @@ void free_bug_info(struct bug_info *bi)
     free(bi);
 }
 
-static unsigned find_best_bt_rating_in_comments(xmlrpc_value *result_xml)
+static GList *parse_comments(xmlrpc_value *result_xml)
 {
+    GList *comments = NULL;
     xmlrpc_value *comments_memb = rhbz_get_member("longdescs", result_xml);
     if (!comments_memb)
-        return 0;
+        return NULL;
 
     int comments_memb_size = rhbz_array_size(comments_memb);
 
     xmlrpc_env env;
     xmlrpc_env_init(&env);
-    int best_bt_rating = 0;
     for (int i = 0; i < comments_memb_size; ++i)
     {
         xmlrpc_value* item = NULL;
@@ -65,8 +65,55 @@ static unsigned find_best_bt_rating_in_comments(xmlrpc_value *result_xml)
 
         char *comment_body = rhbz_bug_read_item("body", item, RHBZ_READ_STR);
         /* attachments are sometimes without comments -- skip them */
-        if (!comment_body)
-            continue;
+        if (comment_body)
+            comments = g_list_prepend(comments, comment_body);
+    }
+
+    return g_list_reverse(comments);
+}
+
+static char *trim_all_whitespace(const char *str)
+{
+    char *trim = xzalloc(sizeof(char) * strlen(str) + 1);
+    int i = 0;
+    while (*str)
+    {
+        if (!isspace(*str))
+            trim[i++] = *str;
+        str++;
+    }
+
+    return trim;
+}
+
+int is_comment_dup(GList *comments, const char *comment)
+{
+    for (GList *l = comments; l; l = l->next)
+    {
+        char *comment_body = (char *) l->data;
+        char *trim_comment_body = trim_all_whitespace(comment_body);
+        char *trim_comment = trim_all_whitespace(comment);
+        if (!strcmp(trim_comment_body, trim_comment))
+        {
+            free(trim_comment_body);
+            free(trim_comment);
+            return 1;
+        }
+    }
+
+    return 0;;
+}
+
+static unsigned find_best_bt_rating_in_comments(GList *comments)
+{
+    if (!comments)
+        return 0;
+
+    int best_bt_rating = 0;
+
+    for (GList *l = comments; l; l = l->next)
+    {
+        char *comment_body = (char *) l->data;
 
         char *start_rating_line = strstr(comment_body, "rating: ");
         if (!start_rating_line)
@@ -324,7 +371,8 @@ struct bug_info *rhbz_bug_info(struct abrt_xmlrpc *ax, int bug_id)
 
     bz->bi_cc_list = rhbz_bug_cc(xml_bug_response);
 
-    bz->bi_best_bt_rating = find_best_bt_rating_in_comments(xml_bug_response);
+    bz->bi_comments = parse_comments(xml_bug_response);
+    bz->bi_best_bt_rating = find_best_bt_rating_in_comments(bz->bi_comments);
 
     xmlrpc_DECREF(xml_bug_response);
 
diff --git a/src/plugins/rhbz.h b/src/plugins/rhbz.h
index 9878dd7..141db7d 100644
--- a/src/plugins/rhbz.h
+++ b/src/plugins/rhbz.h
@@ -56,6 +56,7 @@ struct bug_info {
     char *bi_product;
 
     GList *bi_cc_list;
+    GList *bi_comments;
 };
 
 struct bug_info *new_bug_info();
@@ -94,6 +95,8 @@ int rhbz_attach_blob(struct abrt_xmlrpc *ax, const char *filename,
 int rhbz_attach_fd(struct abrt_xmlrpc *ax, const char *filename,
                     const char *bug_id, int fd, int flags);
 
+int is_comment_dup(GList *comments, const char *comment);
+
 GList *rhbz_bug_cc(xmlrpc_value *result_xml);
 
 struct bug_info *rhbz_bug_info(struct abrt_xmlrpc *ax, int bug_id);
-- 
1.7.7.3