Пример #1
0
 def getCustomField(self, name):
     return youtrack.CustomField(self._get("/admin/customfield/field/" + urlquote(name.encode('utf-8'))), self)
def main(a_pat, yt_url, yt_login, yt_pass):
    """ Creates connections to Asana and your YouTrack site, then migrates tasks """
    a_conn = asana.Client.access_token(a_pat)
    yt_conn = Connection(yt_url, yt_login, yt_pass)
    print 'Logged in with Asana User {} and YouTrack User {}'.format(
        a_conn.users.me()['name'],
        yt_conn.getUser(yt_login).login)

    # Get list of Asana Workspaces to migrate - all except Personal Projects
    a_workspaces = [
        w for w in a_conn.workspaces.find_all()
        if w['name'] != "Personal Projects"
    ]
    print "Found Asana Workspaces (excluding Personal Projects): {}".format(
        [a['name'] for a in a_workspaces])
    yt_projects = [yt_conn.getProject(p) for p in yt_conn.getProjects()]
    print "Found existing YouTrack Projects: {}".format(
        [y.name for y in yt_projects])

    for a_work in a_workspaces:
        if a_work['name'] not in [p.name for p in yt_projects]:
            print "Creating YouTrack Project from {} Workspace".format(
                a_work['name'])
        yt_proj = None
        for p in yt_projects:
            if p.name == a_work['name']:
                yt_proj = p
                break
        if yt_proj is None:
            yt_proj = yt.Project()
            yt_proj.name = a_work['name']
            yt_proj.id = a_work['name'].replace(' ', '').upper()
            yt_proj.lead = yt_login
            print yt_conn.createProject(yt_proj)

        field_name = 'AsanaID'
        cf = yt.CustomField()
        cf.name = field_name
        cf.type = 'string'
        cf.isPrivate = False
        cf.visibleByDefault = False

        # print "Existing Project Fields: {}".format(yt_conn.getProjectCustomFields(yt_proj.id))
        try:
            asana_id_exists = yt_conn.getCustomField(field_name)
        except:
            asana_id_exists = False
        if not asana_id_exists:
            print 'Creating YouTrack Custom Field to save our Asana ID in'
            yt_conn.createCustomField(cf)

        try:
            asana_id_exists = yt_conn.getProjectCustomField(
                yt_proj.id, 'Due Date')
        except:
            asana_id_exists = None
        if not asana_id_exists:
            print 'Adding YouTrack Due Date Field to {} Project'.format(
                yt_proj.id)
            yt_conn.createProjectCustomFieldDetailed(yt_proj.id, 'Due Date',
                                                     '')

        try:
            asana_id_exists = yt_conn.getProjectCustomField(
                yt_proj.id, field_name)
        except:
            asana_id_exists = False
        if not asana_id_exists:
            print 'Adding YouTrack Custom Field {} to {} Project'.format(
                field_name, yt_proj.id)
            yt_conn.createProjectCustomFieldDetailed(yt_proj.id, field_name,
                                                     '')

        # Migrate users and save our list for later so we can assign people
        migrate_workspace_users(a_conn, yt_conn, a_work)
        migrate_projects_to_subsystems(a_conn, yt_conn, a_work, yt_proj)
        migrate_tasks_to_issues(a_conn, yt_conn, a_work, yt_proj, yt_login)