KhodeN
12/1/2013 - 12:23 PM

Import tasks from todolist *.tdl files to YouTrack. Support features and task.

Import tasks from todolist *.tdl files to YouTrack. Support features and task.

import xml.etree.ElementTree as ET
from youtrack.connection import Connection


class Task(object):
    def __init__(self, title, description):
        self.title = title.encode('utf-8')
        self.description = description.encode('utf-8')
        self.feature = False
        self.sub_tasks = []
        self.parent = None
        self.issue_id = None

    def add_sub(self, task):
        task.parent = self
        self.sub_tasks.append(task)

    def __repr__(self):
        return self.title

    @staticmethod
    def create_from_xml(task_el):
        title = task_el.get('TITLE')
        comment = task_el.find('COMMENTS')
        task = Task(title, comment.text if comment is not None else '')

        for sub in task_el.findall('TASK'):
            task.feature = True
            sub_task = Task.create_from_xml(sub) # recursion
            task.add_sub(sub_task)

        return task

    @staticmethod
    def parse_todolist_exported(filename):
        result = []

        tree = ET.parse(filename)
        for task_el in tree.getroot():
            task = Task.create_from_xml(task_el)
            result.append(task)

        return result

    def _extract_id(self, result):
        index = result.rfind('/')
        return result[index + 1:]

    def save(self, connection, project, assignee, iteration):
        print 'save %s' % self.title

        issue_type = 'feature' if self.feature else 'task'

        create_result = connection.createIssue(project, assignee, self.title, self.description, type=issue_type)
        self.issue_id = self._extract_id(create_result)
        connection.executeCommand(self.issue_id, 'Fix versions ' + iteration)

        if self.parent:
            connection.importLinks([dict(
                typeName='Subtask',
                source=self.parent.issue_id,
                target=self.issue_id
            )])

        for sub_task in self.sub_tasks:
            sub_task.save(connection, project, assignee, iteration)


tasks = Task.parse_todolist_exported('todolist.tdl')

connection = Connection('http://example.com/youtrack/', 'user', 'password')

for task in tasks:
    task.save(connection, 'project', 'assignee', 'iteration (sprint)')