From 8f067ebf12d61ac384cbb76e9886547d80d10be5 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Wed, 29 May 2019 11:01:06 -0400 Subject: [PATCH] switch to uuids for report pings --- Bugzilla/App/Command/report_ping.pm | 9 ++++--- Bugzilla/Report/Ping.pm | 39 +++++++++++++++++++++-------- Bugzilla/Report/Ping/Simple.pm | 18 ++++++------- t/report-ping-simple.t | 6 ++--- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/Bugzilla/App/Command/report_ping.pm b/Bugzilla/App/Command/report_ping.pm index c423bf384..d61df7b32 100644 --- a/Bugzilla/App/Command/report_ping.pm +++ b/Bugzilla/App/Command/report_ping.pm @@ -79,9 +79,9 @@ sub run { 'Testing', sub { foreach my $result (@_) { - my @error = $report->test($result); + my @error = $report->test_row($result); if (@error) { - my (undef, $doc) = $report->prepare($result); + my $doc = $report->extract_content($result); die $json->encode({errors => \@error, result => $doc}); } } @@ -94,7 +94,8 @@ sub run { 'Dumping', sub { foreach my $result (@_) { - my ($id, $doc) = $report->prepare($result); + my $doc = $report->extract_content($result); + my $id = $result->id; path($working_dir, "$id.json")->spurt($json->encode($doc)); } } @@ -105,7 +106,7 @@ sub run { $report, 'Sending', sub { - Mojo::Promise->all(map { $report->send($_) } @_)->wait; + Mojo::Promise->all(map { $report->send_row($_) } @_)->wait; } ); } diff --git a/Bugzilla/Report/Ping.pm b/Bugzilla/Report/Ping.pm index 8ea553e08..e3460ffa4 100644 --- a/Bugzilla/Report/Ping.pm +++ b/Bugzilla/Report/Ping.pm @@ -9,12 +9,13 @@ package Bugzilla::Report::Ping; use 5.10.1; use Moo::Role; -use Type::Utils qw(class_type); use Bugzilla::Types qw(URL); -use Types::Standard qw(Str Num Int); -use Scalar::Util qw(blessed); use JSON::Validator; use Mojo::Promise; +use Scalar::Util qw(blessed); +use Type::Utils qw(class_type); +use Types::Standard qw(Str Num Int); +use UUID::Tiny qw(:std); has 'model' => (is => 'ro', required => 1, isa => class_type({class => 'Bugzilla::Model'})); @@ -89,19 +90,37 @@ sub _build_docversion { return $self->VERSION; } -requires 'prepare'; +has 'uuid_namespace' => (is => 'lazy', init_arg => undef, isa => Str); + +sub _build_uuid_namespace { + my ($self) = @_; + + my $name = $self->namespace . '/' . $self->doctype . ':' . $self->docversion; + return create_uuid(UUID_SHA1, UUID_NIL, $name); +} + +requires 'extract_id', 'extract_content'; + +around 'extract_id' => sub { + my ($method, $self, $row) = @_; + + return create_uuid_as_string(UUID_SHA1, $self->uuid_namespace, $self->$method($row)); +}; -sub send { +sub send_row { my ($self, $row) = @_; - my ($id, $doc) = $self->prepare($row); - my $url = $self->base_url; + my $url = $self->base_url; + my $id = $self->extract_id($row); + my $content = $self->extract_content($row); push @{$url->path}, $self->namespace, $self->doctype, $self->docversion, $id; - return $self->user_agent->put_p($url, json => $doc); + return $self->user_agent->put_p($url, json => $content); } -sub test { +sub test_row { my ($self, $row) = @_; - my ($id, $doc) = $self->prepare($row); + my $id = $self->extract_id($row); + my $doc = $self->extract_content($row); + die 'id is not a uuid string' unless is_uuid_string($id); return $self->validate($doc); } diff --git a/Bugzilla/Report/Ping/Simple.pm b/Bugzilla/Report/Ping/Simple.pm index d63553c7c..6e47a0afa 100644 --- a/Bugzilla/Report/Ping/Simple.pm +++ b/Bugzilla/Report/Ping/Simple.pm @@ -63,9 +63,9 @@ sub _build_resultset { return $bugs->search($query, $options); } -sub prepare { +sub extract_content { my ($self, $bug) = @_; - my $doc = { + return { reporter => $bug->reporter->id, assigned_to => $bug->assigned_to->id, qa_contact => $bug->qa_contact ? $bug->qa_contact->id : undef, @@ -84,21 +84,19 @@ sub prepare { duplicates => [map { $_->id } $bug->duplicates->all], blocked_by => [map { $_->dependson } $bug->map_blocked_by->all], depends_on => [map { $_->blocked } $bug->map_depends_on->all], - flags => [map { $self->_prepare_flag($_) } $bug->flags->all], - delta_ts => $bug->delta_ts . "", - creation_ts => $bug->creation_ts . "", + flags => [map { $self->_extract_flag($_) } $bug->flags->all], + delta_ts => $bug->delta_ts . '', + creation_ts => $bug->creation_ts . '', }; - - return ($self->_prepare_doc_id($bug), $doc); } -sub _prepare_doc_id { +sub extract_id { my ($self, $bug) = @_; - return sprintf("%d-%d", $bug->id, $bug->delta_ts->epoch); + return sprintf('%d-%d', $bug->id, $bug->delta_ts->epoch); } -sub _prepare_flag { +sub _extract_flag { my ($self, $flag) = @_; return { diff --git a/t/report-ping-simple.t b/t/report-ping-simple.t index f2cf56dec..ee207cd53 100644 --- a/t/report-ping-simple.t +++ b/t/report-ping-simple.t @@ -70,17 +70,15 @@ is($rs->first->id, 1, "first bug of page 1 is 1"); my ($first, $second, $third, @rest) = $rs->all; { - my ($id, $doc) = $report->prepare( $first ); + my $doc = $report->extract_content( $first ); - is($id, "1-$time{1}", "doc id is correct"); is($doc->{product}, 'Firefox'); is($doc->{keywords}, []); is([map { "$_" } $report->validate($doc)], [], "No errors for first doc"); } { - my ($id, $doc) = $report->prepare( $third ); - is($id, "3-$time{3}", "doc id is correct"); + my $doc = $report->extract_content( $third ); is($doc->{product}, 'Firefox'); is($doc->{keywords}, ['regression']); } -- 2.47.3