Skip to content

Commit c967987

Browse files
committedFeb 7, 2024
feat: add color coding, close #45
1 parent 12164fa commit c967987

File tree

4 files changed

+90
-17
lines changed

4 files changed

+90
-17
lines changed
 

‎src/config.py

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class Config(BaseSettings):
5858

5959
max_number_of_symbols_in_permission_set_name: int = 26
6060

61+
good_result_emoji: str = ":large_green_circle:"
62+
waiting_result_emoji: str = ":large_yellow_circle:"
63+
bad_result_emoji: str = ":red_circle:"
64+
discarded_result_emoji: str = ":white_circle:"
65+
6166
class Config:
6267
frozen = True
6368

‎src/main.py

+50-4
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,26 @@ def handle_button_click(body: dict, client: WebClient, context: BoltContext) ->
122122
cache_for_dublicate_requests["permission_set_name"] = payload.request.permission_set_name
123123

124124
if payload.action == entities.ApproverAction.Discard:
125-
slack_helpers.remove_buttons(payload, client, approver)
125+
blocks = slack_helpers.HeaderSectionBlock.set_color_coding(
126+
blocks=payload.message["blocks"],
127+
color_coding_emoji=cfg.bad_result_emoji,
128+
)
129+
130+
blocks = slack_helpers.remove_blocks(blocks, block_ids=["buttons"])
131+
blocks.append(slack_helpers.button_click_info_block(payload.action, approver.id).to_dict())
132+
133+
text = f"Request was discarded by<@{approver.id}> "
134+
client.chat_update(
135+
channel=payload.channel_id,
136+
ts=payload.thread_ts,
137+
blocks=blocks,
138+
text=text,
139+
)
140+
126141
cache_for_dublicate_requests.clear()
127142
return client.chat_postMessage(
128143
channel=payload.channel_id,
129-
text=f"Request was discarded by<@{approver.id}> ",
144+
text=text,
130145
thread_ts=payload.thread_ts,
131146
)
132147

@@ -147,7 +162,21 @@ def handle_button_click(body: dict, client: WebClient, context: BoltContext) ->
147162
text=f"<@{approver.id}> you can not approve this request",
148163
thread_ts=payload.thread_ts,
149164
)
150-
slack_helpers.remove_buttons(payload, client, approver)
165+
166+
text = f"Permissions granted to <@{requester.id}> by <@{approver.id}>."
167+
blocks = slack_helpers.HeaderSectionBlock.set_color_coding(
168+
blocks=payload.message["blocks"],
169+
color_coding_emoji=cfg.good_result_emoji,
170+
)
171+
172+
blocks = slack_helpers.remove_blocks(blocks, block_ids=["buttons"])
173+
blocks.append(slack_helpers.button_click_info_block(payload.action, approver.id).to_dict())
174+
client.chat_update(
175+
channel=payload.channel_id,
176+
ts=payload.thread_ts,
177+
blocks=blocks,
178+
text=text,
179+
)
151180

152181
access_control.execute_decision(
153182
decision=decision,
@@ -161,7 +190,7 @@ def handle_button_click(body: dict, client: WebClient, context: BoltContext) ->
161190
cache_for_dublicate_requests.clear()
162191
return client.chat_postMessage(
163192
channel=payload.channel_id,
164-
text=f"Permissions granted to <@{requester.id}> by <@{approver.id}>.",
193+
text=text,
165194
thread_ts=payload.thread_ts,
166195
)
167196

@@ -211,6 +240,7 @@ def handle_request_for_access_submittion(
211240
reason=request.reason,
212241
permission_duration=request.permission_duration,
213242
show_buttons=show_buttons,
243+
color_coding_emoji=cfg.waiting_result_emoji,
214244
),
215245
channel=cfg.slack_channel_id,
216246
text=f"Request for access to {account.name} account from {requester.real_name}",
@@ -236,19 +266,35 @@ def handle_request_for_access_submittion(
236266
match decision.reason:
237267
case access_control.DecisionReason.ApprovalNotRequired:
238268
text = "Approval for this Permission Set & Account is not required. Request will be approved automatically."
269+
color_coding_emoji = cfg.good_result_emoji
239270
case access_control.DecisionReason.SelfApproval:
240271
text = "Self approval is allowed and requester is an approver. Request will be approved automatically."
272+
color_coding_emoji = cfg.good_result_emoji
241273
case access_control.DecisionReason.RequiresApproval:
242274
approvers = [slack_helpers.get_user_by_email(client, email) for email in decision.approvers]
243275
mention_approvers = " ".join(f"<@{approver.id}>" for approver in approvers)
244276
text = f"{mention_approvers} there is a request waiting for the approval."
277+
color_coding_emoji = cfg.waiting_result_emoji
245278
case access_control.DecisionReason.NoApprovers:
246279
text = "Nobody can approve this request."
280+
color_coding_emoji = cfg.bad_result_emoji
247281
case access_control.DecisionReason.NoStatements:
248282
text = "There are no statements for this Permission Set & Account."
283+
color_coding_emoji = cfg.bad_result_emoji
249284

250285
client.chat_postMessage(text=text, thread_ts=slack_response["ts"], channel=cfg.slack_channel_id)
251286

287+
blocks = slack_helpers.HeaderSectionBlock.set_color_coding(
288+
blocks=slack_response["message"]["blocks"],
289+
color_coding_emoji=color_coding_emoji,
290+
)
291+
client.chat_update(
292+
channel=cfg.slack_channel_id,
293+
ts=slack_response["ts"],
294+
blocks=blocks,
295+
text=text,
296+
)
297+
252298
access_control.execute_decision(
253299
decision=decision,
254300
permission_set_name=request.permission_set_name,

‎src/revoker.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,25 @@ def handle_discard_buttons_event(
340340
for block in message["blocks"]:
341341
if slack_helpers.get_block_id(block) == "buttons":
342342
blocks = slack_helpers.remove_blocks(message["blocks"], block_ids=["buttons"])
343+
text = f"Request expired after {cfg.request_expiration_hours} hour(s)."
343344
blocks.append(
344345
slack_helpers.SectionBlock(
345346
block_id="footer",
346347
text=slack_helpers.MarkdownTextObject(
347-
text=f"Request expired after {cfg.request_expiration_hours} hour(s).",
348+
text=text,
348349
),
349350
)
350351
)
352+
blocks = slack_helpers.HeaderSectionBlock.set_color_coding(
353+
blocks=blocks,
354+
color_coding_emoji=cfg.discarded_result_emoji,
355+
)
356+
351357
slack_client.chat_update(
352358
channel=event.channel_id,
353359
ts=message["ts"],
354360
blocks=blocks,
355-
text="Request expired",
361+
text=text,
356362
)
357363
logger.info("Buttons were removed", extra={"event": event})
358364
return

‎src/slack_helpers.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
StaticSelectElement,
2424
)
2525
from slack_sdk.models.views import View
26-
from slack_sdk.web.slack_response import SlackResponse
2726

2827
import config
2928
import entities
@@ -231,11 +230,12 @@ def build_approval_request_message_blocks( # noqa: PLR0913
231230
account: entities.aws.Account,
232231
role_name: str,
233232
reason: str,
233+
color_coding_emoji: str,
234234
permission_duration: timedelta,
235235
show_buttons: bool = True,
236236
) -> list[Block]:
237237
blocks: list[Block] = [
238-
SectionBlock(block_id="header", text=MarkdownTextObject(text="AWS account access request.")),
238+
HeaderSectionBlock.new(color_coding_emoji),
239239
SectionBlock(
240240
block_id="content",
241241
fields=[
@@ -270,6 +270,23 @@ def build_approval_request_message_blocks( # noqa: PLR0913
270270
return blocks
271271

272272

273+
class HeaderSectionBlock:
274+
block_id = "header"
275+
276+
@classmethod
277+
def new(cls, color_coding_emoji: str) -> SectionBlock:
278+
return SectionBlock(
279+
block_id=cls.block_id, text=MarkdownTextObject(text=f"{color_coding_emoji} | AWS account access request | {color_coding_emoji}")
280+
)
281+
282+
@staticmethod
283+
def set_color_coding(blocks: list[dict], color_coding_emoji: str) -> list[dict]:
284+
blocks = remove_blocks(blocks, block_ids=[HeaderSectionBlock.block_id])
285+
b = HeaderSectionBlock.new(color_coding_emoji)
286+
blocks.insert(0, b.to_dict())
287+
return blocks
288+
289+
273290
def button_click_info_block(action: entities.ApproverAction, approver_slack_id: str) -> SectionBlock:
274291
return SectionBlock(
275292
block_id="footer",
@@ -352,15 +369,14 @@ def get_user_by_email(client: WebClient, email: str) -> entities.slack.User:
352369
raise e
353370

354371

355-
def remove_buttons(payload: ButtonClickedPayload, client: WebClient, approver: entities.slack.User) -> SlackResponse:
356-
blocks = remove_blocks(payload.message["blocks"], block_ids=["buttons"])
357-
blocks.append(button_click_info_block(payload.action, approver.id))
358-
return client.chat_update(
359-
channel=payload.channel_id,
360-
ts=payload.thread_ts,
361-
blocks=blocks,
362-
text="Buttons were removed.",
363-
)
372+
def remove_buttons_from_message_blocks(
373+
slack_message_blocks: list[Block],
374+
action: entities.ApproverAction,
375+
approver: entities.slack.User,
376+
) -> list[Block]:
377+
blocks = remove_blocks(slack_message_blocks, block_ids=["buttons"])
378+
blocks.append(button_click_info_block(action, approver.id))
379+
return blocks
364380

365381

366382
def create_slack_mention_by_principal_id(

0 commit comments

Comments
 (0)