From 462164a114f91d17a78ed7c5ace5f3b956d3b010 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Thu, 5 Jan 2023 22:06:16 -0700 Subject: [PATCH] feat: update api url and add new features --- github_deploy/commands/_constants.py | 2 +- github_deploy/commands/_repo_utils.py | 40 +++++++++++++++++++++------ github_deploy/commands/delete.py | 2 +- github_deploy/commands/upload.py | 24 ++++++++++------ 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/github_deploy/commands/_constants.py b/github_deploy/commands/_constants.py index a8fb431..ab4c9ba 100644 --- a/github_deploy/commands/_constants.py +++ b/github_deploy/commands/_constants.py @@ -1,2 +1,2 @@ -REPOS_URL = "https://api.github.com/search/repositories?q=org:{org}" +REPOS_URL = "https://api.github.com/users/{org}/repos" FILE_CONTENTS_URL = "https://api.github.com/repos/{repo}/contents/{path}" diff --git a/github_deploy/commands/_repo_utils.py b/github_deploy/commands/_repo_utils.py index 64b7d55..9d42286 100644 --- a/github_deploy/commands/_repo_utils.py +++ b/github_deploy/commands/_repo_utils.py @@ -1,9 +1,8 @@ import base64 import os -import aiofiles import asyncclick as click -from aiofiles import os as aiofiles_os +from aiofiles import os as aiofiles_os, open as aiofiles_open from github_deploy.commands._constants import REPOS_URL, FILE_CONTENTS_URL from github_deploy.commands._http_utils import get, delete, put @@ -69,25 +68,44 @@ async def upload_content( current_content ): async with semaphore: - async with aiofiles.open(source, mode="rb") as f: + async with aiofiles_open(source, mode="rb") as f: output = await f.read() base64_content = base64.b64encode(output).decode("ascii") if current_content == base64_content: - click.echo("Skipping: Contents are the same.") + click.echo( + click.style( + f"Skipping {source} to {repo}/{dest}: No changes detected.", + fg="yellow", + bold=True, + ) + ) return else: if exists: - click.echo("Storing backup of existing file...") + click.echo( + click.style( + "Storing backup of existing file at {repo}/{path}...".format( + repo=repo, path=dest + ), + fg="cyan", + ), + ) dirname, filename = os.path.split(f"{repo}/{dest}") await aiofiles_os.makedirs(dirname, exist_ok=True) - async with aiofiles.open(f"{dirname}/{filename}", mode="wb") as f: + async with aiofiles_open(f"{dirname}/{filename}", mode="wb") as f: await f.write(base64.b64decode(current_content)) elif only_update: - click.echo(f"Skipping: only updating existing files.") + click.echo( + click.style( + f"Updates only: Skipped uploading {source} to {repo}/{dest}. File does not exist.", + fg="yellow", + bold=True, + ) + ) return data = { @@ -101,7 +119,13 @@ async def upload_content( url = FILE_CONTENTS_URL.format(repo=repo, path=dest) - click.echo(f"Uploading {source} to {repo}/{dest}...") + click.echo( + click.style( + f"Uploading {source} to {repo}/{dest}...", + fg="green", + bold=True, + ) + ) async with semaphore: response = await put( diff --git a/github_deploy/commands/delete.py b/github_deploy/commands/delete.py index 9b7209e..f34a8ef 100644 --- a/github_deploy/commands/delete.py +++ b/github_deploy/commands/delete.py @@ -87,7 +87,7 @@ async def main(org, token, dest): response = await list_repos(org=org, token=token, session=session) repos = [ get_repo(org=org, project=v["name"]) - for v in response["items"] + for v in response if not v["archived"] ] click.echo( diff --git a/github_deploy/commands/upload.py b/github_deploy/commands/upload.py index 9d2e32d..52813b0 100644 --- a/github_deploy/commands/upload.py +++ b/github_deploy/commands/upload.py @@ -25,7 +25,7 @@ async def handle_file_upload( if exists: if not overwrite: - return click.style( + click.style( "Skipped uploading {source} to {repo}/{path}: Found an existing copy.".format( source=source, repo=repo, @@ -132,7 +132,7 @@ async def main(org, token, source, dest, overwrite, only_update, private): response = await list_repos(org=org, token=token, session=session) repos = [ get_repo(org=org, project=r["name"]) - for r in response["items"] + for r in response if not r["archived"] and can_upload(repo=r, include_private=private) ] @@ -156,15 +156,21 @@ async def main(org, token, source, dest, overwrite, only_update, private): fg="bright_red", ) ) - deploy_msg = ( - 'Deploying "{source}" to "{path}" for all repositories'.format( - source=source, path=dest - ) - if overwrite - else 'Deploying "{source}" to repositories that don\'t already have contents at "{path}"'.format( + + if overwrite: + if only_update: + deploy_msg = "Updating '{source}' for existing files located at '{dest}'".format( + source=source, dest=dest + ) + else: + deploy_msg = "Overwriting '{dest}' with '{source}'".format( + source=source, dest=dest + ) + else: + deploy_msg = "Deploying '{source}' to repositories that don\'t already have contents at '{path}'".format( source=source, path=dest ) - ) + click.echo(click.style(deploy_msg, fg="blue")) c = click.prompt(click.style("Continue? [YN] ", fg="blue"))