@@ -580,4 +580,93 @@ def perform_ocr(image):
580
580
# Use Tesseract OCR
581
581
ocr_text = pytesseract .image_to_string (image )
582
582
583
- return ocr_text
583
+ return ocr_text
584
+
585
+ def get_jira_accId (args , email ):
586
+ config = get_connection (args )
587
+ jira_config = config .get ('notify' , {}).get ('jira' , {})
588
+ server_url = jira_config .get ('server_url' )
589
+ username = jira_config .get ('username' )
590
+ api_token = jira_config .get ('api_token' )
591
+ db = TinyDB ('user_ids.json' )
592
+ user_query = Query ()
593
+
594
+ # Check if the accountId is already cached
595
+ cached_user = db .search (user_query .email == email )
596
+ if cached_user :
597
+ print_debug (args , f"Using cached accountId for { email } : { cached_user [0 ]['accountId' ]} " )
598
+ return cached_user [0 ]['accountId' ]
599
+
600
+ # Fetch accountId from Jira
601
+ url = f"{ server_url } /rest/api/2/user/search?query={ email } "
602
+ auth = (username , api_token )
603
+ headers = {"Content-Type" : "application/json" }
604
+
605
+ response = requests .get (url , auth = auth , headers = headers )
606
+ if response .status_code == 200 :
607
+ data = response .json ()
608
+ if len (data ):
609
+ account_id = data [0 ]['accountId' ]
610
+ print_debug (args , f"Found accountId for { email } : { account_id } " )
611
+
612
+ # Cache the accountId
613
+ db .insert ({"email" : email , "accountId" : account_id })
614
+ return account_id
615
+ else :
616
+ print_debug (args , f"No accountId found for { email } ." )
617
+ return None
618
+ else :
619
+ print_debug (args , f"Failed to fetch accountId for { email } : { response .status_code } , { response .text } " )
620
+ return None
621
+
622
+ def create_jira_ticket (args , issue_data , message ):
623
+ config = get_connection (args )
624
+ """Creates a Jira ticket using the provided configuration and issue data."""
625
+ jira_config = config .get ('notify' , {}).get ('jira' , {})
626
+
627
+ # Check if Jira is enabled
628
+ if not jira_config .get ('username' ) or jira_config .get ('username' ) == '' :
629
+ print_debug (args , "Jira ticket creation is disabled in the configuration." )
630
+ return
631
+
632
+ # Extract Jira config details
633
+ server_url = jira_config .get ('server_url' )
634
+ username = jira_config .get ('username' )
635
+ api_token = jira_config .get ('api_token' )
636
+ project = jira_config .get ('project' )
637
+ default_issue_type = jira_config .get ('issue_type' )
638
+ issue_fields = jira_config .get ('issue_fields' , {})
639
+ total_matches = len (issue_data .get ('matches' , []))
640
+ summary = "Found " + str (total_matches ) + " " + issue_data .get ('pattern_name' ) + " in " + issue_data .get ('data_source' )
641
+ summary = issue_fields .get ('summary_prefix' , '' ) + summary
642
+ description_template = issue_fields .get ('description_template' , '' )
643
+ description = description_template .format (details = message , ** issue_data )
644
+
645
+ payload = {
646
+ "fields" : {
647
+ "project" : {"key" : project },
648
+ "summary" : summary ,
649
+ "description" : description ,
650
+ "issuetype" : {"name" : default_issue_type }
651
+ }
652
+ }
653
+
654
+ # Check if the assignee is specified in the configuration
655
+ assignee = jira_config .get ('assignee' )
656
+ if assignee :
657
+ payload ['fields' ]['assignee' ] = {"accountId" : get_jira_accId (args , assignee )}
658
+
659
+ labels = jira_config .get ('labels' )
660
+ if labels :
661
+ payload ['fields' ]['labels' ] = labels
662
+
663
+ # Send request to Jira API
664
+ url = f"{ server_url } /rest/api/latest/issue"
665
+ auth = (username , api_token )
666
+ headers = {"Content-Type" : "application/json" }
667
+
668
+ response = requests .post (url , json = payload , auth = auth , headers = headers )
669
+ if response .status_code == 201 :
670
+ print_debug (args , f"Jira ticket created successfully: { response .json ().get ('key' )} " )
671
+ else :
672
+ print_debug (args , f"Failed to create Jira ticket: { response .status_code } - { response .text } " )
0 commit comments