API.pm 54 KB

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