class QGrid_To_Jira: def __init__(self, grid): self.grid = grid self.api_jira = API_Jira() self.api_jira_rest = API_Jira_Rest() def jira_update_issue_status(self, key, value): transitions = self.api_jira.issue_next_transitions(key) transitions_id = transitions.get(value) if transitions_id is None: return { 'status': 'error', 'data': 'transition not available: {0}'.format(value) } result = self.api_jira.jira().transition_issue(key, transitions_id) return {'status': 'ok', 'data': result} def jira_remove_links_to_target(self, from_id, link_type, to_id): all_issue_links = self.api_jira.issue_links(from_id) issue_links = all_issue_links.get(link_type) if issue_links: for issue_link in issue_links: if to_id == issue_link.get('Key'): link_id = issue_link.get('Id') print('deleting link', from_id, link_type, link_id) self.api_jira.issue_delete_link(link_id) return True return False #return True # to handle cases when a mistaske is made on the new issue link type def jira_update_issue_link(self, issue_id, to_id, old_link_issue, new_link_issue): removed_ok = self.jira_remove_links_to_target(issue_id, old_link_issue, to_id) if new_link_issue is '': if removed_ok: return {'status': 'ok', 'data': 'issue link removed'} else: return {'status': 'error', 'data': 'failed to remove link'} try: self.api_jira.issue_add_link(issue_id, new_link_issue, to_id) print('added link', issue_id, new_link_issue, to_id) if removed_ok: return {'status': 'ok', 'data': 'issue link edited'} else: print('Removing link failed', issue_id, old_link_issue, to_id) return { 'status': 'error', 'data': 'removing link failed BUT, adding new link worked' } except Exception as error: print('Failed to add link', issue_id, new_link_issue, to_id) print(error.text) if removed_ok: return { 'status': 'error', 'data': 'adding link failed (after removing link)' } else: return { 'status': 'error', 'data': 'adding and removing links failed' } def jira_update_field(self, key, field, value): if not value: result = {'status': 'error', 'data': 'empty values not supported'} else: value = value.strip() if field == 'Latest_Information': field = 'Latest Information' if field == 'Status': result = self.jira_update_issue_status(key, value) else: data = self.api_jira_rest.issue_update_field(key, field, value) if data is False: result = {'status': 'error', 'data': data} else: result = {'status': 'ok', 'data': data} result['key'] = key result['field'] = field result['value'] = value return result def setup(self): def on_value_change(event, qgrid_widget): key = event.get('index') field = event.get('column') value = event.get('new') print("updating field `{0}` with value `{1}` on issue `{2}".format( field, value, key)) result = self.jira_update_field(key, field, value) print(result) self.grid.on('cell_edited', on_value_change) return self
class Test_API_Jira(unittest.TestCase): def setUp(self): self.api = API_Jira() # check connection def test_jira_server_info(self): jira = self.api.jira() assert jira.server_info().get('version') == '7.5.0' def test_jira(self): jira = self.api.jira() assert jira.__class__.__name__ == 'JIRA' assert jira.current_user() == 'gsbot' # methods def test_covert_issue(self): raw_issue = self.api.jira().issue('TASK-502') # 'FACT-10') # issue = self.api.convert_issue(raw_issue) Dev.pprint(issue) # raw_issue = self.api.jira().issue('SEC-9195')# 'FACT-10') # # issue = self.api.convert_issue(raw_issue) # self.api.convert_issue(self.api.jira().issue('SEC-9195')) # assert issue['Labels' ] == ['SEC-9195', 'SEC-9195-CFO'] # check the fields added recently # assert issue['Priority' ] == 'Minor' # # raw_issue = self.api.jira().issue('GSOKR-900') # check epic link # issue = self.api.convert_issue(raw_issue) # assert issue['Epic Link'] == 'GSOKR-924' # # raw_issue = self.api.jira().issue('GSOKR-872') # check parent (for sub taks) # issue = self.api.convert_issue(raw_issue) # assert issue['Parent'] == 'GSOKR-279' # #Dev.pprint(issue) def test_fields(self): fields = self.api.fields() assert len(fields) > 10 # data = {} # for field in fields: # data[field['name']] = field # print() # print('|--------------------------------------------------------------------------------------------------|') # print('| name | id | custom |') # print('|--------------------------------------------------------------------------------------------------|') # for name in sorted(set(data.keys())): # field = data[name] # print('| {0:55} | {1:25} | {2:10} |'.format(field['name'], field['id'], str(field['custom']))) def test_fields_by_name(self): fields_by_name = self.api.fields_by_name() assert len(set(fields_by_name)) > 300 @unittest.skip('this needs to be fixed top reflect the latest added fields' ) def test_issue(self): issue = self.api.issue('RISK-1083') assert issue == { 'Components': [], 'Issue Type': 'Risk', 'Issue Links': [], 'Key': 'RISK-1083', 'Issue Links': { 'RISK-1060': { 'Direction': 'Inward', 'Issue Type': 'Risk', 'Link Type': 'is blocked by', 'Priority': 'Minor', 'Status': 'Fixed', 'Summary': 'Test Risk' }, 'RISK-45': { 'Direction': 'Outward', 'Issue Type': 'Risk', 'Link Type': 'is child of', 'Priority': 'High', 'Status': 'Fixed', 'Summary': 'Test issue creation' }, 'RISK-32': { 'Direction': 'Outward', 'Issue Type': 'Risk', 'Link Type': 'relates to', 'Priority': 'Medium', 'Status': 'Fixed', 'Summary': 'TEST' } }, 'Rating': 'To be determined', 'Risk Description': 'This is an description', 'Risk Owner': 'Dinis Cruz', 'Status': 'Fixed', 'Summary': 'Test risk to GS-Bot' } def test_issue_add_link__error(self): try: Dev.pprint(self.api.issue_add_link('from', 'link_type', 'to')) except Exception as error: assert error.text == "No issue link type with name 'link_type' found." @unittest.skip( 'needs to be refactored (this method will get the change log for the issues' ) def _test_issue_change_log_only_status(self): statuses = self.api.issue_changes_log_only_status("Project=VULN", 300) Dev.pprint(statuses) #Dev.pprint(list(set(types))) def test_issue_update(self): issue_data = { "Key": "RISK-12", "Summary": "new summary value", "Risk Description": "el risk description", "Description": "the description" } #"Risk Rating" : "Low" } #"Status" : "Blocked" ,} result = self.api.issue_update(issue_data) Dev.pprint(result) # def test_issues_updated_in_last_hour(self): # results = self.api.issues_updated_in_last_hour() # assert len(results) > 0 # # # results_1h = self.api.issues_updated_in_last_hour(1) # results_10h = self.api.issues_updated_in_last_hour(10) # assert results == results_1h # assert set(results_1h) == {'FACT-13', 'RISK-1597'} # assert len(set(results_10h)) == 19 # def test_issues_updated_in_last_day(self): # results = self.api.issues_updated_in_last_day() # assert len(set(results)) > 1 # #Dev.pprint(len(set(results))) def test_projects(self): results = self.api.jira().projects() assert len(results) > 100 def test_search(self): issues = self.api.search('labels=R1') assert issues['RISK-1592']['Summary'] == '6 - Risk to Brand' assert len(set(issues)) == 6 def test_search_just_return_keys(self): keys = self.api.search_just_return_keys('labels=R1') assert keys == [ 'RISK-1592', 'RISK-1534', 'RISK-1498', 'RISK-1496', 'RISK-1495', 'RISK-1494' ] #keys = self.api.search_just_return_keys('project=RISK') #assert len(set(keys)) > 100 def test_covert_issue_vuln_priority(self): assert self.api.issue('VULN-1205').get('VULN Priority') == 'P3' assert self.api.issue('VULN-1624').get('VULN Priority') is None