Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.

Commit 823b31d

Browse files
author
Steffen Ohrendorf
committed
Address first review comments
1 parent 2d71c31 commit 823b31d

File tree

7 files changed

+291
-70
lines changed

7 files changed

+291
-70
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v1
1414

15+
- name: Set up Python
16+
uses: actions/setup-python@v1
17+
with:
18+
python-version: 3.8
19+
20+
- name: Install Poetry
21+
run: pip install poetry==$POETRY_VERSION
22+
env:
23+
POETRY_VERSION: 1.0.2
24+
25+
- name: Cache Poetry virtualenv
26+
uses: actions/cache@v1
27+
id: poetry-cache
28+
with:
29+
path: ~/.cache/pypoetry/virtualenvs
30+
key: poetry-${{ hashFiles('**/poetry.lock') }}
31+
restore-keys: |
32+
poetry-${{ hashFiles('**/poetry.lock') }}
33+
- name: Install Dependencies
34+
if: steps.poetry-cache.outputs.cache-hit != 'true'
35+
run: |
36+
cd docker && poetry install
37+
38+
- name: Lint
39+
run: |
40+
cd docker && poetry run flake8
41+
1542
- name: Build and publish image
1643
run: |
1744
# For branch names other than master (e.g. feature/test), append last branch name component (test) to the tag

docker/ghd.py

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python3
22
import asyncio
3-
import click
43
import os
5-
import colorama
6-
74
from functools import wraps
8-
from github import DeploymentState, GitHub, read_github_event_data, get_current_deployment_id, get_current_environment
9-
from util import get_repo_or_fallback
5+
6+
import click
7+
import colorama
8+
from github import DeploymentState, GitHub, get_current_deployment_id, get_current_environment, read_github_event_data
9+
from util import get_repo_fallback
1010

1111

1212
def coroutine(f):
@@ -17,69 +17,120 @@ def wrapper(*args, **kwargs):
1717
return wrapper
1818

1919

20+
def click_repo_option():
21+
repo_path = get_repo_fallback(read_github_event_data())
22+
return click.option("-r", "--repo",
23+
required=repo_path is None,
24+
default=repo_path,
25+
help="Repository to use, e.g. moneymeets/ghd")
26+
27+
28+
def click_deployment_id_option():
29+
deployment_id = get_current_deployment_id()
30+
return click.option("-d", "--deployment-id",
31+
type=int,
32+
required=deployment_id is None,
33+
default=deployment_id,
34+
help="Deployment ID")
35+
36+
2037
@click.group()
2138
def main_group():
2239
pass
2340

2441

2542
@main_group.command(name="list", short_help="List deployments")
26-
@click.option("-r", "--repo", required=False, help="Repository to use, e.g. moneymeets/ghd")
27-
@click.option("-v", "--verbose", required=False, is_flag=True, flag_value=True, default=False,
43+
@click_repo_option()
44+
@click.option("-v", "--verbose",
45+
required=False,
46+
is_flag=True,
47+
flag_value=True,
48+
default=False,
2849
help="Print deployment states (slow)")
29-
@click.option("-l", "--limit", required=False, default=10, help="How many deployments to list")
50+
@click.option("-l", "--limit",
51+
required=False,
52+
default=10,
53+
help="How many deployments to list")
3054
@coroutine
3155
async def cmd_list(repo: str, verbose: bool, limit: int):
32-
github_event_data = read_github_event_data()
33-
async with GitHub(repo_path=get_repo_or_fallback(repo, github_event_data)) as gh:
56+
async with GitHub(repo_path=repo) as gh:
3457
await gh.list(limit=limit, verbose=verbose)
3558

3659

3760
@main_group.command(name="deploy", short_help="Create new deployment")
38-
@click.option("-r", "--repo", required=False, help="Repository to use, e.g. moneymeets/ghd")
39-
@click.option("-R", "--ref", required=True, help="Reference to create the deployment from")
40-
@click.option("-e", "--environment", required=True, prompt=True, help="Environment name")
41-
@click.option("-T", "--task", default="deploy", help="Deployment task")
42-
@click.option("-t", "--transient", required=False, is_flag=True, flag_value=True, prompt=True, default=False,
61+
@click_repo_option()
62+
@click.option("-R", "--ref",
63+
required=True,
64+
help="Reference to create the deployment from")
65+
@click.option("-e", "--environment",
66+
required=True,
67+
prompt=True,
68+
help="Environment name")
69+
@click.option("-T", "--task",
70+
default="deploy",
71+
help="Deployment task")
72+
@click.option("-t", "--transient",
73+
required=False,
74+
is_flag=True,
75+
flag_value=True,
76+
prompt=True,
77+
default=False,
4378
help="Mark as transient environment")
44-
@click.option("-p", "--production", required=False, is_flag=True, flag_value=True, prompt=True, default=False,
79+
@click.option("-p", "--production",
80+
required=False,
81+
is_flag=True,
82+
flag_value=True,
83+
prompt=True,
84+
default=False,
4585
help="Mark as production environment")
46-
@click.option("-d", "--description", default="Deployed via GHD", prompt=True, help="Deployment description")
86+
@click.option("-d", "--description",
87+
default="Deployed via GHD",
88+
prompt=True,
89+
help="Deployment description")
4790
@coroutine
4891
async def cmd_deploy(repo: str, ref: str, environment: str, task: str, transient: bool, production: bool,
4992
description: str):
50-
github_event_data = read_github_event_data()
51-
async with GitHub(repo_path=get_repo_or_fallback(repo, github_event_data)) as gh:
52-
await gh.deploy(environment=environment, ref=ref or os.environ.get("GITHUB_SHA"),
93+
async with GitHub(repo_path=repo) as gh:
94+
await gh.deploy(environment=environment,
95+
ref=ref or os.environ.get("GITHUB_SHA"),
5396
transient=transient,
5497
production=production,
5598
task=task,
5699
description=description)
57100

58101

59102
@main_group.command(name="set-state", short_help="Set deployment state")
60-
@click.option("-r", "--repo", required=False, help="Repository to use, e.g. moneymeets/ghd")
61-
@click.option("-e", "--environment", required=True, default=get_current_environment(), help="Environment name")
62-
@click.option("-d", "--deployment-id", type=int, required=True, default=get_current_deployment_id(),
63-
help="Deployment ID")
103+
@click_repo_option()
104+
@click.option("-e", "--environment",
105+
required=True,
106+
default=get_current_environment(),
107+
help="Environment name")
108+
@click_deployment_id_option()
64109
@click.option("-s", "--state",
65110
type=click.Choice(choices=DeploymentState.__members__.keys()),
66-
required=True, help="State")
67-
@click.option("-D", "--description", default="Deployed via GHD", help="Deployment description")
111+
required=True,
112+
help="State")
113+
@click.option("-D", "--description",
114+
default="Deployed via GHD",
115+
help="Deployment description")
68116
@coroutine
69117
async def cmd_set_state(repo: str, environment: str, deployment_id: int, state: str, description: str):
70-
async with GitHub(repo_path=get_repo_or_fallback(repo, read_github_event_data())) as gh:
118+
async with GitHub(repo_path=repo) as gh:
71119
await gh.create_deployment_status(deployment_id=deployment_id,
72120
state=DeploymentState[state],
73121
environment=environment,
74122
description=description)
75123

76124

77125
@main_group.command(name="inspect", short_help="Inspect deployment state history")
78-
@click.option("-r", "--repo", required=False, help="Repository to use, e.g. moneymeets/ghd")
79-
@click.argument("deployment-id", type=int, required=True, nargs=1)
126+
@click_repo_option()
127+
@click.argument("deployment-id",
128+
type=int,
129+
required=True,
130+
nargs=1)
80131
@coroutine
81132
async def cmd_inspect(repo: str, deployment_id: int):
82-
async with GitHub(repo_path=get_repo_or_fallback(repo, read_github_event_data())) as gh:
133+
async with GitHub(repo_path=repo) as gh:
83134
await gh.inspect(deployment_id=deployment_id)
84135

85136

@@ -88,5 +139,5 @@ def run_main():
88139
main_group()
89140

90141

91-
if __name__ == '__main__':
142+
if __name__ == "__main__":
92143
run_main()

docker/github.py

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
import enum
12
import json
23
import os
34
import sys
4-
import enum
55

66
import aiohttp
77
import colorama
88
import progressbar
99
import tabulate
10-
11-
from util import short_sha, bool_to_str, deep_dict_get
12-
from output import print_success, color_unknown, print_info, color_str
10+
from output import color_str, color_unknown, print_info, print_success
11+
from util import bool_to_str, deep_dict_get, short_sha
1312

1413

1514
class DeploymentState(enum.Enum):
@@ -22,6 +21,10 @@ class DeploymentState(enum.Enum):
2221

2322

2423
class GitHub:
24+
@staticmethod
25+
def _api_url(path: str) -> str:
26+
return f"https://api.github.com{path}"
27+
2528
def __init__(self, repo_path: str):
2629
assert repo_path
2730

@@ -61,7 +64,7 @@ def __exit__(self,
6164
# __exit__ should exist in pair with __enter__ but never executed
6265
pass # pragma: no cover
6366

64-
async def __aenter__(self) -> 'GitHub':
67+
async def __aenter__(self) -> "GitHub":
6568
return self
6669

6770
async def __aexit__(self,
@@ -72,47 +75,39 @@ async def __aexit__(self,
7275
await self.session_ant_man.close()
7376

7477
async def get(self, path: str):
75-
# print(f"GET {path}")
76-
async with self.session_ant_man.get(f"https://api.github.com{path}") as response:
78+
async with self.session_ant_man.get(self._api_url(path)) as response:
7779
result = await response.json()
78-
# print(result)
7980
return result
8081

8182
async def get_flash(self, path: str):
82-
# print(f"GET {path}")
83-
async with self.session_flash.get(f"https://api.github.com{path}") as response:
83+
async with self.session_flash.get(self._api_url(path)) as response:
8484
result = await response.json()
85-
# print(result)
8685
return result
8786

8887
async def post(self, path: str, json):
89-
# print(f"POST {path}")
90-
async with self.session_ant_man.post(f"https://api.github.com{path}", json=json) as response:
88+
async with self.session_ant_man.post(self._api_url(path), json=json) as response:
9189
result = await response.json()
92-
# print(result)
9390
return result
9491

9592
async def post_flash(self, path: str, json):
96-
# print(f"POST {path}")
97-
async with self.session_flash.post(f"https://api.github.com{path}", json=json) as response:
93+
async with self.session_flash.post(self._api_url(path), json=json) as response:
9894
result = await response.json()
99-
# print(result)
10095
return result
10196

10297
async def get_deployments(self) -> list:
10398
try:
104-
return sorted(await self.get(f'/repos/{self.repo_path}/deployments'), key=lambda e: e["id"], reverse=True)
99+
return sorted(await self.get(f"/repos/{self.repo_path}/deployments"), key=lambda e: e["id"], reverse=True)
105100
except TypeError:
106101
return []
107102

108103
async def get_deployment_statuses(self, deployment_id: int) -> list:
109-
return sorted(await self.get(f'/repos/{self.repo_path}/deployments/{deployment_id}/statuses'),
104+
return sorted(await self.get(f"/repos/{self.repo_path}/deployments/{deployment_id}/statuses"),
110105
key=lambda e: e["id"],
111106
reverse=True)
112107

113108
async def create_deployment(self, ref: str, environment: str, transient: bool, production: bool, task: str,
114109
description: str):
115-
return await self.post(f'/repos/{self.repo_path}/deployments', {
110+
return await self.post(f"/repos/{self.repo_path}/deployments", {
116111
"ref": ref,
117112
"auto_merge": False,
118113
"environment": environment,
@@ -125,7 +120,7 @@ async def create_deployment(self, ref: str, environment: str, transient: bool, p
125120

126121
async def create_deployment_status(self, deployment_id: int, state: DeploymentState, environment: str,
127122
description: str):
128-
return await self.post_flash(f'/repos/{self.repo_path}/deployments/{deployment_id}/statuses', {
123+
return await self.post_flash(f"/repos/{self.repo_path}/deployments/{deployment_id}/statuses", {
129124
"state": state.name,
130125
"description": description,
131126
"environment": environment,
@@ -204,18 +199,18 @@ async def inspect(self, deployment_id: int):
204199

205200
async def deploy(self, environment: str, ref: str, transient: bool, production: bool, task: str, description: str):
206201
print_info("Creating deployment")
207-
tmp = await self.create_deployment(ref=ref,
208-
environment=environment,
209-
transient=transient,
210-
production=production,
211-
task=task,
212-
description=description)
213-
if "id" not in tmp:
214-
print(tmp)
202+
deployment_creation_result = await self.create_deployment(ref=ref,
203+
environment=environment,
204+
transient=transient,
205+
production=production,
206+
task=task,
207+
description=description)
208+
if "id" not in deployment_creation_result:
209+
print(deployment_creation_result)
215210
raise RuntimeError()
216211

217-
print(f"::set-output name=deployment_id::{tmp['id']}")
218-
print_success(f"Deployment {tmp['id']} created")
212+
print(f"::set-output name=deployment_id::{deployment_creation_result['id']}")
213+
print_success(f"Deployment {deployment_creation_result['id']} created")
219214

220215

221216
_github_event_data = None
@@ -228,7 +223,8 @@ def read_github_event_data():
228223
return _github_event_data
229224

230225
_github_event_data = dict()
231-
if (github_event_path := os.environ.get("GITHUB_EVENT_PATH")) and os.path.exists(github_event_path):
226+
github_event_path = os.environ.get("GITHUB_EVENT_PATH") # TODO: Use walrus operator when flake8 supports it
227+
if github_event_path and os.path.exists(github_event_path):
232228
print_info("Found GitHub Event Path")
233229
with open(github_event_path, "r") as f:
234230
_github_event_data = json.load(f)

0 commit comments

Comments
 (0)