testrail-runs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. use Pod::Perldoc 3.10;
  43. sub help {
  44. @ARGV = ($0);
  45. Pod::Perldoc->run();
  46. exit 0;
  47. }
  48. my %opts;
  49. GetOptions(
  50. 'apiurl=s' => \$opts{'apiurl'},
  51. 'password=s' => \$opts{'pass'},
  52. 'user=s' => \$opts{'user'},
  53. 'j|project=s' => \$opts{'project'},
  54. 'c|config=s@' => \$opts{'configs'},
  55. 's|status=s@' => \$opts{'statuses'},
  56. 'mock' => \$opts{'mock'},
  57. 'e|encoding=s' => \$opts{'encoding'},
  58. 'h|help' => \$opts{'help'}
  59. );
  60. if ($opts{help}) { help(); }
  61. #Parse config file if we are missing api url/key or user
  62. my $homedir = my_home() || '.';
  63. if (-e $homedir . '/.testrailrc' && (!$opts{apiurl} || !$opts{pass} || !$opts{user}) ) {
  64. ($opts{apiurl},$opts{pass},$opts{user}) = TestRail::Utils::parseConfig($homedir,1);
  65. }
  66. TestRail::Utils::interrogateUser(\%opts,qw{apiurl user pass project});
  67. if ($opts{mock}) {
  68. use Test::LWP::UserAgent::TestRailMock;
  69. $opts{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  70. $opts{debug} = 1;
  71. }
  72. my $tr = TestRail::API->new($opts{apiurl},$opts{user},$opts{pass},$opts{'encoding'},$opts{'debug'});
  73. $tr->{'browser'} = $opts{'browser'} if $opts{'browser'};
  74. $tr->{'debug'} = 0;
  75. my ($status_ids,$user_ids);
  76. #Process statuses
  77. if ($opts{'statuses'}) {
  78. eval { @$status_ids = $tr->statusNamesToIds(@{$opts{'statuses'}}); };
  79. if ($@) {
  80. print "$@\n";
  81. exit 4;
  82. }
  83. }
  84. my $project = $tr->getProjectByName($opts{'project'});
  85. if (!$project) {
  86. print "No such project '$opts{project}'.\n";
  87. exit 6;
  88. }
  89. my @pconfigs;
  90. if (defined $opts{configs}) {
  91. my $avail_configs = $tr->getConfigurations($project->{'id'});
  92. my ($cname);
  93. @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @{$opts{configs}} } @$avail_configs; #Get a list of IDs from the names passed
  94. }
  95. if (defined($opts{configs}) && (scalar(@pconfigs) != scalar(@{$opts{configs}}))) {
  96. print("One or more configurations passed does not exist in your project!\n");
  97. exit 7;
  98. }
  99. my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
  100. $runs = $tr->getRuns($project->{'id'}) if (!$opts{'configs'}); # If configs are passed, global runs are not in consideration.
  101. $plans = $tr->getPlans($project->{'id'});
  102. foreach my $plan (@$plans) {
  103. $cruns = $tr->getChildRuns($plan);
  104. next if !$cruns;
  105. foreach my $run (@$cruns) {
  106. next if scalar(@pconfigs) != scalar(@{$run->{'config_ids'}});
  107. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  108. $found = 0;
  109. foreach my $cid (@{$run->{'config_ids'}}) {
  110. $found++ if grep {$_ == $cid} @pconfigs;
  111. }
  112. push(@$planRuns, $run) if $found == scalar(@{$run->{'config_ids'}});
  113. }
  114. }
  115. push(@$runs,@$planRuns);
  116. if ($opts{'statuses'}) {
  117. @$runs = $tr->getRunSummary(@$runs);
  118. @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
  119. foreach my $status (@{$opts{'statuses'}}) {
  120. @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it. Otherwise forget it.
  121. }
  122. }
  123. @$runs = map {$_->{name}} @$runs;
  124. print join("\n",@$runs)."\n" if scalar(@$runs);
  125. exit 0;
  126. __END__
  127. L<TestRail::API>
  128. L<File::HomeDir> for the finding of .testrailrc
  129. =head1 SPECIAL THANKS
  130. Thanks to cPanel Inc, for graciously funding the creation of this distribution.