<s> """<STR_LIT>""" <EOL> import datetime <EOL> import urllib <EOL> import yaml <EOL> import time <EOL> from jira . client import JIRA <EOL> from security_monkey . datastore import Account , Technology , AuditorSettings <EOL> from security_monkey import app <EOL> class JiraSync ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , jira_file ) : <EOL> try : <EOL> with open ( jira_file ) as jf : <EOL> data = jf . read ( ) <EOL> data = yaml . load ( data ) <EOL> self . account = data [ '<STR_LIT>' ] <EOL> self . password = data [ '<STR_LIT:password>' ] <EOL> self . project = data [ '<STR_LIT>' ] <EOL> self . server = data [ '<STR_LIT>' ] <EOL> self . issue_type = data [ '<STR_LIT>' ] <EOL> self . url = data [ '<STR_LIT:url>' ] <EOL> self . disable_transitions = data . get ( '<STR_LIT>' , False ) <EOL> except KeyError as e : <EOL> raise Exception ( '<STR_LIT>' . format ( e ) ) <EOL> except IOError as e : <EOL> raise Exception ( '<STR_LIT>' . format ( e ) ) <EOL> except yaml . scanner . ScannerError as e : <EOL> raise Exception ( '<STR_LIT>' . format ( e ) ) <EOL> try : <EOL> self . client = JIRA ( self . server , basic_auth = ( self . account , self . password ) ) <EOL> except Exception as e : <EOL> raise Exception ( "<STR_LIT>" . format ( str ( e ) [ : <NUM_LIT> ] ) ) <EOL> def close_issue ( self , issue ) : <EOL> try : <EOL> self . transition_issue ( issue , '<STR_LIT>' ) <EOL> except Exception as e : <EOL> app . logger . error ( '<STR_LIT>' . format ( issue . fields . summary , issue . key , e ) ) <EOL> def open_issue ( self , issue ) : <EOL> try : <EOL> self . transition_issue ( issue , '<STR_LIT>' ) <EOL> except Exception as e : <EOL> app . logger . error ( '<STR_LIT>' . format ( issue . fields . summary , issue . key , e ) ) <EOL> def transition_issue ( self , issue , transition_name ) : <EOL> transitions = self . client . transitions ( issue ) <EOL> for transition in transitions : <EOL> if transition [ '<STR_LIT:name>' ] . lower ( ) == transition_name . lower ( ) : <EOL> break <EOL> else : <EOL> app . logger . error ( '<STR_LIT>' . format ( transition_name , issue . key ) ) <EOL> return <EOL> self . client . transition_issue ( issue , transition [ '<STR_LIT:id>' ] ) <EOL> def add_or_update_issue ( self , issue , technology , account , count ) : <EOL> """<STR_LIT>""" <EOL> summary = '<STR_LIT>' . format ( issue , technology , account ) <EOL> summary_search = summary . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> jql = '<STR_LIT>' . format ( self . project , summary_search ) <EOL> issues = self . client . search_issues ( jql ) <EOL> url = "<STR_LIT>" . format ( self . url , technology , account , urllib . quote ( issue , '<STR_LIT>' ) ) <EOL> timezone = time . tzname [ time . localtime ( ) . tm_isdst ] <EOL> description = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( count , account , url , datetime . datetime . now ( ) . isoformat ( ) , timezone ) ) <EOL> for issue in issues : <EOL> if issue . fields . summary == summary : <EOL> old_desc = issue . fields . description <EOL> old_desc = old_desc [ : old_desc . find ( '<STR_LIT>' ) ] <EOL> issue . update ( description = old_desc + description ) <EOL> app . logger . debug ( "<STR_LIT>" . format ( summary , issue . key ) ) <EOL> if self . disable_transitions : <EOL> return <EOL> if issue . fields . status . name == '<STR_LIT>' and count : <EOL> self . open_issue ( issue ) <EOL> app . logger . debug ( "<STR_LIT>" . format ( summary , issue . key ) ) <EOL> elif issue . fields . status . name != '<STR_LIT>' and count == <NUM_LIT:0> : <EOL> self . close_issue ( issue ) <EOL> app . logger . debug ( "<STR_LIT>" . format ( summary , issue . key ) ) <EOL> return <EOL> if count == <NUM_LIT:0> : <EOL> return <EOL> jira_args = { '<STR_LIT>' : { '<STR_LIT:key>' : self . project } , <EOL> '<STR_LIT>' : { '<STR_LIT:name>' : self . issue_type } , <EOL> '<STR_LIT>' : summary , <EOL> '<STR_LIT:description>' : description } <EOL> try : <EOL> issue = self . client . create_issue ( ** jira_args ) <EOL> app . logger . debug ( "<STR_LIT>" . format ( summary , issue . key ) ) <EOL> except Exception as e : <EOL> app . logger . error ( "<STR_LIT>" . format ( summary , e ) ) <EOL> def sync_issues ( self , accounts = None , tech_name = None ) : <EOL> """<STR_LIT>""" <EOL> query = AuditorSettings . query . join ( <EOL> ( Technology , Technology . id == AuditorSettings . tech_id ) <EOL> ) . join ( <EOL> ( Account , Account . id == AuditorSettings . account_id ) <EOL> ) . filter ( <EOL> ( AuditorSettings . disabled == False ) <EOL> ) <EOL> if accounts : <EOL> query = query . filter ( Account . name . in_ ( accounts ) ) <EOL> if tech_name : <EOL> query = query . filter ( Technology . name == tech_name ) <EOL> for auditorsetting in query . all ( ) : <EOL> unjustified = [ issue for issue in auditorsetting . issues if not issue . justified ] <EOL> self . add_or_update_issue ( auditorsetting . issue_text , <EOL> auditorsetting . technology . name , <EOL> auditorsetting . account . name , <EOL> len ( unjustified ) ) </s>
