11import argparse
2+ import re
3+ from urllib .parse import quote
24import requests
35
46BOT_USERNAME = 'openshift-crt-jira-release-controller'
57JIRA_URL = 'https://issues.redhat.com/'
8+ ISSUE_KEY_PATTERN = re .compile (r'^[A-Z][A-Z0-9_]+-\d+$' )
9+ COMMENT_ID_PATTERN = re .compile (r'^\d+$' )
610
711def main ():
812 parser = argparse .ArgumentParser (description = "JIRA Issue Comment Management CLI Tool" )
@@ -49,7 +53,11 @@ def main():
4953 comment_count = sum (1 for comment in comments if TARGET_COMMENT in comment .get ('body' , '' ))
5054
5155 if comment_count > 1 :
52- issue_comment_counts [issue ['key' ]] = comment_count
56+ key = issue ['key' ]
57+ if not ISSUE_KEY_PATTERN .match (key ):
58+ print (f"Skipping issue with invalid key format: { key } " )
59+ continue
60+ issue_comment_counts [key ] = comment_count
5361
5462 print ("Issues with the target comment appearing more than once:" )
5563 for issue_key , comment_count in issue_comment_counts .items ():
@@ -66,7 +74,10 @@ def main():
6674 break
6775
6876def delete_comments (issue_key , headers , SUB_TARGET_COMMENT ):
69- issue_url = f'{ JIRA_URL } /rest/api/2/issue/{ issue_key } ?fields=comment'
77+ if not ISSUE_KEY_PATTERN .match (issue_key ):
78+ print (f"Skipping issue with invalid key format: { issue_key } " )
79+ return
80+ issue_url = f'{ JIRA_URL } /rest/api/2/issue/{ quote (issue_key , safe = "" )} ?fields=comment'
7081 response = requests .get (issue_url , headers = headers )
7182
7283 if response .status_code == 200 :
@@ -79,7 +90,10 @@ def delete_comments(issue_key, headers, SUB_TARGET_COMMENT):
7990
8091 for matching_comments in matching_401_comments :
8192 comment_id = matching_comments .get ('id' )
82- comment_delete_url = f'{ JIRA_URL } /rest/api/2/issue/{ issue_key } /comment/{ comment_id } '
93+ if not comment_id or not COMMENT_ID_PATTERN .match (str (comment_id )):
94+ print (f"Skipping comment with invalid ID format: { comment_id } " )
95+ continue
96+ comment_delete_url = f'{ JIRA_URL } /rest/api/2/issue/{ quote (issue_key , safe = "" )} /comment/{ quote (str (comment_id ), safe = "" )} '
8397 response = requests .delete (comment_delete_url , headers = headers )
8498 if response .status_code == 204 :
8599 print (f"Deleted comment { comment_id } from issue { issue_key } " )
0 commit comments