diff options
author | cpettet <rush@wikimedia.org> | 2014-12-16 16:03:11 -0600 |
---|---|---|
committer | cpettet <rush@wikimedia.org> | 2014-12-16 16:03:11 -0600 |
commit | 39d05f179c749aa14cdf9c7fc5e0c95efb5a29da (patch) | |
tree | 633b5e4a6c255a7a668ac7e11a7ebffad7d32065 | |
parent | a339253851065809235d3b1dcd9e6eb315e58152 (diff) |
rt cleanup and user updates for header and comments
-rwxr-xr-x | archive/fab_update_tasks.py (renamed from fab_update_tasks.py) | 0 | ||||
-rwxr-xr-x | archive/fab_update_user.py (renamed from fab_update_user.py) | 0 | ||||
-rwxr-xr-x | fab_fetch.py | 250 | ||||
-rwxr-xr-x | fab_populate_user_relations_table.py | 165 | ||||
-rw-r--r-- | nano | 0 | ||||
-rwxr-xr-x | rt_create.py | 4 | ||||
-rwxr-xr-x | rt_create_new.py | 485 | ||||
-rwxr-xr-x | rt_fetch_new.py | 162 | ||||
-rwxr-xr-x | rt_update_tasks.py | 18 | ||||
-rwxr-xr-x | rt_update_tasks_references.py | 4 | ||||
-rwxr-xr-x | rt_update_user_comments.py | 51 | ||||
-rwxr-xr-x | wmfphablib/__init__.py | 2 |
12 files changed, 46 insertions, 1095 deletions
diff --git a/fab_update_tasks.py b/archive/fab_update_tasks.py index d6a7bf7..d6a7bf7 100755 --- a/fab_update_tasks.py +++ b/archive/fab_update_tasks.py diff --git a/fab_update_user.py b/archive/fab_update_user.py index 8d8440c..8d8440c 100755 --- a/fab_update_user.py +++ b/archive/fab_update_user.py diff --git a/fab_fetch.py b/fab_fetch.py deleted file mode 100755 index 8b8c098..0000000 --- a/fab_fetch.py +++ /dev/null @@ -1,250 +0,0 @@ -import time -import json -import multiprocessing -import sys -import collections -from phabricator import Phabricator -from wmfphablib import Phab as phabmacros -from wmfphablib import phabdb -from wmfphablib import log -from wmfphablib import vlog -from wmfphablib import epoch_to_datetime -from wmfphablib import ipriority -from wmfphablib import get_config_file -from wmfphablib import now -from wmfphablib import fablib -from wmfphablib import return_bug_list -import ConfigParser - -configfile = get_config_file() - -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 - -def run_fetch(fabid, tries=1): - if tries == 0: - pmig = phabdb.phdb(db='fab_migration') - import_priority = pmig.sql_x("SELECT priority FROM fab_meta WHERE id = %s", (fabid,)) - if import_priority: - log('updating existing record') - pmig.sql_x("UPDATE fab_meta SET priority=%s, modified=%s WHERE id = %s", (ipriority['fetch_failed'], - now(), - fabid)) - else: - insert_values = (fabid, ipriority['fetch_failed'], 'nan', 'nan', 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() - log('failed to grab %s' % (fabid,)) - return False - try: - if fetch(fabid): - vlog(str(time.time())) - log('done fetching %s' % (fabid,)) - return True - except Exception as e: - tries -= 1 - time.sleep(5) - import traceback - traceback.print_exc(file=sys.stdout) - log('failed to grab %s (%s)' % (fabid, e)) - return run_fetch(fabid, tries=tries) - - -bugs = return_bug_list() -vlog(bugs) -log("Fetching %s" % (len(bugs),)) -from multiprocessing import Pool -pool = Pool(processes=7) -_ = pool.map(run_fetch, bugs) -complete = len(filter(bool, _)) -failed = len(_) - complete -print 'completed %s, failed %s' % (complete, failed) diff --git a/fab_populate_user_relations_table.py b/fab_populate_user_relations_table.py deleted file mode 100755 index e07860b..0000000 --- a/fab_populate_user_relations_table.py +++ /dev/null @@ -1,165 +0,0 @@ -import time -import json -import multiprocessing -import sys -import collections -from phabricator import Phabricator -from wmfphablib import Phab as phabmacros -from wmfphablib import phabdb -from wmfphablib import log -from wmfphablib import vlog -from wmfphablib import epoch_to_datetime -from wmfphablib import ipriority -from wmfphablib import get_config_file -from wmfphablib import now -from wmfphablib import return_bug_list -import ConfigParser - - -configfile = get_config_file() - - -def fetch(fabid): - ausers = {} - pmig = phabdb.phdb(db='fab_migration') - issue = pmig.sql_x("SELECT id FROM fab_meta WHERE id = %s", fabid) - - if not issue: - log('issue %s does not exist for user population' % (fabid,)) - return True - - fpriority= pmig.sql_x("SELECT priority FROM fab_meta WHERE id = %s", fabid) - if fpriority[0] == ipriority['fetch_failed']: - log('issue %s does not fetched successfully for user population (failed fetch)' % (fabid,)) - return True - - - tid, import_priority, jheader, com, created, modified = pmig.sql_x("SELECT * FROM fab_meta WHERE id = %s", fabid) - header = json.loads(jheader) - vlog(str(header)) - relations = {} - relations['author'] = header['xauthor'] - relations['cc'] = header['xccs'] - relations['owner'] = header['xowner'] - - for k, v in relations.iteritems(): - if relations[k]: - relations[k] = filter(bool, v) - - def add_owner(owner): - ouser = pmig.sql_x("SELECT user FROM user_relations WHERE user = %s", (owner,)) - if ouser: - jassigned = pmig.sql_x("SELECT assigned FROM user_relations WHERE user = %s", (owner,)) - - if jassigned[0]: - assigned = json.loads(jassigned[0]) - else: - assigned = [] - if fabid not in assigned: - log("Assigning %s to %s" % (str(fabid), owner)) - assigned.append(fabid) - vlog("owner %s" % (str(assigned),)) - pmig.sql_x("UPDATE user_relations SET assigned=%s, modified=%s WHERE user = %s", (json.dumps(assigned), - now(), - owner)) - else: - vlog('inserting new record') - assigned = json.dumps([fabid]) - insert_values = (owner, - assigned, - now(), - now()) - - pmig.sql_x("INSERT INTO user_relations (user, assigned, created, modified) VALUES (%s, %s, %s, %s)", - insert_values) - - - def add_author(author): - euser = pmig.sql_x("SELECT user FROM user_relations WHERE user = %s", (relations['author'],)) - if euser: - jauthored = pmig.sql_x("SELECT author FROM user_relations WHERE user = %s", (relations['author'],)) - if jauthored[0]: - authored = json.loads(jauthored[0]) - else: - authored = [] - if fabid not in authored: - authored.append(fabid) - vlog("author %s" % (str(authored),)) - pmig.sql_x("UPDATE user_relations SET author=%s, modified=%s WHERE user = %s", (json.dumps(authored), - now(), - relations['author'])) - else: - vlog('inserting new record') - authored = json.dumps([fabid]) - insert_values = (relations['author'], - authored, - now(), - now()) - pmig.sql_x("INSERT INTO user_relations (user, author, created, modified) VALUES (%s, %s, %s, %s)", - insert_values) - - - def add_cc(ccuser): - eccuser = pmig.sql_x("SELECT user FROM user_relations WHERE user = %s", (ccuser,)) - if eccuser: - jcc = pmig.sql_x("SELECT cc FROM user_relations WHERE user = %s", (ccuser,)) - if jcc[0]: - cc = json.loads(jcc[0]) - else: - cc = [] - if fabid not in cc: - cc.append(fabid) - vlog("cc %s" % (str(cc),)) - pmig.sql_x("UPDATE user_relations SET cc=%s, modified=%s WHERE user = %s", (json.dumps(cc), - now(), - ccuser)) - else: - vlog('inserting new record') - cc = json.dumps([fabid]) - insert_values = (ccuser, - cc, - now(), - now()) - pmig.sql_x("INSERT INTO user_relations (user, cc, created, modified) VALUES (%s, %s, %s, %s)", - insert_values) - - if relations['author']: - add_author(relations['author']) - - if relations['owner']: - add_owner(relations['owner']) - - if relations['cc']: - for u in filter(bool, relations['cc']): - add_cc(u) - - pmig.close() - return True - -def run_fetch(fabid, tries=1): - if tries == 0: - log('failed to populate for %s' % (fabid,)) - return False - try: - if fetch(fabid): - vlog(str(time.time())) - log('done with %s' % (fabid,)) - return True - except Exception as e: - import traceback - tries -= 1 - time.sleep(5) - traceback.print_exc(file=sys.stdout) - log('failed to grab %s (%s)' % (fabid, e)) - return run_fetch(fabid, tries=tries) - - -bugs = return_bug_list() -vlog(bugs) -log("Count %s" % (str(len(bugs)))) -from multiprocessing import Pool -pool = Pool(processes=10) -_ = pool.map(run_fetch, bugs) -complete = len(filter(bool, _)) -failed = len(_) - complete -print 'completed %s, failed %s' % (complete, failed) diff --git a/rt_create.py b/rt_create.py index 0211dfe..43d9eb9 100755 --- a/rt_create.py +++ b/rt_create.py @@ -105,6 +105,7 @@ def create(rtid): vlog("ignoring content line %s" % (line,)) return None + botphid = phabdb.get_phid_by_username(config.phab_user) viewpolicy = phabdb.get_project_phid('WMF-NDA') if not viewpolicy: elog("View policy group not present: %s" % (viewpolicy,)) @@ -380,7 +381,6 @@ def create(rtid): auxiliary={"std:maniphest:external_reference":"rt%s" % (rtid,)}) # XXX: perms - botphid = phabdb.get_phid_by_username(config.phab_user) phabdb.set_task_title_transaction(ticket['phid'], botphid, 'public', @@ -485,9 +485,9 @@ def create(rtid): fmt_comment['content'] = cbody fmt_comment['created'] = created fmt_comment['author'] = dbody['creator'] + fmt_comment['creator'] = rtlib.user_lookup(dbody['creator']) # XXX TRX both ways? - #fmt_comment['creator'] = dbody['creator']user_lookup(name) cid = len(fmt_comments.keys()) + 1 fmt_comment['count'] = cid fmt_comments[cid] = fmt_comment diff --git a/rt_create_new.py b/rt_create_new.py deleted file mode 100755 index 32c53b4..0000000 --- a/rt_create_new.py +++ /dev/null @@ -1,485 +0,0 @@ -#!/usr/bin/env python -#from __future__ import unicode_literals -import time -import json -import os -import re -import sys -import getpass -sys.path.append('/home/rush/python-rtkit/') -from phabricator import Phabricator -from wmfphablib import Phab as phabmacros -from wmfphablib import errorlog as elog -from wmfphablib import return_bug_list -from wmfphablib import phdb -from wmfphablib import phabdb -from wmfphablib import mailinglist_phid -from wmfphablib import set_project_icon -from wmfphablib import log -from wmfphablib import util -from wmfphablib import rtlib -from wmfphablib import vlog -from wmfphablib import config -from wmfphablib import rtlib -from wmfphablib import datetime_to_epoch -from wmfphablib import epoch_to_datetime -from wmfphablib import now -from rtkit import resource -from rtkit import authenticators -from rtkit import errors -from wmfphablib import ipriority - - -def create(rtid): - - phab = Phabricator(config.phab_user, - config.phab_cert, - config.phab_host) - - phabm = phabmacros('', '', '') - phabm.con = phab - - pmig = phdb(db=config.rtmigrate_db) - - response = resource.RTResource(config.rt_url, - config.rt_login, - config.rt_passwd, - authenticators.CookieAuthenticator) - - current = pmig.sql_x("SELECT priority, header, \ - comments, created, modified \ - FROM rt_meta WHERE id = %s", - (rtid,)) - if current: - import_priority, rtinfo, com, created, modified = current[0] - else: - elog('%s not present for migration' % (rtid,)) - return False - - if not rtinfo: - log("ignoring invalid data for issue %s" % (rtid,)) - return False - - def get_ref(id): - refexists = phabdb.reference_ticket('%s%s' % (rtlib.prepend, id)) - if refexists: - return refexists - - if get_ref(rtid): - log('reference ticket %s already exists' % (rtid,)) - return True - - viewpolicy = phabdb.get_project_phid('WMF-NDA') - if not viewpolicy: - elog("View policy group not present: %s" % (viewpolicy,)) - return False - - def remove_sig(content): - return re.split('--\s?\n', content)[0] - - # Example: - # id: ticket/8175/attachments\n - # Attachments: 141490: (Unnamed) (multipart/mixed / 0b), - # 141491: (Unnamed) (text/html / 23b), - # 141492: 0jp9B09.jpg (image/jpeg / 117.4k), - attachments = response.get(path="ticket/%s/attachments/" % (rtid,)) - if not attachments: - raise Exception("no attachment response: %s" % (rtid)) - - history = response.get(path="ticket/%s/history?format=l" % (rtid,)) - - - rtinfo = json.loads(rtinfo) - comments = json.loads(com) - vlog(rtid) - vlog(rtinfo) - - comment_dict = {} - for i, c in enumerate(comments): - cwork = {} - comment_dict[i] = cwork - if not 'Attachments:' in c: - pass - attachsplit = c.split('Attachments:') - if len(attachsplit) > 1: - body, attached = attachsplit[0], attachsplit[1] - else: - body, attached = c, '0' - comment_dict[i]['text_body'] = body - comment_dict[i]['attached'] = attached - - # Example: - # Ticket: 8175\nTimeTaken: 0\n - # Type: - # Create\nField: - # Data: \nDescription: Ticket created by cpettet\n\n - # Content: test ticket description\n\n\n - # Creator: cpettet\nCreated: 2014-08-21 21:21:38\n\n'} - params = {'id': 'id:(.*)', - 'ticket': 'Ticket:(.*)', - 'timetaken': 'TimeTaken:(.*)', - 'content': 'Content:(.*)', - 'creator': 'Creator:(.*)', - 'description': 'Description:(.*)', - 'created': 'Created:(.*)', - 'ovalue': 'OldValue:(.*)', - 'nvalue': 'NewValue:(.*)'} - - for k, v in comment_dict.iteritems(): - text_body = v['text_body'] - comment_dict[k]['body'] = {} - for paramkey, regex in params.iteritems(): - value = re.search(regex, text_body) - if value: - comment_dict[k]['body'][paramkey] = value.group(1).strip() - else: - comment_dict[k]['body'][paramkey] = None - - if 'Content' in text_body: - content = text_body.split('Content:')[1] - content = content.split('Creator:') - comment_dict[k]['body']['content'] = content - - creator = comment_dict[k]['body']['creator'] - if creator and '@' in creator: - comment_dict[k]['body']['creator'] = rtlib.sanitize_email(creator) - - #15475: untitled (18.7k) - comment_attachments= re.findall('(\d+):\s', v['attached']) - comment_dict[k]['body']['attached'] = comment_attachments - - # due to the nature of the RT api sometimes whitespacing becomes - # a noise comment - if not any(comment_dict[comment_dict.keys()[0]]['body'].values()): - vlog('dropping %s comment' % (str(comment_dict[comment_dict.keys()[0]],))) - del comment_dict[0] - - #attachments into a dict - def attach_to_kv(attachments_output): - attached = re.split('Attachments:', attachments_output, 1)[1] - ainfo = {} - for at in attached.strip().splitlines(): - if not at: - continue - k, v = re.split(':', at, 1) - ainfo[k.strip()] = v.strip() - return ainfo - - ainfo = attach_to_kv(attachments) - #lots of junk attachments from emailing comments and ticket creation - ainfo_f = {} - for k, v in ainfo.iteritems(): - if '(Unnamed)' not in v: - ainfo_f[k] = v - - #taking attachment text and convert to tuple (name, content type, size) - ainfo_ext = {} - comments = re.split("\d+\/\d+\s+\(id\/.\d+\/total\)", history) - attachregex = '(.*)\.(\S{3,4})\s\((.*)\s\/\s(.*)\)' - for k, v in ainfo_f.iteritems(): - # Handle general attachment case: - # NO: 686318802.html (application/octet-stream / 19.5k), - # YES: Summary_686318802.pdf (application/unknown / 215.3k), - print attachregex - print v - extract = re.search(attachregex, v) - print extract - # due to goofy email handling of signature/x-header/meta info - # it seems they sometimes - # become malformed attachments. Such as when a response into - # rt was directed to a mailinglist - # Example: - # ->Attached Message Part (text/plain / 158b) - # - # Private-l mailing list - # Private-l@lists.wikimedia.org - # https://lists.wikimedia.org/mailman/listinfo/private-l - if extract: - print "YES" - vlog(extract.groups()) - ainfo_ext[k] = extract.groups() - elif not extract and v.startswith('Attached Message Part'): - continue - else: - elog("no attachment CORRUPT or FAILED extraction: %s %s (%s)" % (k, v, rtid)) - print ainfo_ext - - attachment_types = ['pdf', - 'jpeg', - 'tgz', - 'jpg', - 'png', - 'xls', - 'xlsx', - 'gif', - 'html', - 'htm', - 'txt', - 'log', - 'zip', - 'rtf', - 'vcf', - 'eml'] - - #Uploading attachment - dl = [] - #('Quote Summary_686318802', 'pdf', 'application/unknown', '215.3k') - uploaded = {} - for k, v in ainfo_ext.iteritems(): - file_extension = v[1].lower() - # vendors have this weird habit of capitalizing extension names - # make sure we can handle the extension type otherwise - if file_extension not in attachment_types: - log("%s %s %s" % (rtid, v, file_extension)) - raise Exception('unknown extension: %s (%s)' % (v, rtid)) - full = "ticket/%s/attachments/%s/content" % (rtid, k) - - vcontent = response.get(path=full, - headers={'Content-Type': v[2], 'Content-Length': v[3] }) - try: - #PDF's don't react well to stripping header -- fine without it - if file_extension.strip() == 'pdf': - sanscontent = str(''.join(vcontent.readlines())) - else: - log("%s.%s" % (v[0], file_extension)) - vcontent = str(vcontent.readlines()) - sanscontent = ''.join(vcontent[2:]) - upload = phabm.upload_file("%s.%s" % (v[0], file_extension), - sanscontent, - viewpolicy) - uploaded[k] = upload - - except Exception as e: - print e - #elog("Attachment CORRUPT in source: %s" % (v[0] + file_extension,)) - - return - if rtinfo['Queue'] not in rtlib.enabled: - log("%s not in an enabled queue" % (rtid,)) - return True - - ptags = [] - if rtinfo['Queue'] == 'ops-requests': - ptags.append('operations') - - pname = rtlib.project_translate(rtinfo['Queue']) - ptags.append(pname) - - phids = [] - for p in ptags: - phids.append(phabm.ensure_project(p)) - - rtinfo['xpriority'] = rtlib.priority_convert(rtinfo['Priority']) - rtinfo['xstatus'] = rtlib.status_convert(rtinfo['Status']) - - import collections - # {'ovalue': u'open', - # 'description': u"Status changed from 'open' to 'resolved' by robh", - # 'nvalue': None, 'creator': u'robh', 'attached': [], - # 'timetaken': u'0', 'created': u'2011-07-01 02:47:24', - # 'content': [u' This transaction appears to have no content\n', u' - # robh\nCreated: 2011-07-01 02:47:24\n'], - # 'ticket': u'1000', 'id': u'23192'} - ordered_comments = collections.OrderedDict(sorted(comment_dict.items())) - upfiles = uploaded.keys() - - # much like bugzilla comment 0 is the task description - header = comment_dict[comment_dict.keys()[0]] - del comment_dict[comment_dict.keys()[0]] - dtext = '\n'.join([l.strip() for l in header['body']['content'][0].splitlines()]) - dtext = rtlib.shadow_emails(dtext) - full_description = "**Author:** `%s`\n\n**Description:**\n%s\n" % (rtinfo['Creator'].strip(), - dtext) - - - hafound = header['body']['attached'] - header_attachments = [] - for at in hafound: - if at in upfiles: - header_attachments.append('{F%s}' % uploaded[at]['id']) - if header_attachments: - full_description += '\n__________________________\n\n' - full_description += '\n'.join(header_attachments) - - vlog("Ticket Info: %s" % (full_description,)) - ticket = phab.maniphest.createtask(title=rtinfo['Subject'], - description=full_description, - projectPHIDs=phids, - ccPHIDs=[], - priority=rtinfo['xpriority'], - auxiliary={"std:maniphest:external_reference":"rt%s" % (rtid,)}) - - # XXX: perms - botphid = phabdb.get_phid_by_username(config.phab_user) - phabdb.set_task_title_transaction(ticket['phid'], - botphid, - 'public', - 'public') - - phabdb.set_task_ctime(ticket['phid'], rtlib.str_to_epoch(rtinfo['Created'])) - phabdb.set_task_policy(ticket['phid'], viewpolicy) - - vlog(str(ordered_comments)) - fmt_comments = {} - for comment, contents in comment_dict.iteritems(): - fmt_comment = {} - dbody = contents['body'] - if dbody['content'] is None and dbody['creator'] is None: - continue - elif dbody['content'] is None: - content = 'no content found' - else: - mailsan = rtlib.shadow_emails(dbody['content'][0]) - content_literal = [] - for c in mailsan.splitlines(): - if c.strip() and not c.lstrip().startswith('>'): - # in remarkup having '--' on a new line seems to bold last - # line so signatures really cause issues - if c.strip() == '--': - content_literal.append('%%%{0}%%%'.format(c.strip())) - else: - content_literal.append(unicode('{0}'.format(c.strip()))) - elif c.strip(): - content_literal.append(c.strip()) - else: - vlog("ignoring content line %s" % (c,)) - content = '\n'.join(content_literal) - - if 'This transaction appears to have no content' in content: - content = None - - auto_actions = ['Outgoing email about a comment recorded by RT_System', - 'Outgoing email recorded by RT_System'] - - if dbody['description'] in auto_actions: - vlog("ignoring comment: %s/%s" % (dbody['description'], content)) - continue - - preamble = unicode('') - cbody = unicode('') - if content: - preamble += unicode("`%s wrote:`\n\n" % (dbody['creator'].strip(),)) - cbody += unicode(content).strip() or 'no content' - if dbody['nvalue'] or dbody['ovalue']: - value_update = '' - value_update_text = rtlib.shadow_emails(dbody['description']) - value_update_text = value_update_text.replace('fsck.com-rt', 'https') - relations = ['Reference by ticket', - 'Dependency by', - 'Reference to ticket', - 'Dependency on', - 'Merged into ticket', - 'Membership in'] - - states = ['open', 'resolved', 'new', 'stalled'] - if any(map(lambda x: x in dbody['description'], relations)): - value_update = value_update_text - elif re.search('tags\s\S+\sadded', dbody['description']): - value_update = "%s added tag %s" % (dbody['creator'], dbody['nvalue']) - elif re.search('Taken\sby\s\S+', dbody['description']): - value_update = "Issue taken by **%s**" % (dbody['creator'],) - else: - value_update = "//%s//" % (value_update_text,) - cbody += value_update - - afound = contents['body']['attached'] - cbody_attachments = [] - for a in afound: - if a in upfiles: - cbody_attachments.append('{F%s}' % uploaded[a]['id']) - if cbody_attachments: - cbody += '\n__________________________\n\n' - cbody += '\n'.join(cbody_attachments) - fmt_comment['xattached'] = cbody_attachments - - phabm.task_comment(ticket['id'], preamble + cbody) - ctransaction = phabdb.last_comment(ticket['phid']) - created = rtlib.str_to_epoch_comments(dbody['created']) - phabdb.set_comment_time(ctransaction, - created) - fmt_comment['xctransaction'] = ctransaction - fmt_comment['preamble'] = preamble - fmt_comment['content'] = cbody - fmt_comment['created'] = created - # XXX TRX both ways? - #fmt_comment['creator'] = dbody['creator']user_lookup(name) - fmt_comments[created] = fmt_comment - - if rtinfo['Status'].lower() != 'open': - log('setting %s to status %s' % (rtid, rtinfo['xstatus'].lower())) - phabdb.set_issue_status(ticket['phid'], rtinfo['xstatus'].lower()) - - log("Created task: T%s (%s)" % (ticket['id'], ticket['phid'])) - phabdb.set_task_mtime(ticket['phid'], rtlib.str_to_epoch(rtinfo['LastUpdated'])) - xcomments = json.dumps(fmt_comments) - pmig.sql_x("UPDATE rt_meta SET xcomments=%s WHERE id = %s", (xcomments, rtid)) - pmig.sql_x("UPDATE rt_meta SET priority=%s, modified=%s WHERE id = %s", - (ipriority['creation_success'], now(), rtid)) - pmig.close() - return True - - -def run_create(rtid, tries=1): - if tries == 0: - pmig = phabdb.phdb(db=config.rtmigrate_db) - import_priority = pmig.sql_x("SELECT priority \ - FROM rt_meta \ - WHERE id = %s", \ - (rtid,)) - if import_priority: - pmig.sql_x("UPDATE rt_meta \ - SET priority=%s, modified=%s \ - WHERE id = %s", - (ipriority['creation_failed'], - now(), - rtid)) - else: - elog("%s does not seem to exist" % (rtid)) - elog('failed to create %s' % (rtid,)) - pmig.close() - return False - try: - return create(rtid) - except Exception as e: - import traceback - tries -= 1 - time.sleep(5) - traceback.print_exc(file=sys.stdout) - elog('failed to grab %s (%s)' % (rtid, e)) - return run_create(rtid, tries=tries) - -def main(): - - if not util.can_edit_ref: - elog('%s reference field not editable on this install' % (rtid,)) - sys.exit(1) - - if 'failed' in sys.argv: - priority = ipriority['creation_failed'] - elif 'success' in sys.argv: - priority = ipriority['creation_success'] - else: - priority = None - - vlog("Grabbing for priority: %s" % (priority,)) - pmig = phdb(db=config.rtmigrate_db) - bugs = return_bug_list(dbcon=pmig, - priority=priority, - table='rt_meta') - pmig.close() - - #Serious business - if 'failed' in sys.argv or '-r' in sys.argv: - for b in bugs: - util.notice("Removing rtid %s" % (b,)) - log(util.remove_issue_by_bugid(b, rtlib.prepend)) - - from multiprocessing import Pool - pool = Pool(processes=int(config.bz_createmulti)) - _ = pool.map(run_create, bugs) - complete = len(filter(bool, _)) - failed = len(_) - complete - print '%s completed %s, failed %s' % (sys.argv[0], complete, failed) - -if __name__ == '__main__': - main() diff --git a/rt_fetch_new.py b/rt_fetch_new.py deleted file mode 100755 index 6614b97..0000000 --- a/rt_fetch_new.py +++ /dev/null @@ -1,162 +0,0 @@ -#!/usr/bin/env python -import time -import os -import re -import sys -import getpass -import ConfigParser -import json -sys.path.append('/home/rush/python-rtkit/') -from wmfphablib import phabdb -from wmfphablib import rtlib -from wmfphablib import log -from wmfphablib import vlog -from wmfphablib import errorlog as elog -from wmfphablib import return_bug_list -from rtkit import resource -from rtkit import authenticators -from rtkit import errors -from wmfphablib import ipriority -from wmfphablib import now -from wmfphablib import config - - -def fetch(tid): - - response = resource.RTResource(config.rt_url, - config.rt_login, - config.rt_passwd, - authenticators.CookieAuthenticator) - - log("fetching issue %s" % (tid,)) - tinfo = response.get(path="ticket/%s" % (tid,)) - history = response.get(path="ticket/%s/history?format=l" % (tid,)) - links = response.get(path="ticket/%s/links/show" % (tid,)) - vlog(tinfo) - - if re.search('\#\sTicket\s\d+\sdoes\snot\sexist.$', tinfo.strip()): - log("Skipped as source missing for %s" % (tid,)) - return 'missing' - - # some private todo's and such - if 'You are not allowed to display' in tinfo: - log("Skipped as access denied for %s" % (tid,)) - return 'denied' - - #breaking detailed history into posts - #23/23 (id/114376/total) - comments = re.split("\d+\/\d+\s+\(id\/.\d+\/total\)", history) - comments = [c.rstrip('#').rstrip('--') for c in comments] - - # we get back freeform text and create a dict - dtinfo = {} - link_dict = rtlib.links_to_dict(links) - dtinfo['links'] = link_dict - for cv in tinfo.strip().splitlines(): - if not cv: - continue - cv_kv = re.split(':', cv, 1) - if len(cv_kv) > 1: - k = cv_kv[0] - v = cv_kv[1] - dtinfo[k.strip()] = v.strip() - - vlog("Enabled queues: %s" % (str(rtlib.enabled))) - if dtinfo['Queue'] not in rtlib.enabled: - log("Skipped as disabled queue for %s (%s)" % (str(tid), dtinfo['Queue'])) - return 'disabled' - - com = json.dumps(comments) - tinfo = json.dumps(dtinfo) - - pmig = phabdb.phdb(db=config.rtmigrate_db, - user=config.rtmigrate_user, - passwd=config.rtmigrate_passwd) - - - creation_priority = ipriority['fetch_success'] - current = pmig.sql_x("SELECT * from rt_meta where id = %s", tid) - if current: - update_values = (creation_priority, tinfo, com, now(), now()) - pmig.sql_x("UPDATE rt_meta SET priority=%s, \ - header=%s, \ - comments=%s, \ - modified=%s \ - WHERE id = %s", - update_values) - vlog('update: ' + str(update_values)) - - else: - insert_values = (tid, creation_priority, tinfo, com, now(), now()) - - pmig.sql_x("INSERT INTO rt_meta \ - (id, priority, header, comments, created, modified) \ - VALUES (%s, %s, %s, %s, %s, %s)", - insert_values) - pmig.close() - return True - -def run_fetch(tid, tries=1): - if tries == 0: - pmig = phabdb.phdb(db=config.rtmigrate_db, - user=config.rtmigrate_user, - passwd=config.rtmigrate_passwd) - insert_values = (tid, ipriority['fetch_failed'], '', '', now(), now()) - - pmig.sql_x("INSERT INTO rt_meta \ - (id, priority, header, comments, created, modified) \ - VALUES (%s, %s, %s, %s, %s, %s)", - insert_values) - pmig.close() - elog('failed to grab %s' % (tid,)) - return False - try: - return fetch(tid) - except Exception as e: - import traceback - tries -= 1 - time.sleep(5) - traceback.print_exc(file=sys.stdout) - elog('failed to grab %s (%s)' % (tid, e)) - return run_fetch(tid, tries=tries) - -def main(): - - pmig = phabdb.phdb(db=config.rtmigrate_db, - user=config.rtmigrate_user, - passwd=config.rtmigrate_passwd) - - if 'failed' in sys.argv: - priority = ipriority['fetch_failed'] - else: - priority = None - - bugs = return_bug_list(dbcon=pmig, - priority=priority, - table='rt_meta') - pmig.close() - - from multiprocessing import Pool - pool = Pool(processes=int(config.bz_fetchmulti)) - _ = pool.map(run_fetch, bugs) - vlog(_) - denied = len([i for i in _ if i == 'denied']) - disabled = len([i for i in _ if i == 'disabled']) - missing = len([i for i in _ if i == 'missing']) - complete = len(filter(bool, [i for i in _ if i not in ['denied', 'disabled', 'missing']])) - known_bad = denied + disabled + missing - failed = (len(_) - known_bad) - complete - print '-----------------------------\n \ - %s Total %s\n \ - known bad %s (denied %s, disabled %s, missing %s)\n\n \ - completed %s, failed %s' % (sys.argv[0], - len(bugs), - known_bad, - denied, - disabled, - missing, - complete, - failed) - -if __name__ == '__main__': - main() diff --git a/rt_update_tasks.py b/rt_update_tasks.py index d8adc5c..27a0ebd 100755 --- a/rt_update_tasks.py +++ b/rt_update_tasks.py @@ -30,7 +30,7 @@ def update(bugid): header = pmig.sql_x(query, (bugid,)) if not header: elog('no header found for %s' % (bugid,)) - return False + return 'missing' def extref(ticket): refid = phabdb.reference_ticket("%s%s" % (rtlib.prepend, ticket)) @@ -122,10 +122,16 @@ def main(): bugs = return_bug_list() from multiprocessing import Pool pool = Pool(processes=int(config.bz_updatemulti)) - _ = pool.map(run_update, bugs) - complete = len(filter(bool, _)) - failed = len(_) - complete - print '%s completed %s, failed %s' % (sys.argv[0], complete, failed) - + result = pool.map(run_update, bugs) + missing = len([i for i in result if i == 'missing']) + complete = len(filter(bool, [i for i in result if i not in ['missing']])) + failed = (len(result) - missing) - complete + print '-----------------------------\n \ + %s Total %s (missing %s)\n \ + completed %s, failed %s' % (sys.argv[0], + len(bugs), + missing, + complete, + failed) if __name__ == '__main__': main() diff --git a/rt_update_tasks_references.py b/rt_update_tasks_references.py index 88e6fad..233227b 100755 --- a/rt_update_tasks_references.py +++ b/rt_update_tasks_references.py @@ -29,8 +29,8 @@ def update(bugid): query = "SELECT header FROM rt_meta WHERE id = %s" header = pmig.sql_x(query, (bugid,)) if not header: - elog('no header found for %s' % (bugid,)) - return False + log('no header found for %s' % (bugid,)) + return 'missing' def extref(ticket): refid = phabdb.reference_ticket("%s%s" % (rtlib.prepend, ticket)) diff --git a/rt_update_user_comments.py b/rt_update_user_comments.py index 838c592..d6c0715 100755 --- a/rt_update_user_comments.py +++ b/rt_update_user_comments.py @@ -5,7 +5,6 @@ import time import json import multiprocessing import sys -import collections from phabricator import Phabricator from wmfphablib import Phab as phabmacros from wmfphablib import phabdb @@ -15,11 +14,9 @@ from wmfphablib import bzlib from wmfphablib import config from wmfphablib import vlog from wmfphablib import errorlog as elog -from wmfphablib import epoch_to_datetime from wmfphablib import ipriority from wmfphablib import now from wmfphablib import return_bug_list -from wmfphablib import ipriority def update(user): @@ -39,33 +36,40 @@ def update(user): log("%s is a bot no action" % (user['user'])) return True - epriority = phabdb.get_user_relations_comments_priority(user['user'], pmig) + epriority = phabdb.get_user_relations_comments_priority(user['user'], + pmig) if epriority and len(epriority[0]) > 0: - if epriority[0][0] == ipriority['update_success']: + if epriority[0][0] == ipriority['update_success_comments']: log('Skipping %s as already updated' % (user['user'])) - #return True + return True if not user['issues']: log("%s has no issues to update" % (user['user'],)) return True for i in user['issues']: - comdetails = pmig.sql_x("SELECT comments, xcomments FROM bugzilla_meta WHERE id = %s", (int(i),)) + comdetails = pmig.sql_x("SELECT comments, \ + xcomments \ + FROM rt_meta \ + WHERE id = %s", (int(i),)) jcom, jxcom = comdetails[0] coms = json.loads(jcom) xcoms = json.loads(jxcom) for key, xi in xcoms.iteritems(): - com = coms[util.get_index(coms, "count", int(key))] - content = com['text'] - if com["creator"] == user['user']: + content = xi['content'] + if xi["creator"] == user['user']: log("Updating comment %s for %s" % (xi['xctransaction'], user['user'])) phabdb.set_comment_author(xi['xctransaction'], user['userphid']) - phabdb.set_comment_content(xi['xctransaction'], content + xi['xattached']) + phabdb.set_comment_content(xi['xctransaction'], xi['content']) - current = phabdb.get_user_migration_comment_history(user['user'], pmig) + current = phabdb.get_user_migration_comment_history(user['user'], + pmig) if current: - log(phabdb.set_user_relations_comments_priority(ipriority['update_success'], user['user'], pmig)) + success = ipriority['update_success_comments'] + log(phabdb.set_user_relations_comments_priority(success, + user['user'], + pmig)) else: log('%s user does not exist to update' % (user['user'])) return False @@ -78,9 +82,13 @@ def run_update(user, tries=1): pmig = phabdb.phdb(db=config.rtmigrate_db, user=config.rtmigrate_user, passwd=config.rtmigrate_passwd) - current = phabdb.get_user_migration_history(user['user'], pmig) + current = phabdb.get_user_migration_history(user['user'], + pmig) if current: - log(phabdb.set_user_relations_priority(ipriority['update_failed'], user['user'], pmig)) + failed = ipriority['update_failed_comments'] + log(phabdb.set_user_relations_priority(failed, + user['user'], + pmig)) else: log('%s user does not exist to update' % (user['user'])) pmig.close() @@ -104,7 +112,8 @@ def get_user_histories(verified): for v in verified: vlog(str(v)) - saved_history = phabdb.get_user_migration_comment_history(v[1], pmig) + saved_history = phabdb.get_user_migration_comment_history(v[1], + pmig) if not saved_history: log('%s verified email has no saved history' % (v[1],)) continue @@ -135,7 +144,8 @@ def main(): if args.a: starting_epoch = phabdb.get_user_relations_comments_last_finish(pmig) - users, finish_epoch = phabdb.get_verified_users(starting_epoch, config.bz_updatelimit) + users, finish_epoch = phabdb.get_verified_users(starting_epoch, + config.bz_updatelimit) elif args.email: users = phabdb.get_verified_user(args.email) starting_epoch = 0 @@ -174,24 +184,19 @@ def main(): starting_epoch, user_count, issue_count, pmig) - from multiprocessing import Pool - pool = Pool(processes=int(config.bz_updatemulti)) _ = pool.map(run_update, histories) complete = len(filter(bool, _)) failed = len(_) - complete phabdb.user_relations_finish(pid, int(time.time()), - ipriority['update_success'], + ipriority['update_success_comments'], finish_epoch, complete, failed, pmig) - pm = phabmacros(config.phab_user, config.phab_cert, config.phab_host) - vlog(util.update_blog(source, complete, failed, user_count, issue_count, pm)) - pmig.close() print '%s completed %s, failed %s' % (sys.argv[0], complete, failed) diff --git a/wmfphablib/__init__.py b/wmfphablib/__init__.py index eed8ec9..8f23372 100755 --- a/wmfphablib/__init__.py +++ b/wmfphablib/__init__.py @@ -34,7 +34,9 @@ ipriority = {'creation_failed': 6, 'denied': 2, 'missing': 3, 'update_success': 8, + 'update_success_comments': 10, 'update_failed': 9, + 'update_failed_comments': 11, 'unresolved': 1} def save_attachment(name, data): |