<s> from __future__ import absolute_import , print_function <EOL> from hashlib import sha256 <EOL> import hmac <EOL> import logging <EOL> from simplejson import JSONDecodeError <EOL> from django . http import HttpResponse <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . views . generic import View <EOL> from django . utils . crypto import constant_time_compare <EOL> from django . utils . decorators import method_decorator <EOL> from sentry . api import client <EOL> from sentry . models import ApiKey , Project , ProjectOption <EOL> from sentry . plugins import plugins <EOL> from sentry . utils import json <EOL> class ReleaseWebhookView ( View ) : <EOL> def verify ( self , plugin_id , project_id , token , signature ) : <EOL> return constant_time_compare ( signature , hmac . new ( <EOL> key = str ( token ) , <EOL> msg = '<STR_LIT>' . format ( plugin_id , project_id ) , <EOL> digestmod = sha256 <EOL> ) . hexdigest ( ) ) <EOL> @ method_decorator ( csrf_exempt ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( ReleaseWebhookView , self ) . dispatch ( * args , ** kwargs ) <EOL> def _handle_builtin ( self , request , project ) : <EOL> endpoint = '<STR_LIT>' . format ( <EOL> project . organization . slug , <EOL> project . slug , <EOL> ) <EOL> try : <EOL> data = json . loads ( request . body ) <EOL> except JSONDecodeError as exc : <EOL> return HttpResponse ( <EOL> status = <NUM_LIT> , <EOL> content = json . dumps ( { '<STR_LIT:error>' : unicode ( exc ) } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> try : <EOL> god = ApiKey ( <EOL> organization = project . organization , <EOL> scopes = getattr ( ApiKey . scopes , '<STR_LIT>' ) , <EOL> ) <EOL> resp = client . post ( <EOL> endpoint , <EOL> data = data , <EOL> auth = god , <EOL> ) <EOL> except client . ApiError as exc : <EOL> return HttpResponse ( <EOL> status = exc . status_code , <EOL> content = exc . body , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> return HttpResponse ( <EOL> status = resp . status_code , <EOL> content = json . dumps ( resp . data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def post ( self , request , plugin_id , project_id , signature ) : <EOL> project = Project . objects . get_from_cache ( id = project_id ) <EOL> token = ProjectOption . objects . get_value ( project , '<STR_LIT>' ) <EOL> logging . info ( '<STR_LIT>' , <EOL> project_id , plugin_id ) <EOL> if not self . verify ( plugin_id , project_id , token , signature ) : <EOL> logging . warn ( '<STR_LIT>' ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> if plugin_id == '<STR_LIT>' : <EOL> return self . _handle_builtin ( request , project ) <EOL> plugin = plugins . get ( plugin_id ) <EOL> if not plugin . is_enabled ( project ) : <EOL> logging . warn ( '<STR_LIT>' , <EOL> project_id , plugin_id ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> cls = plugin . get_release_hook ( ) <EOL> hook = cls ( project ) <EOL> hook . handle ( request ) <EOL> return HttpResponse ( status = <NUM_LIT> ) </s>
