API.pm 30 KB

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