Split a given Issue into 2

Bugs encountered:
Once the dialog pops up, filling in the subject and pressing RETURN
doesn't work. It fails to read the subject. The primary button needs to
be clicked on. This is a generic issue, not related to this PR

Behaviour:
-[x] Add button on communication to split thread
-[x] Popup asks for new subject
-[x] Copy "Customer", "Sender" and other relevant fields to this issue
-[x] Move thread and replies to new issue

fixes #14600
diff --git a/erpnext/support/doctype/issue/issue.js b/erpnext/support/doctype/issue/issue.js
index 77c59f4..72a226e 100644
--- a/erpnext/support/doctype/issue/issue.js
+++ b/erpnext/support/doctype/issue/issue.js
@@ -1,9 +1,9 @@
 frappe.ui.form.on("Issue", {
-	"onload": function(frm) {
+	onload: function(frm) {
 		frm.email_field = "raised_by";
 	},
 
-	"refresh": function(frm) {
+	refresh: function(frm) {
 		if(frm.doc.status!=="Closed") {
 			frm.add_custom_button(__("Close"), function() {
 				frm.set_value("status", "Closed");
@@ -33,5 +33,39 @@
 					frappe.set_route('Form', 'Help Article', doc.name);
 				});
 		}
+
+		if (!frm.timeline.wrapper.find('.btn-split-issue').length) {
+			let split_issue = __("Split Issue")
+			$(`<button class="btn btn-xs btn-link btn-add-to-kb text-muted hidden-xs btn-split-issue pull-right" style="display:inline-block; margin-right: 5px">
+				${split_issue} 
+			</button>`)
+				.appendTo(frm.timeline.wrapper.find('.comment-header .asset-details:not([data-communication-type="Comment"])'))
+			if (!frm.timeline.wrapper.data("split-issue-event-attached")){
+				frm.timeline.wrapper.on('click', '.btn-split-issue', (e) => {
+					var dialog = new frappe.ui.Dialog({
+						title: __("Split Issue"),
+						fields: [
+							{fieldname: 'subject', fieldtype: 'Data', reqd:1, label: __('Subject'), description: __('All communications including and above this shall be moved into the new Issue')}
+						],
+						primary_action_label: __("Split"),
+						primary_action: function() {
+							frm.call("split_issue", {
+								subject: dialog.fields_dict.subject.value,
+								communication_id: e.currentTarget.closest(".timeline-item").getAttribute("data-name")
+							}, (r) => {
+								let url = window.location.href
+								let arr = url.split("/");
+								let result = arr[0] + "//" + arr[2]
+								frappe.msgprint(`New issue created: <a href="${result}/desk#Form/Issue/${r.message}">${r.message}</a>`)
+								frm.reload_doc();
+								dialog.hide();
+							});
+						}
+					});
+					dialog.show()
+				})
+				frm.timeline.wrapper.data("split-issue-event-attached", true)
+			}
+		}
 	}
 });
diff --git a/erpnext/support/doctype/issue/issue.py b/erpnext/support/doctype/issue/issue.py
index ef54b20..4eec3e0 100644
--- a/erpnext/support/doctype/issue/issue.py
+++ b/erpnext/support/doctype/issue/issue.py
@@ -81,6 +81,22 @@
 
 		self.db_set("description", "")
 
+	def split_issue(self, subject, communication_id):
+		# Bug: Pressing enter doesn't send subject
+		from copy import deepcopy
+		replicated_issue = deepcopy(self)
+		replicated_issue.subject = subject
+		frappe.get_doc(replicated_issue).insert()
+		# Replicate linked Communications
+		# todo get all communications in timeline before this, and modify them to append them to new doc
+		comm_to_split_from = frappe.get_doc("Communication", communication_id)
+		communications = frappe.get_all("Communication", filters={"reference_name": 'ISS-00001', "reference_doctype": "Issue", "creation": ('>=', comm_to_split_from.creation)})
+		for communication in communications:
+			doc = frappe.get_doc("Communication", communication.name)
+			doc.reference_name = replicated_issue.name
+			doc.save()
+		return replicated_issue.name
+
 def get_list_context(context=None):
 	return {
 		"title": _("Issues"),