@@ -86,21 +86,44 @@ def index():
8686 # Gather data
8787 try :
8888 payload = loads (request .data )
89+
90+ # Determining the branch is tricky, as it only appears for certain event types an at different levels
91+ branch = None
92+ try :
93+ # Case 1: a ref_type indicates the type of ref. This is create and delete events.
94+ if 'ref_type' in payload :
95+ if payload ['ref_type' ] == 'branch' :
96+ branch = paylaod ['ref' ]
97+ # Case 2: a pull_request object is involved. This is pull_request and pull_request_review_comment events.
98+ elif 'pull_request' in payload :
99+ # This is the TARGET branch for the pull-request, not the source branch
100+ branch = payload ['pull_request' ]['base' ]['ref' ]
101+ elif event in ['push' ]:
102+ # Push events provide a full Git ref in 'ref' and not a 'ref_type'. Isn't that great?
103+ branch = payload ['ref' ].split ('/' )[2 ]
104+ except KeyError :
105+ # If the payload structure isn't what we expect, we'll live without the branch name
106+ pass
107+
108+ # All current events have a repository, but some legacy events do not, so let's be safe
109+ name = payload ['repository' ]['name' ] if 'repository' in payload else None
110+
89111 meta = {
90- 'name' : payload [ 'repository' ][ ' name' ] ,
91- 'branch' : payload [ 'ref' ]. split ( '/' )[ 2 ] ,
112+ 'name' : name ,
113+ 'branch' : branch ,
92114 'event' : event
93115 }
94116 except :
95117 abort (400 )
96118
97119 # Possible hooks
98- scripts = [
99- join (hooks , '{event}-{name}-{branch}' .format (** meta )),
100- join (hooks , '{event}-{name}' .format (** meta )),
101- join (hooks , '{event}' .format (** meta )),
102- join (hooks , 'all' )
103- ]
120+ scripts = []
121+ if branch and name :
122+ scripts .append (join (hooks , '{event}-{name}-{branch}' .format (** meta )))
123+ if name :
124+ scripts .append (join (hooks , '{event}-{name}' .format (** meta )))
125+ scripts .append (join (hooks , '{event}' .format (** meta )))
126+ scripts .append (join (hooks , 'all' ))
104127
105128 # Check permissions
106129 scripts = [s for s in scripts if isfile (s ) and access (s , X_OK )]
0 commit comments