Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from datetime import datetime
|
|
| 5 |
from transformers import pipeline
|
| 6 |
from simple_salesforce import Salesforce, SalesforceLogin
|
| 7 |
from dotenv import load_dotenv
|
|
|
|
| 8 |
|
| 9 |
# ---------- Load Environment Variables ----------
|
| 10 |
load_dotenv()
|
|
@@ -34,11 +35,11 @@ severities = {
|
|
| 34 |
"Best Practice": "Low"
|
| 35 |
}
|
| 36 |
|
| 37 |
-
# ----------
|
| 38 |
salesforce_knowledge_base = {
|
| 39 |
"governor limits soql": "In Salesforce, the governor limit for SOQL queries is 100 per synchronous transaction and 200 per asynchronous transaction.",
|
| 40 |
"governor limits dml": "The governor limit for DML statements is 150 per transaction.",
|
| 41 |
-
"bulkify apex trigger": "...",
|
| 42 |
"soql injection": "...",
|
| 43 |
"lwc best practices": "...",
|
| 44 |
"batch apex": "..."
|
|
@@ -69,7 +70,7 @@ except Exception as e:
|
|
| 69 |
sf = None
|
| 70 |
print(f"❌ Failed to connect to Salesforce: {e}")
|
| 71 |
|
| 72 |
-
# ---------- Code Analyzer
|
| 73 |
def analyze_code(code):
|
| 74 |
if not code.strip():
|
| 75 |
return "No code provided.", "", ""
|
|
@@ -103,21 +104,56 @@ def analyze_code(code):
|
|
| 103 |
|
| 104 |
return issue_type, suggestion, severity
|
| 105 |
|
| 106 |
-
# ---------- Metadata Validator ----------
|
| 107 |
-
def validate_metadata(metadata):
|
| 108 |
if not metadata.strip():
|
| 109 |
return "No metadata provided.", "", ""
|
|
|
|
| 110 |
mtype = "Field"
|
| 111 |
-
issue = "
|
| 112 |
-
recommendation = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
log_data = {
|
| 114 |
"Name": f"MetadataLog_{mtype}",
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
-
"
|
| 118 |
-
"
|
| 119 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
log_to_console(log_data, "Metadata Validation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
return mtype, issue, recommendation
|
| 122 |
|
| 123 |
# ---------- Salesforce Chatbot ----------
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
from simple_salesforce import Salesforce, SalesforceLogin
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
+
import xml.etree.ElementTree as ET # NEW: for parsing metadata
|
| 9 |
|
| 10 |
# ---------- Load Environment Variables ----------
|
| 11 |
load_dotenv()
|
|
|
|
| 35 |
"Best Practice": "Low"
|
| 36 |
}
|
| 37 |
|
| 38 |
+
# ---------- Knowledge Base ----------
|
| 39 |
salesforce_knowledge_base = {
|
| 40 |
"governor limits soql": "In Salesforce, the governor limit for SOQL queries is 100 per synchronous transaction and 200 per asynchronous transaction.",
|
| 41 |
"governor limits dml": "The governor limit for DML statements is 150 per transaction.",
|
| 42 |
+
"bulkify apex trigger": "...",
|
| 43 |
"soql injection": "...",
|
| 44 |
"lwc best practices": "...",
|
| 45 |
"batch apex": "..."
|
|
|
|
| 70 |
sf = None
|
| 71 |
print(f"❌ Failed to connect to Salesforce: {e}")
|
| 72 |
|
| 73 |
+
# ---------- Code Analyzer ----------
|
| 74 |
def analyze_code(code):
|
| 75 |
if not code.strip():
|
| 76 |
return "No code provided.", "", ""
|
|
|
|
| 104 |
|
| 105 |
return issue_type, suggestion, severity
|
| 106 |
|
| 107 |
+
# ---------- Metadata Validator (Updated with dynamic XML parsing) ----------
|
| 108 |
+
def validate_metadata(metadata, admin_id=None):
|
| 109 |
if not metadata.strip():
|
| 110 |
return "No metadata provided.", "", ""
|
| 111 |
+
|
| 112 |
mtype = "Field"
|
| 113 |
+
issue = "Unknown"
|
| 114 |
+
recommendation = "No recommendation found."
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
root = ET.fromstring(metadata)
|
| 118 |
+
# Detect missing <description> tag
|
| 119 |
+
description_found = any(elem.tag.endswith('description') for elem in root)
|
| 120 |
+
|
| 121 |
+
if not description_found:
|
| 122 |
+
issue = "Missing description"
|
| 123 |
+
recommendation = "Add a meaningful <description> to improve maintainability and clarity."
|
| 124 |
+
else:
|
| 125 |
+
issue = "Unused field detected"
|
| 126 |
+
recommendation = "Remove it to improve performance or document its purpose."
|
| 127 |
+
|
| 128 |
+
except Exception as e:
|
| 129 |
+
issue = "Invalid XML"
|
| 130 |
+
recommendation = f"Could not parse metadata XML. Error: {str(e)}"
|
| 131 |
+
|
| 132 |
log_data = {
|
| 133 |
"Name": f"MetadataLog_{mtype}",
|
| 134 |
+
"MetadataType__c": mtype,
|
| 135 |
+
"IssueDescription__c": issue,
|
| 136 |
+
"Recommendation__c": recommendation,
|
| 137 |
+
"Status__c": "Open"
|
| 138 |
}
|
| 139 |
+
|
| 140 |
+
if admin_id:
|
| 141 |
+
log_data["Admin__c"] = admin_id
|
| 142 |
+
|
| 143 |
log_to_console(log_data, "Metadata Validation")
|
| 144 |
+
|
| 145 |
+
if sf:
|
| 146 |
+
try:
|
| 147 |
+
result = sf.MetadataAuditLog__c.create(log_data)
|
| 148 |
+
if result.get("success"):
|
| 149 |
+
log_to_console({"Salesforce MetadataAuditLog Record ID": result["id"]}, "Salesforce Create")
|
| 150 |
+
else:
|
| 151 |
+
log_to_console(result, "Salesforce Metadata Error")
|
| 152 |
+
except Exception as e:
|
| 153 |
+
log_to_console({"Salesforce Exception": str(e)}, "Salesforce Error")
|
| 154 |
+
else:
|
| 155 |
+
log_to_console("Salesforce not connected.", "Salesforce Error")
|
| 156 |
+
|
| 157 |
return mtype, issue, recommendation
|
| 158 |
|
| 159 |
# ---------- Salesforce Chatbot ----------
|