API.pm 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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','testlink.baz.foo');
  7. my $tr = TestRail::API->new($username, $password, $host);
  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.
  13. When the server is not responsive, expect a -500 response, and retry accordingly.
  14. I recommend using the excellent L<Attempt> module for this purpose.
  15. =cut
  16. use 5.010;
  17. use strict;
  18. use warnings;
  19. use Carp qw{cluck confess};
  20. use Scalar::Util qw{reftype looks_like_number};
  21. use Clone 'clone';
  22. use Try::Tiny;
  23. use Types::Standard qw( slurpy ClassName Object Str Int Bool HashRef ArrayRef Maybe Optional);
  24. use Type::Params qw( compile );
  25. use JSON::MaybeXS 1.001000 ();
  26. use HTTP::Request;
  27. use LWP::UserAgent;
  28. use Data::Validate::URI qw{is_uri};
  29. use List::Util 1.33;
  30. use Encode ();
  31. =head1 CONSTRUCTOR
  32. =head2 B<new (api_url, user, password, encoding, debug)>
  33. Creates new C<TestRail::API> object.
  34. =over 4
  35. =item STRING C<API URL> - base url for your TestRail api server.
  36. =item STRING C<USER> - Your TestRail User.
  37. =item STRING C<PASSWORD> - Your TestRail password, or a valid API key (TestRail 4.2 and above).
  38. =item STRING C<ENCODING> - The character encoding used by the caller. Defaults to 'UTF-8', see L<Encode::Supported> and for supported encodings.
  39. =item BOOLEAN C<DEBUG> (optional) - Print the JSON responses from TL with your requests. Default false.
  40. =back
  41. Returns C<TestRail::API> object if login is successful.
  42. my $tr = TestRail::API->new('http://tr.test/testrail', 'moo','M000000!');
  43. Dies on all communication errors with the TestRail server.
  44. Does not do above checks if debug is passed.
  45. =cut
  46. sub new {
  47. state $check = compile(ClassName, Str, Str, Str, Optional[Maybe[Str]], Optional[Maybe[Bool]]);
  48. my ($class,$apiurl,$user,$pass,$encoding,$debug) = $check->(@_);
  49. confess("Invalid URI passed to constructor") if !is_uri($apiurl);
  50. $debug //= 0;
  51. my $self = {
  52. user => $user,
  53. pass => $pass,
  54. apiurl => $apiurl,
  55. debug => $debug,
  56. encoding => $encoding || 'UTF-8',
  57. testtree => [],
  58. flattree => [],
  59. user_cache => [],
  60. configurations => {},
  61. tr_fields => undef,
  62. default_request => undef,
  63. global_limit => 250, #Discovered by experimentation
  64. browser => new LWP::UserAgent()
  65. };
  66. #Check chara encoding
  67. $self->{'encoding-nonaliased'} = Encode::resolve_alias($self->{'encoding'});
  68. confess("Invalid encoding alias '".$self->{'encoding'}."' passed, see Encoding::Supported for a list of allowed encodings")
  69. unless $self->{'encoding-nonaliased'};
  70. confess("Invalid encoding '".$self->{'encoding-nonaliased'}."' passed, see Encoding::Supported for a list of allowed encodings")
  71. unless grep {$_ eq $self->{'encoding-nonaliased'}} (Encode->encodings(":all"));
  72. #Create default request to pass on to LWP::UserAgent
  73. $self->{'default_request'} = new HTTP::Request();
  74. $self->{'default_request'}->authorization_basic($user,$pass);
  75. bless( $self, $class );
  76. return $self if $self->debug; #For easy class testing without mocks
  77. #Manually do the get_users call to check HTTP status
  78. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  79. confess "Error: network unreachable" if !defined($res);
  80. if ( (reftype($res) || 'undef') ne 'ARRAY') {
  81. confess "Unexpected return from _doRequest: $res" if !looks_like_number($res);
  82. confess "Could not communicate with TestRail Server! Check that your URI is correct, and your TestRail installation is functioning correctly." if $res == -500;
  83. confess "Could not list testRail users! Check that your TestRail installation has it's API enabled, and your credentials are correct" if $res == -403;
  84. confess "Bad user credentials!" if $res == -401;
  85. confess "HTTP error $res encountered while communicating with TestRail server. Resolve issue and try again." if !$res;
  86. confess "Unknown error occurred: $res";
  87. }
  88. confess "No users detected on TestRail Install! Check that your API is functioning correctly." if !scalar(@$res);
  89. $self->{'user_cache'} = $res;
  90. return $self;
  91. }
  92. =head1 GETTERS
  93. =head2 B<apiurl>
  94. =head2 B<debug>
  95. Accessors for these parameters you pass into the constructor, in case you forget.
  96. =cut
  97. sub apiurl {
  98. state $check = compile(Object);
  99. my ($self) = $check->(@_);
  100. return $self->{'apiurl'}
  101. }
  102. sub debug {
  103. state $check = compile(Object);
  104. my ($self) = $check->(@_);
  105. return $self->{'debug'};
  106. }
  107. #Convenient JSON-HTTP fetcher
  108. sub _doRequest {
  109. state $check = compile(Object, Str, Optional[Maybe[Str]], Optional[Maybe[HashRef]]);
  110. my ($self,$path,$method,$data) = $check->(@_);
  111. my $req = clone $self->{'default_request'};
  112. $method //= 'GET';
  113. $req->method($method);
  114. $req->url($self->apiurl.'/'.$path);
  115. warn "$method ".$self->apiurl."/$path" if $self->debug;
  116. my $coder = JSON::MaybeXS->new;
  117. #Data sent is JSON, and encoded per user preference
  118. my $content = $data ? Encode::encode( $self->{'encoding-nonaliased'}, $coder->encode($data) ) : '';
  119. $req->content($content);
  120. $req->header( "Content-Type" => "application/json; charset=".$self->{'encoding'} );
  121. my $response = $self->{'browser'}->request($req);
  122. #Uncomment to generate mocks
  123. #use Data::Dumper;
  124. #open(my $fh, '>>', 'mock.out');
  125. #print $fh "{\n\n";
  126. #print $fh Dumper($path,'200','OK',$response->headers,$response->content);
  127. #print $fh '$mockObject->map_response(qr/\Q$VAR1\E/,HTTP::Response->new($VAR2, $VAR3, $VAR4, $VAR5));';
  128. #print $fh "\n\n}\n\n";
  129. #close $fh;
  130. return $response if !defined($response); #worst case
  131. if ($response->code == 403) {
  132. cluck "ERROR: Access Denied.";
  133. return -403;
  134. }
  135. if ($response->code != 200) {
  136. cluck "ERROR: Arguments Bad: ".$response->content;
  137. return -int($response->code);
  138. }
  139. try {
  140. return $coder->decode($response->content);
  141. } catch {
  142. if ($response->code == 200 && !$response->content) {
  143. return 1; #This function probably just returns no data
  144. } else {
  145. cluck "ERROR: Malformed JSON returned by API.";
  146. cluck $@;
  147. if (!$self->debug) { #Otherwise we've already printed this, but we need to know if we encounter this
  148. cluck "RAW CONTENT:";
  149. cluck $response->content
  150. }
  151. return 0;
  152. }
  153. }
  154. }
  155. =head1 USER METHODS
  156. =head2 B<getUsers ()>
  157. Get all the user definitions for the provided Test Rail install.
  158. Returns ARRAYREF of user definition HASHREFs.
  159. =cut
  160. sub getUsers {
  161. state $check = compile(Object);
  162. my ($self) = $check->(@_);
  163. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  164. return -500 if !$res || (reftype($res) || 'undef') ne 'ARRAY';
  165. $self->{'user_cache'} = $res;
  166. return $res;
  167. }
  168. =head2 B<getUserByID(id)>
  169. =cut
  170. =head2 B<getUserByName(name)>
  171. =cut
  172. =head2 B<getUserByEmail(email)>
  173. Get user definition hash by ID, Name or Email.
  174. Returns user def HASHREF.
  175. =cut
  176. #I'm just using the cache for the following methods because it's more straightforward and faster past 1 call.
  177. sub getUserByID {
  178. state $check = compile(Object, Int);
  179. my ($self,$user) = $check->(@_);
  180. $self->getUsers() if !defined($self->{'user_cache'});
  181. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  182. foreach my $usr (@{$self->{'user_cache'}}) {
  183. return $usr if $usr->{'id'} == $user;
  184. }
  185. return 0;
  186. }
  187. sub getUserByName {
  188. state $check = compile(Object, Str);
  189. my ($self,$user) = $check->(@_);
  190. $self->getUsers() if !defined($self->{'user_cache'});
  191. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  192. foreach my $usr (@{$self->{'user_cache'}}) {
  193. return $usr if $usr->{'name'} eq $user;
  194. }
  195. return 0;
  196. }
  197. sub getUserByEmail {
  198. state $check = compile(Object, Str);
  199. my ($self,$email) = $check->(@_);
  200. $self->getUsers() if !defined($self->{'user_cache'});
  201. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  202. foreach my $usr (@{$self->{'user_cache'}}) {
  203. return $usr if $usr->{'email'} eq $email;
  204. }
  205. return 0;
  206. }
  207. =head2 userNamesToIds(names)
  208. Convenience method to translate a list of user names to TestRail user IDs.
  209. =over 4
  210. =item ARRAY C<NAMES> - Array of user names to translate to IDs.
  211. =back
  212. Returns ARRAY of user IDs.
  213. Throws an exception in the case of one (or more) of the names not corresponding to a valid username.
  214. =cut
  215. sub userNamesToIds {
  216. state $check = compile(Object, slurpy ArrayRef[Str]);
  217. my ($self,$names) = $check->(@_);
  218. confess("At least one user name must be provided") if !scalar(@$names);
  219. my @ret = grep {defined $_} map {my $user = $_; my @list = grep {$user->{'name'} eq $_} @$names; scalar(@list) ? $user->{'id'} : undef} @{$self->getUsers()};
  220. confess("One or more user names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
  221. return @ret;
  222. };
  223. =head1 PROJECT METHODS
  224. =head2 B<createProject (name, [description,send_announcement])>
  225. Creates new Project (Database of testsuites/tests).
  226. Optionally specify an announcement to go out to the users.
  227. Requires TestRail admin login.
  228. =over 4
  229. =item STRING C<NAME> - Desired name of project.
  230. =item STRING C<DESCRIPTION> (optional) - Description of project. Default value is 'res ipsa loquiter'.
  231. =item BOOLEAN C<SEND ANNOUNCEMENT> (optional) - Whether to confront users with an announcement about your awesome project on next login. Default false.
  232. =back
  233. Returns project definition HASHREF on success, false otherwise.
  234. $tl->createProject('Widgetronic 4000', 'Tests for the whiz-bang new product', true);
  235. =cut
  236. sub createProject {
  237. state $check = compile(Object, Str, Optional[Maybe[Str]], Optional[Maybe[Bool]]);
  238. my ($self,$name,$desc,$announce) = $check->(@_);
  239. $desc //= 'res ipsa loquiter';
  240. $announce //= 0;
  241. my $input = {
  242. name => $name,
  243. announcement => $desc,
  244. show_announcement => $announce
  245. };
  246. my $result = $self->_doRequest('index.php?/api/v2/add_project','POST',$input);
  247. return $result;
  248. }
  249. =head2 B<deleteProject (id)>
  250. Deletes specified project by ID.
  251. Requires TestRail admin login.
  252. =over 4
  253. =item STRING C<NAME> - Desired name of project.
  254. =back
  255. Returns BOOLEAN.
  256. $success = $tl->deleteProject(1);
  257. =cut
  258. sub deleteProject {
  259. state $check = compile(Object, Int);
  260. my ($self,$proj) = $check->(@_);
  261. my $result = $self->_doRequest('index.php?/api/v2/delete_project/'.$proj,'POST');
  262. return $result;
  263. }
  264. =head2 B<getProjects ()>
  265. Get all available projects
  266. Returns array of project definition HASHREFs, false otherwise.
  267. $projects = $tl->getProjects;
  268. =cut
  269. sub getProjects {
  270. state $check = compile(Object);
  271. my ($self) = $check->(@_);
  272. my $result = $self->_doRequest('index.php?/api/v2/get_projects');
  273. #Save state for future use, if needed
  274. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  275. $self->{'testtree'} = $result;
  276. #Note that it's a project for future reference by recursive tree search
  277. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  278. foreach my $pj (@{$result}) {
  279. $pj->{'type'} = 'project';
  280. }
  281. return $result;
  282. }
  283. =head2 B<getProjectByName ($project)>
  284. Gets some project definition hash by it's name
  285. =over 4
  286. =item STRING C<PROJECT> - desired project
  287. =back
  288. Returns desired project def HASHREF, false otherwise.
  289. $project = $tl->getProjectByName('FunProject');
  290. =cut
  291. sub getProjectByName {
  292. state $check = compile(Object, Str);
  293. my ($self,$project) = $check->(@_);
  294. #See if we already have the project list...
  295. my $projects = $self->{'testtree'};
  296. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  297. $projects = $self->getProjects() unless scalar(@$projects);
  298. #Search project list for project
  299. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  300. for my $candidate (@$projects) {
  301. return $candidate if ($candidate->{'name'} eq $project);
  302. }
  303. return 0;
  304. }
  305. =head2 B<getProjectByID ($project)>
  306. Gets some project definition hash by it's ID
  307. =over 4
  308. =item INTEGER C<PROJECT> - desired project
  309. =back
  310. Returns desired project def HASHREF, false otherwise.
  311. $projects = $tl->getProjectByID(222);
  312. =cut
  313. sub getProjectByID {
  314. state $check = compile(Object, Int);
  315. my ($self,$project) = $check->(@_);
  316. #See if we already have the project list...
  317. my $projects = $self->{'testtree'};
  318. $projects = $self->getProjects() unless scalar(@$projects);
  319. #Search project list for project
  320. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  321. for my $candidate (@$projects) {
  322. return $candidate if ($candidate->{'id'} eq $project);
  323. }
  324. return 0;
  325. }
  326. =head1 TESTSUITE METHODS
  327. =head2 B<createTestSuite (project_id, name, [description])>
  328. Creates new TestSuite (folder of tests) in the database of test specifications under given project id having given name and details.
  329. =over 4
  330. =item INTEGER C<PROJECT ID> - ID of project this test suite should be under.
  331. =item STRING C<NAME> - Desired name of test suite.
  332. =item STRING C<DESCRIPTION> (optional) - Description of test suite. Default value is 'res ipsa loquiter'.
  333. =back
  334. Returns TS definition HASHREF on success, false otherwise.
  335. $tl->createTestSuite(1, 'broken tests', 'Tests that should be reviewed');
  336. =cut
  337. sub createTestSuite {
  338. state $check = compile(Object, Int, Str, Optional[Maybe[Str]]);
  339. my ($self,$project_id,$name,$details) = $check->(@_);
  340. $details //= 'res ipsa loquiter';
  341. my $input = {
  342. name => $name,
  343. description => $details
  344. };
  345. my $result = $self->_doRequest('index.php?/api/v2/add_suite/'.$project_id,'POST',$input);
  346. return $result;
  347. }
  348. =head2 B<deleteTestSuite (suite_id)>
  349. Deletes specified testsuite.
  350. =over 4
  351. =item INTEGER C<SUITE ID> - ID of testsuite to delete.
  352. =back
  353. Returns BOOLEAN.
  354. $tl->deleteTestSuite(1);
  355. =cut
  356. sub deleteTestSuite {
  357. state $check = compile(Object, Int);
  358. my ($self,$suite_id) = $check->(@_);
  359. my $result = $self->_doRequest('index.php?/api/v2/delete_suite/'.$suite_id,'POST');
  360. return $result;
  361. }
  362. =head2 B<getTestSuites (project_id)>
  363. Gets the testsuites for a project
  364. =over 4
  365. =item STRING C<PROJECT ID> - desired project's ID
  366. =back
  367. Returns ARRAYREF of testsuite definition HASHREFs, 0 on error.
  368. $suites = $tl->getTestSuites(123);
  369. =cut
  370. sub getTestSuites {
  371. state $check = compile(Object, Int);
  372. my ($self,$proj) = $check->(@_);
  373. return $self->_doRequest('index.php?/api/v2/get_suites/'.$proj);
  374. }
  375. =head2 B<getTestSuiteByName (project_id,testsuite_name)>
  376. Gets the testsuite that matches the given name inside of given project.
  377. =over 4
  378. =item STRING C<PROJECT ID> - ID of project holding this testsuite
  379. =item STRING C<TESTSUITE NAME> - desired parent testsuite name
  380. =back
  381. Returns desired testsuite definition HASHREF, false otherwise.
  382. $suites = $tl->getTestSuitesByName(321, 'hugSuite');
  383. =cut
  384. sub getTestSuiteByName {
  385. state $check = compile(Object, Int, Str);
  386. my ($self,$project_id,$testsuite_name) = $check->(@_);
  387. #TODO cache
  388. my $suites = $self->getTestSuites($project_id);
  389. return -500 if !$suites || (reftype($suites) || 'undef') ne 'ARRAY'; #No suites for project, or no project
  390. foreach my $suite (@$suites) {
  391. return $suite if $suite->{'name'} eq $testsuite_name;
  392. }
  393. return 0; #Couldn't find it
  394. }
  395. =head2 B<getTestSuiteByID (testsuite_id)>
  396. Gets the testsuite with the given ID.
  397. =over 4
  398. =item STRING C<TESTSUITE_ID> - TestSuite ID.
  399. =back
  400. Returns desired testsuite definition HASHREF, false otherwise.
  401. $tests = $tl->getTestSuiteByID(123);
  402. =cut
  403. sub getTestSuiteByID {
  404. state $check = compile(Object, Int);
  405. my ($self,$testsuite_id) = $check->(@_);
  406. my $result = $self->_doRequest('index.php?/api/v2/get_suite/'.$testsuite_id);
  407. return $result;
  408. }
  409. =head1 SECTION METHODS
  410. =head2 B<createSection(project_id,suite_id,name,[parent_id])>
  411. Creates a section.
  412. =over 4
  413. =item INTEGER C<PROJECT ID> - Parent Project ID.
  414. =item INTEGER C<SUITE ID> - Parent TestSuite ID.
  415. =item STRING C<NAME> - desired section name.
  416. =item INTEGER C<PARENT ID> (optional) - parent section id
  417. =back
  418. Returns new section definition HASHREF, false otherwise.
  419. $section = $tr->createSection(1,1,'nugs',1);
  420. =cut
  421. sub createSection {
  422. state $check = compile(Object, Int, Int, Str, Optional[Maybe[Int]]);
  423. my ($self,$project_id,$suite_id,$name,$parent_id) = $check->(@_);
  424. my $input = {
  425. name => $name,
  426. suite_id => $suite_id
  427. };
  428. $input->{'parent_id'} = $parent_id if $parent_id;
  429. my $result = $self->_doRequest('index.php?/api/v2/add_section/'.$project_id,'POST',$input);
  430. return $result;
  431. }
  432. =head2 B<deleteSection (section_id)>
  433. Deletes specified section.
  434. =over 4
  435. =item INTEGER C<SECTION ID> - ID of section to delete.
  436. =back
  437. Returns BOOLEAN.
  438. $tr->deleteSection(1);
  439. =cut
  440. sub deleteSection {
  441. state $check = compile(Object, Int);
  442. my ($self,$section_id) = $check->(@_);
  443. my $result = $self->_doRequest('index.php?/api/v2/delete_section/'.$section_id,'POST');
  444. return $result;
  445. }
  446. =head2 B<getSections (project_id,suite_id)>
  447. Gets sections for a given project and suite.
  448. =over 4
  449. =item INTEGER C<PROJECT ID> - ID of parent project.
  450. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  451. =back
  452. Returns ARRAYREF of section definition HASHREFs.
  453. $tr->getSections(1,2);
  454. =cut
  455. sub getSections {
  456. state $check = compile(Object, Int, Int);
  457. my ($self,$project_id,$suite_id) = $check->(@_);
  458. return $self->_doRequest("index.php?/api/v2/get_sections/$project_id&suite_id=$suite_id");
  459. }
  460. =head2 B<getSectionByID (section_id)>
  461. Gets desired section.
  462. =over 4
  463. =item INTEGER C<PROJECT ID> - ID of parent project.
  464. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  465. =back
  466. Returns section definition HASHREF.
  467. $tr->getSectionByID(344);
  468. =cut
  469. sub getSectionByID {
  470. state $check = compile(Object, Int);
  471. my ($self,$section_id) = $check->(@_);
  472. return $self->_doRequest("index.php?/api/v2/get_section/$section_id");
  473. }
  474. =head2 B<getSectionByName (project_id,suite_id,name)>
  475. Gets desired section.
  476. =over 4
  477. =item INTEGER C<PROJECT ID> - ID of parent project.
  478. =item INTEGER C<SUITE ID> - ID of suite to get section for.
  479. =item STRING C<NAME> - name of section to get
  480. =back
  481. Returns section definition HASHREF.
  482. $tr->getSectionByName(1,2,'nugs');
  483. =cut
  484. sub getSectionByName {
  485. state $check = compile(Object, Int, Int, Str);
  486. my ($self,$project_id,$suite_id,$section_name) = $check->(@_);
  487. my $sections = $self->getSections($project_id,$suite_id);
  488. return -500 if !$sections || (reftype($sections) || 'undef') ne 'ARRAY';
  489. foreach my $sec (@$sections) {
  490. return $sec if $sec->{'name'} eq $section_name;
  491. }
  492. return 0;
  493. }
  494. =head2 sectionNamesToIds(project_id,suite_id,names)
  495. Convenience method to translate a list of section names to TestRail section IDs.
  496. =over 4
  497. =item INTEGER C<PROJECT ID> - ID of parent project.
  498. =item INTEGER C<SUITE ID> - ID of parent suite.
  499. =item ARRAY C<NAMES> - Array of section names to translate to IDs.
  500. =back
  501. Returns ARRAY of section IDs.
  502. Throws an exception in the case of one (or more) of the names not corresponding to a valid section name.
  503. =cut
  504. sub sectionNamesToIds {
  505. state $check = compile(Object, Int, Int, slurpy ArrayRef[Str]);
  506. my ($self,$project_id,$suite_id,$names) = $check->(@_);
  507. confess("At least one section name must be provided") if !scalar(@$names);
  508. my $sections = $self->getSections($project_id,$suite_id);
  509. confess("Invalid project/suite ($project_id,$suite_id) provided.") unless (reftype($sections) || 'undef') eq 'ARRAY';
  510. my @ret = grep {defined $_} map {my $section = $_; my @list = grep {$section->{'name'} eq $_} @$names; scalar(@list) ? $section->{'id'} : undef} @$sections;
  511. confess("One or more user names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
  512. return @ret;
  513. }
  514. =head1 CASE METHODS
  515. =head2 B<getCaseTypes ()>
  516. Gets possible case types.
  517. Returns ARRAYREF of case type definition HASHREFs.
  518. $tr->getCaseTypes();
  519. =cut
  520. sub getCaseTypes {
  521. state $check = compile(Object);
  522. my ($self) = $check->(@_);
  523. return $self->{'type_cache'} if defined($self->{'type_cache'});
  524. my $types = $self->_doRequest("index.php?/api/v2/get_case_types");
  525. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  526. $self->{'type_cache'} = $types;
  527. return $types;
  528. }
  529. =head2 B<getCaseTypeByName (name)>
  530. Gets case type by name.
  531. =over 4
  532. =item STRING C<NAME> - Name of desired case type
  533. =back
  534. Returns case type definition HASHREF.
  535. Dies if named case type does not exist.
  536. $tr->getCaseTypeByName();
  537. =cut
  538. sub getCaseTypeByName {
  539. state $check = compile(Object, Str);
  540. my ($self,$name) = $check->(@_);
  541. my $types = $self->getCaseTypes();
  542. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  543. foreach my $type (@$types) {
  544. return $type if $type->{'name'} eq $name;
  545. }
  546. confess("No such case type '$name'!");
  547. }
  548. =head2 B<createCase(section_id,title,type_id,options,extra_options)>
  549. Creates a test case.
  550. =over 4
  551. =item INTEGER C<SECTION ID> - Parent Section ID.
  552. =item STRING C<TITLE> - Case title.
  553. =item INTEGER C<TYPE_ID> (optional) - desired test type's ID. Defaults to whatever your TR install considers the default type.
  554. =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.
  555. =item HASHREF C<EXTRA OPTIONS> (optional) - contains priority_id, estimate, milestone_id and refs as possible keys. See TestRail API documentation for more info.
  556. =back
  557. Returns new case definition HASHREF, false otherwise.
  558. $custom_opts = {
  559. preconds => "Test harness installed",
  560. steps => "Do the needful",
  561. expected => "cubicle environment transforms into Dali painting"
  562. };
  563. $other_opts = {
  564. priority_id => 4,
  565. milestone_id => 666,
  566. estimate => '2m 45s',
  567. refs => ['TRACE-22','ON-166'] #ARRAYREF of bug IDs.
  568. }
  569. $case = $tr->createCase(1,'Do some stuff',3,$custom_opts,$other_opts);
  570. =cut
  571. sub createCase {
  572. state $check = compile(Object, Int, Str, Optional[Maybe[Int]], Optional[Maybe[HashRef]], Optional[Maybe[HashRef]]);
  573. my ($self,$section_id,$title,$type_id,$opts,$extras) = $check->(@_);
  574. my $stuff = {
  575. title => $title,
  576. type_id => $type_id
  577. };
  578. #Handle sort of optional but baked in options
  579. if (defined($extras) && reftype($extras) eq 'HASH') {
  580. $stuff->{'priority_id'} = $extras->{'priority_id'} if defined($extras->{'priority_id'});
  581. $stuff->{'estimate'} = $extras->{'estimate'} if defined($extras->{'estimate'});
  582. $stuff->{'milestone_id'} = $extras->{'milestone_id'} if defined($extras->{'milestone_id'});
  583. $stuff->{'refs'} = join(',',@{$extras->{'refs'}}) if defined($extras->{'refs'});
  584. }
  585. #Handle custom fields
  586. if (defined($opts) && reftype($opts) eq 'HASH') {
  587. foreach my $key (keys(%$opts)) {
  588. $stuff->{"custom_$key"} = $opts->{$key};
  589. }
  590. }
  591. my $result = $self->_doRequest("index.php?/api/v2/add_case/$section_id",'POST',$stuff);
  592. return $result;
  593. }
  594. =head2 B<deleteCase (case_id)>
  595. Deletes specified section.
  596. =over 4
  597. =item INTEGER C<CASE ID> - ID of case to delete.
  598. =back
  599. Returns BOOLEAN.
  600. $tr->deleteCase(1324);
  601. =cut
  602. sub deleteCase {
  603. state $check = compile(Object, Int);
  604. my ($self,$case_id) = $check->(@_);
  605. my $result = $self->_doRequest("index.php?/api/v2/delete_case/$case_id",'POST');
  606. return $result;
  607. }
  608. =head2 B<getCases (project_id,suite_id,section_id)>
  609. Gets cases for provided section.
  610. =over 4
  611. =item INTEGER C<PROJECT ID> - ID of parent project.
  612. =item INTEGER C<SUITE ID> - ID of parent suite.
  613. =item INTEGER C<SECTION ID> - ID of parent section
  614. =back
  615. Returns ARRAYREF of test case definition HASHREFs.
  616. $tr->getCases(1,2,3);
  617. =cut
  618. sub getCases {
  619. state $check = compile(Object, Int, Int, Int);
  620. my ($self,$project_id,$suite_id,$section_id) = $check->(@_);
  621. my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
  622. $url .= "&section_id=$section_id" if $section_id;
  623. return $self->_doRequest($url);
  624. }
  625. =head2 B<getCaseByName (project_id,suite_id,section_id,name)>
  626. Gets case by name.
  627. =over 4
  628. =item INTEGER C<PROJECT ID> - ID of parent project.
  629. =item INTEGER C<SUITE ID> - ID of parent suite.
  630. =item INTEGER C<SECTION ID> - ID of parent section.
  631. =item STRING <NAME> - Name of desired test case.
  632. =back
  633. Returns test case definition HASHREF.
  634. $tr->getCaseByName(1,2,3,'nugs');
  635. =cut
  636. sub getCaseByName {
  637. state $check = compile(Object, Int, Int, Int, Str);
  638. my ($self,$project_id,$suite_id,$section_id,$name) = $check->(@_);
  639. my $cases = $self->getCases($project_id,$suite_id,$section_id);
  640. return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
  641. foreach my $case (@$cases) {
  642. return $case if $case->{'title'} eq $name;
  643. }
  644. return 0;
  645. }
  646. =head2 B<getCaseByID (case_id)>
  647. Gets case by ID.
  648. =over 4
  649. =item INTEGER C<CASE ID> - ID of case.
  650. =back
  651. Returns test case definition HASHREF.
  652. $tr->getCaseByID(1345);
  653. =cut
  654. sub getCaseByID {
  655. state $check = compile(Object, Int);
  656. my ($self,$case_id) = $check->(@_);
  657. return $self->_doRequest("index.php?/api/v2/get_case/$case_id");
  658. }
  659. =head1 RUN METHODS
  660. =head2 B<createRun (project_id,suite_id,name,description,milestone_id,assigned_to_id,case_ids)>
  661. Create a run.
  662. =over 4
  663. =item INTEGER C<PROJECT ID> - ID of parent project.
  664. =item INTEGER C<SUITE ID> - ID of suite to base run on
  665. =item STRING C<NAME> - Name of run
  666. =item STRING C<DESCRIPTION> (optional) - Description of run
  667. =item INTEGER C<MILESTONE ID> (optional) - ID of milestone
  668. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  669. =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.
  670. =back
  671. Returns run definition HASHREF.
  672. $tr->createRun(1,1345,'RUN AWAY','SO FAR AWAY',22,3,[3,4,5,6]);
  673. =cut
  674. #If you pass an array of case ids, it implies include_all is false
  675. sub createRun {
  676. state $check = compile(Object, Int, Int, Str, Optional[Maybe[Str]], Optional[Maybe[Int]], Optional[Maybe[Int]], Optional[Maybe[ArrayRef[Int]]]);
  677. my ($self,$project_id,$suite_id,$name,$desc,$milestone_id,$assignedto_id,$case_ids) = $check->(@_);
  678. my $stuff = {
  679. suite_id => $suite_id,
  680. name => $name,
  681. description => $desc,
  682. milestone_id => $milestone_id,
  683. assignedto_id => $assignedto_id,
  684. include_all => defined($case_ids) ? 0 : 1,
  685. case_ids => $case_ids
  686. };
  687. my $result = $self->_doRequest("index.php?/api/v2/add_run/$project_id",'POST',$stuff);
  688. return $result;
  689. }
  690. =head2 B<deleteRun (run_id)>
  691. Deletes specified run.
  692. =over 4
  693. =item INTEGER C<RUN ID> - ID of run to delete.
  694. =back
  695. Returns BOOLEAN.
  696. $tr->deleteRun(1324);
  697. =cut
  698. sub deleteRun {
  699. state $check = compile(Object, Int);
  700. my ($self,$run_id) = $check->(@_);
  701. my $result = $self->_doRequest("index.php?/api/v2/delete_run/$run_id",'POST');
  702. return $result;
  703. }
  704. =head2 B<getRuns (project_id)>
  705. Get all runs for specified project.
  706. To do this, it must make (no. of runs/250) HTTP requests.
  707. This is due to the maximum result set limit enforced by testrail.
  708. =over 4
  709. =item INTEGER C<PROJECT_ID> - ID of parent project
  710. =back
  711. Returns ARRAYREF of run definition HASHREFs.
  712. $allRuns = $tr->getRuns(6969);
  713. =cut
  714. sub getRuns {
  715. state $check = compile(Object, Int);
  716. my ($self,$project_id) = $check->(@_);
  717. my $initial_runs = $self->getRunsPaginated($project_id,$self->{'global_limit'},0);
  718. return $initial_runs unless (reftype($initial_runs) || 'undef') eq 'ARRAY';
  719. my $runs = [];
  720. push(@$runs,@$initial_runs);
  721. my $offset = 1;
  722. while (scalar(@$initial_runs) == $self->{'global_limit'}) {
  723. $initial_runs = $self->getRunsPaginated($project_id,$self->{'global_limit'},($self->{'global_limit'} * $offset));
  724. push(@$runs,@$initial_runs);
  725. $offset++;
  726. }
  727. return $runs;
  728. }
  729. =head2 B<getRunsPaginated (project_id,limit,offset)>
  730. Get some runs for specified project.
  731. =over 4
  732. =item INTEGER C<PROJECT_ID> - ID of parent project
  733. =item INTEGER C<LIMIT> - Number of runs to return.
  734. =item INTEGER C<OFFSET> - Page of runs to return.
  735. =back
  736. Returns ARRAYREF of run definition HASHREFs.
  737. $someRuns = $tr->getRunsPaginated(6969,22,4);
  738. =cut
  739. sub getRunsPaginated {
  740. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]]);
  741. my ($self,$project_id,$limit,$offset) = $check->(@_);
  742. confess("Limit greater than ".$self->{'global_limit'}) if $limit > $self->{'global_limit'};
  743. my $apiurl = "index.php?/api/v2/get_runs/$project_id";
  744. $apiurl .= "&offset=$offset" if defined($offset);
  745. $apiurl .= "&limit=$limit" if $limit; #You have problems if you want 0 results
  746. return $self->_doRequest($apiurl);
  747. }
  748. =head2 B<getRunByName (project_id,name)>
  749. Gets run by name.
  750. =over 4
  751. =item INTEGER C<PROJECT ID> - ID of parent project.
  752. =item STRING <NAME> - Name of desired run.
  753. =back
  754. Returns run definition HASHREF.
  755. $tr->getRunByName(1,'R2');
  756. =cut
  757. sub getRunByName {
  758. state $check = compile(Object, Int, Str);
  759. my ($self,$project_id,$name) = $check->(@_);
  760. my $runs = $self->getRuns($project_id);
  761. return -500 if !$runs || (reftype($runs) || 'undef') ne 'ARRAY';
  762. foreach my $run (@$runs) {
  763. return $run if $run->{'name'} eq $name;
  764. }
  765. return 0;
  766. }
  767. =head2 B<getRunByID (run_id)>
  768. Gets run by ID.
  769. =over 4
  770. =item INTEGER C<RUN ID> - ID of desired run.
  771. =back
  772. Returns run definition HASHREF.
  773. $tr->getRunByID(7779311);
  774. =cut
  775. sub getRunByID {
  776. state $check = compile(Object, Int);
  777. my ($self,$run_id) = $check->(@_);
  778. return $self->_doRequest("index.php?/api/v2/get_run/$run_id");
  779. }
  780. =head2 B<closeRun (run_id)>
  781. Close the specified run.
  782. =over 4
  783. =item INTEGER C<RUN ID> - ID of desired run.
  784. =back
  785. Returns run definition HASHREF on success, false on failure.
  786. $tr->closeRun(90210);
  787. =cut
  788. sub closeRun {
  789. state $check = compile(Object, Int);
  790. my ($self,$run_id) = $check->(@_);
  791. return $self->_doRequest("index.php?/api/v2/close_run/$run_id",'POST');
  792. }
  793. =head2 B<getRunSummary(runs)>
  794. Returns array of hashrefs describing the # of tests in the run(s) with the available statuses.
  795. Translates custom_statuses into their system names for you.
  796. =over 4
  797. =item ARRAY C<RUNS> - runs obtained from getRun* or getChildRun* methods.
  798. =back
  799. Returns ARRAY of run HASHREFs with the added key 'run_status' holding a hashref where status_name => count.
  800. $tr->getRunSummary($run,$run2);
  801. =cut
  802. sub getRunSummary {
  803. state $check = compile(Object, slurpy ArrayRef[HashRef]);
  804. my ($self,$runs) = $check->(@_);
  805. confess("At least one run must be passed!") unless scalar(@$runs);
  806. #Translate custom statuses
  807. my $statuses = $self->getPossibleTestStatuses();
  808. my %shash;
  809. #XXX so, they do these tricks with the status names, see...so map the counts to their relevant status ids.
  810. @shash{map { ( $_->{'id'} < 6 ) ? $_->{'name'}."_count" : "custom_status".($_->{'id'} - 5)."_count" } @$statuses } = map { $_->{'id'} } @$statuses;
  811. my @sname;
  812. #Create listing of keys/values
  813. @$runs = map {
  814. my $run = $_;
  815. @{$run->{statuses}}{grep {$_ =~ m/_count$/} keys(%$run)} = grep {$_ =~ m/_count$/} keys(%$run);
  816. foreach my $status (keys(%{$run->{'statuses'}})) {
  817. next if !exists($shash{$status});
  818. @sname = grep {exists($shash{$status}) && $_->{'id'} == $shash{$status}} @$statuses;
  819. $run->{'statuses_clean'}->{$sname[0]->{'name'}} = $run->{$status};
  820. }
  821. $run;
  822. } @$runs;
  823. return map { {'id' => $_->{'id'}, 'name' => $_->{'name'}, 'run_status' => $_->{'statuses_clean'}, 'config_ids' => $_->{'config_ids'} } } @$runs;
  824. }
  825. =head1 RUN AS CHILD OF PLAN METHODS
  826. =head2 B<getChildRuns(plan)>
  827. Extract the child runs from a plan. Convenient, as the structure of this hash is deep, and correct error handling can be tedious.
  828. =over 4
  829. =item HASHREF C<PLAN> - Test Plan definition HASHREF returned by any of the PLAN methods below.
  830. =back
  831. Returns ARRAYREF of run definition HASHREFs. Returns 0 upon failure to extract the data.
  832. =cut
  833. sub getChildRuns {
  834. state $check = compile(Object, HashRef);
  835. my ($self,$plan) = $check->(@_);
  836. return 0 unless defined($plan->{'entries'}) && (reftype($plan->{'entries'}) || 'undef') eq 'ARRAY';
  837. my $entries = $plan->{'entries'};
  838. my $plans = [];
  839. foreach my $entry (@$entries) {
  840. push(@$plans,@{$entry->{'runs'}}) if defined($entry->{'runs'}) && ((reftype($entry->{'runs'}) || 'undef') eq 'ARRAY')
  841. }
  842. return $plans;
  843. }
  844. =head2 B<getChildRunByName(plan,name,configurations)>
  845. =over 4
  846. =item HASHREF C<PLAN> - Test Plan definition HASHREF returned by any of the PLAN methods below.
  847. =item STRING C<NAME> - Name of run to search for within plan.
  848. =item ARRAYREF C<CONFIGURATIONS> (optional) - Names of configurations to filter runs by.
  849. =back
  850. Returns run definition HASHREF, or false if no such run is found.
  851. Convenience method using getChildRuns.
  852. Will throw a fatal error if one or more of the configurations passed does not exist in the project.
  853. =cut
  854. sub getChildRunByName {
  855. state $check = compile(Object, HashRef, Str, Optional[Maybe[ArrayRef[Str]]]);
  856. my ($self,$plan,$name,$configurations) = $check->(@_);
  857. my $runs = $self->getChildRuns($plan);
  858. return 0 if !$runs;
  859. my @pconfigs = ();
  860. #Figure out desired config IDs
  861. if (defined $configurations) {
  862. my $avail_configs = $self->getConfigurations($plan->{'project_id'});
  863. my ($cname);
  864. @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @$configurations } @$avail_configs; #Get a list of IDs from the names passed
  865. }
  866. confess("One or more configurations passed does not exist in your project!") if defined($configurations) && (scalar(@pconfigs) != scalar(@$configurations));
  867. my $found;
  868. foreach my $run (@$runs) {
  869. next if $run->{name} ne $name;
  870. next if scalar(@pconfigs) != scalar(@{$run->{'config_ids'}});
  871. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  872. $found = 0;
  873. foreach my $cid (@{$run->{'config_ids'}}) {
  874. $found++ if grep {$_ == $cid} @pconfigs;
  875. }
  876. return $run if $found == scalar(@{$run->{'config_ids'}});
  877. }
  878. return 0;
  879. }
  880. =head1 PLAN METHODS
  881. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  882. Create a test plan.
  883. =over 4
  884. =item INTEGER C<PROJECT ID> - ID of parent project.
  885. =item STRING C<NAME> - Name of plan
  886. =item STRING C<DESCRIPTION> (optional) - Description of plan
  887. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  888. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  889. =back
  890. Returns test plan definition HASHREF, or false on failure.
  891. $entries = [{
  892. suite_id => 345,
  893. include_all => 1,
  894. assignedto_id => 1
  895. }];
  896. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  897. =cut
  898. sub createPlan {
  899. state $check = compile(Object, Int, Str, Optional[Maybe[Str]], Optional[Maybe[Int]], Optional[Maybe[ArrayRef[HashRef]]]);
  900. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = $check->(@_);
  901. my $stuff = {
  902. name => $name,
  903. description => $desc,
  904. milestone_id => $milestone_id,
  905. entries => $entries
  906. };
  907. my $result = $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  908. return $result;
  909. }
  910. =head2 B<deletePlan (plan_id)>
  911. Deletes specified plan.
  912. =over 4
  913. =item INTEGER C<PLAN ID> - ID of plan to delete.
  914. =back
  915. Returns BOOLEAN.
  916. $tr->deletePlan(8675309);
  917. =cut
  918. sub deletePlan {
  919. state $check = compile(Object, Int);
  920. my ($self,$plan_id) = $check->(@_);
  921. my $result = $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  922. return $result;
  923. }
  924. =head2 B<getPlans (project_id)>
  925. Gets all test plans in specified project.
  926. Like getRuns, must make multiple HTTP requests when the number of results exceeds 250.
  927. =over 4
  928. =item INTEGER C<PROJECT ID> - ID of parent project.
  929. =back
  930. Returns ARRAYREF of all plan definition HASHREFs in a project.
  931. $tr->getPlans(8);
  932. Does not contain any information about child test runs.
  933. Use getPlanByID or getPlanByName if you want that, in particular if you are interested in using getChildRunByName.
  934. =cut
  935. sub getPlans {
  936. state $check = compile(Object, Int);
  937. my ($self,$project_id) = $check->(@_);
  938. my $initial_plans = $self->getPlansPaginated($project_id,$self->{'global_limit'},0);
  939. return $initial_plans unless (reftype($initial_plans) || 'undef') eq 'ARRAY';
  940. my $plans = [];
  941. push(@$plans,@$initial_plans);
  942. my $offset = 1;
  943. while (scalar(@$initial_plans) == $self->{'global_limit'}) {
  944. $initial_plans = $self->getPlansPaginated($project_id,$self->{'global_limit'},($self->{'global_limit'} * $offset));
  945. push(@$plans,@$initial_plans);
  946. $offset++;
  947. }
  948. return $plans;
  949. }
  950. =head2 B<getPlansPaginated (project_id,limit,offset)>
  951. Get some plans for specified project.
  952. =over 4
  953. =item INTEGER C<PROJECT_ID> - ID of parent project
  954. =item INTEGER C<LIMIT> - Number of plans to return.
  955. =item INTEGER C<OFFSET> - Page of plans to return.
  956. =back
  957. Returns ARRAYREF of plan definition HASHREFs.
  958. $someRuns = $tr->getPlansPaginated(6969,222,44);
  959. =cut
  960. sub getPlansPaginated {
  961. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]]);
  962. my ($self,$project_id,$limit,$offset) = $check->(@_);
  963. confess("Limit greater than ".$self->{'global_limit'}) if $limit > $self->{'global_limit'};
  964. my $apiurl = "index.php?/api/v2/get_plans/$project_id";
  965. $apiurl .= "&offset=$offset" if defined($offset);
  966. $apiurl .= "&limit=$limit" if $limit; #You have problems if you want 0 results
  967. return $self->_doRequest($apiurl);
  968. }
  969. =head2 B<getPlanByName (project_id,name)>
  970. Gets specified plan by name.
  971. =over 4
  972. =item INTEGER C<PROJECT ID> - ID of parent project.
  973. =item STRING C<NAME> - Name of test plan.
  974. =back
  975. Returns plan definition HASHREF.
  976. $tr->getPlanByName(8,'GosPlan');
  977. =cut
  978. sub getPlanByName {
  979. state $check = compile(Object, Int, Str);
  980. my ($self,$project_id,$name) = $check->(@_);
  981. my $plans = $self->getPlans($project_id);
  982. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  983. foreach my $plan (@$plans) {
  984. if ($plan->{'name'} eq $name) {
  985. return $self->getPlanByID($plan->{'id'});
  986. }
  987. }
  988. return 0;
  989. }
  990. =head2 B<getPlanByID (plan_id)>
  991. Gets specified plan by ID.
  992. =over 4
  993. =item INTEGER C<PLAN ID> - ID of plan.
  994. =back
  995. Returns plan definition HASHREF.
  996. $tr->getPlanByID(2);
  997. =cut
  998. sub getPlanByID {
  999. state $check = compile(Object, Int);
  1000. my ($self,$plan_id) = $check->(@_);
  1001. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  1002. }
  1003. =head2 B<getPlanSummary(plan_ID)>
  1004. Returns hashref describing the various pass, fail, etc. percentages for tests in the plan.
  1005. The 'totals' key has total cases in each status ('status' => count)
  1006. The 'percentages' key has the same, but as a percentage of the total.
  1007. =over 4
  1008. =item SCALAR C<plan_ID> - ID of your test plan.
  1009. =back
  1010. $tr->getPlanSummary($plan_id);
  1011. =cut
  1012. sub getPlanSummary {
  1013. state $check = compile(Object, Int);
  1014. my ($self,$plan_id) = $check->(@_);
  1015. my $runs = $self->getPlanByID( $plan_id );
  1016. $runs = $self->getChildRuns( $runs );
  1017. @$runs = $self->getRunSummary(@{$runs});
  1018. my $total_sum = 0;
  1019. my $ret = { plan => $plan_id };
  1020. #Compile totals
  1021. foreach my $summary ( @$runs ) {
  1022. my @elems = keys( %{ $summary->{'run_status'} } );
  1023. foreach my $key (@elems) {
  1024. $ret->{'totals'}->{$key} = 0 if !defined $ret->{'totals'}->{$key};
  1025. $ret->{'totals'}->{$key} += $summary->{'run_status'}->{$key};
  1026. $total_sum += $summary->{'run_status'}->{$key};
  1027. }
  1028. }
  1029. #Compile percentages
  1030. foreach my $key (keys(%{$ret->{'totals'}})) {
  1031. next if grep {$_ eq $key} qw{plan configs percentages};
  1032. $ret->{"percentages"}->{$key} = sprintf( "%.2f%%", ( $ret->{'totals'}->{$key} / $total_sum ) * 100 );
  1033. }
  1034. return $ret;
  1035. }
  1036. =head2 B<createRunInPlan (plan_id,suite_id,name,description,milestone_id,assigned_to_id,config_ids,case_ids)>
  1037. Create a run in a plan.
  1038. =over 4
  1039. =item INTEGER C<PLAN ID> - ID of parent project.
  1040. =item INTEGER C<SUITE ID> - ID of suite to base run on
  1041. =item STRING C<NAME> - Name of run
  1042. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  1043. =item ARRAYREF C<CONFIG IDS> (optional) - Array of Configuration IDs (see getConfigurations) to apply to the created run
  1044. =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.
  1045. =back
  1046. Returns run definition HASHREF.
  1047. $tr->createRun(1,1345,'PlannedRun',3,[1,4,77],[3,4,5,6]);
  1048. =cut
  1049. #If you pass an array of case ids, it implies include_all is false
  1050. sub createRunInPlan {
  1051. state $check = compile(Object, Int, Int, Str, Optional[Maybe[Int]], Optional[Maybe[ArrayRef[Int]]], Optional[Maybe[ArrayRef[Int]]]);
  1052. my ($self,$plan_id,$suite_id,$name,$assignedto_id,$config_ids,$case_ids) = $check->(@_);
  1053. my $runs = [
  1054. {
  1055. config_ids => $config_ids,
  1056. include_all => defined($case_ids) ? 0 : 1,
  1057. case_ids => $case_ids
  1058. }
  1059. ];
  1060. my $stuff = {
  1061. suite_id => $suite_id,
  1062. name => $name,
  1063. assignedto_id => $assignedto_id,
  1064. include_all => defined($case_ids) ? 0 : 1,
  1065. case_ids => $case_ids,
  1066. config_ids => $config_ids,
  1067. runs => $runs
  1068. };
  1069. my $result = $self->_doRequest("index.php?/api/v2/add_plan_entry/$plan_id",'POST',$stuff);
  1070. return $result;
  1071. }
  1072. =head2 B<closePlan (plan_id)>
  1073. Close the specified plan.
  1074. =over 4
  1075. =item INTEGER C<PLAN ID> - ID of desired plan.
  1076. =back
  1077. Returns plan definition HASHREF on success, false on failure.
  1078. $tr->closePlan(75020);
  1079. =cut
  1080. sub closePlan {
  1081. state $check = compile(Object, Int);
  1082. my ($self,$plan_id) = $check->(@_);
  1083. return $self->_doRequest("index.php?/api/v2/close_plan/$plan_id",'POST');
  1084. }
  1085. =head1 MILESTONE METHODS
  1086. =head2 B<createMilestone (project_id,name,description,due_on)>
  1087. Create a milestone.
  1088. =over 4
  1089. =item INTEGER C<PROJECT ID> - ID of parent project.
  1090. =item STRING C<NAME> - Name of milestone
  1091. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  1092. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  1093. =back
  1094. Returns milestone definition HASHREF, or false on failure.
  1095. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  1096. =cut
  1097. sub createMilestone {
  1098. state $check = compile(Object, Int, Str, Optional[Maybe[Str]], Optional[Maybe[Int]]);
  1099. my ($self,$project_id,$name,$desc,$due_on) = $check->(@_);
  1100. my $stuff = {
  1101. name => $name,
  1102. description => $desc,
  1103. due_on => $due_on # unix timestamp
  1104. };
  1105. my $result = $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  1106. return $result;
  1107. }
  1108. =head2 B<deleteMilestone (milestone_id)>
  1109. Deletes specified milestone.
  1110. =over 4
  1111. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  1112. =back
  1113. Returns BOOLEAN.
  1114. $tr->deleteMilestone(86);
  1115. =cut
  1116. sub deleteMilestone {
  1117. state $check = compile(Object, Int);
  1118. my ($self,$milestone_id) = $check->(@_);
  1119. my $result = $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  1120. return $result;
  1121. }
  1122. =head2 B<getMilestones (project_id)>
  1123. Get milestones for some project.
  1124. =over 4
  1125. =item INTEGER C<PROJECT ID> - ID of parent project.
  1126. =back
  1127. Returns ARRAYREF of milestone definition HASHREFs.
  1128. $tr->getMilestones(8);
  1129. =cut
  1130. sub getMilestones {
  1131. state $check = compile(Object, Int);
  1132. my ($self,$project_id) = $check->(@_);
  1133. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  1134. }
  1135. =head2 B<getMilestoneByName (project_id,name)>
  1136. Gets specified milestone by name.
  1137. =over 4
  1138. =item INTEGER C<PROJECT ID> - ID of parent project.
  1139. =item STRING C<NAME> - Name of milestone.
  1140. =back
  1141. Returns milestone definition HASHREF.
  1142. $tr->getMilestoneByName(8,'whee');
  1143. =cut
  1144. sub getMilestoneByName {
  1145. state $check = compile(Object, Int, Str);
  1146. my ($self,$project_id,$name) = $check->(@_);
  1147. my $milestones = $self->getMilestones($project_id);
  1148. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  1149. foreach my $milestone (@$milestones) {
  1150. return $milestone if $milestone->{'name'} eq $name;
  1151. }
  1152. return 0;
  1153. }
  1154. =head2 B<getMilestoneByID (milestone_id)>
  1155. Gets specified milestone by ID.
  1156. =over 4
  1157. =item INTEGER C<MILESTONE ID> - ID of milestone.
  1158. =back
  1159. Returns milestone definition HASHREF.
  1160. $tr->getMilestoneByID(2);
  1161. =cut
  1162. sub getMilestoneByID {
  1163. state $check = compile(Object, Int);
  1164. my ($self,$milestone_id) = $check->(@_);
  1165. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  1166. }
  1167. =head1 TEST METHODS
  1168. =head2 B<getTests (run_id,status_ids,assignedto_ids)>
  1169. Get tests for some run. Optionally filter by provided status_ids and assigned_to ids.
  1170. =over 4
  1171. =item INTEGER C<RUN ID> - ID of parent run.
  1172. =item ARRAYREF C<STATUS IDS> (optional) - IDs of relevant test statuses to filter by. Get with getPossibleTestStatuses.
  1173. =item ARRAYREF C<ASSIGNEDTO IDS> (optional) - IDs of users assigned to test to filter by. Get with getUsers.
  1174. =back
  1175. Returns ARRAYREF of test definition HASHREFs.
  1176. $tr->getTests(8,[1,2,3],[2]);
  1177. =cut
  1178. sub getTests {
  1179. state $check = compile(Object, Int, Optional[Maybe[ArrayRef[Int]]], Optional[Maybe[ArrayRef[Int]]]);
  1180. my ($self,$run_id,$status_ids,$assignedto_ids) = $check->(@_);
  1181. my $query_string = '';
  1182. $query_string = '&status_id='.join(',',@$status_ids) if defined($status_ids) && scalar(@$status_ids);
  1183. my $results = $self->_doRequest("index.php?/api/v2/get_tests/$run_id$query_string");
  1184. @$results = grep {my $aid = $_->{'assignedto_id'}; grep {defined($aid) && $aid == $_} @$assignedto_ids} @$results if defined($assignedto_ids) && scalar(@$assignedto_ids);
  1185. return $results;
  1186. }
  1187. =head2 B<getTestByName (run_id,name)>
  1188. Gets specified test by name.
  1189. =over 4
  1190. =item INTEGER C<RUN ID> - ID of parent run.
  1191. =item STRING C<NAME> - Name of milestone.
  1192. =back
  1193. Returns test definition HASHREF.
  1194. $tr->getTestByName(36,'wheeTest');
  1195. =cut
  1196. sub getTestByName {
  1197. state $check = compile(Object, Int, Str);
  1198. my ($self,$run_id,$name) = $check->(@_);
  1199. my $tests = $self->getTests($run_id);
  1200. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  1201. foreach my $test (@$tests) {
  1202. return $test if $test->{'title'} eq $name;
  1203. }
  1204. return 0;
  1205. }
  1206. =head2 B<getTestByID (test_id)>
  1207. Gets specified test by ID.
  1208. =over 4
  1209. =item INTEGER C<TEST ID> - ID of test.
  1210. =back
  1211. Returns test definition HASHREF.
  1212. $tr->getTestByID(222222);
  1213. =cut
  1214. sub getTestByID {
  1215. state $check = compile(Object, Int);
  1216. my ($self,$test_id) = $check->(@_);
  1217. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  1218. }
  1219. =head2 B<getTestResultFields()>
  1220. Gets custom fields that can be set for tests.
  1221. Returns ARRAYREF of result definition HASHREFs.
  1222. =cut
  1223. sub getTestResultFields {
  1224. state $check = compile(Object);
  1225. my ($self) = $check->(@_);
  1226. return $self->{'tr_fields'} if defined($self->{'tr_fields'}); #cache
  1227. $self->{'tr_fields'} = $self->_doRequest('index.php?/api/v2/get_result_fields');
  1228. return $self->{'tr_fields'};
  1229. }
  1230. =head2 B<getTestResultFieldByName(SYSTEM_NAME,PROJECT_ID)>
  1231. Gets a test result field by it's system name. Optionally filter by project ID.
  1232. =over 4
  1233. =item B<SYSTEM NAME> - STRING: system name of a result field.
  1234. =item B<PROJECT ID> - INTEGER (optional): Filter by whether or not the field is enabled for said project
  1235. =back
  1236. =cut
  1237. sub getTestResultFieldByName {
  1238. state $check = compile(Object, Str, Optional[Maybe[Int]]);
  1239. my ($self,$system_name,$project_id) = $check->(@_);
  1240. my @candidates = grep { $_->{'name'} eq $system_name} @{$self->getTestResultFields()};
  1241. return 0 if !scalar(@candidates); #No such name
  1242. return -1 if ref($candidates[0]) ne 'HASH';
  1243. return -2 if ref($candidates[0]->{'configs'}) ne 'ARRAY' && !scalar(@{$candidates[0]->{'configs'}}); #bogofilter
  1244. #Give it to the user
  1245. my $ret = $candidates[0]; #copy/save for later
  1246. return $ret if !defined($project_id);
  1247. #Filter by project ID
  1248. foreach my $config (@{$candidates[0]->{'configs'}}) {
  1249. return $ret if ( grep { $_ == $project_id} @{ $config->{'context'}->{'project_ids'} } )
  1250. }
  1251. return -3;
  1252. }
  1253. =head2 B<getPossibleTestStatuses()>
  1254. Gets all possible statuses a test can be set to.
  1255. Returns ARRAYREF of status definition HASHREFs.
  1256. =cut
  1257. sub getPossibleTestStatuses {
  1258. state $check = compile(Object);
  1259. my ($self) = $check->(@_);
  1260. return $self->_doRequest('index.php?/api/v2/get_statuses');
  1261. }
  1262. =head2 statusNamesToIds(names)
  1263. Convenience method to translate a list of statuses to TestRail status IDs.
  1264. The names referred to here are 'internal names' rather than the labels shown in TestRail.
  1265. =over 4
  1266. =item ARRAY C<NAMES> - Array of status names to translate to IDs.
  1267. =back
  1268. Returns ARRAY of status IDs in the same order as the status names passed.
  1269. Throws an exception in the case of one (or more) of the names not corresponding to a valid test status.
  1270. =cut
  1271. sub statusNamesToIds {
  1272. state $check = compile(Object, slurpy ArrayRef[Str]);
  1273. my ($self,$names) = $check->(@_);
  1274. confess("No status names passed!") unless scalar(@$names);
  1275. my $statuses = $self->getPossibleTestStatuses();
  1276. my @ret;
  1277. foreach my $name (@$names) {
  1278. foreach my $status (@$statuses) {
  1279. if ($status->{'name'} eq $name) {
  1280. push @ret, $status->{'id'};
  1281. last;
  1282. }
  1283. }
  1284. }
  1285. confess("One or more status names provided does not exist in TestRail.") unless scalar(@$names) == scalar(@ret);
  1286. return @ret;
  1287. };
  1288. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  1289. Creates a result entry for a test.
  1290. =over 4
  1291. =item INTEGER C<TEST_ID> - ID of desired test
  1292. =item INTEGER C<STATUS_ID> - ID of desired test result status
  1293. =item STRING C<COMMENT> (optional) - Any comments about this result
  1294. =item HASHREF C<OPTIONS> (optional) - Various "Baked-In" options that can be set for test results. See TR docs for more information.
  1295. =item HASHREF C<CUSTOM OPTIONS> (optional) - Options to set for custom fields. See buildStepResults for a simple way to post up custom steps.
  1296. =back
  1297. Returns result definition HASHREF.
  1298. $options = {
  1299. elapsed => '30m 22s',
  1300. defects => ['TSR-3','BOOM-44'],
  1301. version => '6969'
  1302. };
  1303. $custom_options = {
  1304. step_results => [
  1305. {
  1306. content => 'Step 1',
  1307. expected => "Bought Groceries",
  1308. actual => "No Dinero!",
  1309. status_id => 2
  1310. },
  1311. {
  1312. content => 'Step 2',
  1313. expected => 'Ate Dinner',
  1314. actual => 'Went Hungry',
  1315. status_id => 2
  1316. }
  1317. ]
  1318. };
  1319. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  1320. =cut
  1321. sub createTestResults {
  1322. state $check = compile(Object, Int, Int, Optional[Maybe[Str]], Optional[Maybe[HashRef]], Optional[Maybe[HashRef]]);
  1323. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = $check->(@_);
  1324. my $stuff = {
  1325. status_id => $status_id,
  1326. comment => $comment
  1327. };
  1328. #Handle options
  1329. if (defined($opts) && reftype($opts) eq 'HASH') {
  1330. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  1331. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  1332. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  1333. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  1334. }
  1335. #Handle custom fields
  1336. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  1337. foreach my $field (keys(%$custom_fields)) {
  1338. $stuff->{"custom_$field"} = $custom_fields->{$field};
  1339. }
  1340. }
  1341. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  1342. }
  1343. =head2 bulkAddResults(run_id,results)
  1344. Add multiple results to a run, where each result is a HASHREF with keys as outlined in the get_results API call documentation.
  1345. =over 4
  1346. =item INTEGER C<RUN_ID> - ID of desired run to add results to
  1347. =item ARRAYREF C<RESULTS> - Array of result HASHREFs to upload.
  1348. =back
  1349. Returns ARRAYREF of result definition HASHREFs.
  1350. =cut
  1351. sub bulkAddResults {
  1352. state $check = compile(Object, Int, ArrayRef[HashRef]);
  1353. my ($self,$run_id, $results) = $check->(@_);
  1354. return $self->_doRequest("index.php?/api/v2/add_results/$run_id", 'POST', { 'results' => $results });
  1355. }
  1356. =head2 B<getTestResults(test_id,limit,offset)>
  1357. Get the recorded results for desired test, limiting output to 'limit' entries.
  1358. =over 4
  1359. =item INTEGER C<TEST_ID> - ID of desired test
  1360. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1361. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing result set at.
  1362. =back
  1363. Returns ARRAYREF of result definition HASHREFs.
  1364. =cut
  1365. sub getTestResults {
  1366. state $check = compile(Object, Int, Optional[Maybe[Int]], Optional[Maybe[Int]]);
  1367. my ($self,$test_id,$limit,$offset) = $check->(@_);
  1368. my $url = "index.php?/api/v2/get_results/$test_id";
  1369. $url .= "&limit=$limit" if $limit;
  1370. $url .= "&offset=$offset" if defined($offset);
  1371. return $self->_doRequest($url);
  1372. }
  1373. =head1 CONFIGURATION METHODS
  1374. =head2 B<getConfigurationGroups(project_id)>
  1375. Gets the available configuration groups for a project, with their configurations as children.
  1376. Basically a direct wrapper of The 'get_configs' api call, with caching tacked on.
  1377. =over 4
  1378. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1379. =back
  1380. Returns ARRAYREF of configuration group definition HASHREFs.
  1381. =cut
  1382. sub getConfigurationGroups {
  1383. state $check = compile(Object, Int);
  1384. my ($self,$project_id) = $check->(@_);
  1385. my $url = "index.php?/api/v2/get_configs/$project_id";
  1386. return $self->{'configurations'}->{$project_id} if $self->{'configurations'}->{$project_id}; #cache this since we can't change it with the API
  1387. $self->{'configurations'}->{$project_id} = $self->_doRequest($url);
  1388. return $self->{'configurations'}->{$project_id};
  1389. }
  1390. =head2 B<getConfigurations(project_id)>
  1391. Gets the available configurations for a project.
  1392. Mostly for convenience (no need to write a boilerplate loop over the groups).
  1393. =over 4
  1394. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1395. =back
  1396. Returns ARRAYREF of configuration definition HASHREFs.
  1397. Returns result of getConfigurationGroups (likely -500) in the event that call fails.
  1398. =cut
  1399. sub getConfigurations {
  1400. state $check = compile(Object, Int);
  1401. my ($self,$project_id) = $check->(@_);
  1402. my $cgroups = $self->getConfigurationGroups($project_id);
  1403. my $configs = [];
  1404. return $cgroups unless (reftype($cgroups) || 'undef') eq 'ARRAY';
  1405. foreach my $cfg (@$cgroups) {
  1406. push(@$configs, @{$cfg->{'configs'}});
  1407. }
  1408. return $configs;
  1409. }
  1410. =head2 B<translateConfigNamesToIds(project_id,configs)>
  1411. Transforms a list of configuration names into a list of config IDs.
  1412. =over 4
  1413. =item INTEGER C<PROJECT_ID> - Relevant project ID for configs.
  1414. =item ARRAYREF C<CONFIGS> - Array ref of config names
  1415. =back
  1416. Returns ARRAYREF of configuration names, with undef values for unknown configuration names.
  1417. =cut
  1418. sub translateConfigNamesToIds {
  1419. state $check = compile(Object, Int, ArrayRef[Str]);
  1420. my ($self,$project_id,$configs) = $check->(@_);
  1421. return [] if !scalar(@$configs);
  1422. my $existing_configs = $self->getConfigurations($project_id);
  1423. return map {undef} @$configs if (reftype($existing_configs) || 'undef') ne 'ARRAY';
  1424. my @ret = map {my $name = $_; my @candidates = grep { $name eq $_->{'name'} } @$existing_configs; scalar(@candidates) ? $candidates[0]->{'id'} : undef } @$configs;
  1425. return \@ret;
  1426. }
  1427. =head1 STATIC METHODS
  1428. =head2 B<buildStepResults(content,expected,actual,status_id)>
  1429. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  1430. =over 4
  1431. =item STRING C<CONTENT> (optional) - The step itself.
  1432. =item STRING C<EXPECTED> (optional) - Expected result of test step.
  1433. =item STRING C<ACTUAL> (optional) - Actual result of test step
  1434. =item INTEGER C<STATUS ID> (optional) - Status ID of result
  1435. =back
  1436. =cut
  1437. #Convenience method for building stepResults
  1438. sub buildStepResults {
  1439. state $check = compile(Str, Str, Str, Int);
  1440. my ($content,$expected,$actual,$status_id) = $check->(@_);
  1441. return {
  1442. content => $content,
  1443. expected => $expected,
  1444. actual => $actual,
  1445. status_id => $status_id
  1446. };
  1447. }
  1448. 1;
  1449. __END__
  1450. =head1 SEE ALSO
  1451. L<HTTP::Request>
  1452. L<LWP::UserAgent>
  1453. L<JSON::MaybeXS>
  1454. L<http://docs.gurock.com/testrail-api2/start>
  1455. =head1 SPECIAL THANKS
  1456. Thanks to cPanel Inc, for graciously funding the creation of this module.