From 2f58d306c5a757faa3f084aeddf5d4035073c50c Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Sun, 19 Mar 2023 20:37:12 +0100 Subject: [PATCH 01/10] refactor: list-repository function --- github-rest-api.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index 204f009..7ef3788 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -171,11 +171,14 @@ def list_repositories(limit: int, property: str, role: str) -> None: f"{GITHUB_URL}/user/repos", headers=headers, params=params ) if resp.status_code == 200: - repos = json.loads(resp.text) - repo_names = [repo["full_name"] for repo in repos] - for repo_name in repo_names: - rich_output(f"- {repo_name}", fmt="blink bold green") - rich_output(f"\nTotal repositories: {len(repo_names)}", fmt="blink bold green") + repositories = json.loads(resp.text) + full_name = [repo["full_name"] for repo in repositories] + for repos in full_name: + rich_output(f"- {repos}", fmt="blink bold green") + rich_output( + f"\nTotal repositories: {len(full_name)}", + fmt="blink bold green", + ) else: rich_output( f"Failed to list repositories for {GITHUB_USER}\n" + From 0f93b3a0c1efa4ed69c6a87f5e81beb19d87c210 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Sun, 19 Mar 2023 22:40:41 +0100 Subject: [PATCH 02/10] fix: function type hints --- github-rest-api.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index 7ef3788..adc09cf 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -21,13 +21,13 @@ } -def rich_output(input: str, fmt: str) -> None: +def rich_output(input: str, fmt: str): text = Text(input) text.stylize(fmt) console.print(text) -def get_repository(name: str, org: str) -> None: +def get_repository(name: str, org: str): if org is None: get_user_repository_info = requests.get( f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}", headers=headers @@ -127,7 +127,7 @@ def create_repository(name: str, private: str, org: str): ) -def delete_repository(name: str, org: str) -> None: +def delete_repository(name: str, org: str): if org is not None: resp_org = requests.delete( f"{GITHUB_URL}/repos/{org}/{name}", headers=headers @@ -165,7 +165,7 @@ def delete_repository(name: str, org: str) -> None: ) -def list_repositories(limit: int, property: str, role: str) -> None: +def list_repositories(limit: int, property: str, role: str): params = {"per_page": limit, "sort": property, "type": role} resp = requests.get( f"{GITHUB_URL}/user/repos", headers=headers, params=params @@ -187,7 +187,7 @@ def list_repositories(limit: int, property: str, role: str) -> None: ) -def dependabot_security(name: str, option: str, org: str) -> None: +def dependabot_security(name: str, option: str, org: str): if org is not None: if option == "true": dependabot_on = requests.put( From fdb4a2077e04e4dda237587c12e6c172f72e8f72 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Mon, 20 Mar 2023 08:47:36 +0100 Subject: [PATCH 03/10] feat: fix import python libs --- github-rest-api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github-rest-api.py b/github-rest-api.py index adc09cf..067a76b 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -1,10 +1,10 @@ import requests import argparse +import json from config import settings from rich.console import Console from rich import print from rich.text import Text -import json GITHUB_URL = f"{settings.API_URL}" From ea9c78daecb8e88895d0aac5828d605847f99620 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Mon, 3 Apr 2023 22:10:15 +0200 Subject: [PATCH 04/10] feat: refactor print from rich module as rprint --- github-rest-api.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index 067a76b..f099a48 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -3,7 +3,7 @@ import json from config import settings from rich.console import Console -from rich import print +from rich import print as rprint from rich.text import Text @@ -37,7 +37,7 @@ def get_repository(name: str, org: str): if status_code == 200: source_repo = json.loads(get_user_repository_info.text) - print(source_repo) + rprint(source_repo) elif status_code == 404: rich_output( "The requested repository does not exist!", fmt="blink bold red" @@ -56,7 +56,7 @@ def get_repository(name: str, org: str): if status_code == 200: data_json = json.loads(get_org_repository_info.text) - print(data_json) + rprint(data_json) elif status_code == 404: rich_output( f"Repository not found on {org} organization", @@ -69,7 +69,7 @@ def get_repository(name: str, org: str): fmt="blink bold red" ) else: - print("Failed!") + rprint("Failed!") def create_repository(name: str, private: str, org: str): @@ -269,7 +269,7 @@ def dependabot_security(name: str, option: str, org: str): fmt="blink bold red", ) else: - print("Invalid option!") + rprint("Invalid option!") def deployment_environments(name: str, env: str, org: str): From d6ff97f00b63d4a37e86463024c45cbc39bc61e5 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Mon, 3 Apr 2023 22:46:07 +0200 Subject: [PATCH 05/10] refactor: get_repository function --- github-rest-api.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index f099a48..a0a33d5 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -29,35 +29,29 @@ def rich_output(input: str, fmt: str): def get_repository(name: str, org: str): if org is None: - get_user_repository_info = requests.get( + req = requests.get( f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}", headers=headers ) - - status_code = get_user_repository_info.status_code - - if status_code == 200: - source_repo = json.loads(get_user_repository_info.text) + if req.status_code == 200: + source_repo = json.loads(req.text) rprint(source_repo) - elif status_code == 404: + elif req.status_code == 404: rich_output( "The requested repository does not exist!", fmt="blink bold red" ) else: rich_output( - f"Failed to get repository {name} with status code {status_code}", + f"Failed to get repository {name} with status code {req.status_code}", fmt="blink bold red", ) elif org is not None: - get_org_repository_info = requests.get( + req = requests.get( f"{GITHUB_URL}/repos/{org}/{name}", headers=headers ) - - status_code = get_org_repository_info.status_code - - if status_code == 200: - data_json = json.loads(get_org_repository_info.text) + if req.status_code == 200: + data_json = json.loads(req.text) rprint(data_json) - elif status_code == 404: + elif req.status_code == 404: rich_output( f"Repository not found on {org} organization", fmt="blink bold red", @@ -65,7 +59,7 @@ def get_repository(name: str, org: str): else: rich_output( f"Failed to get repository {name} in organization {org}\n" + - f"Status code: {status_code}", + f"Status code: {req.status_code}", fmt="blink bold red" ) else: From d51fb254803289d72f7cf6a3d5a3937456709223 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Mon, 3 Apr 2023 23:01:03 +0200 Subject: [PATCH 06/10] test: github ssh rsa host key From 7876e1e079e1c89bbd106b2e24db494d664d6800 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Thu, 20 Apr 2023 01:32:02 +0200 Subject: [PATCH 07/10] refactor: try-except for functions --- github-rest-api.py | 178 +++++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 87 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index a0a33d5..67b106b 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -28,157 +28,161 @@ def rich_output(input: str, fmt: str): def get_repository(name: str, org: str): - if org is None: - req = requests.get( - f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}", headers=headers - ) - if req.status_code == 200: + try: + if org is None: + req = requests.get( + f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}", headers=headers + ) + req.raise_for_status() source_repo = json.loads(req.text) rprint(source_repo) - elif req.status_code == 404: - rich_output( - "The requested repository does not exist!", fmt="blink bold red" + elif org is not None: + req = requests.get( + f"{GITHUB_URL}/repos/{org}/{name}", headers=headers ) + req.raise_for_status() + source_repo = json.loads(req.text) + rprint(source_repo) else: + rprint("Failed!") + except requests.exceptions.HTTPError as e: + if e.response.status_code == 401: rich_output( - f"Failed to get repository {name} with status code {req.status_code}", + "Unauthorized access. Please check your token or credentials.", fmt="blink bold red", ) - elif org is not None: - req = requests.get( - f"{GITHUB_URL}/repos/{org}/{name}", headers=headers - ) - if req.status_code == 200: - data_json = json.loads(req.text) - rprint(data_json) - elif req.status_code == 404: + elif e.response.status_code == 404: rich_output( - f"Repository not found on {org} organization", - fmt="blink bold red", + f"The requested repository does not exist!", fmt="blink bold red" ) else: rich_output( - f"Failed to get repository {name} in organization {org}\n" + - f"Status code: {req.status_code}", + f"Failed to get repository {name}\n" + + f"Status code: {str(e.response.status_code)}", fmt="blink bold red" ) - else: - rprint("Failed!") def create_repository(name: str, private: str, org: str): if private == 'true': - is_true = True + is_private = True elif private == 'false': - is_true = False + is_private = False else: - is_true = False + is_private = False data = { "name": name, "auto_init": "true", - "private": is_true, + "private": is_private, } - if org is not None: - resp_org = requests.post( - f"{GITHUB_URL}/orgs/{org}/repos", headers=headers, json=data - ) - if resp_org.status_code == 201: + try: + if org is not None: + req = requests.post( + f"{GITHUB_URL}/orgs/{org}/repos", headers=headers, json=data + ) + req.raise_for_status() rich_output( f"Repository sucessfully created in {org}/{name}", fmt="blink bold green" ) - elif resp_org.status_code == 422: - rich_output( - "Repository name already exists on this organization", - fmt="blink bold red", - ) else: - rich_output( - f"Failed to create repository {name} in organization {org}\n" + - f"Status code: {resp_org.status_code}", - fmt="blink bold red" + req = requests.post( + f"{GITHUB_URL}/user/repos", headers=headers, json=data ) - else: - resp = requests.post( - f"{GITHUB_URL}/user/repos", headers=headers, json=data - ) - if resp.status_code == 201: + req.raise_for_status() rich_output( - f"Repository created sucessfully on {GITHUB_USER}/{name}", + f"Repository sucessfully created in {GITHUB_USER}/{name}", fmt="blink bold green", ) - elif resp.status_code == 422: + except requests.exceptions.HTTPError as e: + if e.response.status_code == 401: + rich_output( + "Unauthorized access. Please check your token or credentials.", + fmt="blink bold red", + ) + elif e.response.status_code == 422: rich_output( - "Repository name already exists on this account!", + "Repository name already exists on this account or organization!", fmt="blink bold red" ) else: rich_output( - f"Failed to create repository {GITHUB_USER}/{name}" + - f" with status code {resp.status_code}", fmt="blink bold red" + f"Failed to create repository {name}" + + f"Status code: {e.response.status_code}", + fmt="blink bold red", ) def delete_repository(name: str, org: str): - if org is not None: - resp_org = requests.delete( - f"{GITHUB_URL}/repos/{org}/{name}", headers=headers - ) - if resp_org.status_code == 204: + try: + if org is not None: + req = requests.delete( + f"{GITHUB_URL}/repos/{org}/{name}", headers=headers + ) + req.raise_for_status() rich_output( f"Repository sucessfully deleted in {org}/{name}", fmt="blink bold green" ) - elif resp_org.status_code == 403: + else: + req = requests.delete( + f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}", headers=headers + ) + req.raise_for_status() rich_output( - "You are not an admin of this repository", fmt="blink bold red" + f"Repository sucessfully deleted in {GITHUB_USER}/{name}", + fmt="blink bold green", ) - elif resp_org.status_code == 404: + except requests.exceptions.HTTPError as e: + if e.response.status_code == 403: rich_output( - f"Repository not found in organization {org}", fmt="blink bold red" + "You are not an admin of this repository!", + fmt="blink bold red", ) - rich_output(f"Repository: {name}", fmt="blink bold red") - else: - resp = requests.delete( - f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}", headers=headers - ) - if resp.status_code == 204: + elif e.response.status_code == 404: rich_output( - f"Repository deleted sucessfully on {GITHUB_USER}/{name}", - fmt="blink bold green", + f"The requested repository was not found!", + fmt="blink bold red", ) - elif resp.status_code == 404: - rich_output("Repository not found!", fmt="blink bold red") else: rich_output( - f"Failed to delete repository {GITHUB_USER}/{name}\ - with status code {resp.status_code}", + f"Failed to delete repository {name}\ + with status code {e.response.status_code}", fmt="blink bold red", ) def list_repositories(limit: int, property: str, role: str): - params = {"per_page": limit, "sort": property, "type": role} - resp = requests.get( - f"{GITHUB_URL}/user/repos", headers=headers, params=params - ) - if resp.status_code == 200: - repositories = json.loads(resp.text) - full_name = [repo["full_name"] for repo in repositories] - for repos in full_name: + try: + params = {"per_page": limit, "sort": property, "type": role} + req = requests.get( + f"{GITHUB_URL}/user/repos", headers=headers, params=params + ) + req.raise_for_status() + + repositories = json.loads(req.text) + repository_full_name = [repo["full_name"] for repo in repositories] + for repos in repository_full_name: rich_output(f"- {repos}", fmt="blink bold green") rich_output( - f"\nTotal repositories: {len(full_name)}", + f"\nTotal repositories: {len(repository_full_name)}", fmt="blink bold green", ) - else: - rich_output( - f"Failed to list repositories for {GITHUB_USER}\n" + - f"Status code: {resp.status_code}", - fmt="blink bold red", - ) + + except requests.exceptions.HTTPError as e: + if e.response.status_code == 401: + rich_output( + "Unauthorized access. Please check your token or credentials.", + fmt="blink bold red", + ) + else: + rich_output( + f"Failed to list repositories for {GITHUB_USER}\n" + + f"Status code: {e.response.status_code}", + fmt="blink bold red", + ) def dependabot_security(name: str, option: str, org: str): From 0c85a9810db0c3b854a2e319bfc33e41368f437c Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Thu, 20 Apr 2023 12:00:46 +0200 Subject: [PATCH 08/10] refactor: dependabot_security function and ruff lint --- github-rest-api.py | 122 +++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 76 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index 67b106b..417c0b3 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -53,7 +53,7 @@ def get_repository(name: str, org: str): ) elif e.response.status_code == 404: rich_output( - f"The requested repository does not exist!", fmt="blink bold red" + "The requested repository does not exist!", fmt="blink bold red" ) else: rich_output( @@ -85,7 +85,7 @@ def create_repository(name: str, private: str, org: str): req.raise_for_status() rich_output( f"Repository sucessfully created in {org}/{name}", - fmt="blink bold green" + fmt="blink bold green", ) else: req = requests.post( @@ -143,8 +143,7 @@ def delete_repository(name: str, org: str): ) elif e.response.status_code == 404: rich_output( - f"The requested repository was not found!", - fmt="blink bold red", + "The requested repository was not found!", fmt="blink bold red", ) else: rich_output( @@ -185,89 +184,60 @@ def list_repositories(limit: int, property: str, role: str): ) -def dependabot_security(name: str, option: str, org: str): - if org is not None: - if option == "true": - dependabot_on = requests.put( - f"{GITHUB_URL}/repos/{org}/{name}/vulnerability-alerts", - headers=headers, - ) - status_code = dependabot_on.status_code - if status_code == 204: - security_on = requests.put( - f"{GITHUB_URL}/repos/{org}/{name}/automated-security-fixes", +def dependabot_security(name: str, enabled: bool, org: str): + if enabled == 'true': + is_enabled = True + elif enabled == 'false': + is_enabled = False + else: + is_enabled = True + + try: + if org is not None and is_enabled is True: + for endpoint in ["vulnerability-alerts", "automated-security-fixes"]: + req = requests.put( + f"{GITHUB_URL}/repos/{org}/{name}/{endpoint}", headers=headers, ) - if security_on.status_code == 204: - rich_output( - f"Enable dependabot on repository: {org}/{name}", - fmt="blink bold green", - ) - else: - rich_output( - f"Failed to enable dependabot for {org}/{name}\n" + - f"Status code: {status_code}", - fmt="blink bold red", - ) - elif option == "false": - dependabot_off = requests.delete( + req.raise_for_status() + rich_output( + f"Dependabot has been activated on repository {org}/{name}", + fmt="blink bold green", + ) + elif org is not None: + req = requests.delete( f"{GITHUB_URL}/repos/{org}/{name}/vulnerability-alerts", headers=headers, - ) - status_code = dependabot_off.status_code - if status_code == 204: - rich_output( - f"Disable dependabot on repository: {org}/{name}", - fmt="blink bold green" - ) - else: - rich_output( - f"Failed to disable dependabot for {org}/{name}\n" + - f"Status code: {status_code}", - fmt="blink bold red", - ) - else: - if option == "true": - dependabot_on = requests.put( - f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/vulnerability-alerts", - headers=headers, ) - status_code = dependabot_on.status_code - if status_code == 204: - security_on = requests.put( - f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/automated-security-fixes", - headers=headers - ) - if security_on.status_code == 204: - rich_output( - f"Enable dependabot on repository: {GITHUB_USER}/{name}", - fmt="blink bold green", - ) - else: - rich_output( - f"Failed to enable dependabot for {GITHUB_USER}/{name}\n" + - f"Status code: {status_code}", - fmt="blink bold red", - ) - elif option == "false": - dependabot_off = requests.delete( - f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/vulnerability-alerts", - headers=headers, + req.raise_for_status() + rich_output( + f"Disable dependabot on repository: {org}/{name}", + fmt="blink bold green" ) - status_code = dependabot_off.status_code - if status_code == 204: + else: + if org is None and is_enabled is True: + for endpoint in ["vulnerability-alerts", "automated-security-fixes"]: + req = requests.put( + f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/{endpoint}", + headers=headers, + ) + req.raise_for_status() rich_output( - f"Disable dependabot on repository: {GITHUB_USER}/{name}", + f"Dependabot has been activated on repository {GITHUB_USER}/{name}", fmt="blink bold green", ) - else: + elif org is None: + req = requests.delete( + f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/vulnerability-alerts", + headers=headers, + ) + req.raise_for_status() rich_output( - f"Failed to disable dependabot for {GITHUB_USER}/{name}\n" + - f"Status code: {status_code}", - fmt="blink bold red", + f"Disable dependabot on repository: {GITHUB_USER}/{name}", + fmt="blink bold green" ) - else: - rprint("Invalid option!") + except requests.exceptions.RequestException as e: + rprint(f"Error: {e}") def deployment_environments(name: str, env: str, org: str): From 959d3436cf5847c7911599eb40b4c8d23d7e19d6 Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Thu, 20 Apr 2023 12:06:43 +0200 Subject: [PATCH 09/10] security: add dependabot to pip package ecosystem --- .github/dependabot.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..3c33479 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,13 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "daily" + time: "13:00" + timezone: "Europe/Madrid" From ed6cb68d1b6a1a962b3f64d742022d520a2ce7be Mon Sep 17 00:00:00 2001 From: lbrealdev Date: Fri, 21 Apr 2023 00:05:51 +0200 Subject: [PATCH 10/10] refactor: deployment_environment function --- github-rest-api.py | 63 +++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/github-rest-api.py b/github-rest-api.py index 417c0b3..079e31b 100644 --- a/github-rest-api.py +++ b/github-rest-api.py @@ -240,55 +240,38 @@ def dependabot_security(name: str, enabled: bool, org: str): rprint(f"Error: {e}") -def deployment_environments(name: str, env: str, org: str): - if org is None: - response = requests.put( - f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/environments/{env}", - headers=headers, - ) - if response.status_code == 200: - rich_output( - f"Create new environment {env.upper()}\n" + - f"Repository: {GITHUB_USER}/{name}", - fmt="blink bold green", +def deployment_environment(name: str, env: str, org: str): + try: + if org is not None and env != "": + req = requests.put( + f"{GITHUB_URL}/repos/{org}/{name}/environments/{env}", + headers=headers, ) - elif response.status_code == 422: + req.raise_for_status() rich_output( - f"Failed to create environment {env.upper()}\n" + - f"Repository: {GITHUB_USER}/{name}", - fmt="blink bold red" + f"Create deployment environment {env.upper()}\n" + + f"Repository: {org}/{name}", + fmt="blink bold green" ) - else: - rich_output( - f"Failed to create environment {env.upper()}\n" + - f"Status code: {response.status_code}", - fmt="blink bold red" + elif env != "": + req = requests.put( + f"{GITHUB_URL}/repos/{GITHUB_USER}/{name}/environments/{env}", + headers=headers, ) - elif org is not None and org != "": - response = requests.put( - f"{GITHUB_URL}/repos/{org}/{name}/environments/{env}", - headers=headers, - ) - if response.status_code == 200: + req.raise_for_status() rich_output( - f"Create new environment {env.upper()}\n" + - f"Repository: {org}/{name}", + f"Create deployment environment {env.upper()}\n" + + f"Repository: {GITHUB_USER}/{name}", fmt="blink bold green" ) - elif response.status_code == 422: + except requests.exceptions.HTTPError as e: + if e.response.status_code == 422: rich_output( - f"Failed to create environment {env.upper()}\n" + - f"Repository: {org}/{name}", + f"Failed to create environment {env.upper()}", fmt="blink bold red" - ) + ) else: - rich_output( - f"Failed to create environment {env.upper()}\n" + - f"Status code: {response.status_code}", - fmt="blink bold red" - ) - else: - return False + rprint(f"Error: {e}") def main(): @@ -459,7 +442,7 @@ def main(): elif args.command == "dependabot": dependabot_security(args.name, args.enabled, args.org) elif args.command == "environment": - deployment_environments(args.name, args.env, args.org) + deployment_environment(args.name, args.env, args.org) else: return False