Skip to content

Commit

Permalink
Merge pull request #144 from sympy/pr-deployment-status
Browse files Browse the repository at this point in the history
Add Deployment link in Pull Request Status
  • Loading branch information
aktech committed Apr 23, 2020
2 parents 64bf6ab + 608fbff commit f8637ea
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,3 +39,6 @@ deploy:
on:
all_branches: true
repo: sympy/sympy_gamma

after_deploy:
- python bin/update_status_on_pr.py
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
69 changes: 69 additions & 0 deletions bin/update_status_on_pr.py
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
numpy==1.6.1
protobuf
enum34
nose
requests
File renamed without changes.

0 comments on commit f8637ea

Please sign in to comment.