diff --git a/busy_beaver/apps/github_integration/summary/blocks.py b/busy_beaver/apps/github_integration/summary/blocks.py index f7b3ce6f..7f4aac47 100644 --- a/busy_beaver/apps/github_integration/summary/blocks.py +++ b/busy_beaver/apps/github_integration/summary/blocks.py @@ -1,10 +1,11 @@ -from datetime import datetime +from datetime import datetime, timedelta import logging from typing import List, NamedTuple from .summary import GitHubUserEvents from busy_beaver.models import GitHubSummaryUser from busy_beaver.toolbox.slack_block_kit import Divider, Section +from busy_beaver.toolbox.slack_block_kit.elements import Image logger = logging.getLogger(__name__) @@ -24,6 +25,7 @@ class GitHubSummaryPost: def __init__(self, users: List[GitHubSummaryUser], boundary_dt: datetime): self.users = users self.boundary_dt = boundary_dt + self.now = boundary_dt + timedelta(days=1) def __repr__(self): # pragma: no cover return "" @@ -40,10 +42,7 @@ def create(self): self.all_user_events = all_user_events def as_blocks(self): - output = [ - Section(f"*Daily GitHub Summary -- {self.boundary_dt:%B %d, %Y}*"), - Divider(), - ] + output = [Section(f"*Daily GitHub Summary -- {self.now:%B %d, %Y}*"), Divider()] if not self.all_user_events: output.append(Section(text="No activity to report.")) @@ -51,6 +50,10 @@ def as_blocks(self): return [block.to_dict() for block in output] for user, events in self.all_user_events: - output.append(Section(text=events.generate_summary_text())) + profile_pic = ( + f"https://avatars.githubusercontent.com/u/{user.github_id}?size=75" + ) + img = Image(image_url=profile_pic, alt_text=f"{user.github_username}") + output.append(Section(text=events.generate_summary_text(), accessory=img)) output.append(Divider()) return [block.to_dict() for block in output] diff --git a/busy_beaver/apps/github_integration/summary/summary.py b/busy_beaver/apps/github_integration/summary/summary.py index d837ad16..a724e0b1 100644 --- a/busy_beaver/apps/github_integration/summary/summary.py +++ b/busy_beaver/apps/github_integration/summary/summary.py @@ -28,11 +28,11 @@ def __init__(self, user: GitHubSummaryUser, boundary_dt: datetime): StarredReposList(), ] - username = user.github_username + self.username = user.github_username start_dt = boundary_dt - end_dt = start_dt + timedelta(days=1) # TODO depends on cadence + end_dt = start_dt + timedelta(days=1) - timeline = github.user_activity_during_range(username, start_dt, end_dt) + timeline = github.user_activity_during_range(self.username, start_dt, end_dt) for event in timeline: for event_list in self.event_lists: if event_list.matches_event(event): diff --git a/busy_beaver/toolbox/slack_block_kit/blocks.py b/busy_beaver/toolbox/slack_block_kit/blocks.py index c2cb6f0c..a1e36b00 100644 --- a/busy_beaver/toolbox/slack_block_kit/blocks.py +++ b/busy_beaver/toolbox/slack_block_kit/blocks.py @@ -7,6 +7,7 @@ - https://api.slack.com/block-kit - https://api.slack.com/tools/block-kit-builder """ +from .elements import Element class Block: @@ -139,7 +140,10 @@ class Section(Block): type = "section" - def __init__(self, text: str, block_id: str = ""): + def __init__(self, text: str, *, block_id: str = "", accessory: Element = None): super().__init__(block_id) self.output["type"] = self.type self.output["text"] = {"type": "mrkdwn", "text": text} + + if accessory: + self.output["accessory"] = accessory.to_dict() diff --git a/busy_beaver/toolbox/slack_block_kit/elements.py b/busy_beaver/toolbox/slack_block_kit/elements.py new file mode 100644 index 00000000..a8163fc0 --- /dev/null +++ b/busy_beaver/toolbox/slack_block_kit/elements.py @@ -0,0 +1,32 @@ +""" +Slack Block Kit: Block Elements + +Block elements can be used inside of section, context, and actions +layout blocks. Inputs can only be used inside of input blocks. + +https://api.slack.com/reference/block-kit/block-elements +""" + + +class Element: + type = None + + def __init__(self): + self.output = {} + self.output["type"] = self.type + + def __repr__(self): # pragma: no cover + cls_name = self.__class__.__name__ + return f"<{cls_name}>" + + def to_dict(self) -> dict: + return self.output + + +class Image(Element): + type = "image" + + def __init__(self, image_url, alt_text): + super().__init__() + self.output["image_url"] = image_url + self.output["alt_text"] = alt_text