コード例 #1
0
    def clone_ticket(self, ticket, parent=None, only_matched=False):
        """Clone one ticket with its parents, subtasks and links.

        Args:
            ticket: Ticket object to clone
            parent: Parent's id in case we know parent ticket, default None
            only_matched: Bool value to choose if only tickets that matched
                          previous query (i.e. tickets in self.tickets) should
                          be cloned, default False
        """
        if ticket.ticket_id in self._cloned:
            return
        if only_matched and ticket.ticket_id not in self._ticket_ids:
            # cloning only matched tickets and this doesn't match
            return
        if ticket.status == 'Deprecated':
            self.log.info('Skipped {0} as it is in deprecated state.'.format(
                ticket.ticket_id))
            return
        if ticket.parent_id and ticket.parent_id not in self._cloned:
            # if it's subtask we first need to create its parent and then
            # subtasks are added when cloning parent task to preserve order
            self.clone_ticket(Ticket(
                prod=self.prod,
                project=self.project,
                ticket_id=ticket.parent_id,
                custom_substitutions=self.custom_substitutions),
                              only_matched=only_matched)
            return
        new = Ticket(prod=self.prod,
                     project=self.project,
                     custom_substitutions=self.custom_substitutions)
        if ticket.issuetype == 'Sub-task':
            self.log.info('Cloning SubTask {0} - {1}'.format(
                ticket.ticket_id, ticket.summary))
        else:
            self.log.info('Cloning Parent {0} - {1}'.format(
                ticket.ticket_id, ticket.summary))
        if not self.dry_run:
            new.clone(ticket,
                      inject=self.inject,
                      parent=parent,
                      custom_substitutions=self.custom_substitutions)
            new.add_comment('This issue was cloned from {0}'.format(
                ticket.ticket_id))
        else:
            # we need generic ticket_id for dry-run
            new.ticket_id = 'ID'
        self._cloned[ticket.ticket_id] = new.ticket_id
        if ticket.subtask_ids:
            for subtask_id in ticket.subtask_ids:
                if subtask_id not in self._cloned:
                    self.clone_ticket(Ticket(
                        prod=self.prod,
                        project=self.project,
                        ticket_id=subtask_id,
                        custom_substitutions=self.custom_substitutions),
                                      parent=new.ticket_id,
                                      only_matched=only_matched)
        if ticket.links:
            for link in ticket.links:
                linked_ticket_id, link_type, direction = link
                if linked_ticket_id not in self._cloned:
                    self.clone_ticket(Ticket(
                        prod=self.prod,
                        project=self.project,
                        ticket_id=linked_ticket_id,
                        custom_substitutions=self.custom_substitutions),
                                      only_matched=only_matched)
                self._links.append(
                    (ticket.ticket_id, linked_ticket_id, link_type, direction))