<s> import os <EOL> import sys <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> SRC_ROOT = os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> sys . path . append ( SRC_ROOT ) <EOL> import requests <EOL> from utils . msg_util import * <EOL> from settings . base import GITHUB_LOGIN , GITHUB_PASSWORD_OR_PERSONAL_ACCESS_TOKEN , GITHUB_TARGET_USERNAME , GITHUB_TARGET_REPOSITORY <EOL> import json <EOL> from github_issues . label_map import LabelMap <EOL> class LabelHelper : <EOL> def __init__ ( self , label_map_filename = None ) : <EOL> """<STR_LIT>""" <EOL> self . auth = ( GITHUB_LOGIN , GITHUB_PASSWORD_OR_PERSONAL_ACCESS_TOKEN ) <EOL> self . label_map_filename = label_map_filename <EOL> self . label_map = None <EOL> self . using_label_map = False <EOL> self . load_map ( ) <EOL> def load_map ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . label_map_filename is None : <EOL> self . using_label_map = False <EOL> return <EOL> self . label_map = LabelMap ( self . label_map_filename ) <EOL> self . using_label_map = True <EOL> self . make_update_map_labels ( ) <EOL> def make_update_map_labels ( self ) : <EOL> """<STR_LIT>""" <EOL> msgt ( '<STR_LIT>' ) <EOL> for label_info in self . label_map . get_label_info_objects ( ) : <EOL> msg ( '<STR_LIT>' % ( label_info . github_label_name , label_info . github_label_color ) ) <EOL> msg ( '<STR_LIT>' ) <EOL> label_url = '<STR_LIT>' % ( GITHUB_TARGET_USERNAME , GITHUB_TARGET_REPOSITORY , label_info . github_label_name ) <EOL> req = requests . get ( label_url , auth = self . auth ) <EOL> msg ( '<STR_LIT>' % label_url ) <EOL> msg ( '<STR_LIT>' % req . status_code ) <EOL> if req . status_code == <NUM_LIT:200> : <EOL> github_label_info = req . json ( ) <EOL> msg ( '<STR_LIT>' % github_label_info ) <EOL> if github_label_info . get ( '<STR_LIT>' , '<STR_LIT>' ) == label_info . github_label_color : <EOL> msg ( '<STR_LIT>' ) <EOL> continue <EOL> msg ( '<STR_LIT>' ) <EOL> label_url = '<STR_LIT>' % ( GITHUB_TARGET_USERNAME , GITHUB_TARGET_REPOSITORY , label_info . github_label_name ) <EOL> data = dict ( name = label_info . github_label_name , color = label_info . github_label_color ) <EOL> req = requests . patch ( label_url , data = json . dumps ( data ) , auth = self . auth ) <EOL> if req . status_code == <NUM_LIT:200> : <EOL> msg ( '<STR_LIT>' ) <EOL> msg ( req . text ) <EOL> continue <EOL> msgx ( '<STR_LIT>' ) <EOL> msg ( '<STR_LIT>' ) <EOL> label_url = '<STR_LIT>' % ( GITHUB_TARGET_USERNAME , GITHUB_TARGET_REPOSITORY ) <EOL> data = dict ( name = label_info . github_label_name , color = label_info . github_label_color ) <EOL> req = requests . post ( label_url , data = json . dumps ( data ) , auth = self . auth ) <EOL> msg ( req . text ) <EOL> msg ( req . status_code ) <EOL> if req . status_code in [ <NUM_LIT:200> , <NUM_LIT> ] : <EOL> msg ( '<STR_LIT>' ) <EOL> continue <EOL> else : <EOL> msgx ( '<STR_LIT>' ) <EOL> def clear_labels ( self , issue_id ) : <EOL> msgt ( '<STR_LIT>' % ( issue_id ) ) <EOL> label_url = '<STR_LIT>' % ( GITHUB_TARGET_USERNAME , GITHUB_TARGET_REPOSITORY , issue_id ) <EOL> req = requests . delete ( label_url , auth = self . auth ) <EOL> msg ( '<STR_LIT>' ) <EOL> def add_labels_to_issue ( self , issue_id , labels = [ ] ) : <EOL> msg ( '<STR_LIT>' % ( issue_id , labels ) ) <EOL> if not issue_id or not type ( labels ) in [ list , tuple ] : <EOL> return <EOL> if len ( labels ) == <NUM_LIT:0> : <EOL> return <EOL> label_url = '<STR_LIT>' % ( GITHUB_TARGET_USERNAME , GITHUB_TARGET_REPOSITORY , issue_id ) <EOL> labels_for_call = json . dumps ( labels ) <EOL> msg ( '<STR_LIT>' % labels_for_call ) <EOL> req = requests . post ( label_url , auth = self . auth , data = labels_for_call ) <EOL> msg ( '<STR_LIT>' % req . text ) <EOL> def get_label_from_id_name ( self , label_info_dict , key_name = None , label_prefix = '<STR_LIT>' , non_formatted = False ) : <EOL> """<STR_LIT>""" <EOL> if not type ( label_info_dict ) is dict : <EOL> return None <EOL> if key_name is not None : <EOL> label_info_dict = label_info_dict . get ( key_name , None ) <EOL> if label_info_dict is None : <EOL> return None <EOL> if label_info_dict . has_key ( '<STR_LIT:id>' ) and label_info_dict . has_key ( '<STR_LIT:name>' ) : <EOL> if non_formatted : <EOL> return label_info_dict [ '<STR_LIT:name>' ] <EOL> if label_prefix : <EOL> return '<STR_LIT>' % ( label_prefix , label_info_dict [ '<STR_LIT:name>' ] ) <EOL> return label_info_dict [ '<STR_LIT:name>' ] <EOL> return None <EOL> def get_label_names_based_on_map ( self , redmine_issue_dict ) : <EOL> """<STR_LIT>""" <EOL> label_names = self . get_label_names ( redmine_issue_dict , non_formatted = True ) <EOL> if len ( label_names ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> mapped_label_names = [ ] <EOL> for name in label_names : <EOL> github_label_name = self . label_map . get_github_label_from_redmine_name ( name ) <EOL> print '<STR_LIT:name>' , name , '<STR_LIT>' , github_label_name <EOL> if github_label_name : <EOL> mapped_label_names . append ( github_label_name ) <EOL> return mapped_label_names <EOL> def get_label_names_from_issue ( self , redmine_issue_dict ) : <EOL> if self . using_label_map is True : <EOL> return self . get_label_names_based_on_map ( redmine_issue_dict ) <EOL> return self . get_label_names ( redmine_issue_dict ) <EOL> def get_label_names ( self , redmine_issue_dict , non_formatted = False ) : <EOL> """<STR_LIT>""" <EOL> if not type ( redmine_issue_dict ) is dict : <EOL> return [ ] <EOL> label_names = [ ] <EOL> status_label_name = self . get_label_from_id_name ( redmine_issue_dict , '<STR_LIT:status>' , '<STR_LIT>' , non_formatted ) <EOL> if status_label_name : <EOL> label_names . append ( status_label_name ) <EOL> tracker_label_name = self . get_label_from_id_name ( redmine_issue_dict , '<STR_LIT>' , '<STR_LIT>' , non_formatted ) <EOL> if tracker_label_name : <EOL> label_names . append ( tracker_label_name ) <EOL> priority_label_name = self . get_label_from_id_name ( redmine_issue_dict , '<STR_LIT>' , '<STR_LIT>' , non_formatted ) <EOL> if priority_label_name : <EOL> label_names . append ( priority_label_name ) <EOL> category_label_name = self . get_label_from_id_name ( redmine_issue_dict , '<STR_LIT>' , '<STR_LIT>' , non_formatted ) <EOL> if category_label_name : <EOL> label_names . append ( category_label_name ) <EOL> custom_fields = redmine_issue_dict . get ( '<STR_LIT>' , None ) <EOL> if custom_fields and len ( custom_fields ) > <NUM_LIT:0> : <EOL> for cf_dict in custom_fields : <EOL> component_label_name = self . get_label_from_id_name ( cf_dict , None , '<STR_LIT>' , non_formatted ) <EOL> if component_label_name : <EOL> label_names . append ( component_label_name ) <EOL> return label_names <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from settings . base import LABEL_MAP_FILE <EOL> LabelHelper ( LABEL_MAP_FILE ) <EOL> """<STR_LIT>""" </s>
