App-Prove-Plugin-Elasticsearch
view release on metacpan or search on metacpan
scripts/testrail-migrate.pl view on Meta::CPAN
sub main {
my @args = @_;
my $options;
GetOptionsFromArray(\@args,
'project=s@' => \$options->{projects},
'index-plan' => \$options->{'index-plan'},
'no-tests' => \$options->{'no-tests'},
'only-last' => \$options->{'only-last'},
'since=i' => \$options->{'since'},
ingest => \$options->{ingest},
);
my @patterns = @args;
my %ret = %$options;
$ret{'since'} = time(); #XXX this is imperfect, and could lead to double ingest, but --ingest shouldn't be used on a long term basis.
$ret{'patterns'} = \@patterns;
my $trconf = TestRail::Utils::parseConfig(my_home());
my $tr = TestRail::Utils::getHandle($trconf);
$tr->{step_field} = $trconf->{step_results};
$tr->{'only-last'} = $options->{'only-last'};
my $esconf = App::Prove::Elasticsearch::Utils::process_configuration();
my $indexer = App::Prove::Elasticsearch::Utils::require_indexer($esconf);
&{ \&{$indexer . "::check_index"} }($esconf);
$tr->{current_status_map} = [];
$tr->{current_status_map} = reduce {
my $ret;
$ret = $a;
$ret->{$b->{id}} = $b->{name};
$ret->{$a->{id}} = $a->{name};
$ret
} @{$tr->getPossibleTestStatuses()};
my $projects = $tr->getProjects();
@$projects = grep {my $subj = $_; any { $subj->{name} eq $_ } @{$options->{projects}} } @$projects if scalar(@{$options->{projects}});
foreach my $project (@$projects) {
my $runs = $tr->getRuns($project->{id});
@$runs = grep {my $subj = $_; any { $subj->{name} =~ m/$_/ } @patterns } @$runs if @patterns;
my $plans = $tr->getPlans($project->{id});
@$plans = grep {my $subj = $_; any { $subj->{name} =~ m/$_/ } @patterns } @$plans if @patterns;
#Filter by completed-on if provided
if ($options->{since}) {
@$runs = grep { $_->{completed_on} && $_->{completed_on} >= $options->{since} } @$runs;
@$plans = grep { $_->{completed_on} && $_->{completed_on} >= $options->{since} } @$plans;
}
foreach my $plan (@$plans) {
my $planRuns = $tr->getChildRuns($tr->getPlanByID($plan->{id}));
push(@$runs,@$planRuns);
}
foreach my $run (reverse @$runs) {
unless ($run->{passed_count} + $run->{failed_count} + $run->{blocked_count} +
$run->{custom_status1_count} + $run->{custom_status2_count} + $run->{custom_status3_count} +
$run->{custom_status4_count} + $run->{custom_status5_count} + $run->{custom_status6_count} +
$run->{custom_status7_count}
) {
print "Run $run->{id} had no results, skipping...\n";
next;
}
my @documents;
print "Examining run $run->{id}...\n";
my $tests = $tr->getTests($run->{id});
foreach my $test (@$tests) {
if (!$test->{case_id}) {
print "Encountered malformed test. Skipping.\n";
next;
}
$test->{section} = get_section_info($tr,$test->{case_id});
$test->{config} = $run->{config};
my @rdocs = build_document($tr,$test);
unless (@rdocs) {
print "No results for $test->{title}, skipping...\n";
next;
}
print "Adding ".scalar(@rdocs)." results for $test->{title}...\n";
push(@documents,@rdocs);
}
next unless scalar(@documents);
print "Indexing ".scalar(@documents)." documents...";
&{ \&{$indexer . "::bulk_index_results"} }(@documents);
print "Done!\n";
}
}
return %ret;
}
sub get_section_info {
my ($tr,$cid) = @_;
return $case_cache->{$cid} if $case_cache->{$cid};
my $c = $tr->getCaseByID($cid);
my $s = _get_sec($tr,$c->{section_id});
$case_cache->{$cid} = $s;
return $s;
}
sub _get_sec {
my ($tr,$sec) = @_;
return $sec_cache->{$sec} if $sec_cache->{$sec};
my $s = $tr->getSectionByID($sec);
$sec_cache->{$sec} = $s->{name};
return $s->{name};
}
sub build_document {
my ($tr,$test) = @_;
( run in 1.815 second using v1.01-cache-2.11-cpan-c221a9de4ec )