<s> import re <EOL> from config import * <EOL> from config import MESSAGES as _ <EOL> from requests import post , get <EOL> from requests . auth import HTTPBasicAuth <EOL> auth_token = HTTPBasicAuth ( GITHUB_USERNAME , GITHUB_TOKEN ) <EOL> reviewer_regex = re . compile ( LIST_REVIEWER_REGEX , re . I ) <EOL> approve_regex = re . compile ( LIST_APPROVE_REGEX , re . I ) <EOL> def get_instance ( * args ) : <EOL> """<STR_LIT>""" <EOL> if MODE == '<STR_LIT:list>' : <EOL> return ListReviewer ( * args ) <EOL> if MODE == '<STR_LIT>' : <EOL> return QuorumReviewer ( * args ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> class Reviewer ( object ) : <EOL> commit_status_url = '<STR_LIT>' <EOL> def __init__ ( self , pull_request_url , comments_url , body ) : <EOL> self . pull_request_url = pull_request_url <EOL> self . comments_url = comments_url <EOL> self . body = body <EOL> self . pr_cache = None <EOL> self . comments_cache = None <EOL> def get_pull_request ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . pr_cache : <EOL> self . pr_cache = get ( self . pull_request_url , auth = auth_token ) . json ( ) <EOL> return self . pr_cache <EOL> def get_comments ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . comments_cache : <EOL> self . comments_cache = get ( self . comments_url , auth = auth_token ) . json ( ) <EOL> return self . comments_cache <EOL> def set_commit_status ( self , status , description , repo , sha ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT:state>' : status , <EOL> '<STR_LIT:description>' : description , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> url = self . commit_status_url % { '<STR_LIT>' : repo , '<STR_LIT>' : sha } <EOL> return post ( url , json = data , auth = auth_token ) <EOL> def set_success_commit_status ( self , desc ) : <EOL> """<STR_LIT>""" <EOL> info = self . get_pull_request ( ) <EOL> sha = info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> repo = info [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return self . set_commit_status ( '<STR_LIT:success>' , desc , repo , sha ) <EOL> def set_pending_commit_status ( self , desc ) : <EOL> """<STR_LIT>""" <EOL> info = self . get_pull_request ( ) <EOL> sha = info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> repo = info [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return self . set_commit_status ( '<STR_LIT>' , desc , repo , sha ) <EOL> def review ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class ListReviewer ( Reviewer ) : <EOL> def get_reviewers ( self ) : <EOL> """<STR_LIT>""" <EOL> match = reviewer_regex . match ( self . body ) <EOL> if not match : <EOL> return [ ] <EOL> return [ x . strip ( '<STR_LIT>' ) for x in match . group ( <NUM_LIT:1> ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> def pending_reviewers ( self ) : <EOL> """<STR_LIT>""" <EOL> pending = self . get_reviewers ( ) <EOL> comments = self . get_comments ( ) <EOL> for comment in comments : <EOL> username = comment [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] <EOL> if username in pending and approve_regex . search ( comment [ '<STR_LIT:body>' ] ) : <EOL> pending . remove ( username ) <EOL> return pending <EOL> def review ( self ) : <EOL> """<STR_LIT>""" <EOL> pending_reviewers = self . pending_reviewers ( ) <EOL> if len ( pending_reviewers ) == <NUM_LIT:0> : <EOL> resp = self . set_success_commit_status ( _ [ '<STR_LIT>' ] ) <EOL> return '<STR_LIT>' , resp . status_code <EOL> msg = _ [ '<STR_LIT>' ] % '<STR_LIT:U+002CU+0020>' . join ( pending_reviewers ) <EOL> resp = self . set_pending_commit_status ( msg ) <EOL> return msg , resp . status_code <EOL> class QuorumReviewer ( Reviewer ) : <EOL> def pending_reviews ( self ) : <EOL> """<STR_LIT>""" <EOL> pending = QUORUM <EOL> comments = self . get_comments ( ) <EOL> for comment in comments : <EOL> username = comment [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] <EOL> if ( approve_regex . search ( comment [ '<STR_LIT:body>' ] ) <EOL> and ( username in QUORUM_USERS or len ( QUORUM_USERS ) == <NUM_LIT:0> ) ) : <EOL> pending = pending - <NUM_LIT:1> <EOL> return pending <EOL> def review ( self ) : <EOL> """<STR_LIT>""" <EOL> pending = self . pending_reviews ( ) <EOL> if pending == <NUM_LIT:0> : <EOL> msg = _ [ '<STR_LIT>' ] % QUORUM <EOL> resp = self . set_success_commit_status ( msg ) <EOL> return msg , resp . status_code <EOL> msg = _ [ '<STR_LIT>' ] % ( pending , QUORUM ) <EOL> resp = self . set_pending_commit_status ( msg ) <EOL> return msg , resp . status_code </s>
