Skip to content

Commit dbff0e7

Browse files
authored
Merge pull request #12 from rohitcoder/feat/jira-ticketing
Added Jira ticketing feature
2 parents 06c40c7 + a7e872a commit dbff0e7

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ connection.yml
22
.DS_Store
33
data/
44
previous_alerts.json
5+
user_ids.json
56
# Byte-compiled / optimized / DLL files
67
__pycache__/
78
*.py[cod]

hawk_scanner/internals/system.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,93 @@ def perform_ocr(image):
580580
# Use Tesseract OCR
581581
ocr_text = pytesseract.image_to_string(image)
582582

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}")

hawk_scanner/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def main():
260260
records_mini = ', '.join(result['matches']) if len(result['matches']) < 25 else ', '.join(result['matches'][:25]) + f" + {len(result['matches']) - 25} more"
261261
slack_message = format_slack_message(group, result, records_mini)
262262
if slack_message:
263+
system.create_jira_ticket(args, result, slack_message)
263264
system.SlackNotify(slack_message, args)
264265

265266
if group == 's3':

0 commit comments

Comments
 (0)