Repository: zaproxy/zaproxy
Issue: Tag changes in Search tab not reflected in History tab

**Describe the bug**
Managing message tags with the "Manage Tags..." dialog in Search view does not affect message tags in History view. This might not be a bug, but actually just a missing feature, not sure though.

**To Reproduce**
1. find message in "Search" tab
2. got to "Manage Tags"
3. add a new tag and finish the dialogue
4. there is no new tag in the tag column of search view. This will appear if the search is repeated. The tag does not appear in the history view at all

**Expected behavior**
It is expected for a tag added in Search to affect the History view and be persisted in history and the ZAP session.

**Software versions**
 - ZAP: w2018-05-14
 - Add-on: Search is part of the core, right?
 - OS: OSX 10.13.4
 - Java: 1.8.0_152


Changed files:
zap/src/main/java/org/zaproxy/zap/extension/search/SearchThread.java, zap/src/main/java/org/zaproxy/zap/extension/search/SearchResultsTableModel.java

diff --git a/zap/src/main/java/org/zaproxy/zap/extension/search/SearchResultsTableModel.java b/zap/src/main/java/org/zaproxy/zap/extension/search/SearchResultsTableModel.java
index b1c4b00c0..f6fdd8832 100644
--- a/zap/src/main/java/org/zaproxy/zap/extension/search/SearchResultsTableModel.java
+++ b/zap/src/main/java/org/zaproxy/zap/extension/search/SearchResultsTableModel.java
@@ -74,7 +74,10 @@ public class SearchResultsTableModel
                 .registerConsumer(
                         this,
                         HistoryReferenceEventPublisher.getPublisher().getPublisherName(),
-                        HistoryReferenceEventPublisher.EVENT_REMOVED);
+                        HistoryReferenceEventPublisher.EVENT_REMOVED,
+                        HistoryReferenceEventPublisher.EVENT_TAG_ADDED,
+                        HistoryReferenceEventPublisher.EVENT_TAG_REMOVED,
+                        HistoryReferenceEventPublisher.EVENT_TAGS_SET);
     }
 
     public void addSearchResult(SearchResult sr) {
@@ -294,6 +297,27 @@ public class SearchResultsTableModel
         String idStr =
                 event.getParameters()
                         .get(HistoryReferenceEventPublisher.FIELD_HISTORY_REFERENCE_ID);
-        this.removeEntry(Integer.parseInt(idStr));
+        int historyId = Integer.parseInt(idStr);
+        switch (event.getEventType()) {
+            case HistoryReferenceEventPublisher.EVENT_REMOVED:
+                this.removeEntry(historyId);
+                break;
+            case HistoryReferenceEventPublisher.EVENT_TAG_ADDED:
+            case HistoryReferenceEventPublisher.EVENT_TAG_REMOVED:
+            case HistoryReferenceEventPublisher.EVENT_TAGS_SET:
+                boolean rowsUpdated = false;
+                for (int i = 0; i < results.size(); i++) {
+                    SearchResultTableEntry entry = results.get(i);
+                    if (entry.getHistoryId() == historyId) {
+                        entry.refreshCachedValues();
+                        rowsUpdated = true;
+                    }
+                }
+
+                if (rowsUpdated) {
+                    fireTableRowsUpdated(0, results.size() - 1);
+                }
+                break;
+        }
     }
 }
diff --git a/zap/src/main/java/org/zaproxy/zap/extension/search/SearchThread.java b/zap/src/main/java/org/zaproxy/zap/extension/search/SearchThread.java
index 0ecf4277f..f6e9833f1 100644
--- a/zap/src/main/java/org/zaproxy/zap/extension/search/SearchThread.java
+++ b/zap/src/main/java/org/zaproxy/zap/extension/search/SearchThread.java
@@ -25,7 +25,9 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+import org.parosproxy.paros.control.Control;
 import org.parosproxy.paros.db.DatabaseException;
+import org.parosproxy.paros.extension.history.ExtensionHistory;
 import org.parosproxy.paros.model.HistoryReference;
 import org.parosproxy.paros.model.Model;
 import org.parosproxy.paros.model.Session;
@@ -191,6 +193,11 @@ public class SearchThread extends Thread {
                 return;
             }
 
+            ExtensionHistory extensionHistory =
+                    Control.getSingleton()
+                            .getExtensionLoader()
+                            .getExtension(ExtensionHistory.class);
+
             List<Integer> list =
                     Model.getSingleton()
                             .getDb()
@@ -211,7 +218,13 @@ public class SearchThread extends Thread {
                 try {
                     currentRecordId = index;
                     // Create the href to ensure the msg is set up correctly
-                    HistoryReference href = new HistoryReference(historyId);
+                    HistoryReference href = null;
+                    if (extensionHistory != null) {
+                        href = extensionHistory.getHistoryReference(historyId);
+                    }
+                    if (href == null) {
+                        href = new HistoryReference(historyId);
+                    }
                     HttpMessage message = href.getHttpMessage();
                     if (searchJustInScope
                             && !session.isInScope(message.getRequestHeader().getURI().toString())) {