From f42e5f03df169f65936d5ff70c11810e7c7946ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 5 Feb 2015 18:51:17 +0100 Subject: [PATCH 1/2] Test required_contexts. --- test/test_app.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_app.py b/test/test_app.py index bfb3fc0..5e7f863 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -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.""" @@ -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 + From 5dbbd1578cdc2cc7c875ebf4a0a797dafd4bd170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Lombra=C3=B1a=20Gonz=C3=A1lez?= Date: Thu, 5 Feb 2015 18:51:57 +0100 Subject: [PATCH 2/2] Add support for required_contexts. This new feature allows you to specify from the config file the required_contexts that you want to check before creating the deployment. --- app.py | 31 +++++++++++++++++-------------- config.py.template | 14 ++++++-------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/app.py b/app.py index 67f508d..8eea432 100644 --- a/app.py +++ b/app.py @@ -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]) @@ -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 diff --git a/config.py.template b/config.py.template index dd22d91..a07b84c 100644 --- a/config.py.template +++ b/config.py.template @@ -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']]} +}