testrail-runs 6.8 KB

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