| 1 | # ABSTRACT: Pick high priority cases for execution and lock them via the test results mechanism. | ||||||||||||||
| 2 | # PODNAME: TestRail::Utils::Lock | ||||||||||||||
| 3 | |||||||||||||||
| 4 | package TestRail::Utils::Lock; | ||||||||||||||
| 5 | 5 | $TestRail::Utils::Lock::VERSION = '0.052'; | |||||||||||||
| 6 | 5 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 5 | ▶ | use Carp qw{confess cluck}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 5 | ▶ | use Scalar::Util qw{blessed}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | use Types::Standard | ||||||||||||||
| 15 | 5 | ▶ | qw( slurpy ClassName Object Str Int Bool HashRef ArrayRef Maybe Optional); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 5 | ▶ | use Type::Params qw( compile ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 5 | ▶ | use TestRail::API; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 5 | ▶ | use TestRail::Utils; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 5 | ▶ | use TestRail::Utils::Find; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 11 | ▶ | sub pickAndLockTest { | ||||||||||||
|
Subroutine
pickAndLockTest: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 23 | 11 | state $check = compile( HashRef, Optional [ Maybe [Object] ] ); | |||||||||||||
| 24 | 11 | my ( $opts, $tr ) = $check->(@_); | |||||||||||||
| 25 | 11 | ▶ | confess("TestRail handle must be provided as argument 2") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | unless blessed($tr) eq 'TestRail::API'; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 11 | my ( $project, $plan, $run ) = | |||||||||||||
| 29 | TestRail::Utils::getRunInformation( $tr, $opts ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 11 | my $status_ids; | |||||||||||||
| 32 | |||||||||||||||
| 33 | # Process statuses | ||||||||||||||
| 34 | @$status_ids = | ||||||||||||||
| 35 | 11 | $tr->statusNamesToIds( $opts->{'lockname'}, 'untested', 'retest' ); | |||||||||||||
| 36 | 11 | my ( $lock_status_id, $untested_id, $retest_id ) = @$status_ids; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 11 | my $cases = $tr->getTests( $run->{'id'} ); | |||||||||||||
| 39 | |||||||||||||||
| 40 | #Filter by case types | ||||||||||||||
| 41 | 11 | ▶ | if ( $opts->{'case-types'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | my @case_types = | ||||||||||||||
| 43 | 8 | map { my $cdef = $tr->getCaseTypeByName($_); $cdef->{'id'} } | |||||||||||||
| 44 | 8 | @{ $opts->{'case-types'} }; | |||||||||||||
| 45 | @$cases = grep { | ||||||||||||||
| 46 | 8 | my $case_type_id = $_->{'type_id'}; | |||||||||||||
| 47 | 48 | grep { $_ eq $case_type_id } @case_types | |||||||||||||
| 48 | } @$cases; | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | # Limit to only non-locked and open cases | ||||||||||||||
| 52 | @$cases = grep { | ||||||||||||||
| 53 | 11 | my $tstatus = $_->{'status_id'}; | |||||||||||||
| 54 | 39 | scalar( grep { $tstatus eq $_ } ( $untested_id, $retest_id ) ) | |||||||||||||
| 55 | } @$cases; | ||||||||||||||
| 56 | 11 | @$cases = sort { $b->{'priority_id'} <=> $a->{'priority_id'} } | |||||||||||||
| 57 | @$cases; #Sort by priority DESC | ||||||||||||||
| 58 | |||||||||||||||
| 59 | # Filter by match options | ||||||||||||||
| 60 | 11 | @$cases = TestRail::Utils::Find::findTests( $opts, @$cases ); | |||||||||||||
| 61 | |||||||||||||||
| 62 | 11 | my ( $title, $test ); | |||||||||||||
| 63 | 11 | ▶ | while (@$cases) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 6 | $test = shift @$cases; | |||||||||||||
| 65 | 6 | $title = lockTest( $test, $lock_status_id, $opts->{'hostname'}, $tr ); | |||||||||||||
| 66 | 6 | ▶ | last if $title; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 11 | ▶ | if ( !$title ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 5 | warn | |||||||||||||
| 71 | "Failed to lock case! This probably means you don't have any cases left to lock."; | ||||||||||||||
| 72 | 5 | return 0; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | return { | ||||||||||||||
| 76 | 6 | 'test' => $test, | |||||||||||||
| 77 | 'path' => $title, | ||||||||||||||
| 78 | 'project' => $project, | ||||||||||||||
| 79 | 'plan' => $plan, | ||||||||||||||
| 80 | 'run' => $run | ||||||||||||||
| 81 | }; | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 7 | ▶ | sub lockTest { | ||||||||||||
|
Subroutine
lockTest: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||
| 85 | 7 | state $check = compile( HashRef, Int, Str, Object ); | |||||||||||||
| 86 | 7 | my ( $test, $lock_status_id, $hostname, $handle ) = $check->(@_); | |||||||||||||
| 87 | |||||||||||||||
| 88 | my $res = $handle->createTestResults( | ||||||||||||||
| 89 | $test->{id}, | ||||||||||||||
| 90 | 7 | $lock_status_id, | |||||||||||||
| 91 | "Test Locked by $hostname.\n | ||||||||||||||
| 92 | If this result is preceded immediately by another lock statement like this, please disregard it; | ||||||||||||||
| 93 | a lock collision occurred." | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | |||||||||||||||
| 96 | #If we've got more than 100 lock conflicts, we have big-time problems | ||||||||||||||
| 97 | 7 | my $results = $handle->getTestResults( $test->{id}, 100 ); | |||||||||||||
| 98 | |||||||||||||||
| 99 | #Remember, we're returned results from newest to oldest... | ||||||||||||||
| 100 | 7 | my $next_one = 0; | |||||||||||||
| 101 | 7 | foreach my $result (@$results) { | |||||||||||||
| 102 | 14 | ▶ | unless ( $result->{'status_id'} == $lock_status_id ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | |||||||||||||||
| 104 | #Clearly no lock conflict going on here if next_one is true | ||||||||||||||
| 105 | 6 | ▶ | last if $next_one; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | #Otherwise just skip it until we get to the test we locked | ||||||||||||||
| 108 | 0 | next; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 8 | ▶ | if ( $result->{id} == $res->{'id'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 7 | $next_one = 1; | |||||||||||||
| 113 | 7 | next; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 1 | ▶ | if ($next_one) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | |||||||||||||||
| 118 | #If we got this far, a lock conflict occurred. Try the next one. | ||||||||||||||
| 119 | 1 | warn "Lock conflict detected. Try again...\n"; | |||||||||||||
| 120 | 1 | return 0; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | #Prefer full titles (match mode) | ||||||||||||||
| 125 | return defined( $test->{'full_title'} ) | ||||||||||||||
| 126 | ? $test->{'full_title'} | ||||||||||||||
| 127 | 6 | ▶ | : $test->{'title'} | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 128 | if $next_one; | ||||||||||||||
| 129 | 0 | return -1; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 5 | 1; | |||||||||||||
| 133 | |||||||||||||||
| 134 | __END__ | ||||||||||||||