@@ -86,21 +86,44 @@ def index():
86
86
# Gather data
87
87
try :
88
88
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
+
89
111
meta = {
90
- 'name' : payload [ 'repository' ][ ' name' ] ,
91
- 'branch' : payload [ 'ref' ]. split ( '/' )[ 2 ] ,
112
+ 'name' : name ,
113
+ 'branch' : branch ,
92
114
'event' : event
93
115
}
94
116
except :
95
117
abort (400 )
96
118
97
119
# 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' ))
104
127
105
128
# Check permissions
106
129
scripts = [s for s in scripts if isfile (s ) and access (s , X_OK )]
0 commit comments