Skip to content

Commit b08f5cb

Browse files
Fixed style and improved application flow.
1 parent fb70db9 commit b08f5cb

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

webhooks.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2014 Carlos Jenkins <[email protected]>
3+
# Copyright (C) 2014, 2015 Carlos Jenkins <[email protected]>
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -86,36 +86,46 @@ 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 = payload['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-
111-
meta = {
112-
'name': name,
113-
'branch': branch,
114-
'event': event
115-
}
11689
except:
11790
abort(400)
11891

92+
# Determining the branch is tricky, as it only appears for certain event
93+
# types an at different levels
94+
branch = None
95+
try:
96+
# Case 1: a ref_type indicates the type of ref.
97+
# This true for create and delete events.
98+
if 'ref_type' in payload:
99+
if payload['ref_type'] == 'branch':
100+
branch = payload['ref']
101+
102+
# Case 2: a pull_request object is involved. This is pull_request and
103+
# pull_request_review_comment events.
104+
elif 'pull_request' in payload:
105+
# This is the TARGET branch for the pull-request, not the source
106+
# branch
107+
branch = payload['pull_request']['base']['ref']
108+
109+
elif event in ['push']:
110+
# Push events provide a full Git ref in 'ref' and not a 'ref_type'.
111+
branch = payload['ref'].split('/')[2]
112+
113+
except KeyError:
114+
# If the payload structure isn't what we expect, we'll live without
115+
# the branch name
116+
pass
117+
118+
# All current events have a repository, but some legacy events do not,
119+
# so let's be safe
120+
name = payload['repository']['name'] if 'repository' in payload else None
121+
122+
meta = {
123+
'name': name,
124+
'branch': branch,
125+
'event': event
126+
}
127+
logging.info('Metadata:\n{}'.format(dumps(meta)))
128+
119129
# Possible hooks
120130
scripts = []
121131
if branch and name:

0 commit comments

Comments
 (0)