diff options
Diffstat (limited to 'extensions/Push/lib/Connector')
-rw-r--r-- | extensions/Push/lib/Connector/Base.pm | 99 | ||||
-rw-r--r-- | extensions/Push/lib/Connector/File.pm | 61 | ||||
-rw-r--r-- | extensions/Push/lib/Connector/Phabricator.pm | 153 | ||||
-rw-r--r-- | extensions/Push/lib/Connector/Spark.pm | 228 |
4 files changed, 269 insertions, 272 deletions
diff --git a/extensions/Push/lib/Connector/Base.pm b/extensions/Push/lib/Connector/Base.pm index ee41bd160..bd46fe6b4 100644 --- a/extensions/Push/lib/Connector/Base.pm +++ b/extensions/Push/lib/Connector/Base.pm @@ -18,59 +18,65 @@ use Bugzilla::Extension::Push::BacklogQueue; use Bugzilla::Extension::Push::Backoff; sub new { - my ($class) = @_; - my $self = {}; - bless($self, $class); - ($self->{name}) = $class =~ /^.+:(.+)$/; - $self->init(); - return $self; + my ($class) = @_; + my $self = {}; + bless($self, $class); + ($self->{name}) = $class =~ /^.+:(.+)$/; + $self->init(); + return $self; } sub name { - my $self = shift; - return $self->{name}; + my $self = shift; + return $self->{name}; } sub init { - my ($self) = @_; - # abstract - # perform any initialisation here - # will be run when created by the web pages or by the daemon - # and also when the configuration needs to be reloaded + my ($self) = @_; + + # abstract + # perform any initialisation here + # will be run when created by the web pages or by the daemon + # and also when the configuration needs to be reloaded } sub stop { - my ($self) = @_; - # abstract - # run from the daemon only; disconnect from remote hosts, etc + my ($self) = @_; + + # abstract + # run from the daemon only; disconnect from remote hosts, etc } sub should_send { - my ($self, $message) = @_; - # abstract - # return boolean indicating if the connector will be sending the message. - # this will be called each message, and should be a very quick simple test. - # the connector can perform a more exhaustive test in the send() method. - return 0; + my ($self, $message) = @_; + + # abstract + # return boolean indicating if the connector will be sending the message. + # this will be called each message, and should be a very quick simple test. + # the connector can perform a more exhaustive test in the send() method. + return 0; } sub send { - my ($self, $message) = @_; - # abstract - # deliver the message, daemon only + my ($self, $message) = @_; + + # abstract + # deliver the message, daemon only } sub options { - my ($self) = @_; - # abstract - # return an array of configuration variables - return (); + my ($self) = @_; + + # abstract + # return an array of configuration variables + return (); } sub options_validate { - my ($class, $config) = @_; - # abstract, static - # die if a combination of options in $config is invalid + my ($class, $config) = @_; + + # abstract, static + # die if a combination of options in $config is invalid } # @@ -78,29 +84,30 @@ sub options_validate { # sub config { - my ($self) = @_; - if (!$self->{config}) { - $self->load_config(); - } - return $self->{config}; + my ($self) = @_; + if (!$self->{config}) { + $self->load_config(); + } + return $self->{config}; } sub load_config { - my ($self) = @_; - my $config = Bugzilla::Extension::Push::Config->new($self->name, $self->options); - $config->load(); - $self->{config} = $config; + my ($self) = @_; + my $config + = Bugzilla::Extension::Push::Config->new($self->name, $self->options); + $config->load(); + $self->{config} = $config; } sub enabled { - my ($self) = @_; - return $self->config->{enabled} eq 'Enabled'; + my ($self) = @_; + return $self->config->{enabled} eq 'Enabled'; } sub backlog { - my ($self) = @_; - $self->{backlog} ||= Bugzilla::Extension::Push::BacklogQueue->new($self->name); - return $self->{backlog}; + my ($self) = @_; + $self->{backlog} ||= Bugzilla::Extension::Push::BacklogQueue->new($self->name); + return $self->{backlog}; } 1; diff --git a/extensions/Push/lib/Connector/File.pm b/extensions/Push/lib/Connector/File.pm index ae249ffe5..7d86953f8 100644 --- a/extensions/Push/lib/Connector/File.pm +++ b/extensions/Push/lib/Connector/File.pm @@ -20,51 +20,46 @@ use Encode; use FileHandle; sub init { - my ($self) = @_; + my ($self) = @_; } sub options { - return ( - { - name => 'filename', - label => 'Filename', - type => 'string', - default => 'push.log', - required => 1, - validate => sub { - my $filename = shift; - $filename =~ m#^/# - && die "Absolute paths are not permitted\n"; - $filename =~ m#\.\.# - && die "Relative paths are not permitted\n"; - }, - }, - ); + return ( + { + name => 'filename', + label => 'Filename', + type => 'string', + default => 'push.log', + required => 1, + validate => sub { + my $filename = shift; + $filename =~ m#^/# && die "Absolute paths are not permitted\n"; + $filename =~ m#\.\.# && die "Relative paths are not permitted\n"; + }, + }, + ); } sub should_send { - my ($self, $message) = @_; - return 1; + my ($self, $message) = @_; + return 1; } sub send { - my ($self, $message) = @_; + my ($self, $message) = @_; - # pretty-format json payload - my $payload = $message->payload_decoded; - $payload = to_json($payload, 1); + # pretty-format json payload + my $payload = $message->payload_decoded; + $payload = to_json($payload, 1); - my $filename = bz_locations()->{'datadir'} . '/' . $self->config->{filename}; - Bugzilla->push_ext->logger->debug("File: Appending to $filename"); - my $fh = FileHandle->new(">>$filename"); - $fh->binmode(':utf8'); - $fh->print( - "[" . scalar(localtime) . "]\n" . - $payload . "\n\n" - ); - $fh->close; + my $filename = bz_locations()->{'datadir'} . '/' . $self->config->{filename}; + Bugzilla->push_ext->logger->debug("File: Appending to $filename"); + my $fh = FileHandle->new(">>$filename"); + $fh->binmode(':utf8'); + $fh->print("[" . scalar(localtime) . "]\n" . $payload . "\n\n"); + $fh->close; - return PUSH_RESULT_OK; + return PUSH_RESULT_OK; } 1; diff --git a/extensions/Push/lib/Connector/Phabricator.pm b/extensions/Push/lib/Connector/Phabricator.pm index 33e2bb6ad..61a39e32b 100644 --- a/extensions/Push/lib/Connector/Phabricator.pm +++ b/extensions/Push/lib/Connector/Phabricator.pm @@ -29,105 +29,96 @@ use Bugzilla::Extension::Push::Constants; use Bugzilla::Extension::Push::Util qw(is_public); sub options { - return ( - { - name => 'phabricator_url', - label => 'Phabricator URL', - type => 'string', - default => '', - required => 1, - } - ); + return ({ + name => 'phabricator_url', + label => 'Phabricator URL', + type => 'string', + default => '', + required => 1, + }); } sub should_send { - my ( $self, $message ) = @_; + my ($self, $message) = @_; - return 0 unless Bugzilla->params->{phabricator_enabled}; + return 0 unless Bugzilla->params->{phabricator_enabled}; - # We are only interested currently in bug group, assignee, qa-contact, or cc changes. - return 0 - unless $message->routing_key =~ - /^(?:attachment|bug)\.modify:.*\b(bug_group|assigned_to|qa_contact|cc)\b/; +# We are only interested currently in bug group, assignee, qa-contact, or cc changes. + return 0 + unless $message->routing_key + =~ /^(?:attachment|bug)\.modify:.*\b(bug_group|assigned_to|qa_contact|cc)\b/; - my $bug = $self->_get_bug_by_data( $message->payload_decoded ) || return 0; + my $bug = $self->_get_bug_by_data($message->payload_decoded) || return 0; - return $bug->has_attachment_with_mimetype(PHAB_CONTENT_TYPE); + return $bug->has_attachment_with_mimetype(PHAB_CONTENT_TYPE); } sub send { - my ( $self, $message ) = @_; - - my $logger = Bugzilla->push_ext->logger; - - my $data = $message->payload_decoded; - - my $bug = $self->_get_bug_by_data($data) || return PUSH_RESULT_OK; - - my $is_public = is_public($bug); - - my $revisions = get_attachment_revisions($bug); - - my $group_change = - ($message->routing_key =~ /^(?:attachment|bug)\.modify:.*\bbug_group\b/) - ? 1 - : 0; - - foreach my $revision (@$revisions) { - if ( $is_public && $group_change ) { - Bugzilla->audit(sprintf( - 'Making revision %s public for bug %s', - $revision->id, - $bug->id - )); - $revision->make_public(); - } - elsif ( !$is_public && $group_change ) { - Bugzilla->audit(sprintf( - 'Giving revision %s a custom policy for bug %s', - $revision->id, - $bug->id - )); - my $set_project_names = [ map { "bmo-" . $_->name } @{ $bug->groups_in } ]; - $revision->make_private($set_project_names); - } - - # Subscriber list of the private revision should always match - # the bug roles such as assignee, qa contact, and cc members. - if (!$is_public) { - Bugzilla->audit(sprintf( - 'Updating subscribers for %s for bug %s', - $revision->id, - $bug->id - )); - my $subscribers = get_bug_role_phids($bug); - $revision->set_subscribers($subscribers) if $subscribers; - } - - $revision->update(); + my ($self, $message) = @_; + + my $logger = Bugzilla->push_ext->logger; + + my $data = $message->payload_decoded; + + my $bug = $self->_get_bug_by_data($data) || return PUSH_RESULT_OK; + + my $is_public = is_public($bug); + + my $revisions = get_attachment_revisions($bug); + + my $group_change + = ($message->routing_key =~ /^(?:attachment|bug)\.modify:.*\bbug_group\b/) + ? 1 + : 0; + + foreach my $revision (@$revisions) { + if ($is_public && $group_change) { + Bugzilla->audit( + sprintf('Making revision %s public for bug %s', $revision->id, $bug->id)); + $revision->make_public(); } + elsif (!$is_public && $group_change) { + Bugzilla->audit(sprintf( + 'Giving revision %s a custom policy for bug %s', + $revision->id, $bug->id + )); + my $set_project_names = [map { "bmo-" . $_->name } @{$bug->groups_in}]; + $revision->make_private($set_project_names); + } + + # Subscriber list of the private revision should always match + # the bug roles such as assignee, qa contact, and cc members. + if (!$is_public) { + Bugzilla->audit( + sprintf('Updating subscribers for %s for bug %s', $revision->id, $bug->id)); + my $subscribers = get_bug_role_phids($bug); + $revision->set_subscribers($subscribers) if $subscribers; + } + + $revision->update(); + } - return PUSH_RESULT_OK; + return PUSH_RESULT_OK; } sub _get_bug_by_data { - my ( $self, $data ) = @_; - my $bug_data = $self->_get_bug_data($data) || return 0; - my $bug = Bugzilla::Bug->new( { id => $bug_data->{id} } ); + my ($self, $data) = @_; + my $bug_data = $self->_get_bug_data($data) || return 0; + my $bug = Bugzilla::Bug->new({id => $bug_data->{id}}); } sub _get_bug_data { - my ( $self, $data ) = @_; - my $target = $data->{event}->{target}; - if ( $target eq 'bug' ) { - return $data->{bug}; - } - elsif ( exists $data->{$target}->{bug} ) { - return $data->{$target}->{bug}; - } - else { - return; - } + my ($self, $data) = @_; + my $target = $data->{event}->{target}; + if ($target eq 'bug') { + return $data->{bug}; + } + elsif (exists $data->{$target}->{bug}) { + return $data->{$target}->{bug}; + } + else { + return; + } } 1; diff --git a/extensions/Push/lib/Connector/Spark.pm b/extensions/Push/lib/Connector/Spark.pm index e58ddfbe4..1eb6f22c6 100644 --- a/extensions/Push/lib/Connector/Spark.pm +++ b/extensions/Push/lib/Connector/Spark.pm @@ -25,150 +25,154 @@ use LWP::UserAgent; use List::MoreUtils qw(any); sub options { - return ( - { - name => 'spark_endpoint', - label => 'Spark API Endpoint', - type => 'string', - default => 'https://api.ciscospark.com/v1', - required => 1, - }, - { - name => 'spark_room_id', - label => 'Spark Room ID', - type => 'string', - default => 'bugzilla', - required => 1, - }, - { - name => 'spark_api_key', - label => 'Spark API Key', - type => 'string', - default => '', - required => 1, - }, - ); + return ( + { + name => 'spark_endpoint', + label => 'Spark API Endpoint', + type => 'string', + default => 'https://api.ciscospark.com/v1', + required => 1, + }, + { + name => 'spark_room_id', + label => 'Spark Room ID', + type => 'string', + default => 'bugzilla', + required => 1, + }, + { + name => 'spark_api_key', + label => 'Spark API Key', + type => 'string', + default => '', + required => 1, + }, + ); } sub stop { - my ($self) = @_; + my ($self) = @_; } sub should_send { - my ($self, $message) = @_; - - my $data = $message->payload_decoded; - my $bug_data = $self->_get_bug_data($data) - || return 0; - - # Send if bug has cisco-spark keyword - my $bug = Bugzilla::Bug->new({ id => $bug_data->{id}, cache => 1 }); - return 0 unless $bug->has_keyword('cisco-spark'); - - if ($message->routing_key eq 'bug.create') { - return 1; - } - else { - foreach my $change (@{ $data->{event}->{changes} }) { - # send status and resolution updates - return 1 if $change->{field} eq 'bug_status' || $change->{field} eq 'resolution'; - # also send if the right keyword has been added to this bug - if ($change->{field} eq 'keywords' && $change->{added}) { - my @added = split(/, /, $change->{added}); - return 1 if any { $_ eq 'cisco-spark' } @added; - } - } + my ($self, $message) = @_; + + my $data = $message->payload_decoded; + my $bug_data = $self->_get_bug_data($data) || return 0; + + # Send if bug has cisco-spark keyword + my $bug = Bugzilla::Bug->new({id => $bug_data->{id}, cache => 1}); + return 0 unless $bug->has_keyword('cisco-spark'); + + if ($message->routing_key eq 'bug.create') { + return 1; + } + else { + foreach my $change (@{$data->{event}->{changes}}) { + + # send status and resolution updates + return 1 + if $change->{field} eq 'bug_status' || $change->{field} eq 'resolution'; + + # also send if the right keyword has been added to this bug + if ($change->{field} eq 'keywords' && $change->{added}) { + my @added = split(/, /, $change->{added}); + return 1 if any { $_ eq 'cisco-spark' } @added; + } } + } - # and nothing else - return 0; + # and nothing else + return 0; } sub send { - my ($self, $message) = @_; + my ($self, $message) = @_; - eval { - my $data = $message->payload_decoded(); - my $bug_data = $self->_get_bug_data($data); - my $bug = Bugzilla::Bug->new({ id => $bug_data->{id}, cache => 1 }); + eval { + my $data = $message->payload_decoded(); + my $bug_data = $self->_get_bug_data($data); + my $bug = Bugzilla::Bug->new({id => $bug_data->{id}, cache => 1}); - my $text = "Bug " . $bug->id . " - " . $bug->short_desc . "\n"; - if ($message->routing_key eq 'bug.create') { - $text = "New " . $text; + my $text = "Bug " . $bug->id . " - " . $bug->short_desc . "\n"; + if ($message->routing_key eq 'bug.create') { + $text = "New " . $text; + } + else { + foreach my $change (@{$data->{event}->{changes}}) { + if ($change->{field} eq 'bug_status') { + $text + .= "Status changed: " . $change->{removed} . " -> " . $change->{added} . "\n"; } - else { - foreach my $change (@{ $data->{event}->{changes} }) { - if ($change->{field} eq 'bug_status') { - $text .= "Status changed: " . - $change->{removed} . " -> " . $change->{added} . "\n"; - } - if ($change->{field} eq 'resolution') { - $text .= "Resolution changed: " . - ($change->{removed} ? $change->{removed} . " -> " : "") . $change->{added} . "\n"; - } - } + if ($change->{field} eq 'resolution') { + $text + .= "Resolution changed: " + . ($change->{removed} ? $change->{removed} . " -> " : "") + . $change->{added} . "\n"; } - $text .= Bugzilla->localconfig->{urlbase} . "show_bug.cgi?id=" . $bug->id; + } + } + $text .= Bugzilla->localconfig->{urlbase} . "show_bug.cgi?id=" . $bug->id; - my $room_id = $self->config->{spark_room_id}; - my $message_uri = $self->_spark_uri('messages'); + my $room_id = $self->config->{spark_room_id}; + my $message_uri = $self->_spark_uri('messages'); - my $json_data = { roomId => $room_id, text => $text }; + my $json_data = {roomId => $room_id, text => $text}; - my $headers = HTTP::Headers->new( - Content_Type => 'application/json' - ); - my $request = HTTP::Request->new('POST', $message_uri, $headers, encode_json($json_data)); - my $resp = $self->_user_agent->request($request); + my $headers = HTTP::Headers->new(Content_Type => 'application/json'); + my $request + = HTTP::Request->new('POST', $message_uri, $headers, encode_json($json_data)); + my $resp = $self->_user_agent->request($request); - if ($resp->code != 200) { - die "Expected HTTP 200 response, got " . $resp->code; - } - }; - if ($@) { - return (PUSH_RESULT_TRANSIENT, clean_error($@)); + if ($resp->code != 200) { + die "Expected HTTP 200 response, got " . $resp->code; } + }; + if ($@) { + return (PUSH_RESULT_TRANSIENT, clean_error($@)); + } - return PUSH_RESULT_OK; + return PUSH_RESULT_OK; } # Private methods sub _get_bug_data { - my ($self, $data) = @_; - my $target = $data->{event}->{target}; - if ($target eq 'bug') { - return $data->{bug}; - } elsif (exists $data->{$target}->{bug}) { - return $data->{$target}->{bug}; - } else { - return; - } + my ($self, $data) = @_; + my $target = $data->{event}->{target}; + if ($target eq 'bug') { + return $data->{bug}; + } + elsif (exists $data->{$target}->{bug}) { + return $data->{$target}->{bug}; + } + else { + return; + } } sub _user_agent { - my ($self) = @_; - my $ua = LWP::UserAgent->new(agent => 'Bugzilla'); - $ua->timeout(10); - $ua->protocols_allowed(['http', 'https']); - - if (my $proxy_url = Bugzilla->params->{proxy_url}) { - $ua->proxy(['http', 'https'], $proxy_url); - } - else { - $ua->env_proxy(); - } - - $ua->default_header( - 'Authorization' => 'Bearer ' . $self->config->{spark_api_key} - ); - - return $ua; + my ($self) = @_; + my $ua = LWP::UserAgent->new(agent => 'Bugzilla'); + $ua->timeout(10); + $ua->protocols_allowed(['http', 'https']); + + if (my $proxy_url = Bugzilla->params->{proxy_url}) { + $ua->proxy(['http', 'https'], $proxy_url); + } + else { + $ua->env_proxy(); + } + + $ua->default_header( + 'Authorization' => 'Bearer ' . $self->config->{spark_api_key}); + + return $ua; } sub _spark_uri { - my ($self, $path) = @_; - return URI->new($self->config->{spark_endpoint} . "/" . $path); + my ($self, $path) = @_; + return URI->new($self->config->{spark_endpoint} . "/" . $path); } 1; |