Skip to content

Commit 1e531ed

Browse files
authored
chore: fixed code smells (#109)
1 parent 27a08b2 commit 1e531ed

File tree

11 files changed

+43
-48
lines changed

11 files changed

+43
-48
lines changed

src/controller/change_request.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def on_domain_selected(ack, body, client, logger):
3535
domain_id = get_selected_action(body)
3636
domain_name = get_selected_action_text(body)
3737

38-
envs = SwitcherService().get_environments(team_id, domain_id)
38+
envs = SwitcherService().get_environments(team_id, domain_id or "") or []
3939

4040
# Clear previous selection
4141
populate_selection(body["view"], "Group", NEW_SELECTION)
@@ -82,7 +82,7 @@ def on_environment_selected(ack, body, client, logger):
8282
team_id = body["team"]["id"]
8383
domain_id = read_request_metadata(body["view"])["domain_id"]
8484

85-
groups = SwitcherService().get_groups(team_id, domain_id, env_selected)
85+
groups = SwitcherService().get_groups(team_id, domain_id, env_selected or "") or []
8686

8787
# Clear previous selection
8888
populate_selection(body["view"], "Group", NEW_SELECTION)
@@ -120,15 +120,15 @@ def on_group_selected(ack, body, client, logger):
120120

121121
try:
122122
# Collect args
123-
env_selected = get_state_value(body["view"], "selection_environment")
123+
env_selected = get_state_value(body["view"], "selection_environment") or ""
124124
group_selected = get_selected_action(body)
125125
group_status = get_selected_action_status(body)
126126
team_id = body["team"]["id"]
127127
domain_id = read_request_metadata(body["view"])["domain_id"]
128128

129129
switchers = SwitcherService().get_switchers(
130-
team_id, domain_id, env_selected, group_selected
131-
)
130+
team_id, domain_id, env_selected, group_selected or ""
131+
) or []
132132

133133
# Clear previous selection
134134
populate_selection(body["view"], "Switcher", NEW_SELECTION)
@@ -191,11 +191,11 @@ def on_change_request_review(ack, body, client, view, logger):
191191
""" Populate context with selections, validate via Switcher API then publish view for review """
192192

193193
ack()
194+
user = body["user"]
195+
team_id = body["team"]["id"]
194196

195197
try:
196198
# Collect args
197-
user = body["user"]
198-
team_id = body["team"]["id"]
199199
environment = get_state_value(view, "selection_environment")
200200

201201
# Create context and validate
@@ -249,7 +249,9 @@ def on_submit(ack, body, client, logger):
249249
""" Create ticket, return to home view then publish approval message """
250250

251251
ack()
252-
252+
user = body["user"]
253+
team_id = body["team"]["id"]
254+
253255
try:
254256
# Collect args
255257
observation = get_state_value(body["view"], "selection_observation")
@@ -259,8 +261,6 @@ def on_submit(ack, body, client, logger):
259261
}
260262

261263
domain_id = context["domain_id"]
262-
user = body["user"]
263-
team_id = body["team"]["id"]
264264

265265
# Return to initial state
266266
client.views_publish(
@@ -306,13 +306,11 @@ def on_request_approved(ack, body, client, logger):
306306
""" Approve ticket through Switcher API and update chat message """
307307

308308
ack()
309+
message_ts = body["message"]["ts"]
310+
team_id = body["team"]["id"]
311+
channel_id = body["channel"]["id"]
309312

310313
try:
311-
# Collect args
312-
message_ts = body["message"]["ts"]
313-
team_id = body["team"]["id"]
314-
channel_id = body["channel"]["id"]
315-
316314
ticket_payload = json.loads(body["actions"][0]["value"])
317315
domain_id = ticket_payload["domain_id"]
318316
ticket_id = ticket_payload["id"]
@@ -343,13 +341,11 @@ def on_request_denied(ack, body, client, logger):
343341
""" Deny ticket through Switcher API and update chat message """
344342

345343
ack()
344+
message_ts = body["message"]["ts"]
345+
team_id = body["team"]["id"]
346+
channel_id = body["channel"]["id"]
346347

347348
try:
348-
# Collect args
349-
message_ts = body["message"]["ts"]
350-
team_id = body["team"]["id"]
351-
channel_id = body["channel"]["id"]
352-
353349
ticket_payload = json.loads(body["actions"][0]["value"])
354350
domain_id = ticket_payload["domain_id"]
355351
ticket_id = ticket_payload["id"]

src/controller/slack_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from store.switcher_store import SwitcherAppInstallationStore
1414

1515
class SlackAppHandler:
16-
def build_app(self, api_url: str = os.environ.get("SWITCHER_API_URL")):
16+
def build_app(self, api_url: str = os.environ.get("SWITCHER_API_URL") or "") -> App:
1717
""" Build the Slack App Settings """
1818

1919
return App(

src/errors/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class SwitcherSlackAppError(Exception):
44
class SwitcherContextError(SwitcherSlackAppError):
55
""" Error raised when Context has missing attributes """
66

7-
def __init__(self, missing: [str]):
7+
def __init__(self, missing: list[str]):
88
attributes = " - ".join([str(elem) for elem in missing])
99
msg = f"Missing [{attributes}]"
1010
super(SwitcherContextError, self).__init__(msg)

src/services/switcher_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def do_delete(self, path: str, params: Optional[dict]) -> Response:
4848
resource = path
4949
),
5050
params = params,
51-
verify = self.__cert
51+
verify = self.__cert_path
5252
)
5353
)
5454

55-
def do_graphql(self, query: str) -> dict:
55+
def do_graphql(self, query_request: str) -> dict:
5656
client = self.__gql_client__("slack-graphql")
57-
query = gql(query)
57+
query = gql(query_request)
5858
return client.execute(query)
5959

6060
def __generate_token__(self, resource: str) -> str:
@@ -94,6 +94,6 @@ def __gql_client__(self, resource: str) -> Client:
9494
},
9595
use_json = True,
9696
retries = 3,
97-
verify = self.__cert_path
97+
verify = self.__cert_path is not None
9898
)
9999
)

src/services/switcher_service.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class SwitcherService(SwitcherClient):
1111
def __init__(self, *, api_url: Optional[str] = None):
1212
SwitcherClient.__init__(
1313
self,
14-
api_url or os.environ.get("SWITCHER_API_URL")
14+
api_url or os.environ.get("SWITCHER_API_URL") or ""
1515
)
1616

17-
def get_domains(self, team_id: str) -> [dict]:
17+
def get_domains(self, team_id: str) -> list[dict]:
1818
response = self.do_get(
1919
path = "/slack/v1/domains",
2020
params = {
@@ -27,7 +27,7 @@ def get_domains(self, team_id: str) -> [dict]:
2727

2828
return json.loads(response.data.decode('UTF-8'))
2929

30-
def get_environments(self, team_id: str, domain_id: str) -> [str]:
30+
def get_environments(self, team_id: str, domain_id: str) -> list[str] | None:
3131
response: dict = self.do_graphql(f'''
3232
query {{
3333
configuration(
@@ -44,9 +44,8 @@ def get_environments(self, team_id: str, domain_id: str) -> [str]:
4444
environments = configuration.get("environments", None)
4545
if environments is not None:
4646
return environments
47-
return []
4847

49-
def get_groups(self, team_id: str, domain_id: str, environment: str) -> [dict]:
48+
def get_groups(self, team_id: str, domain_id: str, environment: str) -> list[dict] | None:
5049
response: dict = self.do_graphql(f'''
5150
query {{
5251
configuration(
@@ -68,7 +67,7 @@ def get_groups(self, team_id: str, domain_id: str, environment: str) -> [dict]:
6867
if groups is not None:
6968
return groups
7069

71-
def get_switchers(self, team_id: str, domain_id: str, environment: str, group: str) -> [dict]:
70+
def get_switchers(self, team_id: str, domain_id: str, environment: str, group: str) -> list[dict] | None:
7271
response: dict = self.do_graphql(f'''
7372
query {{
7473
configuration(

src/store/switcher_store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ async def async_save(self, installation: Installation):
2828
return self.save(installation)
2929

3030
def save(self, installation: Installation):
31-
e_id = installation.enterprise_id
32-
t_id = installation.team_id
33-
u_id = installation.user_id
31+
e_id = installation.enterprise_id or ""
32+
t_id = installation.team_id or ""
33+
u_id = installation.user_id or ""
3434

3535
try:
3636
self._store_service.save_installation(

src/utils/slack_payload_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_state_name(view, option):
9292
if element_name is not None:
9393
return element_name.get("selected_option", None).get("text", None).get("text", None)
9494

95-
def __get_state(key, view, option):
95+
def __get_state(key, view, option) -> str | None:
9696
""" Get selected state element """
9797

9898
element_value = ""

src/utils/switcher_util.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
from errors import SwitcherContextError
22

3-
def get_environment_keyval(values: [str]) -> [dict]:
3+
def get_environment_keyval(values: list[str]) -> list[dict]:
44
""" Returns a dict of environment key and value """
55

6-
key_val: [dict] = []
6+
key_val: list[dict] = []
77
for val in values:
88
key_val.append({
99
"name": "Production" if val == "default" else val,
1010
"value": val
1111
})
1212
return key_val
1313

14-
def get_keyval(key: str, values: [dict]) -> [dict]:
14+
def get_keyval(key: str, values: list[dict]) -> list[dict]:
1515
""" Returns a dict of key and value """
16-
17-
key_val: [dict] = []
16+
17+
key_val: list[dict] = []
1818
for val in values:
1919
activated = "[on]" if val["activated"] else "[off]"
2020
key_val.append({ "name": f"{activated} {val[key]}", "value": val[key] })
2121
return key_val
2222

23-
def get_keyval_of_keys(keys: [str], values: [dict]) -> [dict]:
23+
def get_keyval_of_keys(keys: list[str], values: list[dict]) -> list[dict]:
2424
""" Returns a dict of key and value of a list of keys"""
25-
26-
key_val: [dict] = []
25+
26+
key_val: list[dict] = []
2727
for val in values:
2828
key_val.append({ "name": val[keys[0]], "value": str(val[keys[1]]) })
2929
return key_val

tests/fixtures/change_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def build_request_view(
8282
private_metadata: str = "",
8383
actions_fixture: dict = {},
8484
state_fixture: dict = {},
85-
blocks_fixture: dict = []
85+
blocks_fixture: dict | list = []
8686
):
8787
""" Create a change request input based on the action fixture """
8888

tests/test_switcher_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def test_submit_request(client):
488488
),
489489
client = client,
490490
logger = logging.getLogger()
491-
)
491+
) or {}
492492

493493
with open(ON_SUBMIT) as f:
494494
expected_result = json.load(f)

0 commit comments

Comments
 (0)