From 545de62acd224ea2597aa44a055a53c7a16ba544 Mon Sep 17 00:00:00 2001 From: subodh101 Date: Wed, 28 Jul 2021 18:04:46 +0900 Subject: [PATCH 1/8] Update error handling --- github_token_app/github_app.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/github_token_app/github_app.py b/github_token_app/github_app.py index 5bfc58d..be9eb2e 100644 --- a/github_token_app/github_app.py +++ b/github_token_app/github_app.py @@ -10,25 +10,31 @@ def get_default_app(): if not hasattr(get_default_app, "app"): - pem_key = os.getenv("BASE64_PRIVATE_PEM_KEY") + pem_key = os.getenv("BASE64_PRIVATE_PEM_KEY", "") if not pem_key: raise RuntimeError("Missing Environment Variable: 'BASE64_PRIVATE_PEM_KEY'") pem_key = base64.b64decode(pem_key) - app_id = os.getenv("GITHUB_APP_ID") - try: - app_id = int(app_id) - except ValueError: - raise ValueError(f"Invalid $GITHUB_APP_ID={app_id}, NOT a valid integer") + github_app_id = os.getenv("GITHUB_APP_ID", "") + if not github_app_id: + raise RuntimeError("Missing Environment Variable: 'GITHUB_APP_ID'") - installation_id = os.getenv("INSTALLATION_ID") try: - installation_id = int(installation_id) + github_app_id = int(github_app_id) except ValueError: - raise ValueError(f"Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") - - get_default_app.app = GithubApp(app_id=app_id, private_pem_key=pem_key, installation_id=installation_id) + raise ValueError(f"Invalid $GITHUB_APP_ID={github_app_id}, NOT a valid integer") + + installation_id = os.getenv("INSTALLATION_ID", "") + if not installation_id: + print("[Warning] Missing Environment Variable: 'INSTALLATION_ID'") + else: + try: + installation_id = int(installation_id) + except ValueError: + print(f"[Warning] Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") + + get_default_app.app = GithubApp(app_id=github_app_id, private_pem_key=pem_key, installation_id=installation_id) return get_default_app.app From 8a434c6ae3354aa97015150299879eccb4cc8069 Mon Sep 17 00:00:00 2001 From: subodh101 Date: Wed, 28 Jul 2021 18:14:03 +0900 Subject: [PATCH 2/8] Remove function to obtain Installation ID --- github_token_app/github_app.py | 45 +++++++++------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/github_token_app/github_app.py b/github_token_app/github_app.py index be9eb2e..3ba672f 100644 --- a/github_token_app/github_app.py +++ b/github_token_app/github_app.py @@ -16,25 +16,25 @@ def get_default_app(): pem_key = base64.b64decode(pem_key) - github_app_id = os.getenv("GITHUB_APP_ID", "") - if not github_app_id: + app_id = os.getenv("GITHUB_APP_ID", "") + if not app_id: raise RuntimeError("Missing Environment Variable: 'GITHUB_APP_ID'") try: - github_app_id = int(github_app_id) + app_id = int(app_id) except ValueError: - raise ValueError(f"Invalid $GITHUB_APP_ID={github_app_id}, NOT a valid integer") + raise ValueError(f"Invalid $GITHUB_APP_ID={app_id}, NOT a valid integer") installation_id = os.getenv("INSTALLATION_ID", "") if not installation_id: - print("[Warning] Missing Environment Variable: 'INSTALLATION_ID'") - else: - try: - installation_id = int(installation_id) - except ValueError: - print(f"[Warning] Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") + raise RuntimeError("Missing Environment Variable: 'INSTALLATION_ID'") - get_default_app.app = GithubApp(app_id=github_app_id, private_pem_key=pem_key, installation_id=installation_id) + try: + installation_id = int(installation_id) + except ValueError: + raise ValueError(f"Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") + + get_default_app.app = GithubApp(app_id=app_id, private_pem_key=pem_key, installation_id=installation_id) return get_default_app.app @@ -148,21 +148,6 @@ def get_write_pr_token(self, repo_names: List[str]) -> str: permissions = {"contents": "write", "pull_requests": "write", "metadata": "read"} return self.get_access_token(repo_names, permissions) - def get_installations(self): - headers = apps.create_jwt_headers( - private_key_pem=self.private_pem_key, app_id=self.app_id, expire_in=600 # Max allowed: 60*10 (10 minutes) - ) - url = "https://api.github.com/app/installations" - - response = requests.get(url=url, headers=headers) - if response.status_code != 200: - raise Exception( - "Failed to get fetch repositories. " - f"Status code: {response.status_code} " - f"Response: {response.json()} " - ) - return response.json() - def get_read_token(repo_names: List[str]) -> None: """ @@ -192,11 +177,3 @@ def get_write_pr_token(repo_names: List[str]) -> None: """ github_app = get_default_app() print(github_app.get_write_pr_token(repo_names)) - - -def get_installations(): - """ - Get the list of installations for the authenticated app. - """ - github_app = get_default_app() - pprint(github_app.get_installations()) From 989bf96b47f9bf7d06d56b19603c4d48fcf03b13 Mon Sep 17 00:00:00 2001 From: subodh101 Date: Wed, 28 Jul 2021 18:26:14 +0900 Subject: [PATCH 3/8] Use get_installations function without the installation id --- github_token_app/github_app.py | 45 +++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/github_token_app/github_app.py b/github_token_app/github_app.py index 3ba672f..be9eb2e 100644 --- a/github_token_app/github_app.py +++ b/github_token_app/github_app.py @@ -16,25 +16,25 @@ def get_default_app(): pem_key = base64.b64decode(pem_key) - app_id = os.getenv("GITHUB_APP_ID", "") - if not app_id: + github_app_id = os.getenv("GITHUB_APP_ID", "") + if not github_app_id: raise RuntimeError("Missing Environment Variable: 'GITHUB_APP_ID'") try: - app_id = int(app_id) + github_app_id = int(github_app_id) except ValueError: - raise ValueError(f"Invalid $GITHUB_APP_ID={app_id}, NOT a valid integer") + raise ValueError(f"Invalid $GITHUB_APP_ID={github_app_id}, NOT a valid integer") installation_id = os.getenv("INSTALLATION_ID", "") if not installation_id: - raise RuntimeError("Missing Environment Variable: 'INSTALLATION_ID'") + print("[Warning] Missing Environment Variable: 'INSTALLATION_ID'") + else: + try: + installation_id = int(installation_id) + except ValueError: + print(f"[Warning] Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") - try: - installation_id = int(installation_id) - except ValueError: - raise ValueError(f"Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") - - get_default_app.app = GithubApp(app_id=app_id, private_pem_key=pem_key, installation_id=installation_id) + get_default_app.app = GithubApp(app_id=github_app_id, private_pem_key=pem_key, installation_id=installation_id) return get_default_app.app @@ -148,6 +148,21 @@ def get_write_pr_token(self, repo_names: List[str]) -> str: permissions = {"contents": "write", "pull_requests": "write", "metadata": "read"} return self.get_access_token(repo_names, permissions) + def get_installations(self): + headers = apps.create_jwt_headers( + private_key_pem=self.private_pem_key, app_id=self.app_id, expire_in=600 # Max allowed: 60*10 (10 minutes) + ) + url = "https://api.github.com/app/installations" + + response = requests.get(url=url, headers=headers) + if response.status_code != 200: + raise Exception( + "Failed to get fetch repositories. " + f"Status code: {response.status_code} " + f"Response: {response.json()} " + ) + return response.json() + def get_read_token(repo_names: List[str]) -> None: """ @@ -177,3 +192,11 @@ def get_write_pr_token(repo_names: List[str]) -> None: """ github_app = get_default_app() print(github_app.get_write_pr_token(repo_names)) + + +def get_installations(): + """ + Get the list of installations for the authenticated app. + """ + github_app = get_default_app() + pprint(github_app.get_installations()) From ed7400ce7b93f39a3210e9ed32a022801583cd3c Mon Sep 17 00:00:00 2001 From: subodh101 Date: Wed, 28 Jul 2021 18:27:20 +0900 Subject: [PATCH 4/8] Update the readme with an alternate method to retrieve installation id --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 76d80fe..7a5cf5b 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,17 @@ pip install github-token-app - **BASE64_PRIVATE_PEM_KEY**: This is the private pem key for the github-app encoded in base64. - **GITHUB_APP_ID**: App ID -- **INSTALLATION_ID**: Installation Id for App/Org Pair (if you don't know, it can be generated from the second step below) +- **INSTALLATION_ID**: Installation Id for App/Org Pair (if you don't know, Please check the steps below) ## What the code does? -1. The code is for authenticating a [github app as an installation](https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-an-installation). Installations are created from a Github app settings (Install App). +1. The code is for authenticating a [github app as an installation](https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-an-installation). Installations are created from a Github app settings (Install App). -2. The code contains `get_installations` function which can be called by CLI command `gta installations`. It returns a response of list of installations. The `id` attributes in the responses are the installation ids. Set the **INSTALLATION_ID** as environment variable based on your required access. +2. Under Install App, click on the account settings. You will find the installation ID in the URL https://github.com/apps/my-app-name/installations/**INSTALLATION_ID**. Set the **INSTALLATION_ID** as environment variable based on your required access. + + Alternatively, the code contains `get_installations` function which can be called by CLI command `gta installations`. It returns a response of list of installations. The `id` attributes in the responses are the installation ids. 3. Finally, there are three methods read, write and write-pr. That generates token to perform respective actions to specific repositories. From e31e02386c3196ad4fa8a31bbfd1dc31c031e12a Mon Sep 17 00:00:00 2001 From: subodh101 Date: Wed, 28 Jul 2021 18:28:20 +0900 Subject: [PATCH 5/8] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a5cf5b..3519961 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ pip install github-token-app - **BASE64_PRIVATE_PEM_KEY**: This is the private pem key for the github-app encoded in base64. - **GITHUB_APP_ID**: App ID -- **INSTALLATION_ID**: Installation Id for App/Org Pair (if you don't know, Please check the steps below) +- **INSTALLATION_ID**: Installation Id for App/Org Pair (if you don't know, it can be generated from the second step below) ## What the code does? From e3df09187c49c415741f008b18d844582d0e1b9e Mon Sep 17 00:00:00 2001 From: subodh101 Date: Wed, 28 Jul 2021 18:32:42 +0900 Subject: [PATCH 6/8] If the installation id env is present, raise error is it is invalid --- github_token_app/github_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github_token_app/github_app.py b/github_token_app/github_app.py index be9eb2e..30956ab 100644 --- a/github_token_app/github_app.py +++ b/github_token_app/github_app.py @@ -32,7 +32,7 @@ def get_default_app(): try: installation_id = int(installation_id) except ValueError: - print(f"[Warning] Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") + raise ValueError(f"Invalid $INSTALLATION_ID={installation_id}, NOT a valid integer") get_default_app.app = GithubApp(app_id=github_app_id, private_pem_key=pem_key, installation_id=installation_id) From e9b24b13a8a83e09a29b928cdc4ce8084696aa52 Mon Sep 17 00:00:00 2001 From: subodh101 Date: Thu, 29 Jul 2021 11:47:52 +0900 Subject: [PATCH 7/8] =?UTF-8?q?github-token-app:=200.1.0=20=E2=86=92=200.1?= =?UTF-8?q?.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 55ea9ed..9dca632 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.1.0 +current_version = 0.1.1 commit = True tag = False message = github-token-app: {current_version} → {new_version} diff --git a/setup.py b/setup.py index 943c662..307ed3d 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import find_packages, setup -VERSION = "0.1.0" +VERSION = "0.1.1" def generate_install_requires() -> List[str]: From 88a56e69e55e4653732b3a2df3e05910ddb5d79d Mon Sep 17 00:00:00 2001 From: subodh101 Date: Thu, 29 Jul 2021 11:50:07 +0900 Subject: [PATCH 8/8] update pull request template --- .github/PULL_REQUEST_TEMPLATE.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index c7f8944..238c658 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,4 +1,12 @@ Please read the CLA carefully before submitting your contribution to Mercari. Under any circumstances, by submitting your contribution, you are deemed to accept and agree to be bound by the terms and conditions of the CLA. -https://www.mercari.com/cla/ \ No newline at end of file +https://www.mercari.com/cla/ + + +## WHAT +(Write the change being made with this pull request) + + +## WHY +(Write the motivation why you submit this pull request)