8000 chore: reformatted and restructured modules by jackton1 · Pull Request #26 · tj-python/github-deploy · GitHub
[go: up one dir, main page]

Skip to content

chore: reformatted and restructured modules #26

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 2 commits into from
Oct 28, 2022
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
60 changes: 1 addition & 59 deletions github_deploy/commands/_http_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import ssl
import certifi

import asyncclick as click

from github_deploy.commands._constants import REPOS_URL
import certifi


async def get(*, session, url, headers=None, skip_missing=False):
Expand Down Expand Up @@ -51,58 +48,3 @@ async def delete(*, session, url, data, headers=None):
) as response:
value = await response.json()
return value


async def list_repos(*, session, org, token):
headers = {
"Authorization": "Bearer {token}".format(token=token),
"Accept": "application/vnd.github+json",
}
url = REPOS_URL.format(org=org)
click.echo("Retrieving repos at {}".format(url))
response = await get(session=session, url=url, headers=headers)
return response


async def delete_content(
*,
session,
repo,
dest,
token,
semaphore,
exists,
current_sha,
):
headers = {
"Authorization": "Bearer {token}".format(token=token),
"Accept": "application/vnd.github+json",
}

data = {"message": "Deleted {}".format(dest)}
if exists:
data["sha"] = current_sha

url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await delete(
session=session, url=url, data=data, headers=headers
)

return response


async def check_exists(*, session, repo, dest, token, semaphore, skip_missing):
headers = {"Authorization": "Bearer {token}".format(token=token)}
url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await get(
session=session,
url=url,
headers=headers,
skip_missing=skip_missing,
)

return response
93 changes: 93 additions & 0 deletions github_deploy/commands/_repo_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import base64

import aiofiles
import asyncclick as click

from github_deploy.commands._constants import REPOS_URL, BASE_URL
from github_deploy.commands._http_utils import get, delete, put
from github_deploy.commands._utils import get_headers


async def list_repos(*, session, org, token):
url = REPOS_URL.format(org=org)
click.echo("Retrieving repos at {}".format(url))
response = await get(session=session, url=url, headers=get_headers(token=token))
return response


async def delete_content(
*,
session,
repo,
dest,
token,
semaphore,
exists,
current_sha,
):
data = {"message": "Deleted {}".format(dest)}
if exists:
data["sha"] = current_sha

url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await delete(
session=session, url=url, data=data, headers=get_headers(token=token)
)

return response


async def check_exists(*, session, repo, dest, token, semaphore, skip_missing):
url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await get(
session=session,
url=url,
headers=get_headers(token=token),
skip_missing=skip_missing,
)

return response


async def upload_content(
*,
session,
repo,
source,
dest,
token,
semaphore,
exists,
current_sha,
current_content
):
async with semaphore:
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.")
return

data = {
"message": "Updated {}".format(dest)
if exists
else "Added {}".format(dest),
"content": base64_content,
}
if exists:
data["sha"] = current_sha

url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await put(
session=session, url=url, data=data, headers=get_headers(token=token)
)

return response
7 changes: 7 additions & 0 deletions github_deploy/commands/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ def can_upload(*, repo, include_private):
if include_private and repo["private"] is True
else not repo["private"]
)


def get_headers(*, token):
return {
"Authorization": "Bearer {token}".format(token=token),
"Accept": "application/vnd.github+json",
}
3 changes: 1 addition & 2 deletions github_deploy/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import aiohttp
import asyncclick as click

from github_deploy.commands._constants import BASE_URL
from github_deploy.commands._http_utils import delete, get, list_repos, delete_contents, check_exists
from github_deploy.commands._repo_utils import list_repos, delete_content, check_exists
from github_deploy.commands._utils import get_repo


Expand Down
65 changes: 1 addition & 64 deletions github_deploy/commands/upload.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,12 @@
import asyncio
import base64

import aiofiles
import aiohttp
import asyncclick as click

from github_deploy.commands._constants import BASE_URL
from github_deploy.commands._http_utils import put, list_repos, get
from github_deploy.commands._repo_utils import list_repos, check_exists, upload_content
from github_deploy.commands._utils import get_repo, can_upload


async def upload_content(
*,
session,
repo,
source,
dest,
token,
semaphore,
exists,
current_sha,
current_content
):
headers = {
"Authorization": "token {token}".format(token=token),
"Accept": "application/vnd.github.v3+json",
}

async with semaphore:
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.")
return

data = {
"message": "Updated {}".format(dest)
if exists
else "Added {}".format(dest),
"content": base64_content,
}
if exists:
data["sha"] = current_sha

url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await put(
session=session, url=url, data=data, headers=headers
)

return response


async def check_exists(*, session, repo, dest, token, semaphore, skip_missing):
headers = {"Authorization": "token {token}".format(token=token)}
url = BASE_URL.format(repo=repo, path=dest)

async with semaphore:
response = await get(
session=session,
url=url,
headers=headers,
skip_missing=skip_missing,
)

return response


async def handle_file_upload(
*, repo, source, dest, overwrite, token, semaphore, session
):
Expand Down
3 changes: 2 additions & 1 deletion github_deploy/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncclick as click
import os

import asyncclick as click

plugin_folder = os.path.join(os.path.dirname(__file__), "commands")


Expand Down
0