diff --git a/lib/Synergy/Reactor/LinearNotification.pm b/lib/Synergy/Reactor/LinearNotification.pm index 0ade6ab0..25a6e6ad 100644 --- a/lib/Synergy/Reactor/LinearNotification.pm +++ b/lib/Synergy/Reactor/LinearNotification.pm @@ -80,6 +80,53 @@ has name => ( default => 'lin', ); +has zendesk => ( + is => 'ro', + isa => 'Zendesk::Client', + default => sub ($self) { + return Synergy::Reactor::Zendesk->zendesk_client; + }, + lazy => 1, +); + +sub linear_update_zendesk ($payload) { + my $issue_id = $payload->{data}{issueId}; + my $issue = $self->linear->do_query( + q[ query Issue { + issue(id: $issue_id) { + attachments { nodes { url } } + labels { nodes { name } } + } + }])->get; + + my $has_escalation_label = grep { + lc $_{name} eq lc $self->escalation_label_name + } $issue->{data}{issue}{labels}{nodes}->@*; + + my @zendesk_url = grep { + $_{url} =~ /.*fastmail\.help\/agent\/tickets\/\d*/ + } $issue->{data}{issue}{attachments}{nodes}->@*; + + if ($has_escalation_label && @zendesk_url) { + $zendesk_url[0] =~ /.*\/(\d*)/; + + $self->zendesk->ticket_api->add_comment_to_ticket_f($1, { + body => "Issue updated with a comment: $payload->{url}", + public => \0, + })->else(sub ($err, @) { + $Logger->log([ "something went wrong posting to Zendesk: %s", $err ]); + return Future->done; + })->retain; + + $self->zendesk->ticket_api->update_by_zendesk_id_f($1, { + status => "open", + })->else(sub ($err, @) { + $Logger->log([ "something went wrong changing the zendesk ticket status: %s", $err ]); + return Future->done; + })->retain; + } +} + sub http_app ($self, $env) { my $req = Plack::Request->new($env); @@ -104,6 +151,7 @@ sub http_app ($self, $env) { return [ "200", [], [ '{"bad":"json"}' ] ]; } + # Notify in slack if issue is escalated if ($self->escalation_channel_name && $self->escalation_address) { if (my $channel = $self->hub->channel_named($self->escalation_channel_name)) { @@ -171,6 +219,14 @@ sub http_app ($self, $env) { } } + # Action Zendesk ticket if comments are made on linear issues + my $made_comment = $payload->{type} eq 'Issue comments' + && $payload->{action} eq 'create'; + + if ($made_comment) { + linear_update_zendesk($payload); + } + return [ "200", [], [ '{"o":"k"}' ] ]; }