API.pm 57 KB

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