Skip to content
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
36 changes: 16 additions & 20 deletions src/controller/change_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def on_domain_selected(ack, body, client, logger):
domain_id = get_selected_action(body)
domain_name = get_selected_action_text(body)

envs = SwitcherService().get_environments(team_id, domain_id)
envs = SwitcherService().get_environments(team_id, domain_id or "") or []

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

groups = SwitcherService().get_groups(team_id, domain_id, env_selected)
groups = SwitcherService().get_groups(team_id, domain_id, env_selected or "") or []

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

try:
# Collect args
env_selected = get_state_value(body["view"], "selection_environment")
env_selected = get_state_value(body["view"], "selection_environment") or ""
group_selected = get_selected_action(body)
group_status = get_selected_action_status(body)
team_id = body["team"]["id"]
domain_id = read_request_metadata(body["view"])["domain_id"]

switchers = SwitcherService().get_switchers(
team_id, domain_id, env_selected, group_selected
)
team_id, domain_id, env_selected, group_selected or ""
) or []

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

ack()
user = body["user"]
team_id = body["team"]["id"]

try:
# Collect args
user = body["user"]
team_id = body["team"]["id"]
environment = get_state_value(view, "selection_environment")

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

ack()

user = body["user"]
team_id = body["team"]["id"]

try:
# Collect args
observation = get_state_value(body["view"], "selection_observation")
Expand All @@ -259,8 +261,6 @@ def on_submit(ack, body, client, logger):
}

domain_id = context["domain_id"]
user = body["user"]
team_id = body["team"]["id"]

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

ack()
message_ts = body["message"]["ts"]
team_id = body["team"]["id"]
channel_id = body["channel"]["id"]

try:
# Collect args
message_ts = body["message"]["ts"]
team_id = body["team"]["id"]
channel_id = body["channel"]["id"]

ticket_payload = json.loads(body["actions"][0]["value"])
domain_id = ticket_payload["domain_id"]
ticket_id = ticket_payload["id"]
Expand Down Expand Up @@ -343,13 +341,11 @@ def on_request_denied(ack, body, client, logger):
""" Deny ticket through Switcher API and update chat message """

ack()
message_ts = body["message"]["ts"]
team_id = body["team"]["id"]
channel_id = body["channel"]["id"]

try:
# Collect args
message_ts = body["message"]["ts"]
team_id = body["team"]["id"]
channel_id = body["channel"]["id"]

ticket_payload = json.loads(body["actions"][0]["value"])
domain_id = ticket_payload["domain_id"]
ticket_id = ticket_payload["id"]
Expand Down
2 changes: 1 addition & 1 deletion src/controller/slack_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from store.switcher_store import SwitcherAppInstallationStore

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

return App(
Expand Down
2 changes: 1 addition & 1 deletion src/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SwitcherSlackAppError(Exception):
class SwitcherContextError(SwitcherSlackAppError):
""" Error raised when Context has missing attributes """

def __init__(self, missing: [str]):
def __init__(self, missing: list[str]):
attributes = " - ".join([str(elem) for elem in missing])
msg = f"Missing [{attributes}]"
super(SwitcherContextError, self).__init__(msg)
Expand Down
8 changes: 4 additions & 4 deletions src/services/switcher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def do_delete(self, path: str, params: Optional[dict]) -> Response:
resource = path
),
params = params,
verify = self.__cert
verify = self.__cert_path
)
)

def do_graphql(self, query: str) -> dict:
def do_graphql(self, query_request: str) -> dict:
client = self.__gql_client__("slack-graphql")
query = gql(query)
query = gql(query_request)
return client.execute(query)

def __generate_token__(self, resource: str) -> str:
Expand Down Expand Up @@ -94,6 +94,6 @@ def __gql_client__(self, resource: str) -> Client:
},
use_json = True,
retries = 3,
verify = self.__cert_path
verify = self.__cert_path is not None
)
)
11 changes: 5 additions & 6 deletions src/services/switcher_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class SwitcherService(SwitcherClient):
def __init__(self, *, api_url: Optional[str] = None):
SwitcherClient.__init__(
self,
api_url or os.environ.get("SWITCHER_API_URL")
api_url or os.environ.get("SWITCHER_API_URL") or ""
)

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

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

def get_environments(self, team_id: str, domain_id: str) -> [str]:
def get_environments(self, team_id: str, domain_id: str) -> list[str] | None:
response: dict = self.do_graphql(f'''
query {{
configuration(
Expand All @@ -44,9 +44,8 @@ def get_environments(self, team_id: str, domain_id: str) -> [str]:
environments = configuration.get("environments", None)
if environments is not None:
return environments
return []

def get_groups(self, team_id: str, domain_id: str, environment: str) -> [dict]:
def get_groups(self, team_id: str, domain_id: str, environment: str) -> list[dict] | None:
response: dict = self.do_graphql(f'''
query {{
configuration(
Expand All @@ -68,7 +67,7 @@ def get_groups(self, team_id: str, domain_id: str, environment: str) -> [dict]:
if groups is not None:
return groups

def get_switchers(self, team_id: str, domain_id: str, environment: str, group: str) -> [dict]:
def get_switchers(self, team_id: str, domain_id: str, environment: str, group: str) -> list[dict] | None:
response: dict = self.do_graphql(f'''
query {{
configuration(
Expand Down
6 changes: 3 additions & 3 deletions src/store/switcher_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ async def async_save(self, installation: Installation):
return self.save(installation)

def save(self, installation: Installation):
e_id = installation.enterprise_id
t_id = installation.team_id
u_id = installation.user_id
e_id = installation.enterprise_id or ""
t_id = installation.team_id or ""
u_id = installation.user_id or ""

try:
self._store_service.save_installation(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/slack_payload_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_state_name(view, option):
if element_name is not None:
return element_name.get("selected_option", None).get("text", None).get("text", None)

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

element_value = ""
Expand Down
16 changes: 8 additions & 8 deletions src/utils/switcher_util.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
from errors import SwitcherContextError

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

key_val: [dict] = []
key_val: list[dict] = []
for val in values:
key_val.append({
"name": "Production" if val == "default" else val,
"value": val
})
return key_val

def get_keyval(key: str, values: [dict]) -> [dict]:
def get_keyval(key: str, values: list[dict]) -> list[dict]:
""" Returns a dict of key and value """
key_val: [dict] = []

key_val: list[dict] = []
for val in values:
activated = "[on]" if val["activated"] else "[off]"
key_val.append({ "name": f"{activated} {val[key]}", "value": val[key] })
return key_val

def get_keyval_of_keys(keys: [str], values: [dict]) -> [dict]:
def get_keyval_of_keys(keys: list[str], values: list[dict]) -> list[dict]:
""" Returns a dict of key and value of a list of keys"""
key_val: [dict] = []

key_val: list[dict] = []
for val in values:
key_val.append({ "name": val[keys[0]], "value": str(val[keys[1]]) })
return key_val
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/change_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def build_request_view(
private_metadata: str = "",
actions_fixture: dict = {},
state_fixture: dict = {},
blocks_fixture: dict = []
blocks_fixture: dict | list = []
):
""" Create a change request input based on the action fixture """

Expand Down
2 changes: 1 addition & 1 deletion tests/test_switcher_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def test_submit_request(client):
),
client = client,
logger = logging.getLogger()
)
) or {}

with open(ON_SUBMIT) as f:
expected_result = json.load(f)
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/mock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
mock_response = mock.Mock(**{})
mock_response_path = ""

def set_mock_response(path: str, fixture: dict, status_code: int = 200):
def set_mock_response(path: str, fixture: dict | list, status_code: int = 200):
global mock_response
global mock_response_path

Expand Down Expand Up @@ -99,7 +99,7 @@ def wrapper(*args, **kwargs):
return wrapper
return mock_decorator

def mock_switcher_client(method: str, fixture: dict, status: int = 200):
def mock_switcher_client(method: str, fixture: dict | list, status: int = 200):
def mock_decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
Expand Down