def _close_as_duplicate(client, duplicate, keeper, config): """ Helper function to close an issue as a duplicate. :param jira.client client: JIRA Client :param jira.resources.Issue duplicate: Duplicate JIRA Issue :param jira.resources.Issue keeper: JIRA issue to keep :param Dict config: Config dict :returns: Nothing """ log.info("Closing %s as duplicate of %s", duplicate.permalink(), keeper.permalink()) if config['sync2jira']['testing']: log.info("Testing flag is true. Skipping actual delete.") return # Find the id of some dropped or done state. transitions = client.transitions(duplicate) transitions = dict([(t['name'], t['id']) for t in transitions]) closed = None preferences = ['Dropped', 'Reject', 'Done', 'Closed', 'Closed (2)', ] for preference in preferences: if preference in transitions: closed = transitions[preference] break text = 'Marking as duplicate of %s' % keeper.key if any([text in comment.body for comment in client.comments(duplicate)]): log.info("Skipping comment. Already present.") else: client.add_comment(duplicate, text) text = '%s is a duplicate.' % duplicate.key if any([text in comment.body for comment in client.comments(keeper)]): log.info("Skipping comment. Already present.") else: client.add_comment(keeper, text) if closed: try: client.transition_issue(duplicate, closed, resolution={'name': 'Duplicate'}) except Exception as e: if "Field 'resolution' cannot be set" in e.response.text: # Try closing without a specific resolution. try: client.transition_issue(duplicate, closed) except Exception: log.exception("Failed to close %r", duplicate.permalink()) else: log.exception("Failed to close %r", duplicate.permalink()) else: log.warning("Unable to find close transition for %r" % duplicate.key)
def check_comments(existing, issue, client): """ Updates comments in issue.out_of_sync list Args: existing (jira.resource.Issue): JIRA client issue (jibe.intermediary.Issue): Issue object client (jira.client.JIRA): JIRA client Returns: response (jibe.intermediary.Issue): Issue object with updated out-of-sync updated """ # Get all existing comments comments = client.comments(existing) # Remove any comments that have already been added comments_d = comment_matching(issue.comments, comments) updated_comments = [] for comment in comments_d: # Loop through any comments returned # and format updated_comments.append( Comment(author=comment['author'], body=comment['body'], date_created=comment['date_created'])) # Update and return issue if len(updated_comments) == 0: issue.out_of_sync['comments'] = 'in-sync' else: issue.out_of_sync['comments'] = updated_comments return issue
def check_comments_for_duplicate(client, result, username): """ Checks comment of JIRA issue to see if it has been marked as a duplicate. :param jira.client.JIRA client: JIRA client) :param jira.resource.Issue result: JIRA issue :param string username: Username of JIRA user :returns: True if duplicate comment was not found or JIRA issue if \ we were able to find it :rtype: Bool or jira.resource.Issue """ for comment in client.comments(result): search = re.search(r'Marking as duplicate of (\w*)-(\d*)', comment.body) if search and comment.author.name == username: issue_id = search.groups()[0] + '-' + search.groups()[1] return client.issue(issue_id) return True
def _update_comments(client, existing, issue): """ Helper function to sync comments between existing JIRA issue and upstream issue. :param jira.client.JIRA client: JIRA client :param jira.resource.Issue existing: Existing JIRA issue :param sync2jira.intermediary.Issue issue: Upstream issue :returns: Nothing """ # First get all existing comments comments = client.comments(existing) # Remove any comments that have already been added comments_d = _comment_matching(issue.comments, comments) # Loop through the comments that remain for comment in comments_d: # Format and add them comment_body = _comment_format(comment) client.add_comment(existing, comment_body) if len(comments_d) > 0: log.info("Comments synchronization done on %i comments." % len(comments_d))
def check_comments_for_duplicate(client, result, username): """ Checks comment of JIRA issue to see if it has been marked as a duplicate Args: client (jira.client.JIRA): JIRA client) result (jira.resource.Issue): JIRA issue username (str): Username of JIRA user Returns: return (bool): True if duplicate comment was not found *Or* return (jira.resource.Issue): JIRA issue if we were able to find it """ for comment in client.comments(result): search = re.search(r'Marking as duplicate of (\w*)-(\d*)', comment.body) if search and comment.author.name == username: issue_id = search.groups()[0] + '-' + search.groups()[1] return client.issue(issue_id) return True