<s> from __future__ import absolute_import <EOL> __all__ = [ '<STR_LIT>' ] <EOL> from flask import current_app <EOL> from freight import http <EOL> from freight . exceptions import CheckFailed , CheckPending <EOL> from . base import Check <EOL> ERR_CHECK = '<STR_LIT>' <EOL> ERR_MISSING_CONTEXT = '<STR_LIT>' <EOL> class GitHubContextCheck ( Check ) : <EOL> def get_options ( self ) : <EOL> return { <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : True } , <EOL> } <EOL> def check ( self , app , sha , config ) : <EOL> token = current_app . config [ '<STR_LIT>' ] <EOL> if not token : <EOL> raise CheckFailed ( '<STR_LIT>' ) <EOL> api_root = ( <EOL> config . get ( '<STR_LIT>' ) or current_app . config [ '<STR_LIT>' ] <EOL> ) . rstrip ( '<STR_LIT:/>' ) <EOL> contexts = set ( config . get ( '<STR_LIT>' ) or [ ] ) <EOL> repo = config [ '<STR_LIT>' ] <EOL> url = '<STR_LIT>' . format ( <EOL> api_root = api_root , <EOL> repo = repo , <EOL> ref = sha , <EOL> ) <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( token ) , <EOL> } <EOL> resp = http . get ( url , headers = headers ) <EOL> context_list = resp . json ( ) <EOL> if not context_list : <EOL> raise CheckFailed ( '<STR_LIT>' ) <EOL> valid_contexts = set ( ) <EOL> for data in context_list : <EOL> if data [ '<STR_LIT:state>' ] == '<STR_LIT:success>' : <EOL> valid_contexts . add ( data [ '<STR_LIT>' ] ) <EOL> try : <EOL> contexts . remove ( data [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> pass <EOL> if data [ '<STR_LIT>' ] in valid_contexts : <EOL> continue <EOL> if contexts and data [ '<STR_LIT>' ] not in contexts : <EOL> continue <EOL> if data [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> raise CheckPending ( ERR_CHECK . format ( data [ '<STR_LIT>' ] , data [ '<STR_LIT:state>' ] ) ) <EOL> elif data [ '<STR_LIT:state>' ] != '<STR_LIT:success>' : <EOL> raise CheckFailed ( ERR_CHECK . format ( data [ '<STR_LIT>' ] , data [ '<STR_LIT:state>' ] ) ) <EOL> contexts . remove ( data [ '<STR_LIT>' ] ) <EOL> if contexts : <EOL> raise CheckFailed ( ERR_MISSING_CONTEXT . format ( iter ( contexts ) . next ( ) ) ) </s>
