'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});
}
}
'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));
}
}
$report,
'Sending',
sub {
- Mojo::Promise->all(map { $report->send($_) } @_)->wait;
+ Mojo::Promise->all(map { $report->send_row($_) } @_)->wait;
}
);
}
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'}));
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);
}
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,
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 {
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']);
}