Skip to content

Commit

Permalink
PagerDuty: use BlockKit for better Slack reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Jul 5, 2024
1 parent c74c193 commit 10212ce
Showing 1 changed file with 17 additions and 37 deletions.
54 changes: 17 additions & 37 deletions lib/Synergy/Reactor/PagerDuty.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use IO::Async::Timer::Periodic;
use JSON::MaybeXS qw(decode_json encode_json);
use Lingua::EN::Inflect qw(PL_N PL_V);
use List::Util qw(first uniq);
use Slack::BlockKit::Sugar -all => { -prefix => 'bk_' };
use Synergy::CommandPost;
use Synergy::Logger '$Logger';
use Synergy::Util qw(reformat_help);
Expand Down Expand Up @@ -941,46 +942,38 @@ sub _announce_oncall_change ($self, $before, $after) {
if (@leaving) {
my $verb = @leaving > 1 ? 'have' : 'has';
my $removed = join ', ', sort @leaving;
push @lines, "$removed $verb been removed from the oncall group";
push @lines, "$removed $verb been removed from the oncall group.";
}

if (@joining) {
my $verb = @joining > 1 ? 'have' : 'has';
my $added = join ', ', sort @joining;
push @lines, "$added $verb been added to the oncall group";
push @lines, "$added $verb been added to the oncall group.";
}

my $oncall = join ', ', sort keys %after;
push @lines, "Now oncall: $oncall";

my $text = join qq{\n}, @lines;

my $blocks = [
{
type => "section",
text => {
type => "mrkdwn",
text => "$text",
}
},
];
my @blocks = bk_richsection(bk_richtext($text))->as_struct;

$self->_active_incidents_summary->then(sub ($summary = {}) {
if (my $summary_text = delete $summary->{text}) {
$text .= "\n$summary_text";
}

if (my $slack = delete $summary->{slack}) {
push @$blocks, { type => 'divider' };
push @$blocks, $slack->{blocks}->@*;
push @blocks, { type => 'divider' };
push @blocks, $slack->{blocks}->@*;
}

$self->oncall_channel->send_message(
$self->oncall_change_announce_address,
$text,
{
slack => {
blocks => $blocks,
blocks => \@blocks,
}
}
);
Expand All @@ -1001,42 +994,29 @@ sub _active_incidents_summary ($self) {

my @text;

my $blocks = [
{
type => "section",
text => {
type => "mrkdwn",
text => "*$title*",
}
},
];
my @bk_items;

for my $incident (@incidents) {
my $created = $ISO8601->parse_datetime($incident->{created_at});
my $ago = ago(time - $created->epoch);

push @text, " - $incident->{description} (fired $ago)";

my $slack_text = sprintf("• <%s|#%s> (fired %s): %s",
$incident->{html_url},
$incident->{incident_number},
$ago,
$incident->{description},
push @bk_items, bk_richsection(
bk_link($incident->{html_url}, "#$incident->{incident_number}"),
" (fired $ago): $incident->{description}",
);

push @$blocks, {
type => "section",
text => {
type => "mrkdwn",
text => $slack_text,
}
};
}

my $text = join qq{\n}, $title, @text;

my $slack = {
blocks => $blocks,
blocks => bk_blocks(
bk_richblock(
bk_richsection(bk_bold($title)),
bk_ulist(@bk_items),
)
)->as_struct,
};

return Future->done({ text => $text, slack => $slack });
Expand Down

0 comments on commit 10212ce

Please sign in to comment.