<s> import calendar <EOL> from xml . dom import minidom <EOL> import datetime <EOL> import sys <EOL> from agilezen . client import Client <EOL> from youtrack import YouTrackException , Link , User , Group , StateField , Issue , EnumBundle , StateBundle , Comment <EOL> from youtrack . connection import Connection <EOL> def main ( ) : <EOL> if sys . argv [ <NUM_LIT:0> ] == u'<STR_LIT>' : <EOL> source_url , source_token , target_url , target_api_key = sys . argv [ <NUM_LIT:1> : <NUM_LIT:5> ] <EOL> agilezen2youtrack ( source_url , source_token , target_url , api_key = target_api_key , <EOL> project_names_to_import = sys . argv [ <NUM_LIT:5> : ] ) <EOL> else : <EOL> source_url , source_token , target_url , target_login , target_password = sys . argv [ <NUM_LIT:1> : <NUM_LIT:6> ] <EOL> agilezen2youtrack ( source_url , source_token , target_url , target_login , target_password , sys . argv [ <NUM_LIT:6> : ] ) <EOL> def to_yt_user ( user ) : <EOL> u = User ( ) <EOL> u . login = user [ u'<STR_LIT>' ] <EOL> u . email = user [ u'<STR_LIT:email>' ] <EOL> if u'<STR_LIT:name>' in user : <EOL> u . fullName = user [ u'<STR_LIT:name>' ] <EOL> return u <EOL> def import_user ( target , user ) : <EOL> target . importUsers ( [ to_yt_user ( user ) ] ) <EOL> def import_role ( target , project_id , role ) : <EOL> if u'<STR_LIT>' not in role : <EOL> return <EOL> users_to_import = [ ] <EOL> members = role [ u'<STR_LIT>' ] <EOL> for member in members : <EOL> users_to_import . append ( to_yt_user ( member ) ) <EOL> target . importUsers ( users_to_import ) <EOL> group = Group ( ) <EOL> group . name = project_id + "<STR_LIT:->" + role [ u'<STR_LIT:name>' ] <EOL> try : <EOL> target . createGroup ( group ) <EOL> except YouTrackException , e : <EOL> log_message ( e ) <EOL> for member in members : <EOL> try : <EOL> target . setUserGroup ( member [ u'<STR_LIT>' ] , group . name ) <EOL> except YouTrackException , e : <EOL> log_error ( e ) <EOL> def import_phase ( target , project_id , phase ) : <EOL> bundle = target . getBundle ( "<STR_LIT>" , target . getProjectCustomField ( project_id , "<STR_LIT>" ) . bundle ) <EOL> value = StateField ( ) <EOL> value . name = phase [ u'<STR_LIT:name>' ] <EOL> value . description = phase [ u'<STR_LIT:description>' ] <EOL> try : <EOL> target . addValueToBundle ( bundle , value ) <EOL> except YouTrackException , e : <EOL> pass <EOL> def add_value_to_custom_field ( target , project_id , field_name , field_value ) : <EOL> field_type = target . getCustomField ( field_name ) . type <EOL> bundle = target . getBundle ( field_type , target . getProjectCustomField ( project_id , field_name ) . bundle ) <EOL> try : <EOL> target . addValueToBundle ( bundle , field_value ) <EOL> except YouTrackException : <EOL> pass <EOL> def get_created_date_for_story ( story ) : <EOL> return str ( to_unix_date ( story [ u'<STR_LIT>' ] [ u'<STR_LIT>' ] ) ) <EOL> def to_yt_issue ( target , project_id , story ) : <EOL> parent = Issue ( ) <EOL> parent . numberInProject = str ( story [ u'<STR_LIT:id>' ] ) <EOL> parent . summary = story [ u'<STR_LIT:text>' ] <EOL> parent [ '<STR_LIT>' ] = story [ u'<STR_LIT:size>' ] <EOL> parent [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> parent . created = get_created_date_for_story ( story ) <EOL> color = story [ u'<STR_LIT>' ] <EOL> add_value_to_custom_field ( target , project_id , '<STR_LIT>' , color ) <EOL> parent [ '<STR_LIT>' ] = color <EOL> priority = story [ u'<STR_LIT>' ] <EOL> if len ( priority ) : <EOL> add_value_to_custom_field ( target , project_id , '<STR_LIT>' , priority ) <EOL> parent [ '<STR_LIT>' ] = priority <EOL> if u'<STR_LIT>' in story : <EOL> parent [ '<STR_LIT>' ] = str ( to_unix_date ( story [ u'<STR_LIT>' ] ) ) <EOL> status = story [ u'<STR_LIT:status>' ] <EOL> add_value_to_custom_field ( target , project_id , '<STR_LIT>' , status ) <EOL> parent [ '<STR_LIT>' ] = status <EOL> parent [ '<STR_LIT>' ] = story [ u'<STR_LIT>' ] [ u'<STR_LIT:name>' ] <EOL> creator = story [ u'<STR_LIT>' ] <EOL> import_user ( target , creator ) <EOL> if u'<STR_LIT>' in story : <EOL> owner = story [ u'<STR_LIT>' ] <EOL> import_user ( target , owner ) <EOL> parent [ '<STR_LIT>' ] = owner [ u'<STR_LIT>' ] <EOL> parent . reporterName = creator [ u'<STR_LIT>' ] <EOL> parent . comments = [ ] <EOL> if u'<STR_LIT>' in story : <EOL> for comment in story [ u'<STR_LIT>' ] : <EOL> parent . comments . append ( to_yt_comment ( target , comment ) ) <EOL> if u'<STR_LIT>' in story : <EOL> parent . description = story [ u'<STR_LIT>' ] <EOL> return parent <EOL> def to_yt_sub_task ( target , project_id , story , task ) : <EOL> issue = Issue ( ) <EOL> issue . summary = task [ u'<STR_LIT:text>' ] <EOL> issue . reporterName = story [ u'<STR_LIT>' ] [ u'<STR_LIT>' ] <EOL> issue . created = str ( to_unix_date ( task [ u'<STR_LIT>' ] ) ) <EOL> if u'<STR_LIT>' in task : <EOL> issue . resolved = str ( to_unix_date ( task [ u'<STR_LIT>' ] ) ) <EOL> status = task [ u'<STR_LIT:status>' ] <EOL> add_value_to_custom_field ( target , project_id , "<STR_LIT>" , status ) <EOL> issue [ '<STR_LIT>' ] = status <EOL> issue [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if u'<STR_LIT>' in task : <EOL> finished_by = task [ u'<STR_LIT>' ] <EOL> import_user ( target , finished_by ) <EOL> issue [ '<STR_LIT>' ] = finished_by [ u'<STR_LIT>' ] <EOL> issue . comments = [ ] <EOL> return issue <EOL> def to_yt_comment ( target , comment ) : <EOL> c = Comment ( ) <EOL> author = comment [ u'<STR_LIT>' ] <EOL> import_user ( target , author ) <EOL> c . text = comment [ u'<STR_LIT:text>' ] <EOL> if not len ( c . text ) : <EOL> c . text = "<STR_LIT>" <EOL> c . author = author [ u'<STR_LIT>' ] <EOL> c . created = str ( to_unix_date ( comment [ u'<STR_LIT>' ] ) ) <EOL> return c <EOL> def to_unix_date ( time ) : <EOL> dt = datetime . datetime . strptime ( time , "<STR_LIT>" ) <EOL> return calendar . timegm ( dt . timetuple ( ) ) * <NUM_LIT:1000> <EOL> def is_prefix_of_any_other_tag ( tag , other_tags ) : <EOL> for t in other_tags : <EOL> if t . startswith ( tag ) and ( t != tag ) : <EOL> return True <EOL> return False <EOL> def import_tags ( source , target , project_id , collected_tags ) : <EOL> tags_to_import_now = set ( [ ] ) <EOL> tags_to_import_then = set ( [ ] ) <EOL> for tag in collected_tags : <EOL> if is_prefix_of_any_other_tag ( tag , collected_tags ) : <EOL> tags_to_import_then . add ( tag ) <EOL> else : <EOL> tags_to_import_now . add ( tag ) <EOL> last_page = False <EOL> current_page = <NUM_LIT:1> <EOL> while not last_page : <EOL> stories = source . get_stories_for_project ( project_id , current_page ) <EOL> for story in stories [ u'<STR_LIT>' ] : <EOL> if u'<STR_LIT>' in story : <EOL> for tag in [ t [ u'<STR_LIT:name>' ] for t in story [ u'<STR_LIT>' ] if t [ u'<STR_LIT:name>' ] in tags_to_import_now ] : <EOL> target . executeCommand ( "<STR_LIT>" % ( project_id , story [ u'<STR_LIT:id>' ] ) , "<STR_LIT>" + tag ) <EOL> current_page += <NUM_LIT:1> <EOL> if current_page > stories [ u'<STR_LIT>' ] : <EOL> last_page = True <EOL> if len ( tags_to_import_then ) : <EOL> import_tags ( source , target , project_id , tags_to_import_then ) <EOL> def import_project ( source , target , project ) : <EOL> log_step ( "<STR_LIT>" + project [ u'<STR_LIT:name>' ] ) <EOL> import_user ( target , project [ u'<STR_LIT>' ] ) <EOL> project_id = str ( project [ u'<STR_LIT:id>' ] ) <EOL> try : <EOL> target . getProject ( project_id ) <EOL> except YouTrackException , e : <EOL> target . createProjectDetailed ( project_id , project [ u'<STR_LIT:name>' ] , <EOL> project [ u'<STR_LIT:description>' ] , project [ u'<STR_LIT>' ] [ u'<STR_LIT>' ] ) <EOL> last_page = False <EOL> current_page = <NUM_LIT:1> <EOL> log_step ( "<STR_LIT>" ) <EOL> while not last_page : <EOL> roles = source . get_project_roles ( project_id , current_page ) <EOL> if current_page == roles [ u'<STR_LIT>' ] : <EOL> last_page = True <EOL> for role in roles [ u'<STR_LIT>' ] : <EOL> import_role ( target , project_id , role ) <EOL> current_page += <NUM_LIT:1> <EOL> last_page = False <EOL> current_page = <NUM_LIT:1> <EOL> log_step ( "<STR_LIT>" ) <EOL> while not last_page : <EOL> phases = source . get_project_phases ( project_id , current_page ) <EOL> if current_page == phases [ u'<STR_LIT>' ] : <EOL> last_page = True <EOL> for phase in phases [ u'<STR_LIT>' ] : <EOL> import_phase ( target , project_id , phase ) <EOL> current_page += <NUM_LIT:1> <EOL> last_page = False <EOL> current_page = <NUM_LIT:1> <EOL> max_story_id = <NUM_LIT:0> <EOL> existing_tags = set ( ) <EOL> log_step ( "<STR_LIT>" ) <EOL> while not last_page : <EOL> stories = source . get_stories_for_project ( project_id , current_page ) <EOL> if current_page == stories [ u'<STR_LIT>' ] : <EOL> last_page = True <EOL> stories_to_import = [ ] <EOL> items = stories [ u'<STR_LIT>' ] <EOL> for story in items : <EOL> story_id = story [ u'<STR_LIT:id>' ] <EOL> yt_issue = to_yt_issue ( target , project_id , story ) <EOL> if story_id > max_story_id : <EOL> max_story_id = story_id <EOL> stories_to_import . append ( yt_issue ) <EOL> if u'<STR_LIT>' in story : <EOL> existing_tags |= set ( [ t [ u'<STR_LIT:name>' ] for t in story [ u'<STR_LIT>' ] ] ) <EOL> target . importIssues ( project_id , project_id + "<STR_LIT>" , stories_to_import ) <EOL> current_page += <NUM_LIT:1> <EOL> log_step ( "<STR_LIT>" ) <EOL> import_tags ( source , target , project_id , existing_tags ) <EOL> last_page = False <EOL> current_page = <NUM_LIT:1> <EOL> log_step ( "<STR_LIT>" ) <EOL> while not last_page : <EOL> stories = source . get_stories_for_project ( project_id , current_page ) <EOL> if current_page == stories [ u'<STR_LIT>' ] : <EOL> last_page = True <EOL> for story in stories [ u'<STR_LIT>' ] : <EOL> tasks = [ to_yt_sub_task ( target , project_id , story , task ) for task in <EOL> story [ '<STR_LIT>' ] ] if u'<STR_LIT>' in story else [ ] <EOL> if len ( tasks ) : <EOL> for task in tasks : <EOL> max_story_id += <NUM_LIT:1> <EOL> task . numberInProject = str ( max_story_id ) <EOL> result = target . importIssues ( project_id , project_id + "<STR_LIT>" , tasks ) <EOL> items = minidom . parseString ( result ) . getElementsByTagName ( "<STR_LIT>" ) <EOL> issue_links_to_import = [ ] <EOL> for item in items : <EOL> link = Link ( ) <EOL> link . typeName = "<STR_LIT>" <EOL> link . target = "<STR_LIT>" % ( project_id , item . attributes [ '<STR_LIT:id>' ] . value ) <EOL> link . source = "<STR_LIT>" % ( project_id , story [ u'<STR_LIT:id>' ] ) <EOL> issue_links_to_import . append ( link ) <EOL> target . importLinks ( issue_links_to_import ) <EOL> current_page += <NUM_LIT:1> <EOL> def doImport ( project_names_to_import , source , target ) : <EOL> last_page = False <EOL> current_page = <NUM_LIT:1> <EOL> log_step ( "<STR_LIT>" ) <EOL> try : <EOL> target . createCustomFieldDetailed ( "<STR_LIT>" , "<STR_LIT>" , False , True , True , { "<STR_LIT>" : "<STR_LIT:2>" } ) <EOL> except YouTrackException , e : <EOL> pass <EOL> colors_bundle = EnumBundle ( ) <EOL> colors_bundle . name = "<STR_LIT>" <EOL> try : <EOL> target . createBundle ( colors_bundle ) <EOL> except YouTrackException , e : <EOL> log_message ( e ) <EOL> try : <EOL> target . createCustomFieldDetailed ( "<STR_LIT>" , "<STR_LIT>" , False , True , True , <EOL> { "<STR_LIT>" : colors_bundle . name , "<STR_LIT>" : "<STR_LIT:0>" } ) <EOL> except YouTrackException , e : <EOL> pass <EOL> try : <EOL> status_bundle = StateBundle ( ) <EOL> status_bundle . name = "<STR_LIT>" <EOL> target . createBundle ( status_bundle ) <EOL> target . createCustomFieldDetailed ( "<STR_LIT>" , "<STR_LIT>" , False , True , True , <EOL> { "<STR_LIT>" : status_bundle . name , "<STR_LIT>" : "<STR_LIT:0>" } ) <EOL> except YouTrackException , e : <EOL> pass <EOL> try : <EOL> target . createCustomFieldDetailed ( "<STR_LIT>" , "<STR_LIT>" , False , True , True ) <EOL> except YouTrackException , e : <EOL> pass <EOL> try : <EOL> target . createCustomFieldDetailed ( "<STR_LIT>" , "<STR_LIT:date>" , False , True , True ) <EOL> except YouTrackException , e : <EOL> pass <EOL> while not last_page : <EOL> projects = source . get_projects ( page = <NUM_LIT:1> , page_size = <NUM_LIT> ) <EOL> if current_page == projects [ u'<STR_LIT>' ] : <EOL> last_page = True <EOL> for project in projects [ u'<STR_LIT>' ] : <EOL> if project [ u'<STR_LIT:name>' ] in project_names_to_import : <EOL> import_project ( source , target , project ) <EOL> current_page += <NUM_LIT:1> <EOL> def agilezen2youtrack ( source_url , source_token , target_url , target_login = None , target_password = None , <EOL> project_names_to_import = None , api_key = None ) : <EOL> source = Client ( source_url , source_token ) <EOL> target = None <EOL> if api_key is None : <EOL> target = Connection ( target_url , target_login , target_password ) <EOL> else : <EOL> target = Connection ( target_url , api_key = api_key ) <EOL> doImport ( project_names_to_import , source , target ) <EOL> def log_error ( e ) : <EOL> if not ( isinstance ( e , unicode ) or isinstance ( e , str ) ) : <EOL> msg = repr ( e ) <EOL> else : <EOL> msg = e <EOL> sys . stderr . write ( msg . encode ( '<STR_LIT:utf-8>' ) if isinstance ( msg , unicode ) else msg ) <EOL> def log_message ( e ) : <EOL> if not ( isinstance ( e , unicode ) or isinstance ( e , str ) ) : <EOL> msg = repr ( e ) <EOL> else : <EOL> msg = e <EOL> print msg . encode ( '<STR_LIT:utf-8>' ) if isinstance ( msg , unicode ) else msg <EOL> def log_step ( step ) : <EOL> log_message ( "<STR_LIT>" + step ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
