diff --git a/.travis.yml b/.travis.yml index bc532201..6bdd1257 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,9 @@ virtualenv: before_install: - npm install -g casperjs - - pip install nose install: - - pip install -r requirements.txt -t lib/ - - pip install -r local_requirements.txt + - pip install -r requirements/requirements.txt -t lib/ + - pip install -r requirements/local_requirements.txt before_script: - openssl aes-256-cbc -K $encrypted_2fd045226a67_key -iv $encrypted_2fd045226a67_iv @@ -40,3 +39,6 @@ deploy: on: all_branches: true repo: sympy/sympy_gamma + +after_deploy: + - python bin/update_status_on_pr.py diff --git a/README.rst b/README.rst index c4159fd2..9d44a187 100644 --- a/README.rst +++ b/README.rst @@ -53,14 +53,14 @@ The project depends on some third-party libraries that are not on the list of built-in libraries (in app.yaml) bundled with the runtime, to install them run the following command.:: - pip install -r requirements.txt -t lib/ + pip install -r requirements/requirements.txt -t lib/ Some libraries although available on app engine runtime, but needs to be installed locally for development. Ref: https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#local_development :: - pip install -r local_requirements.txt + pip install -r requirements/local_requirements.txt Development server ------------------ diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py new file mode 100644 index 00000000..f66af031 --- /dev/null +++ b/bin/update_status_on_pr.py @@ -0,0 +1,69 @@ +""" +This script runs only on travis to create the status for the latest commit +in the PR. + +Reference: https://developer.github.com/v3/repos/statuses/#create-a-status +""" +import os +import requests + +GITHUB_REPO = 'sympy/sympy_gamma' +GITHUB_API_URL = 'https://api.github.com' +GITHUB_API_REF_URL = "%s/repos/%s/git/matching-refs/heads/" % (GITHUB_API_URL, GITHUB_REPO) +GITHUB_API_UPDATE_STATUS_URL = "%s/repos/%s/statuses/" % (GITHUB_API_URL, GITHUB_REPO) +SYMPY_BOT_TOKEN_VAR = 'SYMPY_BOT_TOKEN' + + +def get_branch_commit_sha(branch_name): + """Gets the SHA of the last commit of the given branch + :param branch_name: str name of branch on Github + :return: str SHA + """ + response = requests.get(GITHUB_API_REF_URL + branch_name) + if response.status_code == 200: + response_json = response.json() + else: + raise ValueError('Invalid response from github API') + return response_json[0]['object']['sha'] + + +def update_pr_status_with_deployment(branch_name, commit_sha): + """Updates the Status of the commit identified by commit SHA, which is reflected + at the bottom of the PR, above merge button. + :param branch_name: str name of branch on github + :param commit_sha: str SHA + :return: Response POST request to Github API + """ + sympy_bot_token = os.environ.get(SYMPY_BOT_TOKEN_VAR) + deployment_url = "https://%s-dot-sympy-gamma-hrd.appspot.com" % branch_name + payload = { + "state": "success", + "target_url": deployment_url, + "description": "Deployed to version: %s" % branch_name, + "context": "PR Deployment" + } + + headers = { + 'Authorization': 'Bearer %s' % sympy_bot_token, + 'Content-Type': 'application/json' + } + + update_status_url = GITHUB_API_UPDATE_STATUS_URL + commit_sha + print "Update status URL: %s" % update_status_url + response = requests.post(update_status_url, headers=headers, json=payload) + print "Response: %s" % response.json() + + +def main(): + is_on_travis = os.environ.get('TRAVIS') + if not is_on_travis: + raise ValueError('This script run only on travis!') + branch_name = os.environ.get('TRAVIS_BRANCH') + commit_sha = get_branch_commit_sha(branch_name) + print "Branch name: %s Commit SHA: %s" % (branch_name, commit_sha) + print "Creating commit status ..." + update_pr_status_with_deployment(branch_name, commit_sha) + + +if __name__ == '__main__': + main() diff --git a/local_requirements.txt b/requirements/local_requirements.txt similarity index 67% rename from local_requirements.txt rename to requirements/local_requirements.txt index 6f36f686..12522c38 100644 --- a/local_requirements.txt +++ b/requirements/local_requirements.txt @@ -1,3 +1,5 @@ numpy==1.6.1 protobuf enum34 +nose +requests diff --git a/requirements.txt b/requirements/requirements.txt similarity index 100% rename from requirements.txt rename to requirements/requirements.txt