I'm trying to test function that connects to Jira and creates Jira issue
My services.py
def get_jira_client():
global jira_client
if jira_client is None:
jira_client = JIRA(server=JIRA_URL, auth=(jira_user, jira_password))
return jira_client
def register_jira_issue(ticket_id):
jira = get_jira_client()
ticket = Ticket.objects.prefetch_related(Prefetch('attachments', to_attr='ticket_attachments'),
Prefetch('tags', to_attr='ticket_tags')).filter(id=ticket_id).first()
new_issue = add_issue(jira, ticket, ticket.owner)
set_tags(new_issue, *ticket.ticket_tags)
add_attachments(ticket.ticket_attachments, new_issue)
def add_issue(jira, ticket, owner):
issue_dict = {
'project': {'key': project_key},
'summary': ticket.title,
'description': format_text_for_jira(ticket.description),
'issuetype': {'name': issuetype_name},
cf_username: str(owner.username),
cf_hd_grant_id: ticket.grant_id,
cf_user_email: str(owner.email),
cf_user_fullname: f"{owner.first_name} {owner.last_name}"
}
new_issue = jira.create_issue(issue_dict)
ticket.key = new_issue
ticket.save()
logger.info(f'User {ticket.owner} added issue ')
return new_issue
def set_tags(jira_issue, tags):
tags_dict = Tags.objects.filter(id=tags.id).values()[0]
chosen_tags = [key for key, _ in tags_dict.items() if _ is True]
if tags_dict['cluster_calculations'] or tags_dict['cluster_installation']:
chosen_tags.append(tags_dict['cluster'])
jira_issue.update(fields={"labels": chosen_tags})
logger.info(f'User set tags {chosen_tags} to issue {jira_issue}')
def add_attachments(files, jira_issue):
jira = get_jira_client()
for file in files:
jira.add_attachment(issue=jira_issue, attachment=file.file)
logger.info(f'User added attachment {file} to issue {jira_issue}.')
function register jira issue
is asynchronously called in views.py
and it uses these functions:
add_issue
- registers issue in jiraset_tags
- sets labels for issueadd_attachments
- adds attachment to issue
Does anyone know a way to test ticket registration in jira? I've tried requests_mock
but it didn't work for me. I also saw some exapmples of unittest.mock patch
but I don't konow how to use it with jira instance
Aucun commentaire:
Enregistrer un commentaire