|
| 1 | +import base64 |
| 2 | + |
| 3 | +import aiofiles |
| 4 | +import asyncclick as click |
| 5 | + |
| 6 | +from github_deploy.commands._constants import REPOS_URL, BASE_URL |
| 7 | +from github_deploy.commands._http_utils import get, delete, put |
| 8 | +from github_deploy.commands._utils import get_headers |
| 9 | + |
| 10 | + |
| 11 | +async def list_repos(*, session, org, token): |
| 12 | + url = REPOS_URL.format(org=org) |
| 13 | + click.echo("Retrieving repos at {}".format(url)) |
| 14 | + response = await get(session=session, url=url, headers=get_headers(token=token)) |
| 15 | + return response |
| 16 | + |
| 17 | + |
| 18 | +async def delete_content( |
| 19 | + *, |
| 20 | + session, |
| 21 | + repo, |
| 22 | + dest, |
| 23 | + token, |
| 24 | + semaphore, |
| 25 | + exists, |
| 26 | + current_sha, |
| 27 | +): |
| 28 | + data = {"message": "Deleted {}".format(dest)} |
| 29 | + if exists: |
| 30 | + data["sha"] = current_sha |
| 31 | + |
| 32 | + url = BASE_URL.format(repo=repo, path=dest) |
| 33 | + |
| 34 | + async with semaphore: |
| 35 | + response = await delete( |
| 36 | + session=session, url=url, data=data, headers=get_headers(token=token) |
| 37 | + ) |
| 38 | + |
| 39 | + return response |
| 40 | + |
| 41 | + |
| 42 | +async def check_exists(*, session, repo, dest, token, semaphore, skip_missing): |
| 43 | + url = BASE_URL.format(repo=repo, path=dest) |
| 44 | + |
| 45 | + async with semaphore: |
| 46 | + response = await get( |
| 47 | + session=session, |
| 48 | + url=url, |
| 49 | + headers=get_headers(token=token), |
| 50 | + skip_missing=skip_missing, |
| 51 | + ) |
| 52 | + |
| 53 | + return response |
| 54 | + |
| 55 | + |
| 56 | +async def upload_content( |
| 57 | + *, |
| 58 | + session, |
| 59 | + repo, |
| 60 | + source, |
| 61 | + dest, |
| 62 | + token, |
| 63 | + semaphore, |
| 64 | + exists, |
| 65 | + current_sha, |
| 66 | + current_content |
| 67 | +): |
| 68 | + async with semaphore: |
| 69 | + async with aiofiles.open(source, mode="rb") as f: |
| 70 | + output = await f.read() |
| 71 | + base64_content = base64.b64encode(output).decode("ascii") |
| 72 | + |
| 73 | + if current_content == base64_content: |
| 74 | + click.echo("Skipping: Contents are the same.") |
| 75 | + return |
| 76 | + |
| 77 | + data = { |
| 78 | + "message": "Updated {}".format(dest) |
| 79 | + if exists |
| 80 | + else "Added {}".format(dest), |
| 81 | + "content": base64_content, |
| 82 | + } |
| 83 | + if exists: |
| 84 | + data["sha"] = current_sha |
| 85 | + |
| 86 | + url = BASE_URL.format(repo=repo, path=dest) |
| 87 | + |
| 88 | + async with semaphore: |
| 89 | + response = await put( |
| 90 | + session=session, url=url, data=data, headers=get_headers(token=token) |
| 91 | + ) |
| 92 | + |
| 93 | + return response |
0 commit comments