Skip to content

Commit f8637ea

Browse files
authored
Merge pull request #144 from sympy/pr-deployment-status
Add Deployment link in Pull Request Status
2 parents 64bf6ab + 608fbff commit f8637ea

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ virtualenv:
77

88
before_install:
99
- npm install -g casperjs
10-
- pip install nose
1110
install:
12-
- pip install -r requirements.txt -t lib/
13-
- pip install -r local_requirements.txt
11+
- pip install -r requirements/requirements.txt -t lib/
12+
- pip install -r requirements/local_requirements.txt
1413

1514
before_script:
1615
- openssl aes-256-cbc -K $encrypted_2fd045226a67_key -iv $encrypted_2fd045226a67_iv
@@ -40,3 +39,6 @@ deploy:
4039
on:
4140
all_branches: true
4241
repo: sympy/sympy_gamma
42+
43+
after_deploy:
44+
- python bin/update_status_on_pr.py

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ The project depends on some third-party libraries that are not on the list
5353
of built-in libraries (in app.yaml) bundled with the runtime, to install them
5454
run the following command.::
5555

56-
pip install -r requirements.txt -t lib/
56+
pip install -r requirements/requirements.txt -t lib/
5757

5858
Some libraries although available on app engine runtime, but needs to be
5959
installed locally for development.
6060

6161
Ref: https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#local_development ::
6262

63-
pip install -r local_requirements.txt
63+
pip install -r requirements/local_requirements.txt
6464

6565
Development server
6666
------------------

bin/update_status_on_pr.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
This script runs only on travis to create the status for the latest commit
3+
in the PR.
4+
5+
Reference: https://developer.github.com/v3/repos/statuses/#create-a-status
6+
"""
7+
import os
8+
import requests
9+
10+
GITHUB_REPO = 'sympy/sympy_gamma'
11+
GITHUB_API_URL = 'https://api.github.com'
12+
GITHUB_API_REF_URL = "%s/repos/%s/git/matching-refs/heads/" % (GITHUB_API_URL, GITHUB_REPO)
13+
GITHUB_API_UPDATE_STATUS_URL = "%s/repos/%s/statuses/" % (GITHUB_API_URL, GITHUB_REPO)
14+
SYMPY_BOT_TOKEN_VAR = 'SYMPY_BOT_TOKEN'
15+
16+
17+
def get_branch_commit_sha(branch_name):
18+
"""Gets the SHA of the last commit of the given branch
19+
:param branch_name: str name of branch on Github
20+
:return: str SHA
21+
"""
22+
response = requests.get(GITHUB_API_REF_URL + branch_name)
23+
if response.status_code == 200:
24+
response_json = response.json()
25+
else:
26+
raise ValueError('Invalid response from github API')
27+
return response_json[0]['object']['sha']
28+
29+
30+
def update_pr_status_with_deployment(branch_name, commit_sha):
31+
"""Updates the Status of the commit identified by commit SHA, which is reflected
32+
at the bottom of the PR, above merge button.
33+
:param branch_name: str name of branch on github
34+
:param commit_sha: str SHA
35+
:return: Response POST request to Github API
36+
"""
37+
sympy_bot_token = os.environ.get(SYMPY_BOT_TOKEN_VAR)
38+
deployment_url = "https://%s-dot-sympy-gamma-hrd.appspot.com" % branch_name
39+
payload = {
40+
"state": "success",
41+
"target_url": deployment_url,
42+
"description": "Deployed to version: %s" % branch_name,
43+
"context": "PR Deployment"
44+
}
45+
46+
headers = {
47+
'Authorization': 'Bearer %s' % sympy_bot_token,
48+
'Content-Type': 'application/json'
49+
}
50+
51+
update_status_url = GITHUB_API_UPDATE_STATUS_URL + commit_sha
52+
print "Update status URL: %s" % update_status_url
53+
response = requests.post(update_status_url, headers=headers, json=payload)
54+
print "Response: %s" % response.json()
55+
56+
57+
def main():
58+
is_on_travis = os.environ.get('TRAVIS')
59+
if not is_on_travis:
60+
raise ValueError('This script run only on travis!')
61+
branch_name = os.environ.get('TRAVIS_BRANCH')
62+
commit_sha = get_branch_commit_sha(branch_name)
63+
print "Branch name: %s Commit SHA: %s" % (branch_name, commit_sha)
64+
print "Creating commit status ..."
65+
update_pr_status_with_deployment(branch_name, commit_sha)
66+
67+
68+
if __name__ == '__main__':
69+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
numpy==1.6.1
22
protobuf
33
enum34
4+
nose
5+
requests
File renamed without changes.

0 commit comments

Comments
 (0)