API.pm 61 KB

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