From 5157a3e4f8fc4982854ae6978b93f48d9ba02c1c Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 00:00:50 +0100 Subject: [PATCH 01/10] Add Deployment status on PR status --- .travis.yml | 5 +-- bin/update_status_on_pr.py | 45 +++++++++++++++++++ .../local_requirements.txt | 2 + .../requirements.txt | 0 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 bin/update_status_on_pr.py rename local_requirements.txt => requirements/local_requirements.txt (67%) rename requirements.txt => requirements/requirements.txt (100%) diff --git a/.travis.yml b/.travis.yml index bc532201..1c4e892b 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 diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py new file mode 100644 index 00000000..b1466fd6 --- /dev/null +++ b/bin/update_status_on_pr.py @@ -0,0 +1,45 @@ +import os +import requests + +GITHUB_API_REF_URL = "https://api.github.com/repos/sympy/sympy_gamma/git/matching-refs/heads/" +GITHUB_API_UPDATE_STATUS_URL = "https://api.github.com/repos/sympy/sympy_gamma/statuses/" + + +def get_branch_commit_sha(branch_name): + response = requests.request("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): + sympy_bot_token = os.environ.get('SYMPY_BOT_TOKEN') + payload = { + "state": "success", + "target_url": "https://%s-dot-sympy-gamma-hrd.appspot.com" % branch_name, + "description": "Deployment", + "context": "PR Deployment" + } + + headers = { + 'Authorization': 'token %s' % sympy_bot_token, + 'Content-Type': 'application/x-www-form-urlencoded' + } + + response = requests.request("POST", GITHUB_API_UPDATE_STATUS_URL + commit_sha, headers=headers, data=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) + 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 From 2a46b6ffc5f1e3b606a26acb92f8feeeada2df2c Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 00:03:17 +0100 Subject: [PATCH 02/10] Add after_deploy stage in travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1c4e892b..6bdd1257 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,3 +39,6 @@ deploy: on: all_branches: true repo: sympy/sympy_gamma + +after_deploy: + - python bin/update_status_on_pr.py From 5ce03d060d5c53422543b0e698e947b2881def2e Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 00:44:10 +0100 Subject: [PATCH 03/10] Add more logging in update pr status script --- bin/update_status_on_pr.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index b1466fd6..6d0e0c99 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -28,7 +28,9 @@ def update_pr_status_with_deployment(branch_name, commit_sha): 'Content-Type': 'application/x-www-form-urlencoded' } - response = requests.request("POST", GITHUB_API_UPDATE_STATUS_URL + commit_sha, headers=headers, data=payload) + update_status_url = GITHUB_API_UPDATE_STATUS_URL + commit_sha + print "Update status URL: %s" % update_status_url + response = requests.request("POST", update_status_url, headers=headers, data=payload) print "Response: %s" % response.json() @@ -38,6 +40,7 @@ def main(): 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 update_pr_status_with_deployment(branch_name, commit_sha) From fce621deada5de56b6bb75752c9b6258a130c6bb Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 00:48:44 +0100 Subject: [PATCH 04/10] Fix print formatting --- bin/update_status_on_pr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index 6d0e0c99..a2275e7c 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -40,7 +40,7 @@ def main(): 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 "Branch name: %s Commit SHA: %s" % (branch_name, commit_sha) update_pr_status_with_deployment(branch_name, commit_sha) From 455db8b4dc9549e298a5e8a1aebd4f4879478548 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 00:59:27 +0100 Subject: [PATCH 05/10] Fix Auth headers and Content-Type --- bin/update_status_on_pr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index a2275e7c..a3ffc969 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -24,8 +24,8 @@ def update_pr_status_with_deployment(branch_name, commit_sha): } headers = { - 'Authorization': 'token %s' % sympy_bot_token, - 'Content-Type': 'application/x-www-form-urlencoded' + 'Authorization': 'Bearer %s' % sympy_bot_token, + 'Content-Type': 'application/json' } update_status_url = GITHUB_API_UPDATE_STATUS_URL + commit_sha From 6367e95a3e7eaf0b878d3e27e7523f6a8857f2c0 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 01:07:19 +0100 Subject: [PATCH 06/10] Fix request json --- bin/update_status_on_pr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index a3ffc969..803732e2 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -6,7 +6,7 @@ def get_branch_commit_sha(branch_name): - response = requests.request("GET", GITHUB_API_REF_URL + branch_name) + response = requests.get(GITHUB_API_REF_URL + branch_name) if response.status_code == 200: response_json = response.json() else: @@ -30,7 +30,7 @@ def update_pr_status_with_deployment(branch_name, commit_sha): update_status_url = GITHUB_API_UPDATE_STATUS_URL + commit_sha print "Update status URL: %s" % update_status_url - response = requests.request("POST", update_status_url, headers=headers, data=payload) + response = requests.post(update_status_url, headers=headers, json=payload) print "Response: %s" % response.json() From 951c68ff2045cc8e6c543119efdc4b93b9386660 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 01:16:38 +0100 Subject: [PATCH 07/10] Test personal token --- bin/update_status_on_pr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index 803732e2..4148ab36 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -15,7 +15,7 @@ def get_branch_commit_sha(branch_name): def update_pr_status_with_deployment(branch_name, commit_sha): - sympy_bot_token = os.environ.get('SYMPY_BOT_TOKEN') + sympy_bot_token = os.environ.get('SYMPY_BOT_TOKEN_2') payload = { "state": "success", "target_url": "https://%s-dot-sympy-gamma-hrd.appspot.com" % branch_name, From f12b29588d2cd13324470421e355ecc01f9b7dd8 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 12:38:34 +0100 Subject: [PATCH 08/10] Extract constants in update pr status script --- README.rst | 4 ++-- bin/update_status_on_pr.py | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) 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 index 4148ab36..34e833a8 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -1,11 +1,24 @@ +""" +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_API_REF_URL = "https://api.github.com/repos/sympy/sympy_gamma/git/matching-refs/heads/" -GITHUB_API_UPDATE_STATUS_URL = "https://api.github.com/repos/sympy/sympy_gamma/statuses/" +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_2' 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() @@ -15,7 +28,13 @@ def get_branch_commit_sha(branch_name): def update_pr_status_with_deployment(branch_name, commit_sha): - sympy_bot_token = os.environ.get('SYMPY_BOT_TOKEN_2') + """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) payload = { "state": "success", "target_url": "https://%s-dot-sympy-gamma-hrd.appspot.com" % branch_name, @@ -41,6 +60,7 @@ def main(): 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) From 606d408d378217a09571cf57f73bc8deaf142842 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 20:38:04 +0100 Subject: [PATCH 09/10] Use sympy bot token --- bin/update_status_on_pr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index 34e833a8..dcdd8aa4 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -11,7 +11,7 @@ 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_2' +SYMPY_BOT_TOKEN_VAR = 'SYMPY_BOT_TOKEN' def get_branch_commit_sha(branch_name): From 608fbffae52190f5c8e412f721ab48376e2fafeb Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 22 Apr 2020 23:39:59 +0100 Subject: [PATCH 10/10] Add description in deployed to in commit status --- bin/update_status_on_pr.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/update_status_on_pr.py b/bin/update_status_on_pr.py index dcdd8aa4..f66af031 100644 --- a/bin/update_status_on_pr.py +++ b/bin/update_status_on_pr.py @@ -35,10 +35,11 @@ def update_pr_status_with_deployment(branch_name, commit_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": "https://%s-dot-sympy-gamma-hrd.appspot.com" % branch_name, - "description": "Deployment", + "target_url": deployment_url, + "description": "Deployed to version: %s" % branch_name, "context": "PR Deployment" }