From c72d54cb6736c6b5a5b46aa5d0ba8920209e934a Mon Sep 17 00:00:00 2001 From: Bohdan Iakymets Date: Aug 06 2019 11:16:11 +0000 Subject: Refactored status transition and updated README --- diff --git a/README.rst b/README.rst index 74ae335..d55a9b6 100644 --- a/README.rst +++ b/README.rst @@ -25,7 +25,8 @@ ____________ Each project is accompanied by an 'updates' array as seen below:: 'Demo_project': {'project': 'PROJECT', 'component': 'COMP', - 'updates': [...], 'owner': 'project_owner_username'}, + 'updates': [...], 'owner': 'project_owner_username', + 'default_status': 'start_status_for_issue'}, The following can be added to the updates array to specify what to sync with downstream diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index 7facf3a..daea129 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -348,6 +348,31 @@ def assign_user(client, issue, downstream, remove_all=False): return log.warning('Was not able to assign user %s' % issue.assignee[0]['fullname']) +def _change_status(client, downstream, status, issue): + """ + Change status of JIRA issue + Args: + client (jira.client.JIRA): JIRA client + downstream (jira.resources.Issue): JIRA issue object + status (str): Title of status to which issue should be move + issue (sync2jira.intermediary.Issue): Issue object + Returns: + Nothing + """ + transitions = client.transitions(downstream) + id = '' + for t in transitions: + if t['name'] and status.upper() == str(t['name']).upper(): + id = int(t['id']) + break + if id: + try: + client.transition_issue(downstream, id) + log.info(' Updated downstream to %s status for issue %s' % (status, issue.title)) + except JIRAError: + log.error(' Updating downstream issue failed for %s: %s' % (status, issue.title)) + else: + log.warning(' Could not update JIRA %s for %s' % (status, issue.title)) def _create_jira_issue(client, issue, config): """ @@ -367,8 +392,8 @@ def _create_jira_issue(client, issue, config): return custom_fields = issue.downstream.get('custom_fields', {}) - default_status = issue.downstream.get('default_status', None) default_type = issue.downstream.get('type', "Bug") + # Build the description of the JIRA issue if 'description' in issue.downstream.get('updates', {}): description = "Upstream description: {quote}%s{quote}" % issue.content @@ -403,14 +428,9 @@ def _create_jira_issue(client, issue, config): remote_link = dict(url=issue.url, title=remote_link_title) _attach_link(client, downstream, remote_link) + default_status = issue.downstream.get('default_status', None) if default_status != None: - transitions = client.transitions(issue) - id = '' - for t in transitions: - if default_status == t['name']: - id = t['id'] - if id: - client.transition_issue(downstream, id) + _change_status(client, downstream, default_status, issue) # Update relevant information (i.e. tags, assignees etc.) if the # User opted in @@ -548,20 +568,7 @@ def _update_transition(client, existing, issue): # Now we need to update the status of the JIRA issue\ # Ensure that closed_status is a valid choice # Find all possible transactions (i.e. change states) we could `do - transactions = client.transitions(existing) - found = False - for t in transactions: - if t['name'] and str(t['name']).upper() == closed_status.upper(): - # We can perform this transition (i.e. change state) - try: - client.transition_issue(existing, int(t['id'])) - log.info(' Updated downstream closed status for issue %s' % issue.title) - except JIRAError: - log.error(' Updating downstream issue failed for closed_status: %s' % closed_status) - found = True - if not found: - log.warning(' Could not update JIRA closed_status for %s' % issue.title) - + _change_status(client, existing, closed_status, issue) def _update_comments(client, existing, issue): """