|
| 1 | +import json |
| 2 | +import os |
| 3 | +import re # Import the regular expressions module |
| 4 | +from jira import JIRA |
| 5 | + |
| 6 | +def classify_ticket_with_rules_advanced(title, description): |
| 7 | + """ |
| 8 | + Classifies a Jira ticket using an advanced rule-set with |
| 9 | + regex and a scoring system. |
| 10 | + """ |
| 11 | + text = (title + " " + (description or "")).lower() |
| 12 | + |
| 13 | + # --- Initialize default classification and scoring --- |
| 14 | + classification = { |
| 15 | + "is_cldr_related": True, |
| 16 | + "ticket_type": "task", |
| 17 | + "component": "other", |
| 18 | + "needs_engineering_work": False, |
| 19 | + "needs_language_specialist": False, |
| 20 | + "is_potential_duplicate": False, |
| 21 | + "potential_duplicate_of": None, |
| 22 | + "summary": "Classification based on advanced rules." |
| 23 | + } |
| 24 | + scores = {"priority": 0, "sentiment": 0} |
| 25 | + |
| 26 | + # --- Advanced Rule 1: Use Regex to find duplicate ticket keys --- |
| 27 | + # This pattern looks for "CLDR-" followed by one or more digits. |
| 28 | + duplicate_match = re.search(r"cldr-\d+", text) |
| 29 | + if duplicate_match: |
| 30 | + classification["is_potential_duplicate"] = True |
| 31 | + # .group(0) gets the matched text, .upper() makes it standard |
| 32 | + classification["potential_duplicate_of"] = duplicate_match.group(0).upper() |
| 33 | + |
| 34 | + # --- Advanced Rule 2: Scoring System for Priority --- |
| 35 | + # Assign points based on keywords. |
| 36 | + priority_keywords = { |
| 37 | + "critical": ["critical", "crash", "urgent", "blocker", "unusable"], |
| 38 | + "high": ["important", "high priority", "major", "severe"], |
| 39 | + "low": ["trivial", "minor", "cosmetic", "suggestion"] |
| 40 | + } |
| 41 | + for word in priority_keywords["critical"]: |
| 42 | + if word in text: scores["priority"] += 20 |
| 43 | + for word in priority_keywords["high"]: |
| 44 | + if word in text: scores["priority"] += 10 |
| 45 | + for word in priority_keywords["low"]: |
| 46 | + if word in text: scores["priority"] -= 5 |
| 47 | + |
| 48 | + # --- Advanced Rule 3: Simple Sentiment Scoring --- |
| 49 | + sentiment_keywords = { |
| 50 | + "positive": ["great", "love", "helpful", "like", "suggestion"], |
| 51 | + "negative": ["frustrating", "confusing", "broken", "hate", "fails"] |
| 52 | + } |
| 53 | + for word in sentiment_keywords["positive"]: |
| 54 | + if word in text: scores["sentiment"] += 1 |
| 55 | + for word in sentiment_keywords["negative"]: |
| 56 | + if word in text: scores["sentiment"] -= 1 |
| 57 | + |
| 58 | + # --- Rule 4: Basic Keyword Matching (can still be used) --- |
| 59 | + if scores["sentiment"] < 0 or any(w in text for w in ["error", "bug"]): |
| 60 | + classification["ticket_type"] = "bug" |
| 61 | + elif any(w in text for w in ["add", "create", "implement", "feature"]): |
| 62 | + classification["ticket_type"] = "feature" |
| 63 | + |
| 64 | + # --- Make final decisions based on scores --- |
| 65 | + if scores["priority"] > 15: |
| 66 | + classification["priority"] = "critical" |
| 67 | + elif scores["priority"] > 5: |
| 68 | + classification["priority"] = "high" |
| 69 | + elif scores["priority"] < 0: |
| 70 | + classification["priority"] = "low" |
| 71 | + else: |
| 72 | + classification["priority"] = "medium" |
| 73 | + |
| 74 | + # --- Set routing based on ticket type --- |
| 75 | + if classification["ticket_type"] == "bug": |
| 76 | + classification["routing_group"] = "QA Team" |
| 77 | + else: |
| 78 | + classification["routing_group"] = "CLDR Core Team" |
| 79 | + |
| 80 | + # --- Final structure --- |
| 81 | + return { |
| 82 | + "classification_status": "valid_ticket", |
| 83 | + "details": classification |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +# (Configuration and Jira Connection logic) |
| 88 | +JIRA_SERVER = os.getenv('JIRA_SERVER', 'https://unicode-org.atlassian.net') |
| 89 | +JIRA_USER_EMAIL = os. getenv( 'JIRA_USER_EMAIL', '[email protected]') |
| 90 | +JIRA_API_TOKEN = os.getenv('JIRA_API_TOKEN', 'your_api_token_here') |
| 91 | + |
| 92 | + |
| 93 | +if not all([JIRA_SERVER, JIRA_USER_EMAIL, JIRA_API_TOKEN]): |
| 94 | + print(" Error: One or more environment variables are not set.") |
| 95 | +else: |
| 96 | + print(f"\n--- Connecting to Jira server at: {JIRA_SERVER} ---") |
| 97 | + try: |
| 98 | + jira_connection = JIRA(server=JIRA_SERVER, basic_auth=(JIRA_USER_EMAIL, JIRA_API_TOKEN)) |
| 99 | + print(" Connection successful!") |
| 100 | + |
| 101 | + print(f"\n--- Fetching ticket: {TICKET_KEY_TO_FETCH} ---") |
| 102 | + issue = jira_connection.issue(TICKET_KEY_TO_FETCH) |
| 103 | + ticket_summary = issue.fields.summary |
| 104 | + ticket_description = issue.fields.description |
| 105 | + print(f"Title: {ticket_summary}") |
| 106 | + print(f"Description: {ticket_description if ticket_description else 'No description found.'}") |
| 107 | + |
| 108 | + print("\n" + "="*50) |
| 109 | + print("--- Dynamic Advanced Rule-Based Classification ---") |
| 110 | + print("="*50) |
| 111 | + |
| 112 | + advanced_classification = classify_ticket_with_rules_advanced(ticket_summary, ticket_description) |
| 113 | + |
| 114 | + print(json.dumps(advanced_classification, indent=2)) |
| 115 | + |
| 116 | + except Exception as e: |
| 117 | + print(f" An error occurred: {e}") |
0 commit comments