API.pm 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  1. # ABSTRACT: Provides an interface to TestRail's REST api via HTTP
  2. # PODNAME: TestRail::API
  3. package TestRail::API;
  4. =head1 SYNOPSIS
  5. use TestRail::API;
  6. my ($username,$password,$host) = ('foo','bar','http://testrail.baz.foo');
  7. my $tr = TestRail::API->new($host, $username, $password);
  8. =head1 DESCRIPTION
  9. C<TestRail::API> provides methods to access an existing TestRail account using API v2. You can then do things like look up tests, set statuses and create runs from lists of cases.
  10. It is by no means exhaustively implementing every TestRail API function.
  11. =head1 IMPORTANT
  12. All the methods aside from the constructor should not die, but return a false value upon failure (see exceptions below).
  13. When the server is not responsive, expect a -500 response, and retry accordingly by setting the num_tries parameter in the constructor.
  14. Also, all *ByName methods are vulnerable to duplicate naming issues. Try not to use the same name for:
  15. * projects
  16. * testsuites within the same project
  17. * sections within the same testsuite that are peers
  18. * test cases
  19. * test plans and runs outside of plans which are not completed
  20. * configurations
  21. To do so will result in the first of said item found being returned rather than an array of possibilities to choose from.
  22. There are two exceptions to this, in the case of 401 and 403 responses, as these failing generally mean your program has no chance of success anyways.
  23. =cut
  24. use 5.010;
  25. use strict;
  26. use warnings;
  27. use Carp qw{cluck confess};
  28. use Scalar::Util qw{reftype looks_like_number};
  29. use Clone 'clone';
  30. use Try::Tiny;
  31. use Types::Standard qw( slurpy ClassName Object Str Int Bool HashRef ArrayRef Maybe Optional);
  32. use Type::Params qw( compile );
  33. use JSON::MaybeXS 1.001000 ();
  34. use HTTP::Request;
  35. use LWP::UserAgent;
  36. use HTTP::CookieJar::LWP;
  37. use Data::Validate::URI qw{is_uri};
  38. use List::Util 1.33;
  39. use Encode ();
  40. =head1 CONSTRUCTOR
  41. =head2 B<new (api_url, user, password, encoding, debug, do_post_redirect)>
  42. Creates new C<TestRail::API> object.
  43. =over 4
  44. =item STRING C<API URL> - base url for your TestRail api server.
  45. =item STRING C<USER> - Your TestRail User.
  46. =item STRING C<PASSWORD> - Your TestRail password, or a valid API key (TestRail 4.2 and above).
  47. =item STRING C<ENCODING> - The character encoding used by the caller. Defaults to 'UTF-8', see L<Encode::Supported> and for supported encodings.
  48. =item BOOLEAN C<DEBUG> (optional) - Print the JSON responses from TestRail with your requests. Default false.
  49. =item BOOLEAN C<DO_POST_REDIRECT> (optional) - Follow redirects on POST requests (most add/edit/delete calls are POSTs). Default false.
  50. =item INTEGER C<MAX_TRIES> (optional) - Try requests up to X number of times if they fail with anything other than 401/403. Useful with flaky external authenticators, or timeout issues. Default 1.
  51. =item HASHREF C<USER_FETCH_OPTS> (optional) - Options relating to getUsers call done during new:
  52. =over 4
  53. =item BOOLEAN C<skip_userfetch> - Skip fetching all TR users during construction. Default false.
  54. This will save you some time on servers with quite a few users, especially if you don't
  55. particularly have a need to know about things related to TR users themselves.
  56. If you do need this info, you don't really save any time, however, as it will fetch them
  57. in the relevant subroutines that need this information.
  58. Also, on newer versions of TestRail, user fetching is not possible unless you either:
  59. * Are an administrator on the server
  60. * Provide the project_id (https://www.gurock.com/testrail/docs/api/reference/users)
  61. =item STRING C<project_id> - String or number corresponding to a project ID to use when fetching users.
  62. =back
  63. =back
  64. Returns C<TestRail::API> object if login is successful.
  65. my $tr = TestRail::API->new('http://tr.test/testrail', 'moo','M000000!');
  66. Dies on all communication errors with the TestRail server.
  67. Does not do above checks if debug is passed.
  68. =cut
  69. sub new {
  70. state $check = compile(ClassName, Str, Str, Str, Optional[Maybe[Str]], Optional[Maybe[Bool]], Optional[Maybe[Bool]], Optional[Maybe[Int]],Optional[Maybe[HashRef]]);
  71. my ($class,$apiurl,$user,$pass,$encoding,$debug, $do_post_redirect,$max_tries,$userfetch_opts) = $check->(@_);
  72. die("Invalid URI passed to constructor") if !is_uri($apiurl);
  73. $debug //= 0;
  74. my $self = {
  75. user => $user,
  76. pass => $pass,
  77. apiurl => $apiurl,
  78. debug => $debug,
  79. encoding => $encoding || 'UTF-8',
  80. testtree => [],
  81. flattree => [],
  82. user_cache => [],
  83. configurations => {},
  84. tr_fields => undef,
  85. tr_project_id => $userfetch_opts->{'project_id'},
  86. default_request => undef,
  87. global_limit => 250, #Discovered by experimentation
  88. browser => LWP::UserAgent->new(
  89. keep_alive => 10,
  90. cookie_jar => HTTP::CookieJar::LWP->new(),
  91. ),
  92. do_post_redirect => $do_post_redirect,
  93. max_tries => $max_tries // 1,
  94. retry_delay => 5,
  95. };
  96. #Allow POST redirects
  97. if ($self->{do_post_redirect}) {
  98. push @{ $self->{'browser'}->requests_redirectable }, 'POST';
  99. }
  100. #Check chara encoding
  101. $self->{'encoding-nonaliased'} = Encode::resolve_alias($self->{'encoding'});
  102. die("Invalid encoding alias '".$self->{'encoding'}."' passed, see Encoding::Supported for a list of allowed encodings")
  103. unless $self->{'encoding-nonaliased'};
  104. die("Invalid encoding '".$self->{'encoding-nonaliased'}."' passed, see Encoding::Supported for a list of allowed encodings")
  105. unless grep {$_ eq $self->{'encoding-nonaliased'}} (Encode->encodings(":all"));
  106. #Create default request to pass on to LWP::UserAgent
  107. $self->{'default_request'} = HTTP::Request->new();
  108. $self->{'default_request'}->authorization_basic($user,$pass);
  109. bless( $self, $class );
  110. return $self if $self->debug; #For easy class testing without mocks
  111. # Manually do the get_users call to check HTTP status...
  112. # Allow users to skip the check if you have a zillion users etc,
  113. # as apparently that is fairly taxing on TR itself.
  114. if( !$userfetch_opts->{skip_usercache} ) {
  115. my $res = $self->getUsers($userfetch_opts->{project_id});
  116. confess "Error: network unreachable" if !defined($res);
  117. if ( (reftype($res) || 'undef') ne 'ARRAY') {
  118. confess "Unexpected return from _doRequest: $res" if !looks_like_number($res);
  119. confess "Could not communicate with TestRail Server! Check that your URI is correct, and your TestRail installation is functioning correctly." if $res == -500;
  120. confess "Could not list testRail users! Check that your TestRail installation has it's API enabled, and your credentials are correct" if $res == -403;
  121. confess "Bad user credentials!" if $res == -401;
  122. confess "HTTP error $res encountered while communicating with TestRail server. Resolve issue and try again." if $res < 0;
  123. confess "Unknown error occurred: $res";
  124. }
  125. confess "No users detected on TestRail Install! Check that your API is functioning correctly." if !scalar(@$res);
  126. }
  127. return $self;
  128. }
  129. =head1 GETTERS
  130. =head2 B<apiurl>
  131. =head2 B<debug>
  132. Accessors for these parameters you pass into the constructor, in case you forget.
  133. =cut
  134. sub apiurl {
  135. state $check = compile(Object);
  136. my ($self) = $check->(@_);
  137. return $self->{'apiurl'}
  138. }
  139. sub debug {
  140. state $check = compile(Object);
  141. my ($self) = $check->(@_);
  142. return $self->{'debug'};
  143. }
  144. =head2 B<retry_delay>
  145. There is no getter/setter for this parameter, but it is worth mentioning.
  146. This is the number of seconds to wait between failed request retries when max_retries > 1.
  147. #Do something other than the default of 5s, like spam the server mercilessly
  148. $tr->{retry_delay} = 0;
  149. ...
  150. =cut
  151. #Convenient JSON-HTTP fetcher
  152. sub _doRequest {
  153. state $check = compile(Object, Str, Optional[Maybe[Str]], Optional[Maybe[HashRef]]);
  154. my ($self,$path,$method,$data) = $check->(@_);
  155. $self->{num_tries}++;
  156. my $req = clone $self->{'default_request'};
  157. $method //= 'GET';
  158. $req->method($method);
  159. $req->url($self->apiurl.'/'.$path);
  160. warn "$method ".$self->apiurl."/$path" if $self->debug;
  161. my $coder = JSON::MaybeXS->new;
  162. #Data sent is JSON, and encoded per user preference
  163. my $content = $data ? Encode::encode( $self->{'encoding-nonaliased'}, $coder->encode($data) ) : '';
  164. $req->content($content);
  165. $req->header( "Content-Type" => "application/json; charset=".$self->{'encoding'} );
  166. my $response = eval { $self->{'browser'}->request($req) };
  167. #Uncomment to generate mocks
  168. #use Data::Dumper;
  169. #open(my $fh, '>>', 'mock.out');
  170. #print $fh "{\n\n";
  171. #print $fh Dumper($path,'200','OK',$response->headers,$response->content);
  172. #print $fh '$mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));';
  173. #print $fh "\n\n}\n\n";
  174. #close $fh;
  175. if ($@) {
  176. #LWP threw an ex, probably a timeout
  177. if ($self->{num_tries} >= $self->{max_tries}) {
  178. $self->{num_tries} = 0;
  179. confess "Failed to satisfy request after $self->{num_tries} tries!";
  180. }
  181. cluck "WARNING: TestRail API request failed due to timeout, or other LWP fatal condition, re-trying request...\n";
  182. sleep $self->{retry_delay} if $self->{retry_delay};
  183. goto &_doRequest;
  184. }
  185. return $response if !defined($response); #worst case
  186. if ($response->code == 403) {
  187. confess "ERROR 403: Access Denied: ".$response->content;
  188. }
  189. if ($response->code == 401) {
  190. confess "ERROR 401: Authentication failed: ".$response->content;
  191. }
  192. if ($response->code != 200) {
  193. #LWP threw an ex, probably a timeout
  194. if ($self->{num_tries} >= $self->{max_tries}) {
  195. $self->{num_tries} = 0;
  196. cluck "ERROR: Arguments Bad? (got code ".$response->code."): ".$response->content;
  197. return -int($response->code);
  198. }
  199. cluck "WARNING: TestRail API request failed (got code ".$response->code."), re-trying request...\n";
  200. sleep $self->{retry_delay} if $self->{retry_delay};
  201. goto &_doRequest;
  202. }
  203. $self->{num_tries} = 0;
  204. try {
  205. return $coder->decode($response->content);
  206. } catch {
  207. if ($response->code == 200 && !$response->content) {
  208. return 1; #This function probably just returns no data
  209. } else {
  210. cluck "ERROR: Malformed JSON returned by API.";
  211. cluck $@;
  212. if (!$self->debug) { #Otherwise we've already printed this, but we need to know if we encounter this
  213. cluck "RAW CONTENT:";
  214. cluck $response->content
  215. }
  216. return 0;
  217. }
  218. }
  219. }
  220. =head1 USER METHODS
  221. =head2 B<getUsers ()>
  222. Get all the user definitions for the provided Test Rail install.
  223. Returns ARRAYREF of user definition HASHREFs.
  224. =cut
  225. sub getUsers {
  226. state $check = compile(Object,Optional[Maybe[Str]]);
  227. my ($self,$project_id) = $check->(@_);
  228. # Return shallow clone of user_cache if set.
  229. return [ @{ $self->{'user_cache'} } ] if ref $self->{'user_cache'} eq 'ARRAY' && scalar(@{$self->{'user_cache'}});
  230. my $maybe_project = $project_id ? "/$project_id" : '';
  231. my $res = $self->_doRequest("index.php?/api/v2/get_users$maybe_project");
  232. return -500 if !$res || (reftype($res) || 'undef') ne 'ARRAY';
  233. $self->{'user_cache'} = $res;
  234. return clone($res);
  235. }
  236. =head2 B<getUserByID(id)>
  237. =cut
  238. =head2 B<getUserByName(name)>
  239. =cut
  240. =head2 B<getUserByEmail(email)>
  241. Get user definition hash by ID, Name or Email.
  242. Returns user definition HASHREF.
  243. For efficiency's sake, these methods cache the result of getUsers until you explicitly run it again.
  244. =cut
  245. sub getUserByID {
  246. state $check = compile(Object, Int);
  247. my ($self,$user) = $check->(@_);
  248. my $users = $self->getUsers();
  249. return $users if ref $users ne 'ARRAY';
  250. foreach my $usr (@$users) {
  251. return $usr if $usr->{'id'} == $user;
  252. }
  253. return 0;
  254. }
  255. sub getUserByName {
  256. state $check = compile(Object, Str);
  257. my ($self,$user) = $check->(@_);
  258. my $users = $self->getUsers();
  259. return $users if ref $users ne 'ARRAY';
  260. foreach my $usr (@$users) {
  261. return $usr if $usr->{'name'} eq $user;
  262. }
  263. return 0;
  264. }
  265. sub getUserByEmail {
  266. state $check = compile(Object, Str);
  267. my ($self,$email) = $check->(@_);
  268. my $users = $self->getUsers();
  269. return $users if ref $users ne 'ARRAY';
  270. foreach my $usr (@$users) {
  271. return $usr if $usr->{'email'} eq $email;
  272. }
  273. return 0;
  274. }
  275. =head2 userNamesToIds(names)
  276. Convenience method to translate a list of user names to TestRail user IDs.
  277. =over 4
  278. =item ARRAY C<NAMES> - Array of user names to translate to IDs.
  279. =back
  280. Returns ARRAY of user IDs.
  281. Throws an exception in the case of one (or more) of the names not corresponding to a valid username.
  282. =cut
  283. sub userNamesToIds {
  284. state $check = compile(Object, slurpy ArrayRef[Str]);
  285. my ($self,$names) = $check->(@_);
  286. confess("At least one user name must be provided") if !scalar(@$names);
  287. my @ret = grep {defined $_} map {my $user = $_; my @list = grep {$user->{'name'} eq $_} @$names; scalar(@list) ? $user->{'id'} : undef} @{$self->getUsers()};
  288. confess("One or more user names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
  289. return @ret;
  290. };
  291. =head1 PROJECT METHODS
  292. =head2 B<createProject (name, [description,send_announcement])>
  293. Creates new Project (Database of testsuites/tests).
  294. Optionally specify an announcement to go out to the users.
  295. Requires TestRail admin login.
  296. =over 4
  297. =item STRING C<NAME> - Desired name of project.
  298. =item STRING C<DESCRIPTION> (optional) - Description of project. Default value is 'res ipsa loquiter'.
  299. =item BOOLEAN C<SEND ANNOUNCEMENT> (optional) - Whether to confront users with an announcement about your awesome project on next login. Default false.
  300. =back
  301. Returns project definition HASHREF on success, false otherwise.
  302. $tl->createProject('Widgetronic 4000', 'Tests for the whiz-bang new product', true);
  303. =cut
  304. sub createProject {
  305. state $check = compile(Object, Str, Optional[Maybe[Str]], Optional[Maybe[Bool]]);
  306. my ($self,$name,$desc,$announce) = $check->(@_);
  307. $desc //= 'res ipsa loquiter';
  308. $announce //= 0;
  309. my $input = {
  310. name => $name,
  311. announcement => $desc,
  312. show_announcement => $announce
  313. };
  314. return $self->_doRequest('index.php?/api/v2/add_project','POST',$input);
  315. }
  316. =head2 B<deleteProject (id)>
  317. Deletes specified project by ID.
  318. Requires TestRail admin login.
  319. =over 4
  320. =item STRING C<NAME> - Desired name of project.
  321. =back
  322. Returns BOOLEAN.
  323. $success = $tl->deleteProject(1);
  324. =cut
  325. sub deleteProject {
  326. state $check = compile(Object, Int);
  327. my ($self,$proj) = $check->(@_);
  328. return $self->_doRequest('index.php?/api/v2/delete_project/'.$proj,'POST');
  329. }
  330. =head2 B<getProjects ()>
  331. Get all available projects
  332. Returns array of project definition HASHREFs, false otherwise.
  333. $projects = $tl->getProjects;
  334. =cut
  335. sub getProjects {
  336. state $check = compile(Object);
  337. my ($self) = $check->(@_);
  338. my $result = $self->_doRequest('index.php?/api/v2/get_projects');
  339. #Save state for future use, if needed
  340. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  341. $self->{'testtree'} = $result;
  342. #Note that it's a project for future reference by recursive tree search
  343. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  344. foreach my $pj (@{$result}) {
  345. $pj->{'type'} = 'project';
  346. }
  347. return $result;
  348. }
  349. =head2 B<getProjectByName ($project)>
  350. Gets some project definition hash by it's name
  351. =over 4
  352. =item STRING C<PROJECT> - desired project
  353. =back
  354. Returns desired project definition HASHREF, false otherwise.
  355. $project = $tl->getProjectByName('FunProject');
  356. =cut
  357. sub getProjectByName {
  358. state $check = compile(Object, Str);
  359. my ($self,$project) = $check->(@_);
  360. #See if we already have the project list...
  361. my $projects = $self->{'testtree'};
  362. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  363. $projects = $self->getProjects() unless scalar(@$projects);
  364. #Search project list for project
  365. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  366. for my $candidate (@$projects) {
  367. return $candidate if ($candidate->{'name'} eq $project);
  368. }
  369. return 0;
  370. }
  371. =head2 B<getProjectByID ($project)>
  372. Gets some project definition hash by it's ID
  373. =over 4
  374. =item INTEGER C<PROJECT> - desired project
  375. =back
  376. Returns desired project definition HASHREF, false otherwise.
  377. $projects = $tl->getProjectByID(222);
  378. =cut
  379. sub getProjectByID {
  380. state $check = compile(Object, Int);
  381. my ($self,$project) = $check->(@_);
  382. #See if we already have the project list...
  383. my $projects = $self->{'testtree'};
  384. $projects = $self->getProjects() unless scalar(@$projects);
  385. #Search project list for project
  386. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  387. for my $candidate (@$projects) {
  388. return $candidate if ($candidate->{'id'} eq $project);
  389. }
  390. return 0;
  391. }
  392. =head1 TESTSUITE METHODS
  393. =head2 B<createTestSuite (project_id, name, [description])>
  394. Creates new TestSuite (folder of tests) in the database of test specifications under given project id having given name and details.
  395. =over 4
  396. =item INTEGER C<PROJECT ID> - ID of project this test suite should be under.
  397. =item STRING C<NAME> - Desired name of test suite.
  398. =item STRING C<DESCRIPTION> (optional) - Description of test suite. Default value is 'res ipsa loquiter'.
  399. =back
  400. Returns TS definition HASHREF on success, false otherwise.
  401. $tl->createTestSuite(1, 'broken tests', 'Tests that should be reviewed');
  402. =cut
  403. sub createTestSuite {
  404. state $check = compile(Object, Int, Str, Optional[Maybe[Str]]);
  405. my ($self,$project_id,$name,$details) = $check->(@_);
  406. $details //= 'res ipsa loquiter';
  407. my $input = {
  408. name => $name,
  409. description => $details
  410. };
  411. return $self->_doRequest('index.php?/api/v2/add_suite/'.$project_id,'POST',$input);
  412. }
  413. =head2 B<deleteTestSuite (suite_id)>
  414. Deletes specified testsuite.
  415. =over 4
  416. =item INTEGER C<SUITE ID> - ID of testsuite to delete.
  417. =back
  418. Returns BOOLEAN.
  419. $tl->deleteTestSuite(1);
  420. =cut
  421. sub deleteTestSuite {
  422. state $check = compile(Object, Int);
  423. my ($self,$suite_id) = $check->(@_);
  424. return $self->_doRequest('index.php?/api/v2/delete_suite/'.$suite_id,'POST');
  425. }
  426. =head2 B<getTestSuites (project_id)>
  427. Gets the testsuites for a project
  428. =over 4
  429. =item STRING C<PROJECT ID> - desired project's ID
  430. =back
  431. Returns ARRAYREF of testsuite definition HASHREFs, 0 on error.
  432. $suites = $tl->getTestSuites(123);
  433. =cut
  434. sub getTestSuites {
  435. state $check = compile(Object, Int);
  436. my ($self,$proj) = $check->(@_);
  437. return $self->_doRequest('index.php?/api/v2/get_suites/'.$proj);
  438. }
  439. =head2 B<getTestSuiteByName (project_id,testsuite_name)>
  440. Gets the testsuite that matches the given name inside of given project.
  441. =over 4
  442. =item STRING C<PROJECT ID> - ID of project holding this testsuite
  443. =item STRING C<TESTSUITE NAME> - desired parent testsuite name
  444. =back
  445. Returns desired testsuite definition HASHREF, false otherwise.
  446. $suites = $tl->getTestSuitesByName(321, 'hugSuite');
  447. =cut
  448. sub getTestSuiteByName {
  449. state $check = compile(Object, Int, Str);
  450. my ($self,$project_id,$testsuite_name) = $check->(@_);
  451. #TODO cache
  452. my $suites = $self->getTestSuites($project_id);
  453. return -500 if !$suites || (reftype($suites) || 'undef') ne 'ARRAY'; #No suites for project, or no project
  454. foreach my $suite (@$suites) {
  455. return $suite if $suite->{'name'} eq $testsuite_name;
  456. }
  457. return 0; #Couldn't find it
  458. }
  459. =head2 B<getTestSuiteByID (testsuite_id)>
  460. Gets the testsuite with the given ID.
  461. =over 4
  462. =item STRING C<TESTSUITE_ID> - TestSuite ID.
  463. =back
  464. Returns desired testsuite definition HASHREF, false otherwise.
  465. $tests = $tl->getTestSuiteByID(123);
  466. =cut
  467. sub getTestSuiteByID {
  468. state $check = compile(Object, Int);
  469. my ($self,$testsuite_id) = $check->(@_);
  470. return $self->_doRequest('index.php?/api/v2/get_suite/'.$testsuite_id);
  471. }
  472. =head1 SECTION METHODS
  473. =head2 B<createSection(project_id,suite_id,name,[parent_id])>
  474. Creates a section.
  475. =over 4
  476. =item INTEGER C<PROJECT ID> - Parent Project ID.
  477. =item INTEGER C<SUITE ID> - Parent TestSuite ID.
  478. =item STRING C<NAME> - desired section name.
  479. =item INTEGER C<PARENT ID> (optional) - parent section id
  480. =back
  481. Returns new section definition HASHREF, false otherwise.
  482. $section = $tr->createSection(1,1,'nugs',1);
  483. =cut
  484. sub createSection {
  485. state $check = compile(Object, Int, Int, Str, Optional[Maybe[Int]]);
  486. my ($self,$project_id,$suite_id,$name,$parent_id) = $check->(@_);
  487. my $input = {
  488. name => $name,
  489. suite_id => $suite_id
  490. };
  491. $input->{'parent_id'} = $parent_id if $parent_id;
  492. return $self->_doRequest('index.php?/api/v2/add_section/'.$project_id,'POST',$input);
  493. }
  494. =head2 B<deleteSection (section_id)>
  495. Deletes specified section.
  496. =over 4
  497. =item INTEGER C<SECTION ID> - ID of section to delete.
  498. =back
  499. Returns BOOLEAN.
  500. $tr->deleteSection(1);
  501. =cut
  502. sub deleteSection {
  503. state $check = compile(Object, Int);
  504. my ($self,$section_id) = $check->(@_);
  505. return $self->_doRequest('index.php?/api/v2/delete_section/'.$section_id,'POST');
  506. }
  507. =head2 B<getSections (project_id,suite_id)>
  508. Gets sections for a given project and suite.
  509. =over 4
  510. =item INTEGER C<PROJECT ID> - ID of parent project.
  511. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  512. =back
  513. Returns ARRAYREF of section definition HASHREFs.
  514. $tr->getSections(1,2);
  515. =cut
  516. sub getSections {
  517. state $check = compile(Object, Int, Int);
  518. my ($self,$project_id,$suite_id) = $check->(@_);
  519. #Cache sections to reduce requests in tight loops
  520. return $self->{'sections'}->{$suite_id} if $self->{'sections'}->{$suite_id};
  521. $self->{'sections'}->{$suite_id} = $self->_doRequest("index.php?/api/v2/get_sections/$project_id&suite_id=$suite_id");
  522. return $self->{'sections'}->{$suite_id};
  523. }
  524. =head2 B<getSectionByID (section_id)>
  525. Gets desired section.
  526. =over 4
  527. =item INTEGER C<PROJECT ID> - ID of parent project.
  528. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  529. =back
  530. Returns section definition HASHREF.
  531. $tr->getSectionByID(344);
  532. =cut
  533. sub getSectionByID {
  534. state $check = compile(Object, Int);
  535. my ($self,$section_id) = $check->(@_);
  536. return $self->_doRequest("index.php?/api/v2/get_section/$section_id");
  537. }
  538. =head2 B<getSectionByName (project_id,suite_id,name)>
  539. Gets desired section.
  540. =over 4
  541. =item INTEGER C<PROJECT ID> - ID of parent project.
  542. =item INTEGER C<SUITE ID> - ID of suite to get section for.
  543. =item STRING C<NAME> - name of section to get
  544. =back
  545. Returns section definition HASHREF.
  546. $tr->getSectionByName(1,2,'nugs');
  547. =cut
  548. sub getSectionByName {
  549. state $check = compile(Object, Int, Int, Str);
  550. my ($self,$project_id,$suite_id,$section_name) = $check->(@_);
  551. my $sections = $self->getSections($project_id,$suite_id);
  552. return -500 if !$sections || (reftype($sections) || 'undef') ne 'ARRAY';
  553. foreach my $sec (@$sections) {
  554. return $sec if $sec->{'name'} eq $section_name;
  555. }
  556. return 0;
  557. }
  558. =head2 B<getChildSections ($project_id, section)>
  559. Gets desired section's child sections.
  560. =over 4
  561. =item INTEGER C<PROJECT_ID> - parent project ID of section.
  562. =item HASHREF C<SECTION> - section definition HASHREF.
  563. =back
  564. Returns ARRAYREF of section definition HASHREF. ARRAYREF is empty if there are none.
  565. Recursively searches for children, so the children of child sections will be returned as well.
  566. $tr->getChildSections($section);
  567. =cut
  568. sub getChildSections {
  569. state $check = compile(Object, Int, HashRef);
  570. my ($self, $project_id, $section) = $check->(@_);
  571. my $sections_orig = $self->getSections($project_id,$section->{suite_id});
  572. return [] if !$sections_orig || (reftype($sections_orig) || 'undef') ne 'ARRAY';
  573. my @sections = grep { $_->{'parent_id'} ? $_->{'parent_id'} == $section->{'id'} : 0 } @$sections_orig;
  574. foreach my $sec (@sections) {
  575. push(@sections, grep { $_->{'parent_id'} ? $_->{'parent_id'} == $sec->{'id'} : 0 } @$sections_orig);
  576. }
  577. return \@sections;
  578. }
  579. =head2 sectionNamesToIds(project_id,suite_id,names)
  580. Convenience method to translate a list of section names to TestRail section IDs.
  581. =over 4
  582. =item INTEGER C<PROJECT ID> - ID of parent project.
  583. =item INTEGER C<SUITE ID> - ID of parent suite.
  584. =item ARRAY C<NAMES> - Array of section names to translate to IDs.
  585. =back
  586. Returns ARRAY of section IDs.
  587. Throws an exception in the case of one (or more) of the names not corresponding to a valid section name.
  588. =cut
  589. sub sectionNamesToIds {
  590. my ($self,$project_id,$suite_id,@names) = @_;
  591. my $sections = $self->getSections($project_id,$suite_id) or confess("Could not find sections in provided project/suite.");
  592. return _X_in_my_Y($self,$sections,'id',@names);
  593. }
  594. =head1 CASE METHODS
  595. =head2 B<getCaseTypes ()>
  596. Gets possible case types.
  597. Returns ARRAYREF of case type definition HASHREFs.
  598. $tr->getCaseTypes();
  599. =cut
  600. sub getCaseTypes {
  601. state $check = compile(Object);
  602. my ($self) = $check->(@_);
  603. return clone($self->{'type_cache'}) if defined($self->{'type_cache'});
  604. my $types = $self->_doRequest("index.php?/api/v2/get_case_types");
  605. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  606. $self->{'type_cache'} = $types;
  607. return clone $types;
  608. }
  609. =head2 B<getCaseTypeByName (name)>
  610. Gets case type by name.
  611. =over 4
  612. =item STRING C<NAME> - Name of desired case type
  613. =back
  614. Returns case type definition HASHREF.
  615. Dies if named case type does not exist.
  616. $tr->getCaseTypeByName();
  617. =cut
  618. sub getCaseTypeByName {
  619. state $check = compile(Object, Str);
  620. my ($self,$name) = $check->(@_);
  621. my $types = $self->getCaseTypes();
  622. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  623. foreach my $type (@$types) {
  624. return $type if $type->{'name'} eq $name;
  625. }
  626. confess("No such case type '$name'!");
  627. }
  628. =head2 typeNamesToIds(names)
  629. Convenience method to translate a list of case type names to TestRail case type IDs.
  630. =over 4
  631. =item ARRAY C<NAMES> - Array of status names to translate to IDs.
  632. =back
  633. Returns ARRAY of type IDs in the same order as the type names passed.
  634. Throws an exception in the case of one (or more) of the names not corresponding to a valid case type.
  635. =cut
  636. sub typeNamesToIds {
  637. my ($self,@names) = @_;
  638. return _X_in_my_Y($self,$self->getCaseTypes(),'id',@names);
  639. };
  640. =head2 B<createCase(section_id,title,type_id,options,extra_options)>
  641. Creates a test case.
  642. =over 4
  643. =item INTEGER C<SECTION ID> - Parent Section ID.
  644. =item STRING C<TITLE> - Case title.
  645. =item INTEGER C<TYPE_ID> (optional) - desired test type's ID. Defaults to whatever your TR install considers the default type.
  646. =item HASHREF C<OPTIONS> (optional) - Custom fields in the case are the keys, set to the values provided. See TestRail API documentation for more info.
  647. =item HASHREF C<EXTRA OPTIONS> (optional) - contains priority_id, estimate, milestone_id and refs as possible keys. See TestRail API documentation for more info.
  648. =back
  649. Returns new case definition HASHREF, false otherwise.
  650. $custom_opts = {
  651. preconds => "Test harness installed",
  652. steps => "Do the needful",
  653. expected => "cubicle environment transforms into Dali painting"
  654. };
  655. $other_opts = {
  656. priority_id => 4,
  657. milestone_id => 666,
  658. estimate => '2m 45s',
  659. refs => ['TRACE-22','ON-166'] #ARRAYREF of bug IDs.
  660. }
  661. $case = $tr->createCase(1,'Do some stuff',3,$custom_opts,$other_opts);
  662. =cut
  663. sub createCase {
  664. state $check = compile(Object, Int, Str, Optional[Maybe[Int]], Optional[Maybe[HashRef]], Optional[Maybe[HashRef]]);
  665. my ($self,$section_id,$title,$type_id,$opts,$extras) = $check->(@_);
  666. my $stuff = {
  667. title => $title,
  668. type_id => $type_id
  669. };
  670. #Handle sort of optional but baked in options
  671. if (defined($extras) && reftype($extras) eq 'HASH') {
  672. $stuff->{'priority_id'} = $extras->{'priority_id'} if defined($extras->{'priority_id'});
  673. $stuff->{'estimate'} = $extras->{'estimate'} if defined($extras->{'estimate'});
  674. $stuff->{'milestone_id'} = $extras->{'milestone_id'} if defined($extras->{'milestone_id'});
  675. $stuff->{'refs'} = join(',',@{$extras->{'refs'}}) if defined($extras->{'refs'});
  676. }
  677. #Handle custom fields
  678. if (defined($opts) && reftype($opts) eq 'HASH') {
  679. foreach my $key (keys(%$opts)) {
  680. $stuff->{"custom_$key"} = $opts->{$key};
  681. }
  682. }
  683. return $self->_doRequest("index.php?/api/v2/add_case/$section_id",'POST',$stuff);
  684. }
  685. =head2 B<updateCase(case_id,options)>
  686. Updates a test case.
  687. =over 4
  688. =item INTEGER C<CASE ID> - Case ID.
  689. =item HASHREF C<OPTIONS> - Various things about a case to set. Everything except section_id in the output of getCaseBy* methods is a valid input here.
  690. =back
  691. Returns new case definition HASHREF, false otherwise.
  692. =cut
  693. sub updateCase {
  694. state $check = compile(Object, Int, Optional[Maybe[HashRef]]);
  695. my ($self,$case_id,$options) = $check->(@_);
  696. return $self->_doRequest("index.php?/api/v2/update_case/$case_id",'POST',$options);
  697. }
  698. =head2 B<deleteCase (case_id)>
  699. Deletes specified test case.
  700. =over 4
  701. =item INTEGER C<CASE ID> - ID of case to delete.
  702. =back
  703. Returns BOOLEAN.
  704. $tr->deleteCase(1324);
  705. =cut
  706. sub deleteCase {
  707. state $check = compile(Object, Int);
  708. my ($self,$case_id) = $check->(@_);
  709. return $self->_doRequest("index.php?/api/v2/delete_case/$case_id",'POST');
  710. }
  711. =head2 B<getCases (project_id,suite_id,filters)>
  712. Gets cases for provided section.
  713. =over 4
  714. =item INTEGER C<PROJECT ID> - ID of parent project.
  715. =item INTEGER C<SUITE ID> - ID of parent suite.
  716. =item HASHREF C<FILTERS> (optional) - HASHREF describing parameters to filter cases by.
  717. =back
  718. See:
  719. L<http://docs.gurock.com/testrail-api2/reference-cases#get_cases>
  720. for details as to the allowable filter keys.
  721. If the section ID is omitted, all cases for the suite will be returned.
  722. Returns ARRAYREF of test case definition HASHREFs.
  723. $tr->getCases(1,2, {'section_id' => 3} );
  724. =cut
  725. sub getCases {
  726. state $check = compile(Object, Int, Int, Optional[Maybe[HashRef]]);
  727. my ($self,$project_id,$suite_id,$filters) = $check->(@_);
  728. my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
  729. my @valid_keys = qw{section_id created_after created_before created_by milestone_id priority_id type_id updated_after updated_before updated_by};
  730. # Add in filters
  731. foreach my $filter (keys(%$filters)) {
  732. confess("Invalid filter key '$filter' passed") unless grep {$_ eq $filter} @valid_keys;
  733. if (ref $filters->{$filter} eq 'ARRAY') {
  734. confess "$filter cannot be an ARRAYREF" if grep {$_ eq $filter} qw{created_after created_before updated_after updated_before};
  735. $url .= "&$filter=".join(',',@{$filters->{$filter}});
  736. } else {
  737. $url .= "&$filter=".$filters->{$filter} if defined($filters->{$filter});
  738. }
  739. }
  740. return $self->_doRequest($url);
  741. }
  742. =head2 B<getCaseByName (project_id,suite_id,name,filters)>
  743. Gets case by name.
  744. =over 4
  745. =item INTEGER C<PROJECT ID> - ID of parent project.
  746. =item INTEGER C<SUITE ID> - ID of parent suite.
  747. =item STRING C<NAME> - Name of desired test case.
  748. =item HASHREF C<FILTERS> - Filter dictionary acceptable to getCases.
  749. =back
  750. Returns test case definition HASHREF.
  751. $tr->getCaseByName(1,2,'nugs', {'section_id' => 3});
  752. =cut
  753. sub getCaseByName {
  754. state $check = compile(Object, Int, Int, Str, Optional[Maybe[HashRef]]);
  755. my ($self,$project_id,$suite_id,$name,$filters) = $check->(@_);
  756. my $cases = $self->getCases($project_id,$suite_id,$filters);
  757. return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
  758. foreach my $case (@$cases) {
  759. return $case if $case->{'title'} eq $name;
  760. }
  761. return 0;
  762. }
  763. =head2 B<getCaseByID (case_id)>
  764. Gets case by ID.
  765. =over 4
  766. =item INTEGER C<CASE ID> - ID of case.
  767. =back
  768. Returns test case definition HASHREF.
  769. $tr->getCaseByID(1345);
  770. =cut
  771. sub getCaseByID {
  772. state $check = compile(Object, Int);
  773. my ($self,$case_id) = $check->(@_);
  774. return $self->_doRequest("index.php?/api/v2/get_case/$case_id");
  775. }
  776. =head2 getCaseFields
  777. Returns ARRAYREF of available test case custom fields.
  778. $tr->getCaseFields();
  779. Output is cached in the case_fields parameter. Cache is invalidated when addCaseField is called.
  780. =cut
  781. sub getCaseFields {
  782. state $check = compile(Object);
  783. my ($self) = $check->(@_);
  784. return $self->{case_fields} if $self->{case_fields};
  785. $self->{case_fields} = $self->_doRequest("index.php?/api/v2/get_case_fields");
  786. return $self->{case_fields};
  787. }
  788. =head2 addCaseField(%options)
  789. Returns HASHREF describing the case field you just added.
  790. $tr->addCaseField(%options)
  791. =cut
  792. sub addCaseField {
  793. state $check = compile(Object,slurpy HashRef);
  794. my ($self,$options) = $check->(@_);
  795. $self->{case_fields} = undef;
  796. return $self->_doRequest("index.php?/api/v2/add_case_field", 'POST', $options);
  797. }
  798. =head1 PRIORITY METHODS
  799. =head2 B<getPriorities ()>
  800. Gets possible priorities.
  801. Returns ARRAYREF of priority definition HASHREFs.
  802. $tr->getPriorities();
  803. =cut
  804. sub getPriorities {
  805. state $check = compile(Object);
  806. my ($self) = $check->(@_);
  807. return clone($self->{'priority_cache'}) if defined($self->{'priority_cache'});
  808. my $priorities = $self->_doRequest("index.php?/api/v2/get_priorities");
  809. return -500 if !$priorities || (reftype($priorities) || 'undef') ne 'ARRAY';
  810. $self->{'priority_cache'} = $priorities;
  811. return clone $priorities;
  812. }
  813. =head2 B<getPriorityByName (name)>
  814. Gets priority by name.
  815. =over 4
  816. =item STRING C<NAME> - Name of desired priority
  817. =back
  818. Returns priority definition HASHREF.
  819. Dies if named priority does not exist.
  820. $tr->getPriorityByName();
  821. =cut
  822. sub getPriorityByName {
  823. state $check = compile(Object, Str);
  824. my ($self,$name) = $check->(@_);
  825. my $priorities = $self->getPriorities();
  826. return -500 if !$priorities || (reftype($priorities) || 'undef') ne 'ARRAY';
  827. foreach my $priority (@$priorities) {
  828. return $priority if $priority->{'name'} eq $name;
  829. }
  830. confess("No such priority '$name'!");
  831. }
  832. =head2 priorityNamesToIds(names)
  833. Convenience method to translate a list of priority names to TestRail priority IDs.
  834. =over 4
  835. =item ARRAY C<NAMES> - Array of priority names to translate to IDs.
  836. =back
  837. Returns ARRAY of priority IDs in the same order as the priority names passed.
  838. Throws an exception in the case of one (or more) of the names not corresponding to a valid priority.
  839. =cut
  840. sub priorityNamesToIds {
  841. my ($self,@names) = @_;
  842. return _X_in_my_Y($self,$self->getPriorities(),'id',@names);
  843. };
  844. =head1 RUN METHODS
  845. =head2 B<createRun (project_id,suite_id,name,description,milestone_id,assigned_to_id,case_ids)>
  846. Create a run.
  847. =over 4
  848. =item INTEGER C<PROJECT ID> - ID of parent project.
  849. =item INTEGER C<SUITE ID> - ID of suite to base run on
  850. =item STRING C<NAME> - Name of run
  851. =item STRING C<DESCRIPTION> (optional) - Description of run
  852. =item INTEGER C<MILESTONE ID> (optional) - ID of milestone
  853. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  854. =item ARRAYREF C<CASE IDS> (optional) - Array of case IDs in case you don't want to use the whole testsuite when making the build.
  855. =back
  856. Returns run definition HASHREF.
  857. $tr->createRun(1,1345,'RUN AWAY','SO FAR AWAY',22,3,[3,4,5,6]);
  858. =cut
  859. #If you pass an array of case ids, it implies include_all is false
  860. sub createRun {
  861. state $check = compile(Object, Int, Int, Str, Optional[Maybe[Str]], Optional[Maybe[Int]], Optional[Maybe[Int]], Optional[Maybe[ArrayRef[Int]]]);
  862. my ($self,$project_id,$suite_id,$name,$desc,$milestone_id,$assignedto_id,$case_ids) = $check->(@_);
  863. my $stuff = {
  864. suite_id => $suite_id,
  865. name => $name,
  866. description => $desc,
  867. milestone_id => $milestone_id,
  868. assignedto_id => $assignedto_id,
  869. include_all => defined($case_ids) ? 0 : 1,
  870. case_ids => $case_ids
  871. };
  872. return $self->_doRequest("index.php?/api/v2/add_run/$project_id",'POST',$stuff);
  873. }
  874. =head2 B<deleteRun (run_id)>
  875. Deletes specified run.
  876. =over 4
  877. =item INTEGER C<RUN ID> - ID of run to delete.
  878. =back
  879. Returns BOOLEAN.
  880. $tr->deleteRun(1324);
  881. =cut
  882. sub deleteRun {
  883. state $check = compile(Object, Int);
  884. my ($self,$run_id) = $check->(@_);
  885. return $self->_doRequest("index.php?/api/v2/delete_run/$run_id",'POST');
  886. }
  887. =head2 B<getRuns (project_id)>
  888. Get all runs for specified project.
  889. To do this, it must make (no. of runs/250) HTTP requests.
  890. This is due to the maximum result set limit enforced by testrail.
  891. =over 4
  892. =item INTEGER C<PROJECT_ID> - ID of parent project
  893. =item HASHREF C<FILTERS> - (optional) dictionary of filters, with keys corresponding to the documented filters for get_runs (other than limit/offset).
  894. =back
  895. Returns ARRAYREF of run definition HASHREFs.
  896. $allRuns = $tr->getRuns(6969);
  897. Possible filters:
  898. =over 4
  899. =item created_after (UNIX timestamp)
  900. =item created_before (UNIX timestamp)
  901. =item created_by (csv of ints) IDs of users plans were created by
  902. =item is_completed (bool)
  903. =item milestone_id (csv of ints) IDs of milestone assigned to plans
  904. =item refs_filter (string) A single Reference ID (e.g. TR-a, 4291, etc.)
  905. =item suite_id (csv of ints) A comma-separated list of test suite IDs to filter by.
  906. =back
  907. =cut
  908. sub getRuns {
  909. state $check = compile(Object, Int, Optional[Maybe[HashRef]]);
  910. my ($self,$project_id,$filters) = $check->(@_);
  911. my $initial_runs = $self->getRunsPaginated($project_id,$self->{'global_limit'},0,$filters);
  912. return $initial_runs unless (reftype($initial_runs) || 'undef') eq 'ARRAY';
  913. my $runs = [];
  914. push(@$runs,@$initial_runs);
  915. my $offset = 1;
  916. while (scalar(@$initial_runs) == $self->{'global_limit'}) {
  917. $initial_runs = $self->getRunsPaginated($project_id,$self->{'global_limit'},($self->{'global_limit'} * $offset),$filters);
  918. push(@$runs,@$initial_runs);
  919. $offset++;
  920. }
  921. return $runs;
  922. }
  923. =head2 B<getRunsPaginated (project_id,limit,offset)>
  924. Get some runs for specified project.
  925. =over 4
  926. =item INTEGER C<PROJECT_ID> - ID of parent project
  927. =item INTEGER C<LIMIT> - Number of runs to return.
  928. =item INTEGER C<OFFSET> - Page of runs to return.
  929. =item HASHREF C<FILTERS> - (optional) other filters to apply to the requests. See getRuns for more information.
  930. =back
  931. Returns ARRAYREF of run definition HASHREFs.
  932. $someRuns = $tr->getRunsPaginated(6969,22,4);
  933. =cut
  934. sub getRunsPaginated {
  935. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]], Optional[Maybe[HashRef]]);
  936. my ($self,$project_id,$limit,$offset,$filters) = $check->(@_);
  937. $filters //= {};
  938. confess("Limit greater than ".$self->{'global_limit'}) if $limit > $self->{'global_limit'};
  939. my $apiurl = "index.php?/api/v2/get_runs/$project_id";
  940. $apiurl .= "&offset=$offset" if defined($offset);
  941. $apiurl .= "&limit=$limit" if $limit; #You have problems if you want 0 results
  942. foreach my $key (keys(%$filters)) {
  943. $apiurl .= "&$key=$filters->{$key}";
  944. }
  945. return $self->_doRequest($apiurl);
  946. }
  947. =head2 B<getRunByName (project_id,name)>
  948. Gets run by name.
  949. =over 4
  950. =item INTEGER C<PROJECT ID> - ID of parent project.
  951. =item STRING <NAME> - Name of desired run.
  952. =back
  953. Returns run definition HASHREF.
  954. $tr->getRunByName(1,'R2');
  955. =cut
  956. sub getRunByName {
  957. state $check = compile(Object, Int, Str);
  958. my ($self,$project_id,$name) = $check->(@_);
  959. my $runs = $self->getRuns($project_id);
  960. return -500 if !$runs || (reftype($runs) || 'undef') ne 'ARRAY';
  961. foreach my $run (@$runs) {
  962. return $run if $run->{'name'} eq $name;
  963. }
  964. return 0;
  965. }
  966. =head2 B<getRunByID (run_id)>
  967. Gets run by ID.
  968. =over 4
  969. =item INTEGER C<RUN ID> - ID of desired run.
  970. =back
  971. Returns run definition HASHREF.
  972. $tr->getRunByID(7779311);
  973. =cut
  974. sub getRunByID {
  975. state $check = compile(Object, Int);
  976. my ($self,$run_id) = $check->(@_);
  977. return $self->_doRequest("index.php?/api/v2/get_run/$run_id");
  978. }
  979. =head2 B<closeRun (run_id)>
  980. Close the specified run.
  981. =over 4
  982. =item INTEGER C<RUN ID> - ID of desired run.
  983. =back
  984. Returns run definition HASHREF on success, false on failure.
  985. $tr->closeRun(90210);
  986. =cut
  987. sub closeRun {
  988. state $check = compile(Object, Int);
  989. my ($self,$run_id) = $check->(@_);
  990. return $self->_doRequest("index.php?/api/v2/close_run/$run_id",'POST');
  991. }
  992. =head2 B<getRunSummary(runs)>
  993. Returns array of hashrefs describing the # of tests in the run(s) with the available statuses.
  994. Translates custom_statuses into their system names for you.
  995. =over 4
  996. =item ARRAY C<RUNS> - runs obtained from getRun* or getChildRun* methods.
  997. =back
  998. Returns ARRAY of run HASHREFs with the added key 'run_status' holding a hashref where status_name => count.
  999. $tr->getRunSummary($run,$run2);
  1000. =cut
  1001. sub getRunSummary {
  1002. state $check = compile(Object, slurpy ArrayRef[HashRef]);
  1003. my ($self,$runs) = $check->(@_);
  1004. confess("At least one run must be passed!") unless scalar(@$runs);
  1005. #Translate custom statuses
  1006. my $statuses = $self->getPossibleTestStatuses();
  1007. my %shash;
  1008. #XXX so, they do these tricks with the status names, see...so map the counts to their relevant status ids.
  1009. @shash{map { ( $_->{'id'} < 6 ) ? $_->{'name'}."_count" : "custom_status".($_->{'id'} - 5)."_count" } @$statuses } = map { $_->{'id'} } @$statuses;
  1010. my @sname;
  1011. #Create listing of keys/values
  1012. @$runs = map {
  1013. my $run = $_;
  1014. @{$run->{statuses}}{grep {$_ =~ m/_count$/} keys(%$run)} = grep {$_ =~ m/_count$/} keys(%$run);
  1015. foreach my $status (keys(%{$run->{'statuses'}})) {
  1016. next if !exists($shash{$status});
  1017. @sname = grep {exists($shash{$status}) && $_->{'id'} == $shash{$status}} @$statuses;
  1018. $run->{'statuses_clean'}->{$sname[0]->{'label'}} = $run->{$status};
  1019. }
  1020. $run;
  1021. } @$runs;
  1022. return map { {'id' => $_->{'id'}, 'name' => $_->{'name'}, 'run_status' => $_->{'statuses_clean'}, 'config_ids' => $_->{'config_ids'} } } @$runs;
  1023. }
  1024. =head2 B<getRunResults(run_id)>
  1025. Returns array of hashrefs describing the results of the run.
  1026. Warning: This only returns the most recent results of a run.
  1027. If you want to know about the tortured journey a test may have taken to get to it's final status,
  1028. you will need to use getTestResults.
  1029. =over 4
  1030. =item INTEGER C<RUN_ID> - Relevant Run's ID.
  1031. =back
  1032. =cut
  1033. sub getRunResults {
  1034. state $check = compile(Object, Int);
  1035. my ($self,$run_id) = $check->(@_);
  1036. my $initial_results = $self->getRunResultsPaginated($run_id,$self->{'global_limit'},undef);
  1037. return $initial_results unless (reftype($initial_results) || 'undef') eq 'ARRAY';
  1038. my $results = [];
  1039. push(@$results,@$initial_results);
  1040. my $offset = 1;
  1041. while (scalar(@$initial_results) == $self->{'global_limit'}) {
  1042. $initial_results = $self->getRunResultsPaginated($run_id,$self->{'global_limit'},($self->{'global_limit'} * $offset));
  1043. push(@$results,@$initial_results);
  1044. $offset++;
  1045. }
  1046. return $results;
  1047. }
  1048. =head2 B<getRunResultsPaginated(run_id,limit,offset)>
  1049. =cut
  1050. sub getRunResultsPaginated {
  1051. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]]);
  1052. my ($self,$run_id,$limit,$offset) = $check->(@_);
  1053. confess("Limit greater than ".$self->{'global_limit'}) if $limit > $self->{'global_limit'};
  1054. my $apiurl = "index.php?/api/v2/get_results_for_run/$run_id";
  1055. $apiurl .= "&offset=$offset" if defined($offset);
  1056. $apiurl .= "&limit=$limit" if $limit; #You have problems if you want 0 results
  1057. return $self->_doRequest($apiurl);
  1058. }
  1059. =head1 RUN AS CHILD OF PLAN METHODS
  1060. =head2 B<getChildRuns(plan)>
  1061. Extract the child runs from a plan. Convenient, as the structure of this hash is deep, and correct error handling can be tedious.
  1062. =over 4
  1063. =item HASHREF C<PLAN> - Test Plan definition HASHREF returned by any of the PLAN methods below.
  1064. =back
  1065. Returns ARRAYREF of run definition HASHREFs. Returns 0 upon failure to extract the data.
  1066. =cut
  1067. sub getChildRuns {
  1068. state $check = compile(Object, HashRef);
  1069. my ($self,$plan) = $check->(@_);
  1070. return 0 unless defined($plan->{'entries'}) && (reftype($plan->{'entries'}) || 'undef') eq 'ARRAY';
  1071. my $entries = $plan->{'entries'};
  1072. my $plans = [];
  1073. foreach my $entry (@$entries) {
  1074. push(@$plans,@{$entry->{'runs'}}) if defined($entry->{'runs'}) && ((reftype($entry->{'runs'}) || 'undef') eq 'ARRAY')
  1075. }
  1076. return $plans;
  1077. }
  1078. =head2 B<getChildRunByName(plan,name,configurations,testsuite_id)>
  1079. =over 4
  1080. =item HASHREF C<PLAN> - Test Plan definition HASHREF returned by any of the PLAN methods below.
  1081. =item STRING C<NAME> - Name of run to search for within plan.
  1082. =item ARRAYREF C<CONFIGURATIONS> (optional) - Names of configurations to filter runs by.
  1083. =item INTEGER C<TESTSUITE_ID> (optional) - Filter by the provided Testsuite ID. Helpful for when child runs have duplicate names, but are from differing testsuites.
  1084. =back
  1085. Returns run definition HASHREF, or false if no such run is found.
  1086. Convenience method using getChildRuns.
  1087. Will throw a fatal error if one or more of the configurations passed does not exist in the project.
  1088. =cut
  1089. sub getChildRunByName {
  1090. state $check = compile(Object, HashRef, Str, Optional[Maybe[ArrayRef[Str]]], Optional[Maybe[Int]]);
  1091. my ($self,$plan,$name,$configurations,$testsuite_id) = $check->(@_);
  1092. my $runs = $self->getChildRuns($plan);
  1093. @$runs = grep {$_->{suite_id} == $testsuite_id} @$runs if $testsuite_id;
  1094. return 0 if !$runs;
  1095. my @pconfigs = ();
  1096. #Figure out desired config IDs
  1097. if (defined $configurations) {
  1098. my $avail_configs = $self->getConfigurations($plan->{'project_id'});
  1099. my ($cname);
  1100. @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @$configurations } @$avail_configs; #Get a list of IDs from the names passed
  1101. }
  1102. confess("One or more configurations passed does not exist in your project!") if defined($configurations) && (scalar(@pconfigs) != scalar(@$configurations));
  1103. my $found;
  1104. foreach my $run (@$runs) {
  1105. next if $run->{name} ne $name;
  1106. next if scalar(@pconfigs) != scalar(@{$run->{'config_ids'}});
  1107. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  1108. $found = 0;
  1109. foreach my $cid (@{$run->{'config_ids'}}) {
  1110. $found++ if grep {$_ == $cid} @pconfigs;
  1111. }
  1112. return $run if $found == scalar(@{$run->{'config_ids'}});
  1113. }
  1114. return 0;
  1115. }
  1116. =head1 PLAN METHODS
  1117. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  1118. Create a test plan.
  1119. =over 4
  1120. =item INTEGER C<PROJECT ID> - ID of parent project.
  1121. =item STRING C<NAME> - Name of plan
  1122. =item STRING C<DESCRIPTION> (optional) - Description of plan
  1123. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  1124. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  1125. =back
  1126. Returns test plan definition HASHREF, or false on failure.
  1127. $entries = [{
  1128. suite_id => 345,
  1129. include_all => 1,
  1130. assignedto_id => 1
  1131. }];
  1132. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  1133. =cut
  1134. sub createPlan {
  1135. state $check = compile(Object, Int, Str, Optional[Maybe[Str]], Optional[Maybe[Int]], Optional[Maybe[ArrayRef[HashRef]]]);
  1136. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = $check->(@_);
  1137. my $stuff = {
  1138. name => $name,
  1139. description => $desc,
  1140. milestone_id => $milestone_id,
  1141. entries => $entries
  1142. };
  1143. return $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  1144. }
  1145. =head2 B<deletePlan (plan_id)>
  1146. Deletes specified plan.
  1147. =over 4
  1148. =item INTEGER C<PLAN ID> - ID of plan to delete.
  1149. =back
  1150. Returns BOOLEAN.
  1151. $tr->deletePlan(8675309);
  1152. =cut
  1153. sub deletePlan {
  1154. state $check = compile(Object, Int);
  1155. my ($self,$plan_id) = $check->(@_);
  1156. return $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  1157. }
  1158. =head2 B<getPlans (project_id,filters)>
  1159. Gets all test plans in specified project.
  1160. Like getRuns, must make multiple HTTP requests when the number of results exceeds 250.
  1161. =over 4
  1162. =item INTEGER C<PROJECT ID> - ID of parent project.
  1163. =item HASHREF C<FILTERS> - (optional) dictionary of filters, with keys corresponding to the documented filters for get_plans (other than limit/offset).
  1164. =back
  1165. Returns ARRAYREF of all plan definition HASHREFs in a project.
  1166. $tr->getPlans(8);
  1167. Does not contain any information about child test runs.
  1168. Use getPlanByID or getPlanByName if you want that, in particular if you are interested in using getChildRunByName.
  1169. Possible filters:
  1170. =over 4
  1171. =item created_after (UNIX timestamp)
  1172. =item created_before (UNIX timestamp)
  1173. =item created_by (csv of ints) IDs of users plans were created by
  1174. =item is_completed (bool)
  1175. =item milestone_id (csv of ints) IDs of milestone assigned to plans
  1176. =back
  1177. =cut
  1178. sub getPlans {
  1179. state $check = compile(Object, Int, Optional[Maybe[HashRef]]);
  1180. my ($self,$project_id,$filters) = $check->(@_);
  1181. my $initial_plans = $self->getPlansPaginated($project_id,$self->{'global_limit'},0,$filters);
  1182. return $initial_plans unless (reftype($initial_plans) || 'undef') eq 'ARRAY';
  1183. my $plans = [];
  1184. push(@$plans,@$initial_plans);
  1185. my $offset = 1;
  1186. while (scalar(@$initial_plans) == $self->{'global_limit'}) {
  1187. $initial_plans = $self->getPlansPaginated($project_id,$self->{'global_limit'},($self->{'global_limit'} * $offset),$filters);
  1188. push(@$plans,@$initial_plans);
  1189. $offset++;
  1190. }
  1191. return $plans;
  1192. }
  1193. =head2 B<getPlansPaginated (project_id,limit,offset,filters)>
  1194. Get some plans for specified project.
  1195. =over 4
  1196. =item INTEGER C<PROJECT_ID> - ID of parent project
  1197. =item INTEGER C<LIMIT> - Number of plans to return.
  1198. =item INTEGER C<OFFSET> - Page of plans to return.
  1199. =item HASHREF C<FILTERS> - (optional) other filters to apply to the requests. See getPlans for more information.
  1200. =back
  1201. Returns ARRAYREF of plan definition HASHREFs.
  1202. $someRuns = $tr->getPlansPaginated(6969,222,44);
  1203. =cut
  1204. sub getPlansPaginated {
  1205. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]], Optional[Maybe[HashRef]]);
  1206. my ($self,$project_id,$limit,$offset,$filters) = $check->(@_);
  1207. $filters //= {};
  1208. confess("Limit greater than ".$self->{'global_limit'}) if $limit > $self->{'global_limit'};
  1209. my $apiurl = "index.php?/api/v2/get_plans/$project_id";
  1210. $apiurl .= "&offset=$offset" if defined($offset);
  1211. $apiurl .= "&limit=$limit" if $limit; #You have problems if you want 0 results
  1212. foreach my $key (keys(%$filters)) {
  1213. $apiurl .= "&$key=$filters->{$key}";
  1214. }
  1215. return $self->_doRequest($apiurl);
  1216. }
  1217. =head2 B<getPlanByName (project_id,name)>
  1218. Gets specified plan by name.
  1219. =over 4
  1220. =item INTEGER C<PROJECT ID> - ID of parent project.
  1221. =item STRING C<NAME> - Name of test plan.
  1222. =back
  1223. Returns plan definition HASHREF.
  1224. $tr->getPlanByName(8,'GosPlan');
  1225. =cut
  1226. sub getPlanByName {
  1227. state $check = compile(Object, Int, Str);
  1228. my ($self,$project_id,$name) = $check->(@_);
  1229. my $plans = $self->getPlans($project_id);
  1230. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  1231. foreach my $plan (@$plans) {
  1232. if ($plan->{'name'} eq $name) {
  1233. return $self->getPlanByID($plan->{'id'});
  1234. }
  1235. }
  1236. return 0;
  1237. }
  1238. =head2 B<getPlanByID (plan_id)>
  1239. Gets specified plan by ID.
  1240. =over 4
  1241. =item INTEGER C<PLAN ID> - ID of plan.
  1242. =back
  1243. Returns plan definition HASHREF.
  1244. $tr->getPlanByID(2);
  1245. =cut
  1246. sub getPlanByID {
  1247. state $check = compile(Object, Int);
  1248. my ($self,$plan_id) = $check->(@_);
  1249. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  1250. }
  1251. =head2 B<getPlanSummary(plan_ID)>
  1252. Returns hashref describing the various pass, fail, etc. percentages for tests in the plan.
  1253. The 'totals' key has total cases in each status ('status' => count)
  1254. The 'percentages' key has the same, but as a percentage of the total.
  1255. =over 4
  1256. =item SCALAR C<plan_ID> - ID of your test plan.
  1257. =back
  1258. $tr->getPlanSummary($plan_id);
  1259. =cut
  1260. sub getPlanSummary {
  1261. state $check = compile(Object, Int);
  1262. my ($self,$plan_id) = $check->(@_);
  1263. my $runs = $self->getPlanByID( $plan_id );
  1264. $runs = $self->getChildRuns( $runs );
  1265. @$runs = $self->getRunSummary(@{$runs});
  1266. my $total_sum = 0;
  1267. my $ret = { plan => $plan_id };
  1268. #Compile totals
  1269. foreach my $summary ( @$runs ) {
  1270. my @elems = keys( %{ $summary->{'run_status'} } );
  1271. foreach my $key (@elems) {
  1272. $ret->{'totals'}->{$key} = 0 if !defined $ret->{'totals'}->{$key};
  1273. $ret->{'totals'}->{$key} += $summary->{'run_status'}->{$key};
  1274. $total_sum += $summary->{'run_status'}->{$key};
  1275. }
  1276. }
  1277. #Compile percentages
  1278. foreach my $key (keys(%{$ret->{'totals'}})) {
  1279. next if grep {$_ eq $key} qw{plan configs percentages};
  1280. $ret->{"percentages"}->{$key} = sprintf( "%.2f%%", ( $ret->{'totals'}->{$key} / $total_sum ) * 100 );
  1281. }
  1282. return $ret;
  1283. }
  1284. =head2 B<createRunInPlan (plan_id,suite_id,name,assigned_to_id,config_ids,case_ids)>
  1285. Create a run in a plan.
  1286. =over 4
  1287. =item INTEGER C<PLAN ID> - ID of parent project.
  1288. =item INTEGER C<SUITE ID> - ID of suite to base run on
  1289. =item STRING C<NAME> - Name of run
  1290. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  1291. =item ARRAYREF C<CONFIG IDS> (optional) - Array of Configuration IDs (see getConfigurations) to apply to the created run
  1292. =item ARRAYREF C<CASE IDS> (optional) - Array of case IDs in case you don't want to use the whole testsuite when making the build.
  1293. =back
  1294. Returns run definition HASHREF.
  1295. $tr->createRun(1,1345,'PlannedRun',3,[1,4,77],[3,4,5,6]);
  1296. =cut
  1297. #If you pass an array of case ids, it implies include_all is false
  1298. sub createRunInPlan {
  1299. state $check = compile(Object, Int, Int, Str, Optional[Maybe[Int]], Optional[Maybe[ArrayRef[Int]]], Optional[Maybe[ArrayRef[Int]]]);
  1300. my ($self,$plan_id,$suite_id,$name,$assignedto_id,$config_ids,$case_ids) = $check->(@_);
  1301. my $runs = [
  1302. {
  1303. config_ids => $config_ids,
  1304. include_all => defined($case_ids) ? 0 : 1,
  1305. case_ids => $case_ids
  1306. }
  1307. ];
  1308. my $stuff = {
  1309. suite_id => $suite_id,
  1310. name => $name,
  1311. assignedto_id => $assignedto_id,
  1312. include_all => defined($case_ids) ? 0 : 1,
  1313. case_ids => $case_ids,
  1314. config_ids => $config_ids,
  1315. runs => $runs
  1316. };
  1317. return $self->_doRequest("index.php?/api/v2/add_plan_entry/$plan_id",'POST',$stuff);
  1318. }
  1319. =head2 B<closePlan (plan_id)>
  1320. Close the specified plan.
  1321. =over 4
  1322. =item INTEGER C<PLAN ID> - ID of desired plan.
  1323. =back
  1324. Returns plan definition HASHREF on success, false on failure.
  1325. $tr->closePlan(75020);
  1326. =cut
  1327. sub closePlan {
  1328. state $check = compile(Object, Int);
  1329. my ($self,$plan_id) = $check->(@_);
  1330. return $self->_doRequest("index.php?/api/v2/close_plan/$plan_id",'POST');
  1331. }
  1332. =head1 MILESTONE METHODS
  1333. =head2 B<createMilestone (project_id,name,description,due_on)>
  1334. Create a milestone.
  1335. =over 4
  1336. =item INTEGER C<PROJECT ID> - ID of parent project.
  1337. =item STRING C<NAME> - Name of milestone
  1338. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  1339. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  1340. =back
  1341. Returns milestone definition HASHREF, or false on failure.
  1342. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  1343. =cut
  1344. sub createMilestone {
  1345. state $check = compile(Object, Int, Str, Optional[Maybe[Str]], Optional[Maybe[Int]]);
  1346. my ($self,$project_id,$name,$desc,$due_on) = $check->(@_);
  1347. my $stuff = {
  1348. name => $name,
  1349. description => $desc,
  1350. due_on => $due_on # unix timestamp
  1351. };
  1352. return $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  1353. }
  1354. =head2 B<deleteMilestone (milestone_id)>
  1355. Deletes specified milestone.
  1356. =over 4
  1357. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  1358. =back
  1359. Returns BOOLEAN.
  1360. $tr->deleteMilestone(86);
  1361. =cut
  1362. sub deleteMilestone {
  1363. state $check = compile(Object, Int);
  1364. my ($self,$milestone_id) = $check->(@_);
  1365. return $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  1366. }
  1367. =head2 B<getMilestones (project_id)>
  1368. Get milestones for some project.
  1369. =over 4
  1370. =item INTEGER C<PROJECT ID> - ID of parent project.
  1371. =back
  1372. Returns ARRAYREF of milestone definition HASHREFs.
  1373. $tr->getMilestones(8);
  1374. =cut
  1375. sub getMilestones {
  1376. state $check = compile(Object, Int);
  1377. my ($self,$project_id) = $check->(@_);
  1378. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  1379. }
  1380. =head2 B<getMilestoneByName (project_id,name)>
  1381. Gets specified milestone by name.
  1382. =over 4
  1383. =item INTEGER C<PROJECT ID> - ID of parent project.
  1384. =item STRING C<NAME> - Name of milestone.
  1385. =back
  1386. Returns milestone definition HASHREF.
  1387. $tr->getMilestoneByName(8,'whee');
  1388. =cut
  1389. sub getMilestoneByName {
  1390. state $check = compile(Object, Int, Str);
  1391. my ($self,$project_id,$name) = $check->(@_);
  1392. my $milestones = $self->getMilestones($project_id);
  1393. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  1394. foreach my $milestone (@$milestones) {
  1395. return $milestone if $milestone->{'name'} eq $name;
  1396. }
  1397. return 0;
  1398. }
  1399. =head2 B<getMilestoneByID (milestone_id)>
  1400. Gets specified milestone by ID.
  1401. =over 4
  1402. =item INTEGER C<MILESTONE ID> - ID of milestone.
  1403. =back
  1404. Returns milestone definition HASHREF.
  1405. $tr->getMilestoneByID(2);
  1406. =cut
  1407. sub getMilestoneByID {
  1408. state $check = compile(Object, Int);
  1409. my ($self,$milestone_id) = $check->(@_);
  1410. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  1411. }
  1412. =head1 TEST METHODS
  1413. =head2 B<getTests (run_id,status_ids,assignedto_ids)>
  1414. Get tests for some run. Optionally filter by provided status_ids and assigned_to ids.
  1415. =over 4
  1416. =item INTEGER C<RUN ID> - ID of parent run.
  1417. =item ARRAYREF C<STATUS IDS> (optional) - IDs of relevant test statuses to filter by. Get with getPossibleTestStatuses.
  1418. =item ARRAYREF C<ASSIGNEDTO IDS> (optional) - IDs of users assigned to test to filter by. Get with getUsers.
  1419. =back
  1420. Returns ARRAYREF of test definition HASHREFs.
  1421. $tr->getTests(8,[1,2,3],[2]);
  1422. =cut
  1423. sub getTests {
  1424. state $check = compile(Object, Int, Optional[Maybe[ArrayRef[Int]]], Optional[Maybe[ArrayRef[Int]]]);
  1425. my ($self,$run_id,$status_ids,$assignedto_ids) = $check->(@_);
  1426. my $query_string = '';
  1427. $query_string = '&status_id='.join(',',@$status_ids) if defined($status_ids) && scalar(@$status_ids);
  1428. my $results = $self->_doRequest("index.php?/api/v2/get_tests/$run_id$query_string");
  1429. @$results = grep {my $aid = $_->{'assignedto_id'}; grep {defined($aid) && $aid == $_} @$assignedto_ids} @$results if defined($assignedto_ids) && scalar(@$assignedto_ids);
  1430. #Cache stuff for getTestByName
  1431. $self->{tests_cache} //= {};
  1432. $self->{tests_cache}->{$run_id} = $results;
  1433. return clone($results);
  1434. }
  1435. =head2 B<getTestByName (run_id,name)>
  1436. Gets specified test by name.
  1437. This is done by getting the list of all tests in the run and then picking out the relevant test.
  1438. As such, for efficiency the list of tests is cached.
  1439. The cache may be refreshed, or restricted by running getTests (with optional restrictions, such as assignedto_ids, etc).
  1440. =over 4
  1441. =item INTEGER C<RUN ID> - ID of parent run.
  1442. =item STRING C<NAME> - Name of milestone.
  1443. =back
  1444. Returns test definition HASHREF.
  1445. $tr->getTestByName(36,'wheeTest');
  1446. =cut
  1447. sub getTestByName {
  1448. state $check = compile(Object, Int, Str);
  1449. my ($self,$run_id,$name) = $check->(@_);
  1450. $self->{tests_cache} //= {};
  1451. my $tests = $self->{tests_cache}->{$run_id};
  1452. $tests = $self->getTests($run_id) if !$tests;
  1453. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  1454. foreach my $test (@$tests) {
  1455. return $test if $test->{'title'} eq $name;
  1456. }
  1457. return 0;
  1458. }
  1459. =head2 B<getTestByID (test_id)>
  1460. Gets specified test by ID.
  1461. =over 4
  1462. =item INTEGER C<TEST ID> - ID of test.
  1463. =back
  1464. Returns test definition HASHREF.
  1465. $tr->getTestByID(222222);
  1466. =cut
  1467. sub getTestByID {
  1468. state $check = compile(Object, Int);
  1469. my ($self,$test_id) = $check->(@_);
  1470. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  1471. }
  1472. =head2 B<getTestResultFields()>
  1473. Gets custom fields that can be set for tests.
  1474. Returns ARRAYREF of result definition HASHREFs.
  1475. =cut
  1476. sub getTestResultFields {
  1477. state $check = compile(Object);
  1478. my ($self) = $check->(@_);
  1479. return $self->{'tr_fields'} if defined($self->{'tr_fields'}); #cache
  1480. $self->{'tr_fields'} = $self->_doRequest('index.php?/api/v2/get_result_fields');
  1481. return $self->{'tr_fields'};
  1482. }
  1483. =head2 B<getTestResultFieldByName(SYSTEM_NAME,PROJECT_ID)>
  1484. Gets a test result field by it's system name. Optionally filter by project ID.
  1485. =over 4
  1486. =item B<SYSTEM NAME> - STRING: system name of a result field.
  1487. =item B<PROJECT ID> - INTEGER (optional): Filter by whether or not the field is enabled for said project
  1488. =back
  1489. Returns a value less than 0 if unsuccessful.
  1490. =cut
  1491. sub getTestResultFieldByName {
  1492. state $check = compile(Object, Str, Optional[Maybe[Int]]);
  1493. my ($self,$system_name,$project_id) = $check->(@_);
  1494. my @candidates = grep { $_->{'name'} eq $system_name} @{$self->getTestResultFields()};
  1495. return 0 if !scalar(@candidates); #No such name
  1496. return -1 if ref($candidates[0]) ne 'HASH';
  1497. return -2 if ref($candidates[0]->{'configs'}) ne 'ARRAY' && !scalar(@{$candidates[0]->{'configs'}}); #bogofilter
  1498. #Give it to the user
  1499. my $ret = $candidates[0]; #copy/save for later
  1500. return $ret if !defined($project_id);
  1501. #Filter by project ID
  1502. foreach my $config (@{$candidates[0]->{'configs'}}) {
  1503. return $ret if ( grep { $_ == $project_id} @{ $config->{'context'}->{'project_ids'} } )
  1504. }
  1505. return -3;
  1506. }
  1507. =head2 B<getPossibleTestStatuses()>
  1508. Gets all possible statuses a test can be set to.
  1509. Returns ARRAYREF of status definition HASHREFs.
  1510. Caches the result for the lifetime of the TestRail::API object.
  1511. =cut
  1512. sub getPossibleTestStatuses {
  1513. state $check = compile(Object);
  1514. my ($self) = $check->(@_);
  1515. return $self->{'status_cache'} if $self->{'status_cache'};
  1516. $self->{'status_cache'} = $self->_doRequest('index.php?/api/v2/get_statuses');
  1517. return clone $self->{'status_cache'};
  1518. }
  1519. =head2 statusNamesToIds(names)
  1520. Convenience method to translate a list of statuses to TestRail status IDs.
  1521. The names referred to here are 'internal names' rather than the labels shown in TestRail.
  1522. =over 4
  1523. =item ARRAY C<NAMES> - Array of status names to translate to IDs.
  1524. =back
  1525. Returns ARRAY of status IDs in the same order as the status names passed.
  1526. Throws an exception in the case of one (or more) of the names not corresponding to a valid test status.
  1527. =cut
  1528. sub statusNamesToIds {
  1529. my ($self,@names) = @_;
  1530. return _X_in_my_Y($self,$self->getPossibleTestStatuses(),'id',@names);
  1531. };
  1532. =head2 statusNamesToLabels(names)
  1533. Convenience method to translate a list of statuses to TestRail status labels (the 'nice' form of status names).
  1534. This is useful when interacting with getRunSummary or getPlanSummary, which uses these labels as hash keys.
  1535. =over 4
  1536. =item ARRAY C<NAMES> - Array of status names to translate to IDs.
  1537. =back
  1538. Returns ARRAY of status labels in the same order as the status names passed.
  1539. Throws an exception in the case of one (or more) of the names not corresponding to a valid test status.
  1540. =cut
  1541. sub statusNamesToLabels {
  1542. my ($self,@names) = @_;
  1543. return _X_in_my_Y($self,$self->getPossibleTestStatuses(),'label',@names);
  1544. };
  1545. # Reduce code duplication with internal methods?
  1546. # It's more likely than you think
  1547. # Free PC check @ cpan.org
  1548. sub _X_in_my_Y {
  1549. state $check = compile(Object, ArrayRef, Str, slurpy ArrayRef[Str]);
  1550. my ($self,$search_arr,$key,$names) = $check->(@_);
  1551. my @ret;
  1552. foreach my $name (@$names) {
  1553. foreach my $member (@$search_arr) {
  1554. if ($member->{'name'} eq $name) {
  1555. push @ret, $member->{$key};
  1556. last;
  1557. }
  1558. }
  1559. }
  1560. confess("One or more names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
  1561. return @ret;
  1562. }
  1563. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  1564. Creates a result entry for a test.
  1565. =over 4
  1566. =item INTEGER C<TEST_ID> - ID of desired test
  1567. =item INTEGER C<STATUS_ID> - ID of desired test result status
  1568. =item STRING C<COMMENT> (optional) - Any comments about this result
  1569. =item HASHREF C<OPTIONS> (optional) - Various "Baked-In" options that can be set for test results. See TR docs for more information.
  1570. =item HASHREF C<CUSTOM OPTIONS> (optional) - Options to set for custom fields. See buildStepResults for a simple way to post up custom steps.
  1571. =back
  1572. Returns result definition HASHREF.
  1573. $options = {
  1574. elapsed => '30m 22s',
  1575. defects => ['TSR-3','BOOM-44'],
  1576. version => '6969'
  1577. };
  1578. $custom_options = {
  1579. step_results => [
  1580. {
  1581. content => 'Step 1',
  1582. expected => "Bought Groceries",
  1583. actual => "No Dinero!",
  1584. status_id => 2
  1585. },
  1586. {
  1587. content => 'Step 2',
  1588. expected => 'Ate Dinner',
  1589. actual => 'Went Hungry',
  1590. status_id => 2
  1591. }
  1592. ]
  1593. };
  1594. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  1595. =cut
  1596. sub createTestResults {
  1597. state $check = compile(Object, Int, Int, Optional[Maybe[Str]], Optional[Maybe[HashRef]], Optional[Maybe[HashRef]]);
  1598. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = $check->(@_);
  1599. my $stuff = {
  1600. status_id => $status_id,
  1601. comment => $comment
  1602. };
  1603. #Handle options
  1604. if (defined($opts) && reftype($opts) eq 'HASH') {
  1605. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  1606. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  1607. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  1608. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  1609. }
  1610. #Handle custom fields
  1611. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  1612. foreach my $field (keys(%$custom_fields)) {
  1613. $stuff->{"custom_$field"} = $custom_fields->{$field};
  1614. }
  1615. }
  1616. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  1617. }
  1618. =head2 bulkAddResults(run_id,results)
  1619. Add multiple results to a run, where each result is a HASHREF with keys as outlined in the get_results API call documentation.
  1620. =over 4
  1621. =item INTEGER C<RUN_ID> - ID of desired run to add results to
  1622. =item ARRAYREF C<RESULTS> - Array of result HASHREFs to upload.
  1623. =back
  1624. Returns ARRAYREF of result definition HASHREFs.
  1625. =cut
  1626. sub bulkAddResults {
  1627. state $check = compile(Object, Int, ArrayRef[HashRef]);
  1628. my ($self,$run_id, $results) = $check->(@_);
  1629. return $self->_doRequest("index.php?/api/v2/add_results/$run_id", 'POST', { 'results' => $results });
  1630. }
  1631. =head2 bulkAddResultsByCase(run_id,results)
  1632. Basically the same as bulkAddResults, but instead of a test_id for each entry you use a case_id.
  1633. =cut
  1634. sub bulkAddResultsByCase {
  1635. state $check = compile(Object, Int, ArrayRef[HashRef]);
  1636. my ($self,$run_id, $results) = $check->(@_);
  1637. return $self->_doRequest("index.php?/api/v2/add_results_for_cases/$run_id", 'POST', { 'results' => $results });
  1638. }
  1639. =head2 B<getTestResults(test_id,limit,offset)>
  1640. Get the recorded results for desired test, limiting output to 'limit' entries.
  1641. =over 4
  1642. =item INTEGER C<TEST_ID> - ID of desired test
  1643. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1644. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing result set at.
  1645. =back
  1646. Returns ARRAYREF of result definition HASHREFs.
  1647. =cut
  1648. sub getTestResults {
  1649. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]]);
  1650. my ($self,$test_id,$limit,$offset) = $check->(@_);
  1651. my $url = "index.php?/api/v2/get_results/$test_id";
  1652. $url .= "&limit=$limit" if $limit;
  1653. $url .= "&offset=$offset" if defined($offset);
  1654. return $self->_doRequest($url);
  1655. }
  1656. =head2 B<getResultsForCase(run_id,case_id,limit,offset)>
  1657. Get the recorded results for a test run and case combination., limiting output to 'limit' entries.
  1658. =over 4
  1659. =item INTEGER C<RUN_ID> - ID of desired run
  1660. =item INTEGER C<CASE_ID> - ID of desired case
  1661. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1662. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing result set at.
  1663. =back
  1664. Returns ARRAYREF of result definition HASHREFs.
  1665. =cut
  1666. sub getResultsForCase {
  1667. state $check = compile(Object, Int, Int, Optional[Maybe[Int]], Optional[Maybe[Int]]);
  1668. my ($self,$run_id,$case_id,$limit,$offset) = $check->(@_);
  1669. my $url = "index.php?/api/v2/get_results_for_case/$run_id/$case_id";
  1670. $url .= "&limit=$limit" if $limit;
  1671. $url .= "&offset=$offset" if defined($offset);
  1672. return $self->_doRequest($url);
  1673. }
  1674. =head1 CONFIGURATION METHODS
  1675. =head2 B<getConfigurationGroups(project_id)>
  1676. Gets the available configuration groups for a project, with their configurations as children.
  1677. =over 4
  1678. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1679. =back
  1680. Returns ARRAYREF of configuration group definition HASHREFs.
  1681. =cut
  1682. sub getConfigurationGroups {
  1683. state $check = compile(Object, Int);
  1684. my ($self,$project_id) = $check->(@_);
  1685. my $url = "index.php?/api/v2/get_configs/$project_id";
  1686. return $self->_doRequest($url);
  1687. }
  1688. =head2 B<getConfigurationGroupByName(project_id,name)>
  1689. Get the provided configuration group by name.
  1690. Returns false if the configuration group could not be found.
  1691. =cut
  1692. sub getConfigurationGroupByName {
  1693. state $check = compile(Object, Int, Str);
  1694. my ($self,$project_id,$name) = $check->(@_);
  1695. my $cgroups = $self->getConfigurationGroups($project_id);
  1696. return 0 if ref($cgroups) ne 'ARRAY';
  1697. @$cgroups = grep {$_->{'name'} eq $name} @$cgroups;
  1698. return 0 unless scalar(@$cgroups);
  1699. return $cgroups->[0];
  1700. }
  1701. =head2 B<addConfigurationGroup(project_id,name)>
  1702. New in TestRail 5.2.
  1703. Add a configuration group to the specified project.
  1704. =over 4
  1705. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1706. =item STRING C<NAME> - Name for new configuration Group.
  1707. =back
  1708. Returns HASHREF with new configuration group.
  1709. =cut
  1710. sub addConfigurationGroup {
  1711. state $check = compile(Object, Int, Str);
  1712. my ($self,$project_id,$name) = $check->(@_);
  1713. my $url = "index.php?/api/v2/add_config_group/$project_id";
  1714. return $self->_doRequest($url,'POST',{'name' => $name});
  1715. }
  1716. =head2 B<editConfigurationGroup(config_group_id,name)>
  1717. New in TestRail 5.2.
  1718. Change the name of a configuration group.
  1719. =over 4
  1720. =item INTEGER C<CONFIG_GROUP_ID> - ID of relevant configuration group
  1721. =item STRING C<NAME> - Name for new configuration Group.
  1722. =back
  1723. Returns HASHREF with new configuration group.
  1724. =cut
  1725. sub editConfigurationGroup {
  1726. state $check = compile(Object, Int, Str);
  1727. my ($self,$config_group_id,$name) = $check->(@_);
  1728. my $url = "index.php?/api/v2/update_config_group/$config_group_id";
  1729. return $self->_doRequest($url,'POST',{'name' => $name});
  1730. }
  1731. =head2 B<deleteConfigurationGroup(config_group_id)>
  1732. New in TestRail 5.2.
  1733. Delete a configuration group.
  1734. =over 4
  1735. =item INTEGER C<CONFIG_GROUP_ID> - ID of relevant configuration group
  1736. =back
  1737. Returns BOOL.
  1738. =cut
  1739. sub deleteConfigurationGroup {
  1740. state $check = compile(Object, Int);
  1741. my ($self,$config_group_id) = $check->(@_);
  1742. my $url = "index.php?/api/v2/delete_config_group/$config_group_id";
  1743. return $self->_doRequest($url,'POST');
  1744. }
  1745. =head2 B<getConfigurations(project_id)>
  1746. Gets the available configurations for a project.
  1747. Mostly for convenience (no need to write a boilerplate loop over the groups).
  1748. =over 4
  1749. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1750. =back
  1751. Returns ARRAYREF of configuration definition HASHREFs.
  1752. Returns result of getConfigurationGroups (likely -500) in the event that call fails.
  1753. =cut
  1754. sub getConfigurations {
  1755. state $check = compile(Object, Int);
  1756. my ($self,$project_id) = $check->(@_);
  1757. my $cgroups = $self->getConfigurationGroups($project_id);
  1758. my $configs = [];
  1759. return $cgroups unless (reftype($cgroups) || 'undef') eq 'ARRAY';
  1760. foreach my $cfg (@$cgroups) {
  1761. push(@$configs, @{$cfg->{'configs'}});
  1762. }
  1763. return $configs;
  1764. }
  1765. =head2 B<addConfiguration(configuration_group_id,name)>
  1766. New in TestRail 5.2.
  1767. Add a configuration to the specified configuration group.
  1768. =over 4
  1769. =item INTEGER C<CONFIGURATION_GROUP_ID> - ID of relevant configuration group
  1770. =item STRING C<NAME> - Name for new configuration.
  1771. =back
  1772. Returns HASHREF with new configuration.
  1773. =cut
  1774. sub addConfiguration {
  1775. state $check = compile(Object, Int, Str);
  1776. my ($self,$configuration_group_id,$name) = $check->(@_);
  1777. my $url = "index.php?/api/v2/add_config/$configuration_group_id";
  1778. return $self->_doRequest($url,'POST',{'name' => $name});
  1779. }
  1780. =head2 B<editConfiguration(config_id,name)>
  1781. New in TestRail 5.2.
  1782. Change the name of a configuration.
  1783. =over 4
  1784. =item INTEGER C<CONFIG_ID> - ID of relevant configuration.
  1785. =item STRING C<NAME> - New name for configuration.
  1786. =back
  1787. Returns HASHREF with new configuration group.
  1788. =cut
  1789. sub editConfiguration {
  1790. state $check = compile(Object, Int, Str);
  1791. my ($self,$config_id,$name) = $check->(@_);
  1792. my $url = "index.php?/api/v2/update_config/$config_id";
  1793. return $self->_doRequest($url,'POST',{'name' => $name});
  1794. }
  1795. =head2 B<deleteConfiguration(config_id)>
  1796. New in TestRail 5.2.
  1797. Delete a configuration.
  1798. =over 4
  1799. =item INTEGER C<CONFIG_ID> - ID of relevant configuration
  1800. =back
  1801. Returns BOOL.
  1802. =cut
  1803. sub deleteConfiguration {
  1804. state $check = compile(Object, Int);
  1805. my ($self,$config_id) = $check->(@_);
  1806. my $url = "index.php?/api/v2/delete_config/$config_id";
  1807. return $self->_doRequest($url,'POST');
  1808. }
  1809. =head2 B<translateConfigNamesToIds(project_id,configs)>
  1810. Transforms a list of configuration names into a list of config IDs.
  1811. =over 4
  1812. =item INTEGER C<PROJECT_ID> - Relevant project ID for configs.
  1813. =item ARRAY C<CONFIGS> - Array of config names
  1814. =back
  1815. Returns ARRAY of configuration names, with undef values for unknown configuration names.
  1816. =cut
  1817. sub translateConfigNamesToIds {
  1818. my ($self,$project_id,@names) = @_;
  1819. my $configs = $self->getConfigurations($project_id) or confess("Could not determine configurations in provided project.");
  1820. return _X_in_my_Y($self,$configs,'id',@names);
  1821. }
  1822. =head1 REPORT METHODS
  1823. =head2 getReports
  1824. Return the ARRAYREF of reports available for the provided project.
  1825. Requires you to mark a particular report as accessible in the API via the TestRail report interface.
  1826. =over 4
  1827. =item INTEGER C<PROJECT_ID> - Relevant project ID.
  1828. =back
  1829. =cut
  1830. sub getReports {
  1831. state $check = compile(Object, Int);
  1832. my ($self,$project_id) = $check->(@_);
  1833. my $url = "index.php?/api/v2/get_reports/$project_id";
  1834. return $self->_doRequest($url,'GET');
  1835. }
  1836. =head2 runReport
  1837. Compute the provided report using currently available data.
  1838. Returns HASHREF describing URLs to access completed reports.
  1839. =over 4
  1840. =item INTEGER C<REPORT_ID> - Relevant report ID.
  1841. =back
  1842. =cut
  1843. sub runReport {
  1844. state $check = compile(Object, Int);
  1845. my ($self,$report_id) = $check->(@_);
  1846. my $url = "index.php?/api/v2/run_report/$report_id";
  1847. return $self->_doRequest($url,'GET');
  1848. }
  1849. =head1 STATIC METHODS
  1850. =head2 B<buildStepResults(content,expected,actual,status_id)>
  1851. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  1852. =over 4
  1853. =item STRING C<CONTENT> (optional) - The step itself.
  1854. =item STRING C<EXPECTED> (optional) - Expected result of test step.
  1855. =item STRING C<ACTUAL> (optional) - Actual result of test step
  1856. =item INTEGER C<STATUS ID> (optional) - Status ID of result
  1857. =back
  1858. =cut
  1859. #Convenience method for building stepResults
  1860. sub buildStepResults {
  1861. state $check = compile(Str, Str, Str, Int);
  1862. my ($content,$expected,$actual,$status_id) = $check->(@_);
  1863. return {
  1864. content => $content,
  1865. expected => $expected,
  1866. actual => $actual,
  1867. status_id => $status_id
  1868. };
  1869. }
  1870. 1;
  1871. __END__
  1872. =head1 SEE ALSO
  1873. L<HTTP::Request>
  1874. L<LWP::UserAgent>
  1875. L<JSON::MaybeXS>
  1876. L<http://docs.gurock.com/testrail-api2/start>
  1877. =head1 SPECIAL THANKS
  1878. Thanks to cPanel Inc, for graciously funding the creation of this module.