| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #!/usr/bin/env perl
- # ABSTRACT: List runs in a TestRail project matching the provided filters
- # PODNAME: testrail-runs
- =head1 SYNOPSIS
- testrail-runs [OPTIONS] | xargs prove -PTestrail=...
- =head1 DESCRIPTION
- testrail-tests - list runs in a TestRail project matching the provided filters.
- Groups by plan for runs which are children of a plan.
- =head1 PARAMETERS:
- =head2 MANDATORY PARAMETERS
- =over 4
- --apiurl : full URL to get to TestRail index document
- --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
- --user : Your TestRail User Name.
- -j --project : desired project name.
- =back
- All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
- =head2 OPTIONAL PARAMETERS
- =over 4
- -c --config : configuration name to filter runs. Can be passed multiple times.
- -s --status : only list runs with one or more tests having [status] in testrail. Can be passed multiple times.
- -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
- =back
- =head1 CONFIGURATION FILE
- 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.
- Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
- All options specified thereby are overridden by passing the command-line switches above.
- =head1 MISCELLANEOUS OPTIONS:
- =over 4
- --mock : don't do any real HTTP requests.
- --help : show this output
- =back
- =cut
- use strict;
- use warnings;
- use utf8;
- use TestRail::API;
- use TestRail::Utils;
- use Getopt::Long;
- use File::HomeDir qw{my_home};
- my $opts ={};
- # Parse config file
- my $homedir = my_home() || '.';
- if (-e $homedir . '/.testrailrc') {
- $opts = TestRail::Utils::parseConfig($homedir);
- }
- GetOptions(
- 'apiurl=s' => \$opts->{'apiurl'},
- 'password=s' => \$opts->{'password'},
- 'user=s' => \$opts->{'user'},
- 'j|project=s' => \$opts->{'project'},
- 'c|config=s@' => \$opts->{'configs'},
- 's|status=s@' => \$opts->{'statuses'},
- 'mock' => \$opts->{'mock'},
- 'e|encoding=s' => \$opts->{'encoding'},
- 'h|help' => \$opts->{'help'}
- );
- if ($opts->{help}) { TestRail::Utils::help(); }
- TestRail::Utils::interrogateUser($opts,qw{apiurl user password project});
- if ($opts->{mock}) {
- use Test::LWP::UserAgent::TestRailMock;
- $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
- $opts->{debug} = 1;
- }
- my $tr = TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
- $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
- $tr->{'debug'} = 0;
- my ($status_ids,$user_ids);
- #Process statuses
- if ($opts->{'statuses'}) {
- eval { @$status_ids = $tr->statusNamesToIds(@{$opts->{'statuses'}}); };
- if ($@) {
- print "$@\n";
- exit 4;
- }
- }
- my $project = $tr->getProjectByName($opts->{'project'});
- if (!$project) {
- print "No such project '$opts->{project}'.\n";
- exit 6;
- }
- my @pconfigs;
- if (defined $opts->{configs}) {
- my $avail_configs = $tr->getConfigurations($project->{'id'});
- my ($cname);
- @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @{$opts->{configs}} } @$avail_configs; #Get a list of IDs from the names passed
- }
- if (defined($opts->{configs}) && (scalar(@pconfigs) != scalar(@{$opts->{configs}}))) {
- print("One or more configurations passed does not exist in your project!\n");
- exit 7;
- }
- my ($runs,$plans,$planRuns,$cruns,$found) = ([],[],[],[],0);
- $runs = $tr->getRuns($project->{'id'}) if (!$opts->{'configs'}); # If configs are passed, global runs are not in consideration.
- $plans = $tr->getPlans($project->{'id'});
- foreach my $plan (@$plans) {
- $cruns = $tr->getChildRuns($plan);
- next if !$cruns;
- foreach my $run (@$cruns) {
- next if scalar(@pconfigs) != scalar(@{$run->{'config_ids'}});
- #Compare run config IDs against desired, invalidate run if all conditions not satisfied
- $found = 0;
- foreach my $cid (@{$run->{'config_ids'}}) {
- $found++ if grep {$_ == $cid} @pconfigs;
- }
- push(@$planRuns, $run) if $found == scalar(@{$run->{'config_ids'}});
- }
- }
- push(@$runs,@$planRuns);
- if ($opts->{'statuses'}) {
- @$runs = $tr->getRunSummary(@$runs);
- @$runs = grep { defined($_->{'run_status'}) } @$runs; #Filter stuff with no results
- foreach my $status (@{$opts->{'statuses'}}) {
- @$runs = grep { $_->{'run_status'}->{$status} } @$runs; #If it's positive, keep it. Otherwise forget it.
- }
- }
- @$runs = map {$_->{name}} @$runs;
- print join("\n",@$runs)."\n" if scalar(@$runs);
- exit 0;
- __END__
- L<TestRail::API>
- L<File::HomeDir> for the finding of .testrailrc
- =head1 SPECIAL THANKS
- Thanks to cPanel Inc, for graciously funding the creation of this distribution.
|