API.pm 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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. $self->getUsers() if !defined($self->{'user_cache'});
  164. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  165. foreach my $usr (@{$self->{'user_cache'}}) {
  166. return $usr if $usr->{'id'} == $user;
  167. }
  168. return 0;
  169. }
  170. sub getUserByName {
  171. my ($self,$user) = @_;
  172. confess("Object methods must be called by an instance") unless ref($self);
  173. $self->getUsers() if !defined($self->{'user_cache'});
  174. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  175. foreach my $usr (@{$self->{'user_cache'}}) {
  176. return $usr if $usr->{'name'} eq $user;
  177. }
  178. return 0;
  179. }
  180. sub getUserByEmail {
  181. my ($self,$user) = @_;
  182. confess("Object methods must be called by an instance") unless ref($self);
  183. $self->getUsers() if !defined($self->{'user_cache'});
  184. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  185. foreach my $usr (@{$self->{'user_cache'}}) {
  186. return $usr if $usr->{'email'} eq $user;
  187. }
  188. return 0;
  189. }
  190. =head1 PROJECT METHODS
  191. =head2 B<createProject (name, [description,send_announcement])>
  192. Creates new Project (Database of testsuites/tests).
  193. Optionally specify an announcement to go out to the users.
  194. Requires TestRail admin login.
  195. =over 4
  196. =item STRING C<NAME> - Desired name of project.
  197. =item STRING C<DESCRIPTION> (optional) - Description of project. Default value is 'res ipsa loquiter'.
  198. =item BOOLEAN C<SEND ANNOUNCEMENT> (optional) - Whether to confront users with an announcement about your awesome project on next login. Default false.
  199. =back
  200. Returns project definition HASHREF on success, false otherwise.
  201. $tl->createProject('Widgetronic 4000', 'Tests for the whiz-bang new product', true);
  202. =cut
  203. sub createProject {
  204. my ($self,$name,$desc,$announce) = @_;
  205. confess("Object methods must be called by an instance") unless ref($self);
  206. $desc //= 'res ipsa loquiter';
  207. $announce //= 0;
  208. my $input = {
  209. name => $name,
  210. announcement => $desc,
  211. show_announcement => $announce ? Types::Serialiser::true : Types::Serialiser::false
  212. };
  213. my $result = $self->_doRequest('index.php?/api/v2/add_project','POST',$input);
  214. return $result;
  215. }
  216. =head2 B<deleteProject (id)>
  217. Deletes specified project by ID.
  218. Requires TestRail admin login.
  219. =over 4
  220. =item STRING C<NAME> - Desired name of project.
  221. =back
  222. Returns BOOLEAN.
  223. $success = $tl->deleteProject(1);
  224. =cut
  225. sub deleteProject {
  226. my ($self,$proj) = @_;
  227. confess("Object methods must be called by an instance") unless ref($self);
  228. my $result = $self->_doRequest('index.php?/api/v2/delete_project/'.$proj,'POST');
  229. return $result;
  230. }
  231. =head2 B<getProjects ()>
  232. Get all available projects
  233. Returns array of project definition HASHREFs, false otherwise.
  234. $projects = $tl->getProjects;
  235. =cut
  236. sub getProjects {
  237. my $self = shift;
  238. confess("Object methods must be called by an instance") unless ref($self);
  239. my $result = $self->_doRequest('index.php?/api/v2/get_projects');
  240. #Save state for future use, if needed
  241. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  242. $self->{'testtree'} = $result;
  243. #Note that it's a project for future reference by recursive tree search
  244. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  245. foreach my $pj (@{$result}) {
  246. $pj->{'type'} = 'project';
  247. }
  248. return $result;
  249. }
  250. =head2 B<getProjectByName ($project)>
  251. Gets some project definition hash by it's name
  252. =over 4
  253. =item STRING C<PROJECT> - desired project
  254. =back
  255. Returns desired project def HASHREF, false otherwise.
  256. $project = $tl->getProjectByName('FunProject');
  257. =cut
  258. sub getProjectByName {
  259. my ($self,$project) = @_;
  260. confess("Object methods must be called by an instance") unless ref($self);
  261. confess "No project provided." unless $project;
  262. #See if we already have the project list...
  263. my $projects = $self->{'testtree'};
  264. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  265. $projects = $self->getProjects() unless scalar(@$projects);
  266. #Search project list for project
  267. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  268. for my $candidate (@$projects) {
  269. return $candidate if ($candidate->{'name'} eq $project);
  270. }
  271. return 0;
  272. }
  273. =head2 B<getProjectByID ($project)>
  274. Gets some project definition hash by it's ID
  275. =over 4
  276. =item INTEGER C<PROJECT> - desired project
  277. =back
  278. Returns desired project def HASHREF, false otherwise.
  279. $projects = $tl->getProjectByID(222);
  280. =cut
  281. sub getProjectByID {
  282. my ($self,$project) = @_;
  283. confess("Object methods must be called by an instance") unless ref($self);
  284. confess "No project provided." unless $project;
  285. #See if we already have the project list...
  286. my $projects = $self->{'testtree'};
  287. $projects = $self->getProjects() unless scalar(@$projects);
  288. #Search project list for project
  289. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  290. for my $candidate (@$projects) {
  291. return $candidate if ($candidate->{'id'} eq $project);
  292. }
  293. return 0;
  294. }
  295. =head1 TESTSUITE METHODS
  296. =head2 B<createTestSuite (project_id, name, [description])>
  297. Creates new TestSuite (folder of tests) in the database of test specifications under given project id having given name and details.
  298. =over 4
  299. =item INTEGER C<PROJECT ID> - ID of project this test suite should be under.
  300. =item STRING C<NAME> - Desired name of test suite.
  301. =item STRING C<DESCRIPTION> (optional) - Description of test suite. Default value is 'res ipsa loquiter'.
  302. =back
  303. Returns TS definition HASHREF on success, false otherwise.
  304. $tl->createTestSuite(1, 'broken tests', 'Tests that should be reviewed');
  305. =cut
  306. sub createTestSuite {
  307. my ($self,$project_id,$name,$details) = @_;
  308. confess("Object methods must be called by an instance") unless ref($self);
  309. $details ||= 'res ipsa loquiter';
  310. my $input = {
  311. name => $name,
  312. description => $details
  313. };
  314. my $result = $self->_doRequest('index.php?/api/v2/add_suite/'.$project_id,'POST',$input);
  315. return $result;
  316. }
  317. =head2 B<deleteTestSuite (suite_id)>
  318. Deletes specified testsuite.
  319. =over 4
  320. =item INTEGER C<SUITE ID> - ID of testsuite to delete.
  321. =back
  322. Returns BOOLEAN.
  323. $tl->deleteTestSuite(1);
  324. =cut
  325. sub deleteTestSuite {
  326. my ($self,$suite_id) = @_;
  327. confess("Object methods must be called by an instance") unless ref($self);
  328. my $result = $self->_doRequest('index.php?/api/v2/delete_suite/'.$suite_id,'POST');
  329. return $result;
  330. }
  331. =head2 B<getTestSuites (project_id)>
  332. Gets the testsuites for a project
  333. =over 4
  334. =item STRING C<PROJECT ID> - desired project's ID
  335. =back
  336. Returns ARRAYREF of testsuite definition HASHREFs, 0 on error.
  337. $suites = $tl->getTestSuites(123);
  338. =cut
  339. sub getTestSuites {
  340. my ($self,$proj) = @_;
  341. confess("Object methods must be called by an instance") unless ref($self);
  342. return $self->_doRequest('index.php?/api/v2/get_suites/'.$proj);
  343. }
  344. =head2 B<getTestSuiteByName (project_id,testsuite_name)>
  345. Gets the testsuite that matches the given name inside of given project.
  346. =over 4
  347. =item STRING C<PROJECT ID> - ID of project holding this testsuite
  348. =item STRING C<TESTSUITE NAME> - desired parent testsuite name
  349. =back
  350. Returns desired testsuite definition HASHREF, false otherwise.
  351. $suites = $tl->getTestSuitesByName(321, 'hugSuite');
  352. =cut
  353. sub getTestSuiteByName {
  354. my ($self,$project_id,$testsuite_name) = @_;
  355. confess("Object methods must be called by an instance") unless ref($self);
  356. #TODO cache
  357. my $suites = $self->getTestSuites($project_id);
  358. return -500 if !$suites || (reftype($suites) || 'undef') ne 'ARRAY'; #No suites for project, or no project
  359. foreach my $suite (@$suites) {
  360. return $suite if $suite->{'name'} eq $testsuite_name;
  361. }
  362. return 0; #Couldn't find it
  363. }
  364. =head2 B<getTestSuiteByID (testsuite_id)>
  365. Gets the testsuite with the given ID.
  366. =over 4
  367. =item STRING C<TESTSUITE_ID> - TestSuite ID.
  368. =back
  369. Returns desired testsuite definition HASHREF, false otherwise.
  370. $tests = $tl->getTestSuiteByID(123);
  371. =cut
  372. sub getTestSuiteByID {
  373. my ($self,$testsuite_id) = @_;
  374. confess("Object methods must be called by an instance") unless ref($self);
  375. my $result = $self->_doRequest('index.php?/api/v2/get_suite/'.$testsuite_id);
  376. return $result;
  377. }
  378. =head1 SECTION METHODS
  379. =head2 B<createSection(project_id,suite_id,name,[parent_id])>
  380. Creates a section.
  381. =over 4
  382. =item INTEGER C<PROJECT ID> - Parent Project ID.
  383. =item INTEGER C<SUITE ID> - Parent TestSuite ID.
  384. =item STRING C<NAME> - desired section name.
  385. =item INTEGER C<PARENT ID> (optional) - parent section id
  386. =back
  387. Returns new section definition HASHREF, false otherwise.
  388. $section = $tr->createSection(1,1,'nugs',1);
  389. =cut
  390. sub createSection {
  391. my ($self,$project_id,$suite_id,$name,$parent_id) = @_;
  392. confess("Object methods must be called by an instance") unless ref($self);
  393. my $input = {
  394. name => $name,
  395. suite_id => $suite_id
  396. };
  397. $input->{'parent_id'} = $parent_id if $parent_id;
  398. my $result = $self->_doRequest('index.php?/api/v2/add_section/'.$project_id,'POST',$input);
  399. return $result;
  400. }
  401. =head2 B<deleteSection (section_id)>
  402. Deletes specified section.
  403. =over 4
  404. =item INTEGER C<SECTION ID> - ID of section to delete.
  405. =back
  406. Returns BOOLEAN.
  407. $tr->deleteSection(1);
  408. =cut
  409. sub deleteSection {
  410. my ($self,$section_id) = @_;
  411. confess("Object methods must be called by an instance") unless ref($self);
  412. my $result = $self->_doRequest('index.php?/api/v2/delete_section/'.$section_id,'POST');
  413. return $result;
  414. }
  415. =head2 B<getSections (project_id,suite_id)>
  416. Gets sections for a given project and suite.
  417. =over 4
  418. =item INTEGER C<PROJECT ID> - ID of parent project.
  419. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  420. =back
  421. Returns ARRAYREF of section definition HASHREFs.
  422. $tr->getSections(1,2);
  423. =cut
  424. sub getSections {
  425. my ($self,$project_id,$suite_id) = @_;
  426. confess("Object methods must be called by an instance") unless ref($self);
  427. return $self->_doRequest("index.php?/api/v2/get_sections/$project_id&suite_id=$suite_id");
  428. }
  429. =head2 B<getSectionByID (section_id)>
  430. Gets desired section.
  431. =over 4
  432. =item INTEGER C<PROJECT ID> - ID of parent project.
  433. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  434. =back
  435. Returns section definition HASHREF.
  436. $tr->getSectionByID(344);
  437. =cut
  438. sub getSectionByID {
  439. my ($self,$section_id) = @_;
  440. confess("Object methods must be called by an instance") unless ref($self);
  441. return $self->_doRequest("index.php?/api/v2/get_section/$section_id");
  442. }
  443. =head2 B<getSectionByName (project_id,suite_id,name)>
  444. Gets desired section.
  445. =over 4
  446. =item INTEGER C<PROJECT ID> - ID of parent project.
  447. =item INTEGER C<SUITE ID> - ID of suite to get section for.
  448. =item STRING C<NAME> - name of section to get
  449. =back
  450. Returns section definition HASHREF.
  451. $tr->getSectionByName(1,2,'nugs');
  452. =cut
  453. sub getSectionByName {
  454. my ($self,$project_id,$suite_id,$section_name) = @_;
  455. confess("Object methods must be called by an instance") unless ref($self);
  456. my $sections = $self->getSections($project_id,$suite_id);
  457. return -500 if !$sections || (reftype($sections) || 'undef') ne 'ARRAY';
  458. foreach my $sec (@$sections) {
  459. return $sec if $sec->{'name'} eq $section_name;
  460. }
  461. return 0;
  462. }
  463. =head1 CASE METHODS
  464. =head2 B<getCaseTypes ()>
  465. Gets possible case types.
  466. Returns ARRAYREF of case type definition HASHREFs.
  467. $tr->getCaseTypes();
  468. =cut
  469. sub getCaseTypes {
  470. my $self = shift;
  471. confess("Object methods must be called by an instance") unless ref($self);
  472. my $types = $self->_doRequest("index.php?/api/v2/get_case_types");
  473. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  474. $self->{'type_cache'} = $types if !$self->{'type_cache'}; #We can't change this with API, so assume it is static
  475. return $self->{'type_cache'};
  476. }
  477. =head2 B<getCaseTypeByName (name)>
  478. Gets case type by name.
  479. =over 4
  480. =item STRING C<NAME> - Name of desired case type
  481. =back
  482. Returns case type definition HASHREF.
  483. $tr->getCaseTypeByName();
  484. =cut
  485. sub getCaseTypeByName {
  486. #Useful for marking automated tests, etc
  487. my ($self,$name) = @_;
  488. confess("Object methods must be called by an instance") unless ref($self);
  489. my $types = $self->getCaseTypes();
  490. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  491. foreach my $type (@$types) {
  492. return $type if $type->{'name'} eq $name;
  493. }
  494. return 0;
  495. }
  496. =head2 B<createCase(section_id,title,type_id,options,extra_options)>
  497. Creates a test case.
  498. =over 4
  499. =item INTEGER C<SECTION ID> - Parent Project ID.
  500. =item STRING C<TITLE> - Case title.
  501. =item INTEGER C<TYPE_ID> - desired test type's ID.
  502. =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.
  503. =item HASHREF C<EXTRA OPTIONS> (optional) - contains priority_id, estimate, milestone_id and refs as possible keys. See TestRail API documentation for more info.
  504. =back
  505. Returns new case definition HASHREF, false otherwise.
  506. $custom_opts = {
  507. preconds => "Test harness installed",
  508. steps => "Do the needful",
  509. expected => "cubicle environment transforms into Dali painting"
  510. };
  511. $other_opts = {
  512. priority_id => 4,
  513. milestone_id => 666,
  514. estimate => '2m 45s',
  515. refs => ['TRACE-22','ON-166'] #ARRAYREF of bug IDs.
  516. }
  517. $case = $tr->createCase(1,'Do some stuff',3,$custom_opts,$other_opts);
  518. =cut
  519. sub createCase {
  520. my ($self,$section_id,$title,$type_id,$opts,$extras) = @_;
  521. confess("Object methods must be called by an instance") unless ref($self);
  522. my $stuff = {
  523. title => $title,
  524. type_id => $type_id
  525. };
  526. #Handle sort of optional but baked in options
  527. if (defined($extras) && reftype($extras) eq 'HASH') {
  528. $stuff->{'priority_id'} = $extras->{'priority_id'} if defined($extras->{'priority_id'});
  529. $stuff->{'estimate'} = $extras->{'estimate'} if defined($extras->{'estimate'});
  530. $stuff->{'milestone_id'} = $extras->{'milestone_id'} if defined($extras->{'milestone_id'});
  531. $stuff->{'refs'} = join(',',@{$extras->{'refs'}}) if defined($extras->{'refs'});
  532. }
  533. #Handle custom fields
  534. if (defined($opts) && reftype($opts) eq 'HASH') {
  535. foreach my $key (keys(%$opts)) {
  536. $stuff->{"custom_$key"} = $opts->{$key};
  537. }
  538. }
  539. my $result = $self->_doRequest("index.php?/api/v2/add_case/$section_id",'POST',$stuff);
  540. return $result;
  541. }
  542. =head2 B<deleteCase (case_id)>
  543. Deletes specified section.
  544. =over 4
  545. =item INTEGER C<CASE ID> - ID of case to delete.
  546. =back
  547. Returns BOOLEAN.
  548. $tr->deleteCase(1324);
  549. =cut
  550. sub deleteCase {
  551. my ($self,$case_id) = @_;
  552. confess("Object methods must be called by an instance") unless ref($self);
  553. my $result = $self->_doRequest("index.php?/api/v2/delete_case/$case_id",'POST');
  554. return $result;
  555. }
  556. =head2 B<getCases (project_id,suite_id,section_id)>
  557. Gets cases for provided section.
  558. =over 4
  559. =item INTEGER C<PROJECT ID> - ID of parent project.
  560. =item INTEGER C<SUITE ID> - ID of parent suite.
  561. =item INTEGER C<SECTION ID> - ID of parent section
  562. =back
  563. Returns ARRAYREF of test case definition HASHREFs.
  564. $tr->getCases(1,2,3);
  565. =cut
  566. sub getCases {
  567. my ($self,$project_id,$suite_id,$section_id) = @_;
  568. confess("Object methods must be called by an instance") unless ref($self);
  569. my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
  570. $url .= "&section_id=$section_id" if $section_id;
  571. return $self->_doRequest($url);
  572. }
  573. =head2 B<getCaseByName (project_id,suite_id,section_id,name)>
  574. Gets case by name.
  575. =over 4
  576. =item INTEGER C<PROJECT ID> - ID of parent project.
  577. =item INTEGER C<SUITE ID> - ID of parent suite.
  578. =item INTEGER C<SECTION ID> - ID of parent section.
  579. =item STRING <NAME> - Name of desired test case.
  580. =back
  581. Returns test case definition HASHREF.
  582. $tr->getCaseByName(1,2,3,'nugs');
  583. =cut
  584. sub getCaseByName {
  585. my ($self,$project_id,$suite_id,$section_id,$name) = @_;
  586. confess("Object methods must be called by an instance") unless ref($self);
  587. my $cases = $self->getCases($project_id,$suite_id,$section_id);
  588. return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
  589. foreach my $case (@$cases) {
  590. return $case if $case->{'title'} eq $name;
  591. }
  592. return 0;
  593. }
  594. =head2 B<getCaseByID (case_id)>
  595. Gets case by ID.
  596. =over 4
  597. =item INTEGER C<CASE ID> - ID of case.
  598. =back
  599. Returns test case definition HASHREF.
  600. $tr->getCaseByID(1345);
  601. =cut
  602. sub getCaseByID {
  603. my ($self,$case_id) = @_;
  604. confess("Object methods must be called by an instance") unless ref($self);
  605. return $self->_doRequest("index.php?/api/v2/get_case/$case_id");
  606. }
  607. =head1 RUN METHODS
  608. =head2 B<createRun (project_id)>
  609. Create a run.
  610. =over 4
  611. =item INTEGER C<PROJECT ID> - ID of parent project.
  612. =item INTEGER C<SUITE ID> - ID of suite to base run on
  613. =item STRING C<NAME> - Name of run
  614. =item STRING C<DESCRIPTION> (optional) - Description of run
  615. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  616. =item INTEGER C<ASSIGNED_TO_ID> (optional) - User to assign the run to
  617. =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.
  618. =back
  619. Returns run definition HASHREF.
  620. $tr->createRun(1,1345,'RUN AWAY','SO FAR AWAY',22,3,[3,4,5,6]);
  621. =cut
  622. #If you pass an array of case ids, it implies include_all is false
  623. sub createRun {
  624. my ($self,$project_id,$suite_id,$name,$desc,$milestone_id,$assignedto_id,$case_ids) = @_;
  625. confess("Object methods must be called by an instance") unless ref($self);
  626. my $stuff = {
  627. suite_id => $suite_id,
  628. name => $name,
  629. description => $desc,
  630. milestone_id => $milestone_id,
  631. assignedto_id => $assignedto_id,
  632. include_all => $case_ids ? Types::Serialiser::false : Types::Serialiser::true,
  633. case_ids => $case_ids
  634. };
  635. my $result = $self->_doRequest("index.php?/api/v2/add_run/$project_id",'POST',$stuff);
  636. return $result;
  637. }
  638. =head2 B<deleteRun (run_id)>
  639. Deletes specified run.
  640. =over 4
  641. =item INTEGER C<RUN ID> - ID of run to delete.
  642. =back
  643. Returns BOOLEAN.
  644. $tr->deleteRun(1324);
  645. =cut
  646. sub deleteRun {
  647. my ($self,$suite_id) = @_;
  648. confess("Object methods must be called by an instance") unless ref($self);
  649. my $result = $self->_doRequest("index.php?/api/v2/delete_run/$suite_id",'POST');
  650. return $result;
  651. }
  652. =head2 B<getRuns (project_id)>
  653. Get all runs for specified project.
  654. =over 4
  655. =item INTEGER C<PROJECT_ID> - ID of parent project
  656. =back
  657. Returns ARRAYREF of run definition HASHREFs.
  658. $allRuns = $tr->getRuns(6969);
  659. =cut
  660. sub getRuns {
  661. my ($self,$project_id) = @_;
  662. confess("Object methods must be called by an instance") unless ref($self);
  663. return $self->_doRequest("index.php?/api/v2/get_runs/$project_id");
  664. }
  665. =head2 B<getRunByName (project_id,name)>
  666. Gets run by name.
  667. =over 4
  668. =item INTEGER C<PROJECT ID> - ID of parent project.
  669. =item STRING <NAME> - Name of desired run.
  670. =back
  671. Returns run definition HASHREF.
  672. $tr->getRunByName(1,'gravy');
  673. =cut
  674. sub getRunByName {
  675. my ($self,$project_id,$name) = @_;
  676. confess("Object methods must be called by an instance") unless ref($self);
  677. my $runs = $self->getRuns($project_id);
  678. return -500 if !$runs || (reftype($runs) || 'undef') ne 'ARRAY';
  679. foreach my $run (@$runs) {
  680. return $run if $run->{'name'} eq $name;
  681. }
  682. return 0;
  683. }
  684. =head2 B<getRunByID (run_id)>
  685. Gets run by ID.
  686. =over 4
  687. =item INTEGER C<RUN ID> - ID of desired run.
  688. =back
  689. Returns run definition HASHREF.
  690. $tr->getRunByID(7779311);
  691. =cut
  692. sub getRunByID {
  693. my ($self,$run_id) = @_;
  694. confess("Object methods must be called by an instance") unless ref($self);
  695. return $self->_doRequest("index.php?/api/v2/get_run/$run_id");
  696. }
  697. =head1 PLAN METHODS
  698. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  699. Create a run.
  700. =over 4
  701. =item INTEGER C<PROJECT ID> - ID of parent project.
  702. =item STRING C<NAME> - Name of plan
  703. =item STRING C<DESCRIPTION> (optional) - Description of plan
  704. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  705. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  706. =back
  707. Returns test plan definition HASHREF, or false on failure.
  708. $entries = {
  709. suite_id => 345,
  710. include_all => Types::Serialiser::true,
  711. assignedto_id => 1
  712. }
  713. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  714. =cut
  715. sub createPlan {
  716. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = @_;
  717. confess("Object methods must be called by an instance") unless ref($self);
  718. my $stuff = {
  719. name => $name,
  720. description => $desc,
  721. milestone_id => $milestone_id,
  722. entries => $entries
  723. };
  724. my $result = $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  725. return $result;
  726. }
  727. =head2 B<deletePlan (plan_id)>
  728. Deletes specified plan.
  729. =over 4
  730. =item INTEGER C<PLAN ID> - ID of plan to delete.
  731. =back
  732. Returns BOOLEAN.
  733. $tr->deletePlan(8675309);
  734. =cut
  735. sub deletePlan {
  736. my ($self,$plan_id) = @_;
  737. confess("Object methods must be called by an instance") unless ref($self);
  738. my $result = $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  739. return $result;
  740. }
  741. =head2 B<getPlans (project_id)>
  742. Deletes specified plan.
  743. =over 4
  744. =item INTEGER C<PROJECT ID> - ID of parent project.
  745. =back
  746. Returns ARRAYREF of plan definition HASHREFs.
  747. $tr->getPlans(8);
  748. =cut
  749. sub getPlans {
  750. my ($self,$project_id) = @_;
  751. confess("Object methods must be called by an instance") unless ref($self);
  752. return $self->_doRequest("index.php?/api/v2/get_plans/$project_id");
  753. }
  754. =head2 B<getPlanByName (project_id,name)>
  755. Gets specified plan by name.
  756. =over 4
  757. =item INTEGER C<PROJECT ID> - ID of parent project.
  758. =item STRING C<NAME> - Name of test plan.
  759. =back
  760. Returns plan definition HASHREF.
  761. $tr->getPlanByName(8,'GosPlan');
  762. =cut
  763. sub getPlanByName {
  764. my ($self,$project_id,$name) = @_;
  765. confess("Object methods must be called by an instance") unless ref($self);
  766. my $plans = $self->getPlans($project_id);
  767. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  768. foreach my $plan (@$plans) {
  769. return $plan if $plan->{'name'} eq $name;
  770. }
  771. return 0;
  772. }
  773. =head2 B<getPlanByID (plan_id)>
  774. Gets specified plan by ID.
  775. =over 4
  776. =item INTEGER C<PLAN ID> - ID of plan.
  777. =back
  778. Returns plan definition HASHREF.
  779. $tr->getPlanByID(2);
  780. =cut
  781. sub getPlanByID {
  782. my ($self,$plan_id) = @_;
  783. confess("Object methods must be called by an instance") unless ref($self);
  784. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  785. }
  786. =head1 MILESTONE METHODS
  787. =head2 B<createMilestone (project_id,name,description,due_on)>
  788. Create a milestone.
  789. =over 4
  790. =item INTEGER C<PROJECT ID> - ID of parent project.
  791. =item STRING C<NAME> - Name of milestone
  792. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  793. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  794. =back
  795. Returns milestone definition HASHREF, or false on failure.
  796. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  797. =cut
  798. sub createMilestone {
  799. my ($self,$project_id,$name,$desc,$due_on) = @_;
  800. confess("Object methods must be called by an instance") unless ref($self);
  801. my $stuff = {
  802. name => $name,
  803. description => $desc,
  804. due_on => $due_on # unix timestamp
  805. };
  806. my $result = $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  807. return $result;
  808. }
  809. =head2 B<deleteMilestone (milestone_id)>
  810. Deletes specified milestone.
  811. =over 4
  812. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  813. =back
  814. Returns BOOLEAN.
  815. $tr->deleteMilestone(86);
  816. =cut
  817. sub deleteMilestone {
  818. my ($self,$milestone_id) = @_;
  819. confess("Object methods must be called by an instance") unless ref($self);
  820. my $result = $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  821. return $result;
  822. }
  823. =head2 B<getMilestones (project_id)>
  824. Get milestones for some project.
  825. =over 4
  826. =item INTEGER C<PROJECT ID> - ID of parent project.
  827. =back
  828. Returns ARRAYREF of milestone definition HASHREFs.
  829. $tr->getMilestones(8);
  830. =cut
  831. sub getMilestones {
  832. my ($self,$project_id) = @_;
  833. confess("Object methods must be called by an instance") unless ref($self);
  834. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  835. }
  836. =head2 B<getMilestoneByName (project_id,name)>
  837. Gets specified milestone by name.
  838. =over 4
  839. =item INTEGER C<PROJECT ID> - ID of parent project.
  840. =item STRING C<NAME> - Name of milestone.
  841. =back
  842. Returns milestone definition HASHREF.
  843. $tr->getMilestoneByName(8,'whee');
  844. =cut
  845. sub getMilestoneByName {
  846. my ($self,$project_id,$name) = @_;
  847. confess("Object methods must be called by an instance") unless ref($self);
  848. my $milestones = $self->getMilestones($project_id);
  849. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  850. foreach my $milestone (@$milestones) {
  851. return $milestone if $milestone->{'name'} eq $name;
  852. }
  853. return 0;
  854. }
  855. =head2 B<getMilestoneByID (plan_id)>
  856. Gets specified milestone by ID.
  857. =over 4
  858. =item INTEGER C<MILESTONE ID> - ID of milestone.
  859. =back
  860. Returns milestone definition HASHREF.
  861. $tr->getMilestoneByID(2);
  862. =cut
  863. sub getMilestoneByID {
  864. my ($self,$milestone_id) = @_;
  865. confess("Object methods must be called by an instance") unless ref($self);
  866. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  867. }
  868. =head1 TEST METHODS
  869. =head2 B<getTests (run_id)>
  870. Get tests for some run.
  871. =over 4
  872. =item INTEGER C<RUN ID> - ID of parent run.
  873. =back
  874. Returns ARRAYREF of test definition HASHREFs.
  875. $tr->getTests(8);
  876. =cut
  877. sub getTests {
  878. my ($self,$run_id) = @_;
  879. confess("Object methods must be called by an instance") unless ref($self);
  880. return $self->_doRequest("index.php?/api/v2/get_tests/$run_id");
  881. }
  882. =head2 B<getTestByName (run_id,name)>
  883. Gets specified test by name.
  884. =over 4
  885. =item INTEGER C<RUN ID> - ID of parent run.
  886. =item STRING C<NAME> - Name of milestone.
  887. =back
  888. Returns test definition HASHREF.
  889. $tr->getTestByName(36,'wheeTest');
  890. =cut
  891. sub getTestByName {
  892. my ($self,$run_id,$name) = @_;
  893. confess("Object methods must be called by an instance") unless ref($self);
  894. my $tests = $self->getTests($run_id);
  895. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  896. foreach my $test (@$tests) {
  897. return $test if $test->{'title'} eq $name;
  898. }
  899. return 0;
  900. }
  901. =head2 B<getTestByID (test_id)>
  902. Gets specified test by ID.
  903. =over 4
  904. =item INTEGER C<TEST ID> - ID of test.
  905. =back
  906. Returns test definition HASHREF.
  907. $tr->getTestByID(222222);
  908. =cut
  909. sub getTestByID {
  910. my ($self,$test_id) = @_;
  911. confess("Object methods must be called by an instance") unless ref($self);
  912. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  913. }
  914. =head2 B<getTestResultFields()>
  915. Gets custom fields that can be set for tests.
  916. Returns ARRAYREF of result definition HASHREFs.
  917. =cut
  918. sub getTestResultFields {
  919. my $self = shift;
  920. confess("Object methods must be called by an instance") unless ref($self);
  921. return $self->_doRequest('index.php?/api/v2/get_result_fields');
  922. }
  923. =head2 B<getPossibleTestStatuses()>
  924. Gets all possible statuses a test can be set to.
  925. Returns ARRAYREF of status definition HASHREFs.
  926. =cut
  927. sub getPossibleTestStatuses {
  928. my $self = shift;
  929. confess("Object methods must be called by an instance") unless ref($self);
  930. return $self->_doRequest('index.php?/api/v2/get_statuses');
  931. }
  932. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  933. Creates a result entry for a test.
  934. Returns result definition HASHREF.
  935. $options = {
  936. elapsed => '30m 22s',
  937. defects => ['TSR-3','BOOM-44'],
  938. version => '6969'
  939. };
  940. $custom_options = {
  941. step_results => [
  942. {
  943. content => 'Step 1',
  944. expected => "Bought Groceries",
  945. actual => "No Dinero!",
  946. status_id => 2
  947. },
  948. {
  949. content => 'Step 2',
  950. expected => 'Ate Dinner',
  951. actual => 'Went Hungry',
  952. status_id => 2
  953. }
  954. ]
  955. };
  956. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  957. =cut
  958. sub createTestResults {
  959. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = @_;
  960. confess("Object methods must be called by an instance") unless ref($self);
  961. my $stuff = {
  962. status_id => $status_id,
  963. comment => $comment
  964. };
  965. #Handle options
  966. if (defined($opts) && reftype($opts) eq 'HASH') {
  967. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  968. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  969. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  970. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  971. }
  972. #Handle custom fields
  973. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  974. foreach my $field (keys(%$custom_fields)) {
  975. $stuff->{"custom_$field"} = $custom_fields->{$field};
  976. }
  977. }
  978. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  979. }
  980. =head2 B<getTestResults(test_id,limit)>
  981. Get the recorded results for desired test, limiting output to 'limit' entries.
  982. =over 4
  983. =item INTEGER C<TEST_ID> - ID of desired test
  984. =item POSITIVE INTEGER C<LIMIT> - provide no more than this number of results.
  985. =back
  986. Returns ARRAYREF of result definition HASHREFs.
  987. =cut
  988. sub getTestResults {
  989. my ($self,$test_id,$limit) = @_;
  990. confess("Object methods must be called by an instance") unless ref($self);
  991. my $url = "index.php?/api/v2/get_results/$test_id";
  992. $url .= "&limit=$limit" if defined($limit);
  993. return $self->_doRequest($url);
  994. }
  995. =head1 STATIC METHODS
  996. =head2 B<buildStepResults(content,expected,actual,status_id)>
  997. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  998. =cut
  999. #Convenience method for building stepResults
  1000. sub buildStepResults {
  1001. my ($content,$expected,$actual,$status_id) = @_;
  1002. return {
  1003. content => $content,
  1004. expected => $expected,
  1005. actual => $actual,
  1006. status_id => $status_id
  1007. };
  1008. }
  1009. 1;
  1010. __END__
  1011. =head1 SEE ALSO
  1012. L<HTTP::Request>
  1013. L<LWP::UserAgent>
  1014. L<JSON::XS>
  1015. L<http://docs.gurock.com/testrail-api2/start>
  1016. =head1 SPECIAL THANKS
  1017. Thanks to cPanel Inc, for graciously funding the creation of this module.