testrail-runs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env perl
  2. # ABSTRACT: List runs in a TestRail project matching the provided filters
  3. # PODNAME: testrail-runs
  4. =head1 SYNOPSIS
  5. testrail-runs [OPTIONS] | xargs prove -PTestrail=...
  6. =head1 DESCRIPTION
  7. testrail-tests - list runs in a TestRail project matching the provided filters.
  8. Groups by plan for runs which are children of a plan.
  9. =head2 PARAMETERS:
  10. =head3 MANDATORY PARAMETERS
  11. -j --project [project]: desired project name.
  12. =head3 OPTIONAL PARAMETERS
  13. -c --config [config]: configuration name to filter runs. Can be passed multiple times.
  14. -s --status [status]: only list runs with one or more tests having [status] in testrail. Can be passed multiple times.
  15. =head3 CONFIG OPTIONS
  16. In your \$HOME, (or the current directory, if your system has no
  17. concept of a home directory) put a file called .testrailrc with
  18. key=value syntax separated by newlines.
  19. Valid Keys are: apiurl,user,password
  20. =head3 CONFIG OVERRIDES
  21. These override the config, if present.
  22. If neither are used, you will be prompted.
  23. --apiurl [url] : full URL to get to TestRail index document
  24. --password [key] : Your TestRail Password.
  25. --user [name] : Your TestRail User Name.
  26. =head2 TESTING OPTIONS:
  27. --mock: don't do any real HTTP requests.
  28. --help: show this output
  29. =cut
  30. use strict;
  31. use warnings;
  32. use utf8;
  33. use TestRail::API;
  34. use TestRail::Utils;
  35. use Getopt::Long;
  36. use File::HomeDir qw{my_home};
  37. use File::Find;
  38. use Cwd qw{abs_path};
  39. use File::Basename qw{basename};
  40. sub help {
  41. print("
  42. testrail-tests - list tests in a run matching the provided filters.
  43. USAGE:
  44. testrail-tests [OPTIONS] | xargs prove -PTestrail=...
  45. PARAMETERS:
  46. [MANDATORY PARAMETERS]
  47. -j --project [project]: desired project name.
  48. [OPTIONAL PARAMETERS]
  49. -c --config [config]: configuration name to filter runs. Can be passed multiple times.
  50. -s --status [status]: only list runs with one or more tests having [status] in testrail. Can be passed multiple times.
  51. [CONFIG OPTIONS]
  52. In your \$HOME, (or the current directory, if your system has no
  53. concept of a home directory) put a file called .testrailrc with
  54. key=value syntax separated by newlines.
  55. Valid Keys are: apiurl,user,password
  56. [CONFIG OVERRIDES]
  57. These override the config, if present.
  58. If neither are used, you will be prompted.
  59. --apiurl [url] : full URL to get to TestRail index document
  60. --password [key] : Your TestRail Password.
  61. --user [name] : Your TestRail User Name.
  62. TESTING OPTIONS:
  63. --mock: don't do any real HTTP requests.
  64. --help: show this output
  65. ");
  66. exit 0;
  67. }
  68. my %opts;
  69. GetOptions(
  70. 'apiurl' => \$opts{'apiurl'},
  71. 'password' => \$opts{'pass'},
  72. 'user' => \$opts{'user'},
  73. 'j|project=s' => \$opts{'project'},
  74. 'c|config=s@' => \$opts{'configs'},
  75. 's|status=s@' => \$opts{'statuses'},
  76. 'mock' => \$opts{'mock'},
  77. 'h|help' => \$opts{'help'}
  78. );
  79. if ($opts{help}) { help(); }
  80. #Parse config file if we are missing api url/key or user
  81. my $homedir = my_home() || '.';
  82. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  83. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir);
  84. }
  85. #Interrogate user if they didn't provide info
  86. if (!$opts{apiurl}) {
  87. print "Type the API endpoint url for your testLink install below:\n";
  88. $opts{apiurl} = TestRail::Utils::userInput();
  89. }
  90. if (!$opts{user}) {
  91. print "Type your testLink user name below:\n";
  92. $opts{user} = TestRail::Utils::userInput();
  93. }
  94. if (!$opts{pass}) {
  95. print "Type the password for your testLink user below:\n";
  96. $opts{pass} = TestRail::Utils::userInput();
  97. }
  98. if (!$opts{apiurl} || !$opts{pass} || !$opts{user}) {
  99. print "ERROR: api url, username and password cannot be blank.\n";
  100. exit 1;
  101. }
  102. #Interrogate user if they didn't provide info
  103. if (!$opts{project}) {
  104. print "Type the name of the project you are testing under:\n";
  105. $opts{project} = TestRail::Utils::userInput();
  106. }
  107. if ($opts{mock}) {
  108. use Test::LWP::UserAgent::TestRailMock;
  109. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  110. $opts{debug} = 1;
  111. }
  112. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'debug'});
  113. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  114. $tr->{'debug'} = 0;
  115. my ($status_ids,$user_ids);
  116. #Process statuses
  117. if ($opts{'statuses'}) {
  118. eval { @$status_ids = $tr->statusNamesToIds(@{$opts{'statuses'}}); };
  119. if ($@) {
  120. print "$@\n";
  121. exit 4;
  122. }
  123. }
  124. my $project = $tr->getProjectByName($opts{'project'});
  125. if (!$project) {
  126. print "No such project '$opts{project}'.\n";
  127. exit 6;
  128. }
  129. my @pconfigs;
  130. if (defined $opts{configs}) {
  131. my $avail_configs = $tr->getConfigurations($project->{'id'});
  132. my ($cname);
  133. @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @{$opts{configs}} } @$avail_configs; #Get a list of IDs from the names passed
  134. }
  135. if (defined($opts{configs}) && (scalar(@pconfigs) != scalar(@{$opts{configs}}))) {
  136. print("One or more configurations passed does not exist in your project!\n");
  137. exit 7;
  138. }
  139. my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
  140. $runs = $tr->getRuns($project->{'id'}) if (!$opts{'configs'}); # If configs are passed, global runs are not in consideration.
  141. $plans = $tr->getPlans($project->{'id'});
  142. foreach my $plan (@$plans) {
  143. $cruns = $tr->getChildRuns($plan);
  144. next if !$cruns;
  145. foreach my $run (@$cruns) {
  146. next if scalar(@pconfigs) != scalar(@{$run->{'config_ids'}});
  147. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  148. $found = 0;
  149. foreach my $cid (@{$run->{'config_ids'}}) {
  150. $found++ if grep {$_ == $cid} @pconfigs;
  151. }
  152. push(@$planRuns, $run) if $found == scalar(@{$run->{'config_ids'}});
  153. }
  154. }
  155. push(@$runs,@$planRuns);
  156. if ($opts{'statuses'}) {
  157. @$runs = $tr->getRunSummary(@$runs);
  158. @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
  159. foreach my $status (@{$opts{'statuses'}}) {
  160. @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it. Otherwise forget it.
  161. }
  162. }
  163. @$runs = map {$_->{name}} @$runs;
  164. print join("\n",@$runs)."\n" if scalar(@$runs);
  165. exit 0;
  166. __END__
  167. L<TestRail::API>
  168. L<File::HomeDir> for the finding of .testrailrc
  169. =head1 SPECIAL THANKS
  170. Thanks to cPanel Inc, for graciously funding the creation of this module.