Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: update api url and add new features #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion github_deploy/commands/_constants.py
Original file line number Diff line number Diff line change
@@ -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}"
40 changes: 32 additions & 8 deletions github_deploy/commands/_repo_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion github_deploy/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 15 additions & 9 deletions github_deploy/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
]
Expand All @@ -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"))

Expand Down