App-RoboBot

 view release on metacpan or  search on metacpan

lib/App/RoboBot/Plugin/API/PagerDuty.pm  view on Meta::CPAN

            $data = $self->make_pd_api_call($group, [qw( api v1 schedules ), $schedule->{'id'}, qw( entries )], { since => $now, until => $now });

            unless (defined $data) {
                $message->response->raise('An error was encountered contacting the PagerDuty API. Please try again.');
                return;
            }

            $self->cache->{'oncall'}{$group->{'name'}}{$schedule->{'id'}} = {
                data    => $data,
                expires => time() + 60,
            };
        }

        next unless exists $data->{'entries'} && ref($data->{'entries'}) eq 'ARRAY' && scalar(@{$data->{'entries'}}) > 0;
        my $entry = $data->{'entries'}[0];

        $message->response->push(sprintf('*%s*: %s <%s>',
            $schedule->{'name'},
            ($entry->{'user'}{'name'} // '_Nobody_'),
            ($entry->{'user'}{'email'} // '...'),
        ));
    }

    # TODO: Ugly hack for now to suppress defaulted string of "message" in the
    #       various !oncall<X> macros we use in Slack. Should be fixed long-term
    #       by revamping the macro variables code.
    if (@extras && @extras > 0 && lc($extras[0]) ne 'message') {
        $message->response->push(sprintf('<%s> %s', $message->sender->name, join(' ', @extras)));
    }

    return;
}

sub make_pd_api_call {
    my ($self, $group, $path, $args) = @_;

    my $uri = URI->new;
    $uri->scheme('https');
    $uri->host($group->{'domain'} . '.pagerduty.com');

    if (ref($path) eq 'ARRAY') {
        $uri->path_segments(@{$path});
    } else {
        $uri->path($path);
    }

    if (defined $args && ref($args) eq 'HASH' && scalar(keys(%{$args})) > 0) {
        $uri->query_form($args);
    }

    my $req = HTTP::Request->new( GET => $uri->as_string );
    $req->header( 'Content-type'  => 'application/json' );
    $req->header( 'Authorization' => sprintf('Token token=%s', $group->{'api_key'}) );

    my $response = $self->ua->request($req);

    return unless $response->is_success;

    my $json;
    eval {
        $json = decode_json($response->decoded_content);
    };
    return if $@;
    return $json;
}

sub now {
    my ($self) = @_;

    return DateTime->now->iso8601 . 'Z';
}

sub _validate_config {
    my ($self, $message) = @_;

    unless (exists $self->bot->config->plugins->{'pagerduty'}
            && exists $self->bot->config->plugins->{'pagerduty'}{'group'}
            && ref($self->bot->config->plugins->{'pagerduty'}{'group'}) eq 'HASH')
    {
        $message->response->raise('PagerDuty groups not properly configured. Please contact bot administrator.');
        return 0;
    }

    return 1;
}

sub _validate_group_config {
    my ($self, $message, $group) = @_;

    return 0 unless $self->_validate_config($message);

    my $lgroup = lc($group);

    unless (exists $self->bot->config->plugins->{'pagerduty'}{'group'}{$lgroup}
            && exists $self->bot->config->plugins->{'pagerduty'}{'group'}{$lgroup}{'api_key'}
            && exists $self->bot->config->plugins->{'pagerduty'}{'group'}{$lgroup}{'domain'})
    {
        $message->response->raise('The PagerDuty group %s is not properly configured. Please contact bot administrator.', $group);
        return 0;
    }

    return 1;
}

sub coerce_schedules {
    my ($self, $schedule) = @_;

    # If a group only has a single schedule defined, the config is going to
    # return it as a simple hashref with that schedule's data, but for simplicity
    # we want all the other functions to be able to assume it will be an arrayref
    # containing 0+ schedules.
    return $schedule if ref($schedule) eq 'ARRAY';
    return [$schedule] if ref($schedule) eq 'HASH';
    return [];
}

__PACKAGE__->meta->make_immutable;

1;



( run in 0.509 second using v1.01-cache-2.11-cpan-5b529ec07f3 )