API.pm 30 KB

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