def mantis2youtrack(target_url, target_login, target_pass, mantis_db_name, mantis_db_host, mantis_db_port,
                    mantis_db_login, mantis_db_pass, mantis_project_names):
    print "target_url             : " + target_url
    print "target_login           : "******"target_pass            : "******"mantis_db_name         : " + mantis_db_name
    print "mantis_db_host         : " + mantis_db_host
    print "mantis_db_port         : " + mantis_db_port
    print "mantis_db_login        : "******"mantis_db_pass         : "******"mantis_project_names   : " + repr(mantis_project_names)

    #connacting to yt
    target = Connection(target_url, target_login, target_pass)
    #connacting to mantis
    client = MantisClient(mantis_db_host, int(mantis_db_port), mantis_db_login,
        mantis_db_pass, mantis_db_name, mantis.CHARSET)
    if not len(mantis_project_names):
        print "You should declarer at least one project to import"
        sys.exit()

    print "Creating custom fields definitions"
    create_yt_custom_field(target, u"priority")
    create_yt_custom_field(target, u"severity")
    create_yt_custom_field(target, u"category_id")
    create_yt_custom_field(target, u"version", "1")
    create_yt_custom_field(target, u"fixed_in_version", "1")
    create_yt_custom_field(target, u"build", "1")
    create_yt_custom_field(target, u"platform")
    create_yt_custom_field(target, u"os")
    create_yt_custom_field(target, u"os_build")
    create_yt_custom_field(target, u"due_date")
    create_yt_custom_field(target, u"Reproducibility")
    create_yt_custom_field(target, u"target_version", u'1')
    create_yt_custom_field(target, u"status")
    create_yt_custom_field(target, u"resolution")
    create_yt_custom_field(target, u'project_id', u'1')

    # adding some custom fields that are predefined in mantis
    project_ids = []
    for name in mantis_project_names:
        project_ids.append(client.get_project_id_by_name(name))

    custom_fields = client.get_mantis_custom_fields(project_ids)

    for cf_def in custom_fields:
        print "Processing custom field [ %s ]" % cf_def.name.encode('utf-8')
        process_mantis_custom_field(target, cf_def)

    print "Creating custom fields definitions finished"

    issue_tags = set([])
    for name in mantis_project_names:
        project_id = str(client.get_project_id_by_name(name))
        name = name.replace("/", " ")
        print "Creating project [ %s ] with name [ %s ]" % (project_id, name)
        try:
            target.getProject(project_id)
        except YouTrackException:
            target.createProjectDetailed(project_id, name, client.get_project_description(project_id),
                target_login)

        print "Importing components to project [ %s ]" % project_id
        add_values_to_fields(target, project_id, u"category_id",
            client.get_mantis_categories(project_id),
            lambda component, yt_bundle, value_mapping:
            to_yt_subsystem(component, yt_bundle, value_mapping))
        print "Importing components to project [ %s ] finished" % project_id

        print "Importing versions to project [ %s ]" % project_id
        mantis_versions = client.get_mantis_versions(project_id)
        add_values_to_fields(target, project_id, u"version", mantis_versions,
            lambda version, yt_bundle, value_mapping:
            to_yt_version(version, yt_bundle, value_mapping))

        add_values_to_fields(target, project_id, u"fixed_in_version",
            mantis_versions,
            lambda version, yt_bundle, value_mapping:
            to_yt_version(version, yt_bundle, value_mapping))

        print "Importing versions to project [ %s ] finished" % project_id

        print "Attaching custom fields to project [ %s ]" % project_id
        cf_ids = client.get_custom_fields_attached_to_project(project_id)

        for cf in custom_fields:
            if cf.field_id in cf_ids:
                attach_field_to_project(target, project_id, cf.name)

        print "Attaching custom fields to project [ %s ] finished" % project_id

        print "Importing issues to project [ %s ]" % project_id
        max_count = 100
        after = 0
        go_on = True
        while go_on:
            go_on = False
            mantis_issues = client.get_mantis_issues(project_id, after, max_count)
            after += max_count
            if len(mantis_issues):
                go_on = True
                target.importIssues(project_id, name + " Assignees",
                    [to_yt_issue(issue, project_id, target) for issue in mantis_issues])

                # import attachments
                for issue in mantis_issues:
                    issue_attachments = client.get_attachments(issue['id'])
                    issue_id = "%s-%s" % (project_id, issue['id'])
                    import_attachments(issue_attachments, issue_id, target)
                    issue_tags |= set(client.get_issue_tags_by_id(issue['id']))

        print "Importing issues to project [ %s ] finished" % project_id

    import_tags(client, target, project_ids, issue_tags)

    print "Importing issue links"
    go_on = True
    after = 0
    max_count = 200
    while go_on:
        go_on = False
        mantis_issue_links = client.get_issue_links(after, max_count)
        yt_issue_links = []
        for link in mantis_issue_links:
            go_on = True
            print "Processing issue link for source issue [ %s ]" % str(link.source)
            yt_issue_links.append(to_yt_link(link))
        after += max_count
        print target.importLinks(yt_issue_links)

    print "Importing issue links finished"
def mantis2youtrack(target_url, target_login, target_pass, mantis_db_name,
                    mantis_db_host, mantis_db_port, mantis_db_login,
                    mantis_db_pass, mantis_project_names):
    print "target_url             : " + target_url
    print "target_login           : "******"target_pass            : "******"mantis_db_name         : " + mantis_db_name
    print "mantis_db_host         : " + mantis_db_host
    print "mantis_db_port         : " + mantis_db_port
    print "mantis_db_login        : "******"mantis_db_pass         : "******"mantis_project_names   : " + repr(mantis_project_names)

    #connacting to yt
    target = Connection(target_url, target_login, target_pass)
    #connacting to mantis
    client = MantisClient(mantis_db_host, int(mantis_db_port), mantis_db_login,
                          mantis_db_pass, mantis_db_name, mantis.CHARSET)
    if not len(mantis_project_names):
        print "You should declarer at least one project to import"
        sys.exit()

    print "Creating custom fields definitions"
    create_yt_custom_field(target, u"priority")
    create_yt_custom_field(target, u"severity")
    create_yt_custom_field(target, u"category_id")
    create_yt_custom_field(target, u"version", "1")
    create_yt_custom_field(target, u"fixed_in_version", "1")
    create_yt_custom_field(target, u"build", "1")
    create_yt_custom_field(target, u"platform")
    create_yt_custom_field(target, u"os")
    create_yt_custom_field(target, u"os_build")
    create_yt_custom_field(target, u"due_date")
    create_yt_custom_field(target, u"Reproducibility")
    create_yt_custom_field(target, u"target_version", u'1')
    create_yt_custom_field(target, u"status")
    create_yt_custom_field(target, u"resolution")
    create_yt_custom_field(target, u'project_id', u'1')

    # adding some custom fields that are predefined in mantis
    project_ids = []
    for name in mantis_project_names:
        project_ids.append(client.get_project_id_by_name(name))

    custom_fields = client.get_mantis_custom_fields(project_ids)

    for cf_def in custom_fields:
        print "Processing custom field [ %s ]" % cf_def.name.encode('utf-8')
        process_mantis_custom_field(target, cf_def)

    print "Creating custom fields definitions finished"

    issue_tags = set([])
    for name in mantis_project_names:
        project_id = str(client.get_project_id_by_name(name))
        name = name.replace("/", " ")
        print "Creating project [ %s ] with name [ %s ]" % (project_id, name)
        try:
            target.getProject(project_id)
        except YouTrackException:
            target.createProjectDetailed(
                project_id, name, client.get_project_description(project_id),
                target_login)

        print "Importing components to project [ %s ]" % project_id
        add_values_to_fields(
            target, project_id, u"category_id",
            client.get_mantis_categories(project_id),
            lambda component, yt_bundle, value_mapping: to_yt_subsystem(
                component, yt_bundle, value_mapping))
        print "Importing components to project [ %s ] finished" % project_id

        print "Importing versions to project [ %s ]" % project_id
        mantis_versions = client.get_mantis_versions(project_id)
        add_values_to_fields(
            target, project_id, u"version", mantis_versions,
            lambda version, yt_bundle, value_mapping: to_yt_version(
                version, yt_bundle, value_mapping))

        add_values_to_fields(
            target, project_id, u"fixed_in_version", mantis_versions,
            lambda version, yt_bundle, value_mapping: to_yt_version(
                version, yt_bundle, value_mapping))

        print "Importing versions to project [ %s ] finished" % project_id

        print "Attaching custom fields to project [ %s ]" % project_id
        cf_ids = client.get_custom_fields_attached_to_project(project_id)

        for cf in custom_fields:
            if cf.field_id in cf_ids:
                attach_field_to_project(target, project_id, cf.name)

        print "Attaching custom fields to project [ %s ] finished" % project_id

        print "Importing issues to project [ %s ]" % project_id
        max_count = 100
        after = 0
        go_on = True
        while go_on:
            go_on = False
            mantis_issues = client.get_mantis_issues(project_id, after,
                                                     max_count)
            after += max_count
            if len(mantis_issues):
                go_on = True
                target.importIssues(project_id, name + " Assignees", [
                    to_yt_issue(issue, project_id, target)
                    for issue in mantis_issues
                ])

                # import attachments
                for issue in mantis_issues:
                    issue_attachments = client.get_attachments(issue['id'])
                    issue_id = "%s-%s" % (project_id, issue['id'])
                    import_attachments(issue_attachments, issue_id, target)
                    issue_tags |= set(client.get_issue_tags_by_id(issue['id']))

        print "Importing issues to project [ %s ] finished" % project_id

    import_tags(client, target, project_ids, issue_tags)

    print "Importing issue links"
    go_on = True
    after = 0
    max_count = 200
    while go_on:
        go_on = False
        mantis_issue_links = client.get_issue_links(after, max_count)
        yt_issue_links = []
        for link in mantis_issue_links:
            go_on = True
            print "Processing issue link for source issue [ %s ]" % str(
                link.source)
            yt_issue_links.append(to_yt_link(link))
        after += max_count
        print target.importLinks(yt_issue_links)

    print "Importing issue links finished"
def mantis2youtrack(
    target_url,
    target_login,
    target_pass,
    mantis_db_name,
    mantis_db_host,
    mantis_db_port,
    mantis_db_login,
    mantis_db_pass,
    mantis_project_names,
):
    print "target_url             : " + target_url
    print "target_login           : "******"target_pass            : "******"mantis_db_name         : " + mantis_db_name
    print "mantis_db_host         : " + mantis_db_host
    print "mantis_db_port         : " + mantis_db_port
    print "mantis_db_login        : "******"mantis_db_pass         : "******"mantis_project_names   : " + repr(mantis_project_names)

    # connacting to yt
    target = Connection(target_url, target_login, target_pass)
    # connacting to mantis
    client = MantisClient(
        mantis_db_host, int(mantis_db_port), mantis_db_login, mantis_db_pass, mantis_db_name, mantis.CHARSET
    )
    if not len(mantis_project_names):
        print "You should declarer at least one project to import"
        sys.exit()

    value_sets = dict([])

    print "Importing users"
    yt_users = []
    value_sets["user"] = set([])
    for user in client.get_mantis_users():
        print "Processing user [ %s ]" % user.user_name
        value_sets["user"].add(user.user_name)
        yt_users.append(to_yt_user(user))
    target.importUsers(yt_users)
    print "Importing users finished"

    print "Creating custom fields definitions"
    value_sets.update(create_custom_fields(target, u"priority", mantis.PRIORITY_VALUES.values()))
    value_sets.update(create_custom_fields(target, u"severity", mantis.SEVERITY_VALUES.values()))
    value_sets.update(create_custom_fields(target, u"category", ["No subsystem"], "1"))
    value_sets.update(create_custom_fields(target, u"version", [], "1"))
    value_sets.update(create_custom_fields(target, u"fixed_in_version", [], "1"))
    value_sets.update(create_custom_fields(target, u"build", [], "1"))
    value_sets.update(create_custom_fields(target, u"platform"))
    value_sets.update(create_custom_fields(target, u"os"))
    value_sets.update(create_custom_fields(target, u"os_build"))
    value_sets.update(create_custom_fields(target, u"due_date"))
    value_sets.update(create_custom_fields(target, u"Reproducibility", mantis.REPRODUCIBILITY_VALUES.values()))

    # create custom field for target version
    field = None
    try:
        field = target.getCustomField("Fix versions")
    except YouTrackException:
        pass
    if field is not None:
        if hasattr(field, "defaultBundle"):
            bundle = field.defaultBundle
            for name in get_yt_cf_name_from_mantis_cf_name("target_version"):
                try:
                    target.createCustomFieldDetailed(
                        name,
                        mantis.FIELD_TYPES[name],
                        False,
                        True,
                        True,
                        {"defaultBundle": bundle, "attachBundlePolicy": "1"},
                    )
                except YouTrackException:
                    pass

    value_sets.update(
        create_auto_attached_bundle_custom_fields(
            target,
            u"status",
            mantis.STATUS_VALUES.values(),
            lambda status, bundle, value_mapping: to_yt_state(status, bundle, value_mapping),
        )
    )

    value_sets.update(
        create_auto_attached_bundle_custom_fields(
            target,
            u"resolution",
            mantis.RESOLUTION_VALUES.values(),
            lambda resolution, bundle, value_mapping: to_yt_state(resolution, bundle, value_mapping),
        )
    )

    if mantis.CREATE_CF_FOR_SUBPROJECT:
        value_sets.update(create_custom_fields(target, u"subproject", [], "1"))

    handler_field_name = u"handler"
    value_sets.update(create_custom_fields(target, handler_field_name, [], "1"))
    for name in get_yt_cf_name_from_mantis_cf_name(handler_field_name):
        value_sets[name] = value_sets["user"]

    # adding some custom fields that are predefined in mantis
    project_ids = []
    for name in mantis_project_names:
        project_ids.append(client.get_project_id_by_name(name))

    custom_fields = client.get_mantis_custom_fields(project_ids)

    for cf_def in custom_fields:
        print "Processing custom field [ %s ]" % cf_def.name.encode("utf-8")
        value_sets.update(process_mantis_custom_field(target, cf_def))

    print "Creating custom fields definitions finished"

    for name in mantis_project_names:
        project_id = int(client.get_project_id_by_name(name))
        print "Creating project [ %s ] with name [ %s ]" % (project_id, name)
        try:
            target.getProject(str(project_id))
        except YouTrackException:
            target.createProjectDetailed(
                str(project_id), name, client.get_project_description(project_id), target_login
            )

        print "Importing components to project [ %s ]" % project_id
        value_sets.update(
            add_values_to_fields(
                target,
                project_id,
                u"category",
                client.get_mantis_categories(project_id),
                lambda component, yt_bundle, value_mapping: to_yt_subsystem(component, yt_bundle, value_mapping),
            )
        )
        print "Importing components to project [ %s ] finished" % project_id

        print "Importing versions to project [ %s ]" % project_id
        mantis_versions = client.get_mantis_versions(project_id)
        value_sets.update(
            add_values_to_fields(
                target,
                project_id,
                u"version",
                mantis_versions,
                lambda version, yt_bundle, value_mapping: to_yt_version(version, yt_bundle, value_mapping),
            )
        )

        value_sets.update(
            add_values_to_fields(
                target,
                project_id,
                u"fixed_in_version",
                mantis_versions,
                lambda version, yt_bundle, value_mapping: to_yt_version(version, yt_bundle, value_mapping),
            )
        )

        for name in get_yt_cf_name_from_mantis_cf_name("target_version"):
            value_sets[name] = value_sets["Fix versions"]

        print "Importing versions to project [ %s ] finished" % project_id

        print "Attaching custom fields to project [ %s ]" % project_id
        cf_ids = client.get_custom_fields_attached_to_project(project_id)

        ignore = []
        for cf in custom_fields:
            if cf.field_id in cf_ids:
                attach_field_to_project(target, project_id, cf.name)
            else:
                ignore.append(cf.name)

        if mantis.CREATE_CF_FOR_SUBPROJECT:
            value_sets.update(
                add_values_to_fields(
                    target,
                    project_id,
                    u"subproject",
                    client.get_mantis_subprojects(project_id),
                    lambda sp_name, yt_bundle, value_mapping: yt_bundle.createElement(sp_name)
                    if sp_name not in value_mapping
                    else yt_bundle.createElement(value_sets[sp_name]),
                )
            )
        print "Attaching custom fields to project [ %s ] finished" % project_id

        print "Importing issues to project [ %s ]" % project_id
        mantis_issues = client.get_mantis_issues(project_id)
        yt_issues = []
        max_count = 100
        for issue in mantis_issues:
            # print "Processing issue [ %s ]" % str(issue.id)
            yt_issues.append(to_yt_issue(issue, value_sets, ignore))
            if len(yt_issues) >= max_count:
                print target.importIssues(str(project_id), name + "Assignees", yt_issues)
                yt_issues = []
        target.importIssues(str(project_id), str(project_id) + "Assignees", yt_issues)
        print "Importing issues to project [ %s ] finished" % project_id

        print "Importing issue attachments to project [ %s ]" % project_id
        mantis_attachments = client.get_mantis_attachments(project_id)
        for attachment in mantis_attachments:
            print "Processing issue attachment [ %s ]" % str(attachment.id)
            content = StringIO(attachment.content)
            authorLogin = client.get_user_name_by_id(attachment.user_id)
            target.createAttachment(
                "%s-%s" % (project_id, attachment.bug_id),
                attachment.filename,
                content,
                authorLogin,
                attachment.file_type,
                None,
                str(attachment.date_added * 1000),
            )
        print "Importing issue attachments to project [ %s ] finished" % project_id

        print "Importing tags to issues from project [ %s ]" % project_id
        for issue in mantis_issues:
            print "Processing tags for issue [ %s ]" % str(issue.id)
            for tag in issue.tags:
                print "Processing tag [ %s ]" % tag.encode("utf8")
                target.executeCommand(str(project_id) + "-" + str(issue.id), "tag " + tag.encode("utf8"))
        print "Importing tags to issues from project [ %s ] finished" % project_id

    print "Importing issue links"
    mantis_issue_links = client.get_issue_links()
    yt_issue_links = []
    for link in mantis_issue_links:
        print "Processing issue link for source issue [ %s ]" % str(link.source)
        yt_issue_links.append(to_yt_link(link))
    print target.importLinks(yt_issue_links)
    print "Importing issue links finished"