<s> from __future__ import unicode_literals <EOL> import json <EOL> import logging <EOL> import os <EOL> from collections import defaultdict <EOL> from django import forms <EOL> from django . conf . urls import patterns , url <EOL> from django . http import HttpResponse <EOL> from django . utils import six <EOL> from django . utils . six . moves . urllib . error import HTTPError , URLError <EOL> from django . utils . six . moves . urllib . parse import quote <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . decorators . http import require_POST <EOL> from reviewboard . admin . server import get_server_url <EOL> from reviewboard . hostingsvcs . forms import HostingServiceForm <EOL> from reviewboard . hostingsvcs . hook_utils import ( close_all_review_requests , <EOL> get_review_request_id ) <EOL> from reviewboard . hostingsvcs . service import HostingService <EOL> from reviewboard . scmtools . crypto_utils import ( decrypt_password , <EOL> encrypt_password ) <EOL> from reviewboard . scmtools . errors import FileNotFoundError <EOL> class BeanstalkForm ( HostingServiceForm ) : <EOL> beanstalk_account_domain = forms . CharField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> max_length = <NUM_LIT:64> , <EOL> required = True , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT:size>' : '<STR_LIT>' } ) , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> beanstalk_repo_name = forms . CharField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> max_length = <NUM_LIT:64> , <EOL> required = True , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT:size>' : '<STR_LIT>' } ) ) <EOL> class Beanstalk ( HostingService ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> needs_authorization = True <EOL> supports_bug_trackers = False <EOL> supports_repositories = True <EOL> supported_scmtools = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> form = BeanstalkForm <EOL> repository_fields = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:path>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:path>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } <EOL> repository_url_patterns = patterns ( <EOL> '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> def check_repository ( self , beanstalk_account_domain = None , <EOL> beanstalk_repo_name = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _api_get_repository ( beanstalk_account_domain , beanstalk_repo_name ) <EOL> def authorize ( self , username , password , hosting_url , <EOL> local_site_name = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . account . data [ '<STR_LIT:password>' ] = encrypt_password ( password ) <EOL> self . account . save ( ) <EOL> def is_authorized ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . account . data . get ( '<STR_LIT:password>' , None ) is not None <EOL> def get_password ( self ) : <EOL> """<STR_LIT>""" <EOL> return decrypt_password ( self . account . data [ '<STR_LIT:password>' ] ) <EOL> def get_file ( self , repository , path , revision , base_commit_id = None , <EOL> * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _api_get_node ( repository , path , revision , <EOL> base_commit_id , contents = True ) <EOL> except ( HTTPError , URLError ) : <EOL> raise FileNotFoundError ( path , revision ) <EOL> def get_file_exists ( self , repository , path , revision , base_commit_id = None , <EOL> * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _api_get_node ( repository , path , revision , base_commit_id ) <EOL> return True <EOL> except ( HTTPError , URLError , FileNotFoundError ) : <EOL> return False <EOL> def _api_get_repository ( self , account_domain , repository_name ) : <EOL> url = self . _build_api_url ( account_domain , <EOL> '<STR_LIT>' % repository_name ) <EOL> return self . _api_get ( url ) <EOL> def _api_get_node ( self , repository , path , revision , base_commit_id , <EOL> contents = False ) : <EOL> is_git = ( repository . tool . name == '<STR_LIT>' ) <EOL> if is_git and ( contents or not base_commit_id ) : <EOL> url_path = ( '<STR_LIT>' <EOL> % ( quote ( revision ) , quote ( os . path . basename ( path ) ) ) ) <EOL> raw_content = True <EOL> else : <EOL> if is_git : <EOL> expected_revision = base_commit_id <EOL> else : <EOL> expected_revision = revision <EOL> url_path = ( '<STR_LIT>' <EOL> % ( quote ( path ) , quote ( expected_revision ) ) ) <EOL> if contents : <EOL> url_path += '<STR_LIT>' <EOL> raw_content = False <EOL> url = self . _build_api_url ( <EOL> self . _get_repository_account_domain ( repository ) , <EOL> '<STR_LIT>' <EOL> % ( repository . extra_data [ '<STR_LIT>' ] , url_path ) ) <EOL> result = self . _api_get ( url , raw_content = raw_content ) <EOL> if not raw_content and contents : <EOL> result = result [ '<STR_LIT>' ] <EOL> return result <EOL> def _build_api_url ( self , account_domain , url ) : <EOL> return '<STR_LIT>' % ( account_domain , url ) <EOL> def _get_repository_account_domain ( self , repository ) : <EOL> return repository . extra_data [ '<STR_LIT>' ] <EOL> def _api_get ( self , url , raw_content = False ) : <EOL> try : <EOL> data , headers = self . client . http_get ( <EOL> url , <EOL> username = self . account . username , <EOL> password = self . get_password ( ) ) <EOL> if raw_content : <EOL> return data <EOL> else : <EOL> return json . loads ( data ) <EOL> except HTTPError as e : <EOL> data = e . read ( ) <EOL> try : <EOL> rsp = json . loads ( data ) <EOL> except : <EOL> rsp = None <EOL> if rsp and '<STR_LIT>' in rsp : <EOL> raise Exception ( '<STR_LIT>' . join ( rsp [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> raise Exception ( six . text_type ( e ) ) <EOL> @ require_POST <EOL> def process_post_receive_hook ( request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> server_url = get_server_url ( request = request ) <EOL> if '<STR_LIT>' in request . POST : <EOL> payload = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> close_git_review_requests ( payload , server_url ) <EOL> else : <EOL> payload = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> close_svn_review_request ( payload , server_url ) <EOL> except KeyError as e : <EOL> logging . error ( '<STR_LIT>' , e ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> except ValueError as e : <EOL> logging . error ( '<STR_LIT>' , e ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> return HttpResponse ( ) <EOL> def close_git_review_requests ( payload , server_url ) : <EOL> """<STR_LIT>""" <EOL> review_id_to_commits_map = defaultdict ( list ) <EOL> branch_name = payload . get ( '<STR_LIT>' ) <EOL> if not branch_name : <EOL> return review_id_to_commits_map <EOL> commits = payload . get ( '<STR_LIT>' , [ ] ) <EOL> for commit in commits : <EOL> commit_hash = commit . get ( '<STR_LIT:id>' ) <EOL> commit_message = commit . get ( '<STR_LIT:message>' ) <EOL> review_request_id = get_review_request_id ( commit_message , server_url , <EOL> commit_hash ) <EOL> commit_entry = '<STR_LIT>' % ( branch_name , commit_hash [ : <NUM_LIT:7> ] ) <EOL> review_id_to_commits_map [ review_request_id ] . append ( commit_entry ) <EOL> close_all_review_requests ( review_id_to_commits_map ) <EOL> def close_svn_review_request ( payload , server_url ) : <EOL> """<STR_LIT>""" <EOL> review_id_to_commits_map = defaultdict ( list ) <EOL> commit_message = payload . get ( '<STR_LIT:message>' ) <EOL> branch_name = payload . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> revision = '<STR_LIT>' % ( '<STR_LIT>' , payload . get ( '<STR_LIT>' ) ) <EOL> review_request_id = get_review_request_id ( commit_message , server_url , <EOL> None ) <EOL> commit_entry = '<STR_LIT>' % ( branch_name , revision ) <EOL> review_id_to_commits_map [ review_request_id ] . append ( commit_entry ) <EOL> close_all_review_requests ( review_id_to_commits_map ) </s>
