(payload)
return result
Massage the github webhook payload into something a little more
    usable. Idea comes from gith by danheberden.
Massage the github webhook payload into something a little more
    usable. Idea comes from gith by danheberden.
Massage the github webhook payload into something a little more usable . Idea comes from gith by danheberden .
def simplify_payload(payload):
    """
    Massage the github webhook payload into something a little more
    usable. Idea comes from gith by danheberden.
    """
    result = {
        'branch': None,
        'tag': None,
        'pusher': None,
        'files': {
            'all': [],
            'added': [],
            'removed': [],
            'modified': []
        },
        'original': payload
    }

    # Try to find the branch/tag name from `ref`, falling back to `base_ref`.
    ref_r = re.compile(r'refs/(heads|tags)/(.*)$')
    for ref in (payload.get('ref', ''), payload.get('base_ref', '')):
        match = ref_r.match(ref)
        if match:
            type_, name = match.group(1, 2)
            result[{'heads': 'branch', 'tags': 'tag'}[type_]] = name
            break

    # Github (for whatever reason) doesn't always know the pusher. This field
    # is always missing/nil for commits generated by github itself, and for
    # web hooks coming from the "Test Hook" button.
    if 'pusher' in payload:
        result['pusher'] = payload['pusher'].get('name')
        # Github returns the string 'none' when a deploy key pushes
        if result['pusher'] == 'none':
            result['pusher'] = u'A deploy key'

    # Summarize file movement over all the commits.
    for commit in payload.get('commits', tuple()):
        for type_ in ('added', 'removed', 'modified'):
            result['files'][type_].extend(commit[type_])
            result['files']['all'].extend(commit[type_])

    return result
def simplify_payload ( payload ) : result = { 'branch' : None , 'tag' : None , 'pusher' : None , 'files' : { 'all' : [ ] , 'added' : [ ] , 'removed' : [ ] , 'modified' : [ ] } , 'original' : payload } # Try to find the branch/tag name from `ref`, falling back to `base_ref`. ref_r = re . compile ( r'refs/(heads|tags)/(.*)$' ) for ref in ( payload . get ( 'ref' , '' ) , payload . get ( 'base_ref' , '' ) ) : match = ref_r . match ( ref ) if match : type_ , name = match . group ( 1 , 2 ) result [ { 'heads' : 'branch' , 'tags' : 'tag' } [ type_ ] ] = name break # Github (for whatever reason) doesn't always know the pusher. This field # is always missing/nil for commits generated by github itself, and for # web hooks coming from the "Test Hook" button. if 'pusher' in payload : result [ 'pusher' ] = payload [ 'pusher' ] . get ( 'name' ) # Github returns the string 'none' when a deploy key pushes if result [ 'pusher' ] == 'none' : result [ 'pusher' ] = u'A deploy key' # Summarize file movement over all the commits. for commit in payload . get ( 'commits' , tuple ( ) ) : for type_ in ( 'added' , 'removed' , 'modified' ) : result [ 'files' ] [ type_ ] . extend ( commit [ type_ ] ) result [ 'files' ] [ 'all' ] . extend ( commit [ type_ ] ) return result