API.pm 31 KB

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