testrail-runs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. =head1 PARAMETERS:
  10. =head2 MANDATORY PARAMETERS
  11. =over 4
  12. --apiurl : full URL to get to TestRail index document
  13. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  14. --user : Your TestRail User Name.
  15. -j --project : desired project name.
  16. =back
  17. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  18. =head2 OPTIONAL PARAMETERS
  19. =over 4
  20. -c --config : configuration name to filter runs. Can be passed multiple times.
  21. -s --status : only list runs with one or more tests having [status] in testrail. Can be passed multiple times.
  22. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  23. =back
  24. =head1 CONFIGURATION FILE
  25. In your \$HOME, (or the current directory, if your system has no concept of a home directory) put a file called .testrailrc with key=value syntax separated by newlines.
  26. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  27. All options specified thereby are overridden by passing the command-line switches above.
  28. =head1 MISCELLANEOUS OPTIONS:
  29. =over 4
  30. --mock : don't do any real HTTP requests.
  31. --help : show this output
  32. =back
  33. =cut
  34. use strict;
  35. use warnings;
  36. use utf8;
  37. use TestRail::API;
  38. use TestRail::Utils;
  39. use Getopt::Long;
  40. use File::HomeDir qw{my_home};
  41. my $opts ={};
  42. # Parse config file
  43. my $homedir = my_home() || '.';
  44. if (-e $homedir . '/.testrailrc') {
  45. $opts = TestRail::Utils::parseConfig($homedir);
  46. }
  47. GetOptions(
  48. 'apiurl=s' => \$opts->{'apiurl'},
  49. 'password=s' => \$opts->{'password'},
  50. 'user=s' => \$opts->{'user'},
  51. 'j|project=s' => \$opts->{'project'},
  52. 'c|config=s@' => \$opts->{'configs'},
  53. 's|status=s@' => \$opts->{'statuses'},
  54. 'mock' => \$opts->{'mock'},
  55. 'e|encoding=s' => \$opts->{'encoding'},
  56. 'h|help' => \$opts->{'help'}
  57. );
  58. if ($opts->{help}) { TestRail::Utils::help(); }
  59. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project});
  60. if ($opts->{mock}) {
  61. use Test::LWP::UserAgent::TestRailMock;
  62. $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  63. $opts->{debug} = 1;
  64. }
  65. my $tr = TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
  66. $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
  67. $tr->{'debug'} = 0;
  68. my ($status_ids,$user_ids);
  69. #Process statuses
  70. if ($opts->{'statuses'}) {
  71. eval { @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}); };
  72. if ($@) {
  73. print "$@\n";
  74. exit 4;
  75. }
  76. }
  77. my $project = $tr->getProjectByName($opts->{'project'});
  78. if (!$project) {
  79. print "No such project '$opts->{project}'.\n";
  80. exit 6;
  81. }
  82. my @pconfigs;
  83. if (defined $opts->{configs}) {
  84. my $avail_configs = $tr->getConfigurations($project->{'id'});
  85. my ($cname);
  86. @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @{$opts->{configs}} } @$avail_configs; #Get a list of IDs from the names passed
  87. }
  88. if (defined($opts->{configs}) && (scalar(@pconfigs) != scalar(@{$opts->{configs}}))) {
  89. print("One or more configurations passed does not exist in your project!\n");
  90. exit 7;
  91. }
  92. my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
  93. $runs = $tr->getRuns($project->{'id'}) if (!$opts->{'configs'}); # If configs are passed, global runs are not in consideration.
  94. $plans = $tr->getPlans($project->{'id'});
  95. foreach my $plan (@$plans) {
  96. $cruns = $tr->getChildRuns($plan);
  97. next if !$cruns;
  98. foreach my $run (@$cruns) {
  99. next if scalar(@pconfigs) != scalar(@{$run->{'config_ids'}});
  100. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  101. $found = 0;
  102. foreach my $cid (@{$run->{'config_ids'}}) {
  103. $found++ if grep {$_ == $cid} @pconfigs;
  104. }
  105. push(@$planRuns, $run) if $found == scalar(@{$run->{'config_ids'}});
  106. }
  107. }
  108. push(@$runs,@$planRuns);
  109. if ($opts->{'statuses'}) {
  110. @$runs = $tr->getRunSummary(@$runs);
  111. @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
  112. foreach my $status (@{$opts->{'statuses'}}) {
  113. @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it. Otherwise forget it.
  114. }
  115. }
  116. @$runs = map {$_->{name}} @$runs;
  117. print join("\n",@$runs)."\n" if scalar(@$runs);
  118. exit 0;
  119. __END__
  120. L<TestRail::API>
  121. L<File::HomeDir> for the finding of .testrailrc
  122. =head1 SPECIAL THANKS
  123. Thanks to cPanel Inc, for graciously funding the creation of this distribution.