11import os
2- import json
2+ import argparse
33import requests
4+ from requests .auth import HTTPBasicAuth
45from dotenv import load_dotenv
56from atlassian import Jira
6-
7- # Initializing jira client
8- load_dotenv ()
9- JIRA_URL = os .getenv ('JIRA_URL' )
10- JIRA_USERNAME = os .getenv ('JIRA_USERNAME' )
11- JIRA_PASSWORD = os .getenv ('JIRA_PASSWORD' )
12-
7+ from github_scanning import scan_for_dependabot_alerts_and_issues
8+ from repos import list_repos_in_org
9+ from date_time import date_time
10+ from common_utils import limit_string_length , compare_summaries_from_epic_and_scan
1311
1412
1513# Function to create a story under a specific epic
16- def create_story_under_epic (epic_key , summary , description ):
14+ def create_story_under_epic (epic_key : str , summary : str , description : str ):
15+ """
16+ Create jira task under a specific epic
17+ :param epic_key:
18+ :param summary:
19+ :param description:
20+ :return:
21+ """
22+ # Initializing jira client
23+ load_dotenv ()
24+ JIRA_URL = os .getenv ('JIRA_URL' )
25+ JIRA_USERNAME = os .getenv ('JIRA_USERNAME' )
26+ JIRA_PASSWORD = os .getenv ('JIRA_PASSWORD' )
27+ jira = Jira (url = os .getenv ('JIRA_URL' ), username = os .getenv ('JIRA_USERNAME' ), password = os .getenv ('JIRA_PASSWORD' ),
28+ cloud = True )
29+ # for issue in unmatched_issues:
30+ # print('h')
1731 issue_dict = {
1832 'project' : {'key' : 'DEVOPS' },
1933 'summary' : summary ,
@@ -24,48 +38,110 @@ def create_story_under_epic(epic_key, summary, description):
2438 }
2539 }
2640 new_issue = jira .issue_create (fields = issue_dict )
27- # pic = new_issue.update(fields={'parent': {'id': epic_key}} )
41+ print ( f"Created story { new_issue [ 'key' ] } with epic { epic_key } at { date_time () } IST" )
2842
29- return new_issue
3043
31-
32- def issues_in_epic (epic_key : str , board_id : int ):
44+ def issues_in_epic (epic_key : str ):
3345 """
3446 list available issues in a epic
3547 :param epic_key:
3648 :return:
3749 """
50+ # Initializing jira client
51+ load_dotenv ()
52+ JIRA_URL = os .getenv ('JIRA_URL' )
53+ JIRA_USERNAME = os .getenv ('JIRA_USERNAME' )
54+ JIRA_PASSWORD = os .getenv ('JIRA_PASSWORD' )
3855 jira = Jira (url = os .getenv ('JIRA_URL' ), username = os .getenv ('JIRA_USERNAME' ), password = os .getenv ('JIRA_PASSWORD' ),
3956 cloud = True )
4057
41- # todo
58+ url = f'{ JIRA_URL } /rest/api/3/search'
59+
60+ auth = HTTPBasicAuth (JIRA_USERNAME , JIRA_PASSWORD )
61+
62+ headers = {
63+ "Accept" : "application/json"
64+ }
65+
66+ query = {
67+ 'jql' : f'Parent = { epic_key } '
68+ }
69+ response = requests .request (
70+ "GET" ,
71+ url ,
72+ headers = headers ,
73+ params = query ,
74+ auth = auth
75+ )
76+ response_json = response .json ()
77+ response_json_issues = response_json ['issues' ]
78+ if not response_json_issues :
79+ issues_in_epic = []
80+ else :
81+ issues_in_epic = response_json ['issues' ]
82+
83+ epic_details = []
84+ for issues in issues_in_epic :
85+ issue = {}
86+ issue ['key' ] = issues ['key' ]
87+ issue ['issue_type' ] = issues ['fields' ]['issuetype' ]['name' ]
88+ issue ['issue_summary' ] = issues ['fields' ]['summary' ]
89+ issue ['description' ] = 'NA'
90+ epic_details .append (issue )
91+
92+ return epic_details
93+
94+
95+ def compare_summaries_and_create_story (repo_names : list , organization : str , epic_key : str ):
96+ """
97+ The function helps create stories by calling another function.
98+ :param issues_list:
99+ :param epic_details:
100+ :return:
101+ """
102+ epic_details = issues_in_epic (epic_key = epic_key )
103+
104+ for repo in repo_names :
105+ # scan details
106+ issues_list = scan_for_dependabot_alerts_and_issues (organization = organization , repository = repo )
107+ if not issues_list :
108+ print (f'No dependabot security alerts found on { organization } /{ repo } repository' )
109+ continue
110+ create_story_true = compare_summaries_from_epic_and_scan (issues_list = issues_list , epic_details = epic_details )
111+ for issue in create_story_true :
112+ summary_from_gh = f"{ issue ['cve_id' ]} - { issue ['repository' ]} - { issue ['issue_severity' ]} - { issue ['issue_summary' ]} "
113+ description_from_gh = issue ['issue_description' ]
114+ summary_after_character_length_check = limit_string_length (input_string = summary_from_gh )
115+ description_after_character_length_check = limit_string_length (input_string = description_from_gh )
116+
117+ create_story_under_epic (summary = summary_after_character_length_check ,
118+ description = description_after_character_length_check , epic_key = epic_key )
119+
42120
43- def get_board_id (project_key : str ):
44- # jira = Jira(url=os.getenv('JIRA_URL_0'), username=os.getenv('JIRA_USERNAME'), password=os.getenv('JIRA_PASSWORD'),
45- # cloud=True)
46- response = requests .get (url = os .getenv ('JIRA_URL_0' ),auth = (os .getenv ('JIRA_USERNAME' ), os .getenv ('JIRA_PASSWORD' )))
47- if response .status_code == 200 :
48- boards = response .json ()['values' ]
49- if boards :
50- # Assuming you want the first board ID
51- return boards [0 ]['id' ]
52- return None
53121
54122def main ():
55123 """testing script"""
56- # Replace these values with your specific details
57- epic_key = 'DEVOPS-74'
58- summary = 'New Story Summary135'
59- description = 'Description -- of the new story'
60124
61- # Create the story
62- # story = create_story_under_epic(epic_key, summary, description)
63- # print(f"Created story {story['key']} under epic {epic_key}")
125+ parser = argparse .ArgumentParser (description = 'Create Jira issues prograatically based on dependabot alerts' )
126+ parser .add_argument ("--epic_key" , help = "jira epic ID" , type = str , required = True )
127+ parser .add_argument ('--organization' , help = 'Github org name' , type = str , required = True )
128+
129+ args = parser .parse_args ()
130+
131+ epic_key = args .epic_key
132+ organization = args .organization
64133
65134 # List stories under an epic
66- stories = issues_in_epic (epic_key = epic_key , board_id = 1 )
135+ epic_details = issues_in_epic (epic_key = epic_key )
136+
137+ # Repos in GitHub org
138+ repo_names = list_repos_in_org (org_name = organization )
139+
140+ # compare summaries to decide to create a new story or not
141+ compare_summaries_and_create_story (repo_names = repo_names , organization = organization ,
142+ epic_key = epic_key )
143+
67144
68- # get_board_id('DEVOPS')
69145
70146if __name__ == "__main__" :
71147 main ()
0 commit comments