<s> import os <EOL> import re <EOL> import json <EOL> import yaml <EOL> import requests <EOL> from github import Github <EOL> def get_config ( ) : <EOL> config_files = ( <EOL> '<STR_LIT>' , <EOL> os . path . expanduser ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> for config_file in config_files : <EOL> try : <EOL> with open ( os . path . realpath ( config_file ) ) as f : <EOL> config = yaml . load ( f ) <EOL> except : <EOL> pass <EOL> else : <EOL> return config <EOL> raise SystemExit ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( config_files ) ) <EOL> def alert ( config , item , known , repo ) : <EOL> if repo not in known : <EOL> known [ repo ] = [ ] <EOL> known [ repo ] . append ( item . number ) <EOL> return requests . post ( <EOL> '<STR_LIT>' % config [ '<STR_LIT>' ] , <EOL> auth = ( '<STR_LIT>' , config [ '<STR_LIT>' ] ) , <EOL> data = { <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT:to>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : config . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:text>' : ( '<STR_LIT>' % <EOL> ( item . html_url , item . title , item . user . login ) ) <EOL> } <EOL> ) <EOL> def scan_github_issues ( config ) : <EOL> pattern = re . compile ( config [ '<STR_LIT>' ] , flags = re . I ) <EOL> os . chdir ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> try : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> known = json . load ( f ) <EOL> except IOError , ValueError : <EOL> known = { } <EOL> g = Github ( client_id = config [ '<STR_LIT>' ] , <EOL> client_secret = config [ '<STR_LIT>' ] , <EOL> per_page = <NUM_LIT:100> ) <EOL> if not isinstance ( config [ '<STR_LIT>' ] , list ) : <EOL> repos = [ config [ '<STR_LIT>' ] ] <EOL> else : <EOL> repos = config [ '<STR_LIT>' ] <EOL> for repo_name in repos : <EOL> repo = g . get_repo ( repo_name ) <EOL> for pull in repo . get_pulls ( ) : <EOL> if pull . number in known . get ( repo_name , [ ] ) : <EOL> continue <EOL> try : <EOL> if pattern . search ( pull . title ) : <EOL> alert ( config , pull , known , repo_name ) <EOL> continue <EOL> except TypeError : <EOL> pass <EOL> try : <EOL> if pattern . search ( pull . body ) : <EOL> alert ( config , pull , known , repo_name ) <EOL> continue <EOL> except TypeError : <EOL> pass <EOL> for pull_file in pull . get_files ( ) : <EOL> if pattern . search ( pull_file . filename ) : <EOL> alert ( config , pull , known , repo_name ) <EOL> break <EOL> for comment in pull . get_comments ( ) : <EOL> try : <EOL> if pattern . search ( comment . body ) : <EOL> alert ( config , pull , known , repo_name ) <EOL> break <EOL> except TypeError : <EOL> pass <EOL> for commit in pull . get_commits ( ) : <EOL> try : <EOL> if pattern . search ( commit . commit . message ) : <EOL> alert ( config , pull , known , repo_name ) <EOL> break <EOL> except TypeError : <EOL> pass <EOL> for comment in pull . get_issue_comments ( ) : <EOL> try : <EOL> if pattern . search ( comment . body ) : <EOL> alert ( config , pull , known , repo_name ) <EOL> break <EOL> except TypeError : <EOL> pass <EOL> for issue in repo . get_issues ( ) : <EOL> if issue . number in known . get ( repo_name , [ ] ) : <EOL> continue <EOL> if issue . pull_request is not None : <EOL> continue <EOL> try : <EOL> if pattern . search ( issue . title ) : <EOL> alert ( config , issue , known , repo_name ) <EOL> continue <EOL> except TypeError : <EOL> pass <EOL> try : <EOL> if pattern . search ( issue . body ) : <EOL> alert ( config , issue , known , repo_name ) <EOL> continue <EOL> except TypeError : <EOL> pass <EOL> for comment in issue . get_comments ( ) : <EOL> try : <EOL> if pattern . search ( comment . body ) : <EOL> alert ( config , issue , known , repo_name ) <EOL> break <EOL> except TypeError : <EOL> pass <EOL> try : <EOL> with open ( '<STR_LIT>' , '<STR_LIT>' ) as f : <EOL> json . dump ( known , f , indent = <NUM_LIT:4> , sort_keys = True ) <EOL> except ( IOError , ValueError ) : <EOL> pass <EOL> def main ( ) : <EOL> scan_github_issues ( get_config ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
