API.pm 30 KB

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