|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | + |
| 6 | +# Dictionaries to store different options |
| 7 | +affected_areas = { |
| 8 | + "Devtron dashboard completely down": 100, |
| 9 | + "Login issues": 50, |
| 10 | + "RBAC Issues": 40, |
| 11 | + "CI": 50, |
| 12 | + "CD": 50, |
| 13 | + "App creation": 30, |
| 14 | + "Deployment from Chart store": 40, |
| 15 | + "Security features": 50, |
| 16 | + "CI/CD Plugins": 30, |
| 17 | + "Other CRITICAL functionality": 30, |
| 18 | + "Other NON-CRITICAL functionality": 20, |
| 19 | + "None": 0 |
| 20 | +} |
| 21 | + |
| 22 | +additional_affected_areas = { |
| 23 | + "Devtron dashboard completely down": 100, |
| 24 | + "Login issues": 50, |
| 25 | + "RBAC Issues": 40, |
| 26 | + "CI": 50, |
| 27 | + "CD": 50, |
| 28 | + "App creation": 30, |
| 29 | + "Deployment from Chart store": 40, |
| 30 | + "Security features": 50, |
| 31 | + "CI/CD Plugins": 30, |
| 32 | + "Other CRITICAL functionality": 30, |
| 33 | + "Other NON-CRITICAL functionality": 20, |
| 34 | + "None": 0 |
| 35 | +} |
| 36 | + |
| 37 | +prod_environment = { |
| 38 | + "Prod": 2, |
| 39 | + "Non-prod": 1, |
| 40 | + "None": 1 |
| 41 | +} |
| 42 | + |
| 43 | +user_unblocked = { |
| 44 | + "Yes": 1, |
| 45 | + "No": 2, |
| 46 | + "None": 1 |
| 47 | +} |
| 48 | + |
| 49 | +user_unblocked_reason = { |
| 50 | + "TEMPORARILY - By disabling a CRITICAL functionality": 3, |
| 51 | + "TEMPORARILY - By disabling a NON-CRITICAL functionality": 1.2, |
| 52 | + "TEMPORARILY - By doing some changes from the backend/DB": 1, |
| 53 | + "PERMANENTLY - By giving a workaround (From outside Devtron)": 2, |
| 54 | + "PERMANENTLY - By giving a workaround (Within Devtron)": 1, |
| 55 | + "None": 1 |
| 56 | +} |
| 57 | +# Function to extract and process information from the issue body |
| 58 | +def process_issue_body(issue_body): |
| 59 | + # Regular expressions to extract specific sections from the issue body |
| 60 | + affected_areas_pattern = r'###\s*Affected\s*areas\s*\n\n(.*?)\n\n###' |
| 61 | + additional_affected_areas_pattern = r'###\s*Additional\s*affected\s*areas\s*\n\n(.*?)\n\n###' |
| 62 | + prod_non_prod_pattern = r'###\s*Prod/Non-prod\s*environments\?\s*\n\n(.*?)\n\n###' |
| 63 | + user_unblocked_pattern = r'###\s*Is\s*User\s*unblocked\?\s*\n\n(.*?)\n\n###' |
| 64 | + user_unblocked_reason_pattern = r'###\s*How\s*was\s*the\s*user\s*un-blocked\?\s*\n\n(.*?)\n\n###' |
| 65 | + |
| 66 | + # Matching patterns in the issue body |
| 67 | + affected_areas_match = re.search(affected_areas_pattern, issue_body) |
| 68 | + additional_affected_areas_match = re.search(additional_affected_areas_pattern, issue_body) |
| 69 | + prod_non_prod_match = re.search(prod_non_prod_pattern, issue_body) |
| 70 | + user_unblocked_match = re.search(user_unblocked_pattern, issue_body) |
| 71 | + user_unblocked_reason_match = re.search(user_unblocked_reason_pattern, issue_body) |
| 72 | + |
| 73 | + # Extracting values from matches or setting default value to "None" if match not found |
| 74 | + affected_area_value = affected_areas_match.group(1).strip() if affected_areas_match else "None" |
| 75 | + additional_affected_area_value = additional_affected_areas_match.group(1).strip() if additional_affected_areas_match else "None" |
| 76 | + prod_non_prod_value = prod_non_prod_match.group(1).strip() if prod_non_prod_match else "None" |
| 77 | + user_unblocked_value = user_unblocked_match.group(1).strip() if user_unblocked_match else "None" |
| 78 | + user_unblocked_reason_value = user_unblocked_reason_match.group(1).strip() if user_unblocked_reason_match else "None" |
| 79 | + |
| 80 | + # Retrieving values from dictionaries |
| 81 | + affected_areas_score = affected_areas.get(affected_area_value, 0) |
| 82 | + additional_affected_areas_score = additional_affected_areas.get(additional_affected_area_value, 0) |
| 83 | + prod_non_prod_score = prod_environment.get(prod_non_prod_value, 1) |
| 84 | + user_unblocked_score = user_unblocked.get(user_unblocked_value, 1) |
| 85 | + user_unblocked_reason_score = user_unblocked_reason.get(user_unblocked_reason_value, 1) |
| 86 | + |
| 87 | + print("Affected areas:", affected_area_value) |
| 88 | + print("Additional affected areas:", additional_affected_area_value) |
| 89 | + print("Prod/Non-prod environments?:", prod_non_prod_value) |
| 90 | + print("Is User unblocked?:", user_unblocked_value) |
| 91 | + print("How was the user un-blocked?:", user_unblocked_reason_value) |
| 92 | + |
| 93 | + # Checking for required values and skipping execution of script, if not found |
| 94 | + if affected_areas_score == 0 or prod_non_prod_score == 0 or user_unblocked_score == 0: |
| 95 | + print("One or more required values are missing. Exiting...") |
| 96 | + sys.exit(0) |
| 97 | + |
| 98 | + if user_unblocked_reason_score == 0: |
| 99 | + user_unblocked_reason_score = 1 |
| 100 | + |
| 101 | + # Adding 'urgent' label to the issue if user_unblocked_reason is 'TEMPORARILY - By disabling a CRITICAL functionality' or affected_areas is 'Devtron dashboard completely down' |
| 102 | + if user_unblocked_reason_score == 3 or affected_areas_score == 100: |
| 103 | + try: |
| 104 | + |
| 105 | + result = subprocess.run(['gh', 'issue', 'edit', str(issue_number), '--add-label', 'urgent'], capture_output=True, check=True, text=True) |
| 106 | + print("urgent label added to issue", issue_number) |
| 107 | + except subprocess.CalledProcessError as e: |
| 108 | + print(e.stderr) |
| 109 | + #calculating final score |
| 110 | + final_score = affected_areas_score + additional_affected_areas_score * prod_non_prod_score * user_unblocked_score * user_unblocked_reason_score |
| 111 | + print("Final Score:", final_score) |
| 112 | + |
| 113 | + # Commenting the final score in the issue |
| 114 | + comment = f"Final Score: {final_score}" |
| 115 | + try: |
| 116 | + result1 = subprocess.run(['gh', 'issue', 'comment', str(issue_number), '--body', comment], capture_output=True, check=True, text=True) |
| 117 | + print("Final score commented on issue", issue_number) |
| 118 | + except subprocess.CalledProcessError as e: |
| 119 | + print(e.stderr) |
| 120 | + return final_score |
| 121 | + |
| 122 | +token = os.environ.get('GITHUB_TOKEN') |
| 123 | +subprocess.run(['gh', 'auth', 'login', '--with-token'], input=token, text=True, capture_output=True) |
| 124 | + |
| 125 | +# Retrieving environment variables |
| 126 | +issue_body = os.environ.get('ISSUE_BODY') |
| 127 | +issue_number = os.environ.get('ISSUE_NUMBER') |
| 128 | +pagerduty_score_threshold = os.environ.get('PAGERDUTY_SCORE_THRESHOLD') |
| 129 | + |
| 130 | +final_score = process_issue_body(issue_body) |
| 131 | + |
| 132 | + |
| 133 | +# Removing 'pager-duty' label from issue if final score is below the threshold |
| 134 | +if final_score <= int(pagerduty_score_threshold): |
| 135 | + try: |
| 136 | + result = subprocess.run(['gh', 'issue', 'edit', str(issue_number), '--remove-label', 'pager-duty']) |
| 137 | + print("pager-duty label removed from issue", issue_number) |
| 138 | + except subprocess.CalledProcessError as e: |
| 139 | + print(e) |
0 commit comments