def comments_by_task(taskphid):
    #get the comment transactions by task
    coms =  phabdb.comment_transactions_by_task_phid(taskphid)
    final_comments = {}
    if not coms:
        return {}
    for i, c in enumerate(coms):
        comdetail = {}
        vlog('looking for email by user phid: %s' % (c[2],))
        comdetail['xuserphid'] = c[2]
        comdetail['xuseremail'] = phabdb.email_by_userphid(c[2])
        #for a comment transaction get all records (edits, etc)
        content = phabdb.comment_by_transaction(c[1])
        if len(content) > 1:
            iter = 0
            comver = 0
            while 1:
                if iter == len(content):
                    break
                for edit in content:
                    if edit[6] > comver:
                        comver = edit[6]
                        comdetail['text'] = edit[7]
                        comdetail['created'] = edit[10]
                        comdetail['last_edit'] = edit[11]
                iter += 1
        else:
            fcomment = phabdb.comment_by_transaction(c[1])[0]
            comdetail['text'] = fcomment[7]
            comdetail['created'] = fcomment[10]
            comdetail['last_edit'] = fcomment[11]

        final_comments[i] = comdetail

    return final_comments
def fetch(PHABTICKETID):

    PHABTICKETID = int(PHABTICKETID)

    parser = ConfigParser.SafeConfigParser()
    parser_mode = 'oldfab'
    parser.read(configfile)
    oldfab = Phabricator(parser.get(parser_mode, 'user'),
                         parser.get(parser_mode, 'cert'),
                         parser.get(parser_mode, 'host'))

    #dummy instance of phabapi
    phabm = phabmacros('', '', '')
    #assign newphab instance as self.con for dummyphab
    phabm.con = oldfab

    """
    <Result: {u'authorPHID': u'PHID-USER-qbtllnzb6pwl3ttzqa3m',
                   u'status': u'open',
                     u'phid': u'PHID-TASK-qr3fpbtk6kdx4slhgnsd',
              u'description': u'',
               u'objectName': u'T10',
                    u'title': u'Get icinga alerts into logstash',
            u'priorityColor': u'red',
       u'dependsOnTaskPHIDs': [],
                u'auxiliary': [],
                      u'uri': u'http://fab.wmflabs.org/T10',
                  u'ccPHIDs': [u'PHID-USER-qbtllnzb6pwl3ttzqa3m'],
                 u'isClosed': False,
             u'dateModified': u'1399311492',
                u'ownerPHID': None,
               u'statusName': u'Open',
              u'dateCreated': u'1391716779',
             u'projectPHIDs': [u'PHID-PROJ-5ncvaivs3upngr7ijqy2'],
                       u'id': u'10',
                 u'priority': u'High'}>
    """

    tinfo = oldfab.maniphest.info(task_id=PHABTICKETID).response
    vlog(tinfo)
    if 'objectName' in tinfo:
        log("Fetching %s" % (tinfo['objectName']))

    comments = comments_by_task(tinfo['phid'])
    for i, c in comments.iteritems():
        comments[i]['xcommenter'] = dict(oldfab.user.info(phid=c['xuserphid']))
    ordered_comments =  collections.OrderedDict(sorted(comments.items()))
    vlog(str(ordered_comments))

    """
    <Result: {u'userName': u'bd808',
                  u'phid': u'PHID-USER-qbtllnzb6pwl3ttzqa3m',
              u'realName': u'Bryan Davis',
                 u'roles': [u'admin',u'verified', u'approved', u'activated'],
                 u'image': u'http://fab.wmflabs.org/file/data/fijwoqt62w6atpond4vb/PHID-FILE-37htsfegn7bnlfvzwsts/profile-profile-gravatar',
                   u'uri': u'http://fab.wmflabs.org/p/bd808/'}>
    """

    authorInfo = oldfab.user.info(phid=tinfo['authorPHID'])
    tinfo['xauthor'] = phabdb.email_by_userphid(authorInfo['phid'])
    lauthor = tinfo['xauthor'] or 'no author'
    vlog('author: ' + lauthor)

    ccs = []
    if tinfo['ccPHIDs']:
        for c in tinfo['ccPHIDs']:
            ccInfo = oldfab.user.info(phid=c)
            ccs.append(phabdb.email_by_userphid(ccInfo['phid']))
    tinfo['xccs'] = ccs
    vlog('ccs: ' + str(ccs))

    if tinfo['ownerPHID']:
        tinfo['xowner'] = phabdb.email_by_userphid(tinfo['ownerPHID'])
    else:
        tinfo['xowner'] = None

    """
    u'data':
      {u'PHID-PROJ-5ncvaivs3upngr7ijqy2':
        {u'phid': u'PHID-PROJ-5ncvaivs3upngr7ijqy2',
         u'name': u'logstash',
    u'dateCreated': u'1391641549',
      u'members': [u'PHID-USER-65zhggegfvhojb4nynay'],
           u'id': u'3',
    u'dateModified': u'1398282408',
        u'slugs': [u'logstash']}}, u'slugMap': []}>
    """

    project_names = []
    associated_projects = tinfo['projectPHIDs']

    vlog('associated projects: %s' % (str(tinfo['projectPHIDs'])))
    #if we try to query for an empty list we get back ALLLLLL
    if associated_projects:
        pinfo = oldfab.project.query(phids=associated_projects)
        if pinfo['data']:
            for p in pinfo['data'].values():
                project_names.append(p['name'])

    def norm(pname):
        return pname.lower().replace(' ', '_').replace('-', '_')

    norm_projects = [norm(p) for p in fablib.saved]
    saved_projects = [norm(p) in norm_projects for p in project_names]
    if not any(saved_projects):
        print "Skipping %s as it's not in a saved project" % (PHABTICKETID)
        return True

    vlog('project names: ' + str(project_names))
    tinfo['xprojects'] = project_names

    status = tinfo['status']
    if status != 'open':
        creation_priority = ipriority['na']
    else:
        creation_priority = ipriority['unresolved']


    blocked_tasks = phabdb.get_tasks_blocked(tinfo['phid'])
    if blocked_tasks:
        blocked = []
        block_details = oldfab.maniphest.find(phids=blocked_tasks)
        for k, v in block_details.iteritems():
            blocked.append(v['id'])
    else:
        blocked = []

    vlog('blocking: %s' % (str(blocked)))
    tinfo['xblocking'] = blocked

    pmig = phabdb.phdb(db='fab_migration')
    current = pmig.sql_x("SELECT * from fab_meta where id = %s", PHABTICKETID)
    if current:
        log('updating current record %s' % (PHABTICKETID,))
        update_values = (creation_priority,
                         json.dumps(tinfo),
                         json.dumps(comments),
                         now(),
                         PHABTICKETID)
        pmig.sql_x("UPDATE fab_meta SET priority=%s, header=%s, comments=%s, modified=%s WHERE id = %s",
                    update_values)
    else:
        log('inserting new record %s' % (PHABTICKETID,))
        insert_values =  (PHABTICKETID,
                          creation_priority,
                          json.dumps(tinfo),
                          json.dumps(comments),
                          now(),
                          now())

        pmig.sql_x("INSERT INTO fab_meta (id, priority, header, comments, created, modified) VALUES (%s, %s, %s, %s, %s, %s)",
                   insert_values)
    pmig.close()
    return True
print policyPHID
taskPHID = phabdb.get_task_phid_by_id(100039)
print taskPHID
phabdb.set_task_policy(taskPHID, policyPHID)
#print util.remove_issue_by_bugid(sys.argv[1], 'rt')
exit()

email_tuples =  phabdb.get_verified_emails()
flat_list =  [e[1] for e in email_tuples]
print ','.join(flat_list)
exit()
print phabdb.set_tasks_blocked('PHID-TASK-llxzmfbbcc4adujigg4w', 'PHID-TASK-cjibddnd5lsa5n5hdsyx')

#print phabdb.get_blocking_tasks('PHID-TASK-llxzmfbbcc4adujigg4w')
#print phabdb.phid_by_custom_field('fl563')
print 'email', phabdb.email_by_userphid('PHID-USER-nnipdbdictreyx7qhaii')

#get the comment transactions by task
coms =  phabdb.comment_transactions_by_task_phid('PHID-TASK-3eivod3do3vzdviblbfr')

final_comments = {}
for i, c in enumerate(coms):
    comdetail = {}
    comdetail['userphid'] = c[3]
    #for a comment transaction get all records (edits, etc)
    content = phabdb.comment_by_transaction(c[1])
    if len(content) > 1:
        iter = 0
        while 1:
            if iter == len(content):
                break