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 2 |
+
import re
|
| 3 |
+
from neo4j import GraphDatabase
|
| 4 |
+
|
| 5 |
+
# Static Neo4j connection details
|
| 6 |
+
NEO4J_URI = "bolt://localhost:7687"
|
| 7 |
+
NEO4J_USER = "neo4j"
|
| 8 |
+
NEO4J_PASSWORD = "0507@#Ny"
|
| 9 |
+
|
| 10 |
+
# Function to query details from Neo4j
|
| 11 |
+
def query_details_from_neo4j(issue_key, driver):
|
| 12 |
+
details = {}
|
| 13 |
+
try:
|
| 14 |
+
with driver.session() as session:
|
| 15 |
+
result = session.run(
|
| 16 |
+
"""
|
| 17 |
+
MATCH (i:IssueKey {key: $issue_key})
|
| 18 |
+
OPTIONAL MATCH (i)-[:HAS_SUMMARY]->(s:Summary)
|
| 19 |
+
OPTIONAL MATCH (i)-[:HAS_CREATION_DATE]->(cd:CreationDate)
|
| 20 |
+
OPTIONAL MATCH (i)-[:HAS_ASSIGNEE]->(a:Assignee)
|
| 21 |
+
OPTIONAL MATCH (i)-[:HAS_REPORTER]->(r:Reporter)
|
| 22 |
+
OPTIONAL MATCH (i)-[:HAS_REQUIREMENT_ID]->(rid:RequirementId)
|
| 23 |
+
OPTIONAL MATCH (i)-[:HAS_LABELS]->(l:Labels)
|
| 24 |
+
OPTIONAL MATCH (i)-[:HAS_COMPONENTS]->(c:Components)
|
| 25 |
+
RETURN s.text AS summary, cd.value AS creation_date, a.name AS assignee,
|
| 26 |
+
r.name AS reporter, rid.value AS requirement_id, l.value AS labels,
|
| 27 |
+
c.value AS components
|
| 28 |
+
""",
|
| 29 |
+
issue_key=issue_key
|
| 30 |
+
)
|
| 31 |
+
record = result.single()
|
| 32 |
+
if record:
|
| 33 |
+
details["summary"] = record["summary"]
|
| 34 |
+
details["creation_date"] = record["creation_date"]
|
| 35 |
+
details["assignee"] = record["assignee"]
|
| 36 |
+
details["reporter"] = record["reporter"]
|
| 37 |
+
details["requirement_id"] = record["requirement_id"]
|
| 38 |
+
details["labels"] = record["labels"]
|
| 39 |
+
details["components"] = record["components"]
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error querying Neo4j: {e}")
|
| 42 |
+
return details
|
| 43 |
+
|
| 44 |
+
# Function to extract entities from user input
|
| 45 |
+
def extract_entities_from_user_input(user_input):
|
| 46 |
+
entities = {}
|
| 47 |
+
issue_key_match = re.search(r"[A-Z]+-\d+", user_input)
|
| 48 |
+
if issue_key_match:
|
| 49 |
+
entities["issue_key"] = issue_key_match.group(0)
|
| 50 |
+
|
| 51 |
+
requested_details = re.findall(r"(issue key|creation date|assignee|reporter|requirement id|labels|components|summary)", user_input, flags=re.IGNORECASE)
|
| 52 |
+
entities["requested_details"] = requested_details
|
| 53 |
+
|
| 54 |
+
return entities
|
| 55 |
+
|
| 56 |
+
# Function to handle the random response from the chatbot
|
| 57 |
+
def random_response(driver, message, history):
|
| 58 |
+
try:
|
| 59 |
+
# Process user input
|
| 60 |
+
entities = extract_entities_from_user_input(message)
|
| 61 |
+
# Check if issue key is provided
|
| 62 |
+
issue_key = entities.get("issue_key")
|
| 63 |
+
if issue_key:
|
| 64 |
+
details = query_details_from_neo4j(issue_key, driver)
|
| 65 |
+
if details:
|
| 66 |
+
requested_details = entities.get("requested_details", [])
|
| 67 |
+
if not requested_details:
|
| 68 |
+
bot_message = "Please specify the details you want (e.g., 'show me creation date and summary')"
|
| 69 |
+
else:
|
| 70 |
+
bot_message = ""
|
| 71 |
+
for detail in requested_details:
|
| 72 |
+
if detail.lower() == "issue key":
|
| 73 |
+
bot_message += f"Issue Key: {issue_key}\n"
|
| 74 |
+
elif detail.lower() == "creation date":
|
| 75 |
+
bot_message += f"Creation Date: {details['creation_date']}\n"
|
| 76 |
+
elif detail.lower() == "assignee":
|
| 77 |
+
bot_message += f"Assignee: {details['assignee']}\n"
|
| 78 |
+
elif detail.lower() == "reporter":
|
| 79 |
+
bot_message += f"Reporter: {details['reporter']}\n"
|
| 80 |
+
elif detail.lower() == "requirement id":
|
| 81 |
+
bot_message += f"Requirement ID: {details['requirement_id']}\n"
|
| 82 |
+
elif detail.lower() == "labels":
|
| 83 |
+
bot_message += f"Labels: {details['labels']}\n"
|
| 84 |
+
elif detail.lower() == "components":
|
| 85 |
+
bot_message += f"Components: {details['components']}\n"
|
| 86 |
+
elif detail.lower() == "summary":
|
| 87 |
+
bot_message += f"Summary: {details['summary']}\n"
|
| 88 |
+
else:
|
| 89 |
+
bot_message += f"Unknown detail: {detail}\n"
|
| 90 |
+
else:
|
| 91 |
+
bot_message = f"No details found for issue key {issue_key}"
|
| 92 |
+
else:
|
| 93 |
+
bot_message = "Issue key not found in the query"
|
| 94 |
+
|
| 95 |
+
history.append(bot_message)
|
| 96 |
+
return history, "\n".join(history)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return history, f"Error connecting to Neo4j: {e}"
|
| 99 |
+
|
| 100 |
+
# Function to handle the authentication process
|
| 101 |
+
def authenticate(user, password):
|
| 102 |
+
try:
|
| 103 |
+
# Neo4j driver initialization
|
| 104 |
+
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
|
| 105 |
+
with driver.session() as session:
|
| 106 |
+
session.run("RETURN 1")
|
| 107 |
+
return driver, True, "Authentication successful!"
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return None, False, f"Authentication failed: {e}"
|
| 110 |
+
|
| 111 |
+
# Function to show the chatbot page upon successful authentication
|
| 112 |
+
def show_chatbot_page(auth_status, auth_message):
|
| 113 |
+
if auth_status:
|
| 114 |
+
return gr.update(visible=False), gr.update(visible=True), auth_message
|
| 115 |
+
else:
|
| 116 |
+
return gr.update(visible=True), gr.update(visible=False), auth_message
|
| 117 |
+
|
| 118 |
+
# Main Gradio app
|
| 119 |
+
with gr.Blocks() as demo:
|
| 120 |
+
auth_status = gr.State(False)
|
| 121 |
+
driver = gr.State(None)
|
| 122 |
+
history = gr.State([])
|
| 123 |
+
|
| 124 |
+
# Authentication Page
|
| 125 |
+
with gr.Column() as login_page:
|
| 126 |
+
user = gr.Textbox(label="Jira Username")
|
| 127 |
+
password = gr.Textbox(label="Jira Password", type="password")
|
| 128 |
+
login_button = gr.Button("Login")
|
| 129 |
+
auth_message = gr.Textbox(label="Authentication Status", interactive=False)
|
| 130 |
+
|
| 131 |
+
# Chatbot Interface
|
| 132 |
+
with gr.Column(visible=False) as chatbot_page:
|
| 133 |
+
message = gr.Textbox(label="Enter your query", lines=3)
|
| 134 |
+
output = gr.Textbox(label="Response", lines=15)
|
| 135 |
+
submit_button = gr.Button("Query")
|
| 136 |
+
clear_button = gr.Button("Clear")
|
| 137 |
+
undo_button = gr.Button("Undo")
|
| 138 |
+
|
| 139 |
+
def clear_chat(history):
|
| 140 |
+
history.clear()
|
| 141 |
+
return [], ""
|
| 142 |
+
|
| 143 |
+
def undo_last(history):
|
| 144 |
+
if history:
|
| 145 |
+
history.pop()
|
| 146 |
+
return history, "\n".join(history)
|
| 147 |
+
|
| 148 |
+
login_button.click(authenticate, inputs=[user, password], outputs=[driver, auth_status, auth_message]).then(
|
| 149 |
+
show_chatbot_page, inputs=[auth_status, auth_message], outputs=[login_page, chatbot_page, auth_message]
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
submit_button.click(random_response, inputs=[driver, message, history], outputs=[history, output])
|
| 153 |
+
clear_button.click(clear_chat, inputs=[history], outputs=[history, output])
|
| 154 |
+
undo_button.click(undo_last, inputs=[history], outputs=[history, output])
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
demo.launch(share=True)
|