API.pm 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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::XS;
  24. use HTTP::Request;
  25. use LWP::UserAgent;
  26. use Types::Serialiser; #Not necesarily shared by JSON::XS on all platforms
  27. use Data::Validate::URI qw{is_uri};
  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. default_request => undef,
  59. browser => new LWP::UserAgent()
  60. };
  61. #Create default request to pass on to LWP::UserAgent
  62. $self->{'default_request'} = new HTTP::Request();
  63. $self->{'default_request'}->authorization_basic($user,$pass);
  64. bless( $self, $class );
  65. return $self if $self->debug; #For easy class testing without mocks
  66. #Manually do the get_users call to check HTTP status
  67. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  68. confess "Error: network unreachable" if !defined($res);
  69. if ( (reftype($res) || 'undef') ne 'ARRAY') {
  70. confess "Unexpected return from _doRequest: $res" if !looks_like_number($res);
  71. confess "Could not communicate with TestRail Server! Check that your URI is correct, and your TestRail installation is functioning correctly." if $res == -500;
  72. confess "Could not list testRail users! Check that your TestRail installation has it's API enabled, and your credentials are correct" if $res == -403;
  73. confess "Bad user credentials!" if $res == -401;
  74. confess "HTTP error $res encountered while communicating with TestRail server. Resolve issue and try again." if !$res;
  75. confess "Unknown error occurred: $res";
  76. }
  77. confess "No users detected on TestRail Install! Check that your API is functioning correctly." if !scalar(@$res);
  78. $self->{'user_cache'} = $res;
  79. #Check that the User is actually in the list
  80. my $usr = $self->getUserByEmail($user);
  81. confess "Could not find your TestRail user on the system!" if !(reftype($usr) eq 'HASH');
  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. #Data sent is JSON
  109. my $content = $data ? encode_json($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 decode_json($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 ? Types::Serialiser::true : Types::Serialiser::false
  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 => $case_ids ? Types::Serialiser::false : Types::Serialiser::true,
  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,'gravy');
  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 PLAN METHODS
  752. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  753. Create a test plan.
  754. =over 4
  755. =item INTEGER C<PROJECT ID> - ID of parent project.
  756. =item STRING C<NAME> - Name of plan
  757. =item STRING C<DESCRIPTION> (optional) - Description of plan
  758. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  759. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  760. =back
  761. Returns test plan definition HASHREF, or false on failure.
  762. $entries = {
  763. suite_id => 345,
  764. include_all => Types::Serialiser::true,
  765. assignedto_id => 1
  766. }
  767. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  768. =cut
  769. sub createPlan {
  770. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = @_;
  771. confess("Object methods must be called by an instance") unless ref($self);
  772. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  773. confess("Plan name must be string") unless $self->_checkString($name);
  774. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  775. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  776. confess("Entries must be ARRAYREF") unless !defined($entries) || (reftype($entries) || 'undef') eq 'ARRAY';
  777. my $stuff = {
  778. name => $name,
  779. description => $desc,
  780. milestone_id => $milestone_id,
  781. entries => $entries
  782. };
  783. my $result = $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  784. return $result;
  785. }
  786. =head2 B<deletePlan (plan_id)>
  787. Deletes specified plan.
  788. =over 4
  789. =item INTEGER C<PLAN ID> - ID of plan to delete.
  790. =back
  791. Returns BOOLEAN.
  792. $tr->deletePlan(8675309);
  793. =cut
  794. sub deletePlan {
  795. my ($self,$plan_id) = @_;
  796. confess("Object methods must be called by an instance") unless ref($self);
  797. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  798. my $result = $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  799. return $result;
  800. }
  801. =head2 B<getPlans (project_id)>
  802. Deletes specified plan.
  803. =over 4
  804. =item INTEGER C<PROJECT ID> - ID of parent project.
  805. =back
  806. Returns ARRAYREF of plan definition HASHREFs.
  807. $tr->getPlans(8);
  808. =cut
  809. sub getPlans {
  810. my ($self,$project_id) = @_;
  811. confess("Object methods must be called by an instance") unless ref($self);
  812. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  813. return $self->_doRequest("index.php?/api/v2/get_plans/$project_id");
  814. }
  815. =head2 B<getPlanByName (project_id,name)>
  816. Gets specified plan by name.
  817. =over 4
  818. =item INTEGER C<PROJECT ID> - ID of parent project.
  819. =item STRING C<NAME> - Name of test plan.
  820. =back
  821. Returns plan definition HASHREF.
  822. $tr->getPlanByName(8,'GosPlan');
  823. =cut
  824. sub getPlanByName {
  825. my ($self,$project_id,$name) = @_;
  826. confess("Object methods must be called by an instance") unless ref($self);
  827. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  828. confess("Plan name must be string") unless $self->_checkString($name);
  829. my $plans = $self->getPlans($project_id);
  830. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  831. foreach my $plan (@$plans) {
  832. return $plan if $plan->{'name'} eq $name;
  833. }
  834. return 0;
  835. }
  836. =head2 B<getPlanByID (plan_id)>
  837. Gets specified plan by ID.
  838. =over 4
  839. =item INTEGER C<PLAN ID> - ID of plan.
  840. =back
  841. Returns plan definition HASHREF.
  842. $tr->getPlanByID(2);
  843. =cut
  844. sub getPlanByID {
  845. my ($self,$plan_id) = @_;
  846. confess("Object methods must be called by an instance") unless ref($self);
  847. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  848. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  849. }
  850. =head1 MILESTONE METHODS
  851. =head2 B<createMilestone (project_id,name,description,due_on)>
  852. Create a milestone.
  853. =over 4
  854. =item INTEGER C<PROJECT ID> - ID of parent project.
  855. =item STRING C<NAME> - Name of milestone
  856. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  857. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  858. =back
  859. Returns milestone definition HASHREF, or false on failure.
  860. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  861. =cut
  862. sub createMilestone {
  863. my ($self,$project_id,$name,$desc,$due_on) = @_;
  864. confess("Object methods must be called by an instance") unless ref($self);
  865. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  866. confess("Name must be string") unless $self->_checkString($name);
  867. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  868. confess("Due on must be unix time stamp (integer)") unless !defined($due_on) || $self->_checkInteger($due_on);
  869. my $stuff = {
  870. name => $name,
  871. description => $desc,
  872. due_on => $due_on # unix timestamp
  873. };
  874. my $result = $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  875. return $result;
  876. }
  877. =head2 B<deleteMilestone (milestone_id)>
  878. Deletes specified milestone.
  879. =over 4
  880. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  881. =back
  882. Returns BOOLEAN.
  883. $tr->deleteMilestone(86);
  884. =cut
  885. sub deleteMilestone {
  886. my ($self,$milestone_id) = @_;
  887. confess("Object methods must be called by an instance") unless ref($self);
  888. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  889. my $result = $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  890. return $result;
  891. }
  892. =head2 B<getMilestones (project_id)>
  893. Get milestones for some project.
  894. =over 4
  895. =item INTEGER C<PROJECT ID> - ID of parent project.
  896. =back
  897. Returns ARRAYREF of milestone definition HASHREFs.
  898. $tr->getMilestones(8);
  899. =cut
  900. sub getMilestones {
  901. my ($self,$project_id) = @_;
  902. confess("Object methods must be called by an instance") unless ref($self);
  903. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  904. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  905. }
  906. =head2 B<getMilestoneByName (project_id,name)>
  907. Gets specified milestone by name.
  908. =over 4
  909. =item INTEGER C<PROJECT ID> - ID of parent project.
  910. =item STRING C<NAME> - Name of milestone.
  911. =back
  912. Returns milestone definition HASHREF.
  913. $tr->getMilestoneByName(8,'whee');
  914. =cut
  915. sub getMilestoneByName {
  916. my ($self,$project_id,$name) = @_;
  917. confess("Object methods must be called by an instance") unless ref($self);
  918. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  919. confess("Milestone name must be string") unless $self->_checkString($name);
  920. my $milestones = $self->getMilestones($project_id);
  921. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  922. foreach my $milestone (@$milestones) {
  923. return $milestone if $milestone->{'name'} eq $name;
  924. }
  925. return 0;
  926. }
  927. =head2 B<getMilestoneByID (milestone_id)>
  928. Gets specified milestone by ID.
  929. =over 4
  930. =item INTEGER C<MILESTONE ID> - ID of milestone.
  931. =back
  932. Returns milestone definition HASHREF.
  933. $tr->getMilestoneByID(2);
  934. =cut
  935. sub getMilestoneByID {
  936. my ($self,$milestone_id) = @_;
  937. confess("Object methods must be called by an instance") unless ref($self);
  938. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  939. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  940. }
  941. =head1 TEST METHODS
  942. =head2 B<getTests (run_id)>
  943. Get tests for some run.
  944. =over 4
  945. =item INTEGER C<RUN ID> - ID of parent run.
  946. =back
  947. Returns ARRAYREF of test definition HASHREFs.
  948. $tr->getTests(8);
  949. =cut
  950. sub getTests {
  951. my ($self,$run_id) = @_;
  952. confess("Object methods must be called by an instance") unless ref($self);
  953. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  954. return $self->_doRequest("index.php?/api/v2/get_tests/$run_id");
  955. }
  956. =head2 B<getTestByName (run_id,name)>
  957. Gets specified test by name.
  958. =over 4
  959. =item INTEGER C<RUN ID> - ID of parent run.
  960. =item STRING C<NAME> - Name of milestone.
  961. =back
  962. Returns test definition HASHREF.
  963. $tr->getTestByName(36,'wheeTest');
  964. =cut
  965. sub getTestByName {
  966. my ($self,$run_id,$name) = @_;
  967. confess("Object methods must be called by an instance") unless ref($self);
  968. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  969. confess("Test name must be string") unless $self->_checkString($name);
  970. my $tests = $self->getTests($run_id);
  971. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  972. foreach my $test (@$tests) {
  973. return $test if $test->{'title'} eq $name;
  974. }
  975. return 0;
  976. }
  977. =head2 B<getTestByID (test_id)>
  978. Gets specified test by ID.
  979. =over 4
  980. =item INTEGER C<TEST ID> - ID of test.
  981. =back
  982. Returns test definition HASHREF.
  983. $tr->getTestByID(222222);
  984. =cut
  985. sub getTestByID {
  986. my ($self,$test_id) = @_;
  987. confess("Object methods must be called by an instance") unless ref($self);
  988. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  989. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  990. }
  991. =head2 B<getTestResultFields()>
  992. Gets custom fields that can be set for tests.
  993. Returns ARRAYREF of result definition HASHREFs.
  994. =cut
  995. sub getTestResultFields {
  996. my $self = shift;
  997. confess("Object methods must be called by an instance") unless ref($self);
  998. return $self->_doRequest('index.php?/api/v2/get_result_fields');
  999. }
  1000. =head2 B<getPossibleTestStatuses()>
  1001. Gets all possible statuses a test can be set to.
  1002. Returns ARRAYREF of status definition HASHREFs.
  1003. =cut
  1004. sub getPossibleTestStatuses {
  1005. my $self = shift;
  1006. confess("Object methods must be called by an instance") unless ref($self);
  1007. return $self->_doRequest('index.php?/api/v2/get_statuses');
  1008. }
  1009. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  1010. Creates a result entry for a test.
  1011. =over 4
  1012. =item INTEGER C<TEST_ID> - ID of desired test
  1013. =item INTEGER C<STATUS_ID> - ID of desired test result status
  1014. =item STRING C<COMMENT> (optional) - Any comments about this result
  1015. =item HASHREF C<OPTIONS> (optional) - Various "Baked-In" options that can be set for test results. See TR docs for more information.
  1016. =item HASHREF C<CUSTOM OPTIONS> (optional) - Options to set for custom fields. See L<TestRail::API::buildStepResults> for a simple way to post up custom steps.
  1017. =back
  1018. Returns result definition HASHREF.
  1019. $options = {
  1020. elapsed => '30m 22s',
  1021. defects => ['TSR-3','BOOM-44'],
  1022. version => '6969'
  1023. };
  1024. $custom_options = {
  1025. step_results => [
  1026. {
  1027. content => 'Step 1',
  1028. expected => "Bought Groceries",
  1029. actual => "No Dinero!",
  1030. status_id => 2
  1031. },
  1032. {
  1033. content => 'Step 2',
  1034. expected => 'Ate Dinner',
  1035. actual => 'Went Hungry',
  1036. status_id => 2
  1037. }
  1038. ]
  1039. };
  1040. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  1041. =cut
  1042. sub createTestResults {
  1043. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = @_;
  1044. confess("Object methods must be called by an instance") unless ref($self);
  1045. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  1046. confess("Status ID must be integer") unless $self->_checkInteger($status_id);
  1047. confess("Comment must be string") unless !defined($comment) || $self->_checkString($comment);
  1048. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') eq 'HASH';
  1049. confess("Custom Options must be HASHREF") unless !defined($custom_fields) || (reftype($custom_fields) || 'undef') eq 'HASH';
  1050. my $stuff = {
  1051. status_id => $status_id,
  1052. comment => $comment
  1053. };
  1054. #Handle options
  1055. if (defined($opts) && reftype($opts) eq 'HASH') {
  1056. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  1057. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  1058. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  1059. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  1060. }
  1061. #Handle custom fields
  1062. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  1063. foreach my $field (keys(%$custom_fields)) {
  1064. $stuff->{"custom_$field"} = $custom_fields->{$field};
  1065. }
  1066. }
  1067. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  1068. }
  1069. =head2 B<getTestResults(test_id,limit)>
  1070. Get the recorded results for desired test, limiting output to 'limit' entries.
  1071. =over 4
  1072. =item INTEGER C<TEST_ID> - ID of desired test
  1073. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1074. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing resultset at.
  1075. =back
  1076. Returns ARRAYREF of result definition HASHREFs.
  1077. =cut
  1078. sub getTestResults {
  1079. my ($self,$test_id,$limit,$offset) = @_;
  1080. confess("Object methods must be called by an instance") unless ref($self);
  1081. confess("Test ID must be positive integer") unless $self->_checkInteger($test_id);
  1082. confess("Result limitation must be positive integer") unless !defined($limit) || ($self->_checkInteger($limit) && $limit > 0);
  1083. confess("Result offset must be integer") unless !defined($offset) || $self->_checkInteger($offset);
  1084. my $url = "index.php?/api/v2/get_results/$test_id";
  1085. $url .= "&limit=$limit" if defined($limit);
  1086. $url .= "&offset=$offset" if defined($offset);
  1087. return $self->_doRequest($url);
  1088. }
  1089. =head1 STATIC METHODS
  1090. =head2 B<buildStepResults(content,expected,actual,status_id)>
  1091. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  1092. =over 4
  1093. =item STRING C<CONTENT> (optional) - The step itself.
  1094. =item STRING C<EXPECTED> (optional) - Expected result of test step.
  1095. =item STRING C<ACTUAL> (optional) - Actual result of test step
  1096. =item INTEGER C<STATUS ID> (optional) - Status ID of result
  1097. =back
  1098. =cut
  1099. #Convenience method for building stepResults
  1100. sub buildStepResults {
  1101. my ($content,$expected,$actual,$status_id) = @_;
  1102. return {
  1103. content => $content,
  1104. expected => $expected,
  1105. actual => $actual,
  1106. status_id => $status_id
  1107. };
  1108. }
  1109. #Type checks
  1110. sub _checkInteger {
  1111. shift;
  1112. my $integer = shift;
  1113. return ( defined $integer && looks_like_number($integer) && int($integer) == $integer );
  1114. }
  1115. sub _checkString {
  1116. shift;
  1117. my $str = shift;
  1118. return ( defined($str) && !ref($str) );
  1119. }
  1120. 1;
  1121. __END__
  1122. =head1 SEE ALSO
  1123. L<HTTP::Request>
  1124. L<LWP::UserAgent>
  1125. L<JSON::XS>
  1126. L<http://docs.gurock.com/testrail-api2/start>
  1127. =head1 SPECIAL THANKS
  1128. Thanks to cPanel Inc, for graciously funding the creation of this module.