<s> import json <EOL> import re <EOL> import github3 <EOL> import sqlalchemy <EOL> from flask import request , redirect , url_for <EOL> from kozmic import db , csrf <EOL> from kozmic . models import Project , Build , Hook , HookCall <EOL> from . import bp , tasks <EOL> def get_ref_and_sha ( payload ) : <EOL> action = payload . get ( '<STR_LIT:action>' ) <EOL> if action is None : <EOL> ref = payload . get ( '<STR_LIT>' ) <EOL> if not ref or not ref . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> prefix_length = len ( '<STR_LIT>' ) <EOL> ref = ref [ prefix_length : ] <EOL> sha = payload . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:id>' ) <EOL> if not sha : <EOL> return None <EOL> return ref , sha <EOL> elif action in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> gh_pull = github3 . pulls . PullRequest ( payload . get ( '<STR_LIT>' , { } ) ) <EOL> try : <EOL> return gh_pull . head . ref , gh_pull . head . sha <EOL> except : <EOL> return None <EOL> else : <EOL> return None <EOL> @ csrf . exempt <EOL> @ bp . route ( '<STR_LIT>' , methods = ( '<STR_LIT:POST>' , ) ) <EOL> def hook ( id ) : <EOL> def need_skip_build ( gh_commit , payload ) : <EOL> search_string = gh_commit . message <EOL> if '<STR_LIT>' in payload : <EOL> pr_title = payload [ '<STR_LIT>' ] [ '<STR_LIT:title>' ] or '<STR_LIT>' <EOL> pr_body = payload [ '<STR_LIT>' ] [ '<STR_LIT:body>' ] or '<STR_LIT>' <EOL> search_string += pr_title + pr_body <EOL> skip_regexp = re . compile ( '<STR_LIT>' , re . IGNORECASE ) <EOL> if skip_regexp . search ( search_string ) : <EOL> return True <EOL> else : <EOL> return False <EOL> hook = Hook . query . get_or_404 ( id ) <EOL> payload = json . loads ( request . data ) <EOL> if set ( payload . keys ( ) ) == { '<STR_LIT>' , '<STR_LIT>' } : <EOL> if hook . gh_id != payload [ '<STR_LIT>' ] : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> else : <EOL> return '<STR_LIT:OK>' <EOL> ref_and_sha = get_ref_and_sha ( payload ) <EOL> if not ref_and_sha : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> ref , sha = ref_and_sha <EOL> gh_commit = hook . project . gh . git_commit ( sha ) <EOL> if need_skip_build ( gh_commit , payload ) : <EOL> return '<STR_LIT:OK>' <EOL> build = hook . project . builds . filter ( <EOL> Build . gh_commit_ref == ref , <EOL> Build . gh_commit_sha == gh_commit . sha ) . first ( ) <EOL> if not build : <EOL> build = Build ( <EOL> project = hook . project , <EOL> status = '<STR_LIT>' , <EOL> gh_commit_ref = ref , <EOL> gh_commit_sha = gh_commit . sha , <EOL> gh_commit_author = gh_commit . author [ '<STR_LIT:name>' ] , <EOL> gh_commit_message = gh_commit . message ) <EOL> build . calculate_number ( ) <EOL> db . session . add ( build ) <EOL> hook_call = HookCall ( <EOL> hook = hook , <EOL> build = build , <EOL> gh_payload = payload ) <EOL> db . session . add ( hook_call ) <EOL> try : <EOL> db . session . commit ( ) <EOL> except sqlalchemy . exc . IntegrityError : <EOL> db . session . rollback ( ) <EOL> return '<STR_LIT:OK>' <EOL> tasks . do_job . delay ( hook_call_id = hook_call . id ) <EOL> return '<STR_LIT:OK>' <EOL> @ bp . route ( '<STR_LIT>' ) <EOL> def badge ( gh_login , gh_name , ref ) : <EOL> project = Project . query . filter_by ( <EOL> gh_login = gh_login , gh_name = gh_name ) . first_or_404 ( ) <EOL> build = project . get_latest_build ( ref = ref ) <EOL> badge = build and build . status or '<STR_LIT:success>' <EOL> response = redirect ( url_for ( <EOL> '<STR_LIT>' , <EOL> filename = '<STR_LIT>' . format ( badge ) , <EOL> _external = True , <EOL> _scheme = '<STR_LIT>' ) ) <EOL> response . status_code = <NUM_LIT> <EOL> return response </s>
