def on_change_request_review(ack, body, client, view, logger): """ Populate context with selections, validate via Switcher API then publish view for review """ user = body["user"] team_id = body["team"]["id"] ack() environment = get_state_value(view, "selection_environment") context = { "environment": environment, "environment_alias": "Production" if environment == "default" else environment, "group": get_state_value(view, "selection_group"), "switcher": get_state_value(view, "selection_switcher"), "status": get_state_value(view, "selection_status") } view = create_request_review(context) view["private_metadata"] = json.dumps(context) try: validate_context_request(context) SwitcherService().validate_ticket(team_id, context) # Request review client.views_publish( user_id = user["id"], view = view ) except Exception as e: logger.error(f"Error request review: {e}") client.chat_postMessage( channel = user["id"], text = f"There was an error with your request: {e}" )
def on_group_selected(ack, body, client, logger): """ Load switchers when group is selected """ env_selected = get_state_value(body["view"], "selection_environment") group_selected = get_selected_action(body) team_id = body["team"]["id"] ack() try: switchers = SwitcherService().get_switchers( team_id, env_selected, group_selected ) populate_selection( body = body["view"], item = "Switcher", values = get_keyval("key", switchers) ) populate_selection(body["view"], "Status", [ { "name": "Enable", "value": "true" }, { "name": "Disable", "value": "false" } ]) view_hash = body["view"]["hash"] view_id = body["view"]["id"] prepare_body(body) client.views_update( view_id = view_id, hash = view_hash, view = body["view"] ) except Exception as e: logger.error(f"Error selecting group: {e}")
def on_request_denied(ack, body, client, logger): """ Deny ticket through Switcher API and update chat message """ message_ts = body["message"]["ts"] team_id = body["team"]["id"] ticket_id = body["actions"][0]["value"] channel_id = body["channel"]["id"] ack() message_blocks = create_block_message(":large_red_square: *Change request denied*") message_blocks.append(body["message"]["blocks"][2]) try: SwitcherService().deny_request(team_id, ticket_id) client.chat_update( channel = channel_id, text = "Change request denied", ts = message_ts, blocks = message_blocks ) except Exception as e: logger.error(f"Error on denying: {e}") client.chat_update( channel = channel_id, text = e.args[0], ts = message_ts, blocks = create_block_message(f":large_yellow_square: *{e.args[0]}*") )
def on_environment_selected(ack, body, client, logger): """ Load groups when environment is selected """ env_selected = get_selected_action(body) team_id = body["team"]["id"] ack() try: groups = SwitcherService().get_groups(team_id, env_selected) populate_selection( body = body["view"], item = "Group", values = get_keyval("name", groups) ) view_hash = body["view"]["hash"] view_id = body["view"]["id"] prepare_body(body) client.views_update( view_id = view_id, hash = view_hash, view = body["view"] ) except Exception as e: logger.error(f"Error selecting environment: {e}")
def on_change_request_opened(ack, body, client, logger): change_request = copy.deepcopy(MODAL_REQUEST) team_id = body["team"]["id"] logger.warning(f"Team ID: {team_id}") ack() try: switcher_service = SwitcherService() envs = switcher_service.get_environments(team_id) populate_selection(body=change_request, item="Environment", values=get_environment_keyval(envs)) client.views_open(trigger_id=body["trigger_id"], view=change_request) except Exception as e: logger.error(f"Error opening change request form: {e}")
def on_submit(ack, body, client, logger): """ Create ticket, return to home view then publish approval message """ user = body["user"] team_id = body["team"]["id"] ack() observation = get_state_value(body["view"], "selection_observation") context = { **read_request_metadata(body["view"]), "observations": "" if observation is None else observation, } try: ticket = SwitcherService().create_ticket(team_id, context) # Return to initial state client.views_publish( response_action = "push", user_id = user["id"], view = APP_HOME ) # Redirect approval client.chat_postMessage( channel = ticket.get("channel_id"), text = "The following request has been opened for approval.", blocks = get_request_message(ticket.get("ticket_id"), context) ) except SlackApiError as e: logger.error(f"API has errors on submitting: {e}") message = e.response["error"] client.chat_postMessage( channel = user["id"], text = f"There was an error with your submission: {message}" ) except Exception as e: logger.error(f"Error on submitting: {e}") client.chat_postMessage( channel = user["id"], text = f"There was an error with your request: {e}" )