<s> import json <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db . models import Q <EOL> from djblets . db . query import LocalDataQuerySet <EOL> from djblets . util . decorators import augment_method_from <EOL> from djblets . webapi . decorators import ( webapi_login_required , <EOL> webapi_request_fields , <EOL> webapi_response_errors ) <EOL> from djblets . webapi . errors import ( DOES_NOT_EXIST , <EOL> INVALID_FORM_DATA , <EOL> NOT_LOGGED_IN , <EOL> PERMISSION_DENIED ) <EOL> from reviewboard . diffviewer . models import FileDiff <EOL> from reviewboard . extensions . base import get_extension_manager <EOL> from reviewboard . reviews . models import BaseComment , Review <EOL> from reviewboard . webapi . decorators import webapi_check_local_site <EOL> from reviewboard . webapi . resources import resources , WebAPIResource <EOL> from reviewbotext . models import ManualPermission , Profile , Tool , ToolExecution <EOL> EXTENSION_MANAGER = get_extension_manager ( ) <EOL> class ToolResource ( WebAPIResource ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , ) <EOL> model_object_key = '<STR_LIT:id>' <EOL> uri_object_key = '<STR_LIT>' <EOL> @ webapi_login_required <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , INVALID_FORM_DATA , <EOL> NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> required = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> ) <EOL> def create ( self , request , hostname , tools , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from reviewbotext . extension import ReviewBotExtension <EOL> extension = EXTENSION_MANAGER . get_enabled_extension ( <EOL> ReviewBotExtension . id ) <EOL> if request . user . id != extension . settings [ '<STR_LIT:user>' ] : <EOL> return PERMISSION_DENIED <EOL> try : <EOL> tools = json . loads ( tools ) <EOL> except : <EOL> return INVALID_FORM_DATA , { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> for tool in tools : <EOL> obj , created = Tool . objects . get_or_create ( <EOL> entry_point = tool [ '<STR_LIT>' ] , <EOL> version = tool [ '<STR_LIT:version>' ] , <EOL> defaults = { <EOL> '<STR_LIT:name>' : tool [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:description>' : tool [ '<STR_LIT:description>' ] , <EOL> '<STR_LIT>' : tool [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> if not created and not obj . in_last_update : <EOL> obj . in_last_update = True <EOL> obj . save ( ) <EOL> return <NUM_LIT> , { } <EOL> tool_resource = ToolResource ( ) <EOL> class ToolExecutionResource ( WebAPIResource ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> model = ToolExecution <EOL> model_object_key = '<STR_LIT:id>' <EOL> uri_object_key = '<STR_LIT>' <EOL> fields = { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:status>' : { <EOL> '<STR_LIT:type>' : ( '<STR_LIT>' , '<STR_LIT:R>' , '<STR_LIT:S>' , '<STR_LIT:F>' , '<STR_LIT:T>' ) , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } <EOL> last_modified_field = '<STR_LIT>' <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' ) <EOL> def has_access_permissions ( self , request , tool_execution , * args , ** kwargs ) : <EOL> return _review_request_is_accessible ( <EOL> request , <EOL> tool_execution . review_request_id , <EOL> tool_execution . diff_revision , <EOL> * args , ** kwargs ) <EOL> def has_list_access_permissions ( self , request , * args , ** kwargs ) : <EOL> return _review_request_is_accessible ( <EOL> request , <EOL> request . GET . get ( '<STR_LIT>' ) , <EOL> request . GET . get ( '<STR_LIT>' ) , <EOL> * args , ** kwargs ) <EOL> def has_modify_permissions ( self , request , * args , ** kwargs ) : <EOL> from reviewbotext . extension import ReviewBotExtension <EOL> extension = EXTENSION_MANAGER . get_enabled_extension ( <EOL> ReviewBotExtension . id ) <EOL> if request . user . id == extension . settings [ '<STR_LIT:user>' ] : <EOL> return True <EOL> return False <EOL> def has_create_permissions ( self , request , review_request , profile , * args , <EOL> ** kwargs ) : <EOL> from reviewbotext . extension import ReviewBotExtension <EOL> extension = EXTENSION_MANAGER . get_enabled_extension ( <EOL> ReviewBotExtension . id ) <EOL> user = request . user <EOL> if user . id == extension . settings [ '<STR_LIT:user>' ] : <EOL> return True <EOL> if not review_request . is_accessible_by ( user ) : <EOL> return False <EOL> local_site = self . _get_local_site ( kwargs . get ( '<STR_LIT>' ) ) <EOL> is_admin = user . is_superuser <EOL> is_submitter = ( user == review_request . submitter ) <EOL> is_in_manual_group = ManualPermission . objects . filter ( <EOL> user = user , local_site = local_site , allow = True ) . exists ( ) <EOL> if ( ( profile . allow_manual and is_admin ) or <EOL> ( profile . allow_manual_submitter and is_submitter ) or <EOL> ( profile . allow_manual_group and is_in_manual_group ) ) : <EOL> return True <EOL> return False <EOL> def get_queryset ( self , request , is_list = False , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if is_list : <EOL> review_request_id = request . GET . get ( '<STR_LIT>' ) <EOL> diff_revision = request . GET . get ( '<STR_LIT>' ) <EOL> q = Q ( ) <EOL> if '<STR_LIT:status>' in request . GET : <EOL> statuses = request . GET . get ( '<STR_LIT:status>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> for status in statuses : <EOL> if status in ( u'<STR_LIT>' , u'<STR_LIT:R>' , u'<STR_LIT:S>' , u'<STR_LIT:F>' , u'<STR_LIT:T>' ) : <EOL> q = q | Q ( status = status ) <EOL> queryset = self . model . objects . filter ( <EOL> q , <EOL> review_request_id = review_request_id , <EOL> diff_revision = diff_revision ) <EOL> if '<STR_LIT>' in request . GET : <EOL> get_latest = request . GET . get ( '<STR_LIT>' ) <EOL> if get_latest in ( '<STR_LIT:1>' , '<STR_LIT:true>' , '<STR_LIT:True>' ) : <EOL> profiles = set ( ) <EOL> tool_executions = [ ] <EOL> for tool_execution in queryset : <EOL> profile = tool_execution . profile . id <EOL> if profile not in profiles : <EOL> profiles . add ( profile ) <EOL> tool_executions . append ( tool_execution ) <EOL> queryset = LocalDataQuerySet ( tool_executions ) <EOL> return queryset <EOL> else : <EOL> return self . model . objects . all ( ) <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> required = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> optional = { <EOL> '<STR_LIT:status>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> ) <EOL> @ augment_method_from ( WebAPIResource ) <EOL> def get_list ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , PERMISSION_DENIED ) <EOL> @ augment_method_from ( WebAPIResource ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ webapi_login_required <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> required = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> ) <EOL> def create ( self , request , review_request_id , diff_revision , profile_id , <EOL> * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> review_request = resources . review_request . get_object ( <EOL> request , review_request_id = review_request_id , * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return DOES_NOT_EXIST <EOL> if not _diff_revision_exists ( review_request , diff_revision ) : <EOL> return DOES_NOT_EXIST <EOL> try : <EOL> profile = Profile . objects . get ( pk = profile_id ) <EOL> except ObjectDoesNotExist : <EOL> return DOES_NOT_EXIST <EOL> if not self . has_create_permissions ( request , review_request , profile ) : <EOL> return self . _no_access_error ( request . user ) <EOL> execution_exists = self . model . objects . filter ( <EOL> Q ( status = '<STR_LIT>' ) | Q ( status = '<STR_LIT:R>' ) | Q ( status = '<STR_LIT:S>' ) , <EOL> profile = profile , <EOL> review_request_id = review_request_id , <EOL> diff_revision = diff_revision ) . exists ( ) <EOL> if execution_exists : <EOL> return <NUM_LIT> , { } <EOL> tool_execution = self . model . objects . create ( <EOL> profile = profile , <EOL> review_request_id = review_request_id , <EOL> diff_revision = diff_revision , <EOL> status = '<STR_LIT>' ) <EOL> tool_execution . save ( ) <EOL> from reviewbotext . extension import ReviewBotExtension <EOL> extension = EXTENSION_MANAGER . get_enabled_extension ( <EOL> ReviewBotExtension . id ) <EOL> request_payload = { <EOL> '<STR_LIT>' : tool_execution . id , <EOL> '<STR_LIT>' : profile_id , <EOL> '<STR_LIT>' : review_request_id , <EOL> '<STR_LIT>' : diff_revision , <EOL> } <EOL> extension . notify ( request_payload ) <EOL> return <NUM_LIT> , { <EOL> self . item_result_key : tool_execution , <EOL> } <EOL> @ webapi_login_required <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> optional = { <EOL> '<STR_LIT:status>' : { <EOL> '<STR_LIT:type>' : ( '<STR_LIT>' , '<STR_LIT:R>' , '<STR_LIT:S>' , '<STR_LIT:F>' , '<STR_LIT:T>' ) , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> ) <EOL> def update ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_modify_permissions ( request ) : <EOL> return self . _no_access_error ( request . user ) <EOL> try : <EOL> tool_execution = self . get_object ( request , * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return DOES_NOT_EXIST <EOL> for field in ( '<STR_LIT:status>' , '<STR_LIT:result>' ) : <EOL> value = kwargs . get ( field , None ) <EOL> if value is not None : <EOL> setattr ( tool_execution , field , value ) <EOL> tool_execution . save ( ) <EOL> return <NUM_LIT:200> , { <EOL> self . item_result_key : tool_execution , <EOL> } <EOL> tool_execution_resource = ToolExecutionResource ( ) <EOL> class ToolExecutableResource ( WebAPIResource ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> model = Profile <EOL> model_object_key = '<STR_LIT:id>' <EOL> fields = { <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:name>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> allowed_methods = ( '<STR_LIT:GET>' , ) <EOL> def has_list_access_permissions ( self , request , * args , ** kwargs ) : <EOL> return _review_request_is_accessible ( <EOL> request , <EOL> request . GET . get ( '<STR_LIT>' ) , <EOL> request . GET . get ( '<STR_LIT>' ) , <EOL> * args , ** kwargs ) <EOL> def get_queryset ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = request . user <EOL> review_request_id = request . GET . get ( '<STR_LIT>' ) <EOL> diff_revision = request . GET . get ( '<STR_LIT>' ) <EOL> local_site = self . _get_local_site ( kwargs . get ( '<STR_LIT>' ) ) <EOL> is_admin = user . is_superuser <EOL> is_submitter = ( user == resources . review_request . get_object ( <EOL> request , review_request_id = review_request_id , * args , ** kwargs <EOL> ) . submitter ) <EOL> is_in_manual_group = ManualPermission . objects . filter ( <EOL> user = user , local_site = local_site , allow = True ) . exists ( ) <EOL> if not ( is_admin or is_submitter or is_in_manual_group ) : <EOL> return Profile . objects . none ( ) <EOL> q = Q ( ) <EOL> if is_admin : <EOL> q = q | Q ( allow_manual = True ) <EOL> if is_submitter : <EOL> q = q | Q ( allow_manual_submitter = True ) <EOL> if is_in_manual_group : <EOL> q = q | Q ( allow_manual_group = True ) <EOL> queryset = Profile . objects . filter ( q , local_site = local_site ) <EOL> queryset = queryset . exclude ( <EOL> ( Q ( toolexecution__review_request_id = review_request_id ) & <EOL> Q ( toolexecution__diff_revision = diff_revision ) ) & <EOL> ( Q ( toolexecution__status = '<STR_LIT>' ) | <EOL> Q ( toolexecution__status = '<STR_LIT:R>' ) | <EOL> Q ( toolexecution__status = '<STR_LIT:S>' ) ) <EOL> ) <EOL> return queryset <EOL> def get_serializer_for_object ( self , obj ) : <EOL> return self <EOL> @ webapi_login_required <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> required = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> ) <EOL> @ augment_method_from ( WebAPIResource ) <EOL> def get_list ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> tool_executable_resource = ToolExecutableResource ( ) <EOL> class ReviewBotReviewResource ( WebAPIResource ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , ) <EOL> @ webapi_login_required <EOL> @ webapi_check_local_site <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , INVALID_FORM_DATA , <EOL> NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> required = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> optional = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : str , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> ) <EOL> def create ( self , request , review_request_id , ship_it = False , body_top = '<STR_LIT>' , <EOL> body_bottom = '<STR_LIT>' , diff_comments = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> review_request = resources . review_request . get_object ( <EOL> request , <EOL> review_request_id = review_request_id , <EOL> * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return DOES_NOT_EXIST <EOL> if not body_top : <EOL> body_top = '<STR_LIT>' <EOL> if not body_bottom : <EOL> body_bottom = '<STR_LIT>' <EOL> new_review = Review . objects . create ( <EOL> review_request = review_request , <EOL> user = request . user , <EOL> body_top = body_top , <EOL> body_bottom = body_bottom , <EOL> ship_it = ship_it ) <EOL> if diff_comments : <EOL> try : <EOL> diff_comments = json . loads ( diff_comments ) <EOL> for comment in diff_comments : <EOL> filediff = FileDiff . objects . get ( <EOL> pk = comment [ '<STR_LIT>' ] , <EOL> diffset__history__review_request = review_request ) <EOL> if comment [ '<STR_LIT>' ] : <EOL> issue = True <EOL> issue_status = BaseComment . OPEN <EOL> else : <EOL> issue = False <EOL> issue_status = None <EOL> new_review . comments . create ( <EOL> filediff = filediff , <EOL> interfilediff = None , <EOL> text = comment [ '<STR_LIT:text>' ] , <EOL> first_line = comment [ '<STR_LIT>' ] , <EOL> num_lines = comment [ '<STR_LIT>' ] , <EOL> issue_opened = issue , <EOL> issue_status = issue_status ) <EOL> except KeyError : <EOL> return INVALID_FORM_DATA , { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> except ObjectDoesNotExist : <EOL> return INVALID_FORM_DATA , { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> new_review . publish ( user = request . user ) <EOL> return <NUM_LIT> , { <EOL> self . item_result_key : new_review , <EOL> } <EOL> review_bot_review_resource = ReviewBotReviewResource ( ) <EOL> def _review_request_is_accessible ( request , review_request_id , diff_revision , <EOL> * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> review_request = resources . review_request . get_object ( <EOL> request , review_request_id = review_request_id , * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return False <EOL> if not _diff_revision_exists ( review_request , diff_revision ) : <EOL> return False <EOL> return review_request . is_accessible_by ( request . user ) <EOL> def _diff_revision_exists ( review_request , diff_revision ) : <EOL> """<STR_LIT>""" <EOL> num_diffs = review_request . diffset_history . diffsets . get_query_set ( ) . count ( ) <EOL> diff_revision = int ( diff_revision ) <EOL> if diff_revision <= <NUM_LIT:0> or diff_revision > num_diffs : <EOL> return False <EOL> return True </s>
