Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ def event_handler():
def process_deployment(deployment):
"""Process deployment."""
try:
for repo in config.REPOS:
if repo['repo'] == deployment['repository']['full_name']:
update_deployment(deployment, status='pending')
for command in repo['commands']:
p = Popen(command, cwd=repo['folder'], stderr=PIPE)
return_code = p.wait()
if return_code != 0:
raise CalledProcessError(return_code,
command,
output=p.communicate())
update_deployment(deployment, status='success')
return True
repo = config.REPOS.get(deployment['repository']['full_name'])
if repo:
update_deployment(deployment, status='pending')
for command in repo['commands']:
p = Popen(command, cwd=repo['folder'], stderr=PIPE)
return_code = p.wait()
if return_code != 0:
raise CalledProcessError(return_code,
command,
output=p.communicate())
update_deployment(deployment, status='success')
return True
# update_deployment(deployment, status='error')
except CalledProcessError as e:
message = "command: %s ERROR: %s" % (e.cmd, e.output[1])
Expand All @@ -81,16 +81,19 @@ def process_deployment(deployment):

def create_deployment(pull_request, token):
"""Create a deployment."""
repo = config.REPOS.get(pull_request['head']['repo']['full_name'])
user = pull_request['user']['login']
# owner = pull_request['head']['repo']['owner']['login']
repo = pull_request['head']['repo']['full_name']
repo_name = pull_request['head']['repo']['full_name']
payload = {'environment': 'production', 'deploy_user': user}
url = 'https://api.github.com/repos/%s/deployments' % (repo)
url = 'https://api.github.com/repos/%s/deployments' % (repo_name)
headers = {'Content-type': 'application/json'}
auth = (token, '')
data = {'ref': pull_request['head']['ref'],
'payload': payload,
'description': 'mydesc'}
if repo.get('required_contexts'):
data['required_contexts'] = repo.get('required_contexts')
deployment = requests.post(url, data=json.dumps(data), headers=headers,
auth=auth)
# print deployment
Expand Down
14 changes: 6 additions & 8 deletions config.py.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ DEBUG = False
SECRET = 'yoursecret'
TOKEN = 'your-github-token'
SLACK_WEBHOOK = 'yourslackwebhook'
REPOS = [{'repo': 'user/repo',
'folder': '/your/repo',
'commands': [
['git', 'fetch'],
['git', 'pull', 'origin', 'master']
]
}
]
REPOS = {
'user/repo': {'folder': '/repo',
'required_contexts': ["continuous-integration/travis-ci"],
'commands': [['git', 'fetch'],
['git', 'pull', 'origin', 'master']]}
}
14 changes: 14 additions & 0 deletions test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ def test_create_deployment(self, requests):
res = create_deployment(data, config.TOKEN)
assert res == deployment, res

@patch('app.requests')
def test_create_deployment_with_context(self, requests):
"""Test create_deployment with context works."""
repo = {'user/repo': {'folder': '/repo',
'required_contexts': ['ci/travis'],
'commands': [['ls']]}}
with patch('config.REPOS', repo):
data = pull_request_closed_merged['pull_request']
requests.post.return_value = deployment
res = create_deployment(data, config.TOKEN)
assert res == deployment, res


@patch('app.requests')
def test_update_deployment(self, requests):
"""Test create_deployment works."""
Expand Down Expand Up @@ -302,3 +315,4 @@ def test_authorize_case_6(self):
request.headers = {'X-Hub-Signature': signature}
res = authorize(request, config)
assert res is False, res