API.pm 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. # ABSTRACT: Provides an interface to TestRail's REST api via HTTP
  2. # PODNAME: TestRail::API
  3. package TestRail::API;
  4. =head1 SYNOPSIS
  5. use TestRail::API;
  6. my ($username,$password,$host) = ('foo','bar','testlink.baz.foo');
  7. my $tr = TestRail::API->new($username, $password, $host);
  8. =head1 DESCRIPTION
  9. C<TestRail::API> provides methods to access an existing TestRail account using API v2. You can then do things like look up tests, set statuses and create runs from lists of cases.
  10. It is by no means exhaustively implementing every TestRail API function.
  11. =head1 IMPORTANT
  12. All the methods aside from the constructor should not die, but return a false value upon failure.
  13. When the server is not responsive, expect a -500 response, and retry accordingly.
  14. I recommend using the excellent L<Attempt> module for this purpose.
  15. =cut
  16. use 5.010;
  17. use strict;
  18. use warnings;
  19. use Carp qw{cluck confess};
  20. use Scalar::Util qw{reftype looks_like_number};
  21. use Clone 'clone';
  22. use Try::Tiny;
  23. use JSON::MaybeXS ();
  24. use HTTP::Request;
  25. use LWP::UserAgent;
  26. use Data::Validate::URI qw{is_uri};
  27. =head1 CONSTRUCTOR
  28. =head2 B<new (api_url, user, password)>
  29. Creates new C<TestRail::API> object.
  30. =over 4
  31. =item STRING C<API URL> - base url for your TestRail api server.
  32. =item STRING C<USER> - Your TestRail User.
  33. =item STRING C<PASSWORD> - Your TestRail password.
  34. =item BOOLEAN C<DEBUG> - Print the JSON responses from TL with your requests.
  35. =back
  36. Returns C<TestRail::API> object if login is successful.
  37. my $tr = TestRail::API->new('http://tr.test/testrail', 'moo','M000000!');
  38. Dies on all communication errors with the TestRail server.
  39. Does not do above checks if debug is passed.
  40. =cut
  41. sub new {
  42. my ($class,$apiurl,$user,$pass,$debug) = @_;
  43. confess("Constructor must be called statically, not by an instance") if ref($class);
  44. confess("Invalid URI passed to constructor") if !is_uri($apiurl);
  45. $user //= $ENV{'TESTRAIL_USER'};
  46. $pass //= $ENV{'TESTRAIL_PASSWORD'};
  47. $debug //= 0;
  48. my $self = {
  49. user => $user,
  50. pass => $pass,
  51. apiurl => $apiurl,
  52. debug => $debug,
  53. testtree => [],
  54. flattree => [],
  55. user_cache => [],
  56. type_cache => [],
  57. configurations => undef,
  58. tr_fields => undef,
  59. default_request => undef,
  60. browser => new LWP::UserAgent()
  61. };
  62. #Create default request to pass on to LWP::UserAgent
  63. $self->{'default_request'} = new HTTP::Request();
  64. $self->{'default_request'}->authorization_basic($user,$pass);
  65. bless( $self, $class );
  66. return $self if $self->debug; #For easy class testing without mocks
  67. #Manually do the get_users call to check HTTP status
  68. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  69. confess "Error: network unreachable" if !defined($res);
  70. if ( (reftype($res) || 'undef') ne 'ARRAY') {
  71. confess "Unexpected return from _doRequest: $res" if !looks_like_number($res);
  72. confess "Could not communicate with TestRail Server! Check that your URI is correct, and your TestRail installation is functioning correctly." if $res == -500;
  73. confess "Could not list testRail users! Check that your TestRail installation has it's API enabled, and your credentials are correct" if $res == -403;
  74. confess "Bad user credentials!" if $res == -401;
  75. confess "HTTP error $res encountered while communicating with TestRail server. Resolve issue and try again." if !$res;
  76. confess "Unknown error occurred: $res";
  77. }
  78. confess "No users detected on TestRail Install! Check that your API is functioning correctly." if !scalar(@$res);
  79. $self->{'user_cache'} = $res;
  80. return $self;
  81. }
  82. =head1 GETTERS
  83. =head2 B<apiurl>
  84. =head2 B<debug>
  85. Accessors for these parameters you pass into the constructor, in case you forget.
  86. =cut
  87. sub apiurl {
  88. my $self = shift;
  89. confess("Object methods must be called by an instance") unless ref($self);
  90. return $self->{'apiurl'}
  91. }
  92. sub debug {
  93. my $self = shift;
  94. confess("Object methods must be called by an instance") unless ref($self);
  95. return $self->{'debug'};
  96. }
  97. #Convenient JSON-HTTP fetcher
  98. sub _doRequest {
  99. my ($self,$path,$method,$data) = @_;
  100. confess("Object methods must be called by an instance") unless ref($self);
  101. my $req = clone $self->{'default_request'};
  102. $method //= 'GET';
  103. $req->method($method);
  104. $req->url($self->apiurl.'/'.$path);
  105. warn "$method ".$self->apiurl."/$path" if $self->debug;
  106. my $coder = JSON::MaybeXS->new;
  107. #Data sent is JSON
  108. my $content = $data ? $coder->encode($data) : '';
  109. $req->content($content);
  110. $req->header( "Content-Type" => "application/json" );
  111. my $response = $self->{'browser'}->request($req);
  112. return $response if !defined($response); #worst case
  113. if ($response->code == 403) {
  114. cluck "ERROR: Access Denied.";
  115. return -403;
  116. }
  117. if ($response->code != 200) {
  118. cluck "ERROR: Arguments Bad: ".$response->content;
  119. return -int($response->code);
  120. }
  121. try {
  122. return $coder->decode($response->content);
  123. } catch {
  124. if ($response->code == 200 && !$response->content) {
  125. return 1; #This function probably just returns no data
  126. } else {
  127. cluck "ERROR: Malformed JSON returned by API.";
  128. cluck $@;
  129. if (!$self->debug) { #Otherwise we've already printed this, but we need to know if we encounter this
  130. cluck "RAW CONTENT:";
  131. cluck $response->content
  132. }
  133. return 0;
  134. }
  135. }
  136. }
  137. =head1 USER METHODS
  138. =head2 B<getUsers ()>
  139. Get all the user definitions for the provided Test Rail install.
  140. Returns ARRAYREF of user definition HASHREFs.
  141. =cut
  142. sub getUsers {
  143. my $self = shift;
  144. confess("Object methods must be called by an instance") unless ref($self);
  145. my $res = $self->_doRequest('index.php?/api/v2/get_users');
  146. return -500 if !$res || (reftype($res) || 'undef') ne 'ARRAY';
  147. $self->{'user_cache'} = $res;
  148. return $res;
  149. }
  150. =head2 B<getUserByID(id)>
  151. =cut
  152. =head2 B<getUserByName(name)>
  153. =cut
  154. =head2 B<getUserByEmail(email)>
  155. Get user definition hash by ID, Name or Email.
  156. Returns user def HASHREF.
  157. =cut
  158. #I'm just using the cache for the following methods because it's more straightforward and faster past 1 call.
  159. sub getUserByID {
  160. my ($self,$user) = @_;
  161. confess("Object methods must be called by an instance") unless ref($self);
  162. confess("User ID must be integer") unless $self->_checkInteger($user);
  163. $self->getUsers() if !defined($self->{'user_cache'});
  164. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  165. foreach my $usr (@{$self->{'user_cache'}}) {
  166. return $usr if $usr->{'id'} == $user;
  167. }
  168. return 0;
  169. }
  170. sub getUserByName {
  171. my ($self,$user) = @_;
  172. confess("Object methods must be called by an instance") unless ref($self);
  173. confess("User must be string") unless $self->_checkString($user);
  174. $self->getUsers() if !defined($self->{'user_cache'});
  175. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  176. foreach my $usr (@{$self->{'user_cache'}}) {
  177. return $usr if $usr->{'name'} eq $user;
  178. }
  179. return 0;
  180. }
  181. sub getUserByEmail {
  182. my ($self,$email) = @_;
  183. confess("Object methods must be called by an instance") unless ref($self);
  184. confess("Email must be string") unless $self->_checkString($email);
  185. $self->getUsers() if !defined($self->{'user_cache'});
  186. return -500 if (!defined($self->{'user_cache'}) || (reftype($self->{'user_cache'}) || 'undef') ne 'ARRAY');
  187. foreach my $usr (@{$self->{'user_cache'}}) {
  188. return $usr if $usr->{'email'} eq $email;
  189. }
  190. return 0;
  191. }
  192. =head1 PROJECT METHODS
  193. =head2 B<createProject (name, [description,send_announcement])>
  194. Creates new Project (Database of testsuites/tests).
  195. Optionally specify an announcement to go out to the users.
  196. Requires TestRail admin login.
  197. =over 4
  198. =item STRING C<NAME> - Desired name of project.
  199. =item STRING C<DESCRIPTION> (optional) - Description of project. Default value is 'res ipsa loquiter'.
  200. =item BOOLEAN C<SEND ANNOUNCEMENT> (optional) - Whether to confront users with an announcement about your awesome project on next login. Default false.
  201. =back
  202. Returns project definition HASHREF on success, false otherwise.
  203. $tl->createProject('Widgetronic 4000', 'Tests for the whiz-bang new product', true);
  204. =cut
  205. sub createProject {
  206. my ($self,$name,$desc,$announce) = @_;
  207. confess("Object methods must be called by an instance") unless ref($self);
  208. confess("Project name must be string") unless $self->_checkString($name);
  209. $desc //= 'res ipsa loquiter';
  210. $announce //= 0;
  211. confess("Project description must be string") unless $self->_checkString($desc);
  212. confess("Announce must be integer") unless $self->_checkInteger($announce);
  213. my $input = {
  214. name => $name,
  215. announcement => $desc,
  216. show_announcement => $announce
  217. };
  218. my $result = $self->_doRequest('index.php?/api/v2/add_project','POST',$input);
  219. return $result;
  220. }
  221. =head2 B<deleteProject (id)>
  222. Deletes specified project by ID.
  223. Requires TestRail admin login.
  224. =over 4
  225. =item STRING C<NAME> - Desired name of project.
  226. =back
  227. Returns BOOLEAN.
  228. $success = $tl->deleteProject(1);
  229. =cut
  230. sub deleteProject {
  231. my ($self,$proj) = @_;
  232. confess("Object methods must be called by an instance") unless ref($self);
  233. confess("Project ID must be integer") unless $self->_checkInteger($proj);
  234. my $result = $self->_doRequest('index.php?/api/v2/delete_project/'.$proj,'POST');
  235. return $result;
  236. }
  237. =head2 B<getProjects ()>
  238. Get all available projects
  239. Returns array of project definition HASHREFs, false otherwise.
  240. $projects = $tl->getProjects;
  241. =cut
  242. sub getProjects {
  243. my $self = shift;
  244. confess("Object methods must be called by an instance") unless ref($self);
  245. my $result = $self->_doRequest('index.php?/api/v2/get_projects');
  246. #Save state for future use, if needed
  247. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  248. $self->{'testtree'} = $result;
  249. #Note that it's a project for future reference by recursive tree search
  250. return -500 if !$result || (reftype($result) || 'undef') ne 'ARRAY';
  251. foreach my $pj (@{$result}) {
  252. $pj->{'type'} = 'project';
  253. }
  254. return $result;
  255. }
  256. =head2 B<getProjectByName ($project)>
  257. Gets some project definition hash by it's name
  258. =over 4
  259. =item STRING C<PROJECT> - desired project
  260. =back
  261. Returns desired project def HASHREF, false otherwise.
  262. $project = $tl->getProjectByName('FunProject');
  263. =cut
  264. sub getProjectByName {
  265. my ($self,$project) = @_;
  266. confess("Object methods must be called by an instance") unless ref($self);
  267. confess("Project must be string.") unless $self->_checkString($project);
  268. #See if we already have the project list...
  269. my $projects = $self->{'testtree'};
  270. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  271. $projects = $self->getProjects() unless scalar(@$projects);
  272. #Search project list for project
  273. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  274. for my $candidate (@$projects) {
  275. return $candidate if ($candidate->{'name'} eq $project);
  276. }
  277. return 0;
  278. }
  279. =head2 B<getProjectByID ($project)>
  280. Gets some project definition hash by it's ID
  281. =over 4
  282. =item INTEGER C<PROJECT> - desired project
  283. =back
  284. Returns desired project def HASHREF, false otherwise.
  285. $projects = $tl->getProjectByID(222);
  286. =cut
  287. sub getProjectByID {
  288. my ($self,$project) = @_;
  289. confess("Object methods must be called by an instance") unless ref($self);
  290. confess("No project provided.") unless $project;
  291. confess("Project ID must be integer") unless $self->_checkInteger($project);
  292. #See if we already have the project list...
  293. my $projects = $self->{'testtree'};
  294. $projects = $self->getProjects() unless scalar(@$projects);
  295. #Search project list for project
  296. return -500 if !$projects || (reftype($projects) || 'undef') ne 'ARRAY';
  297. for my $candidate (@$projects) {
  298. return $candidate if ($candidate->{'id'} eq $project);
  299. }
  300. return 0;
  301. }
  302. =head1 TESTSUITE METHODS
  303. =head2 B<createTestSuite (project_id, name, [description])>
  304. Creates new TestSuite (folder of tests) in the database of test specifications under given project id having given name and details.
  305. =over 4
  306. =item INTEGER C<PROJECT ID> - ID of project this test suite should be under.
  307. =item STRING C<NAME> - Desired name of test suite.
  308. =item STRING C<DESCRIPTION> (optional) - Description of test suite. Default value is 'res ipsa loquiter'.
  309. =back
  310. Returns TS definition HASHREF on success, false otherwise.
  311. $tl->createTestSuite(1, 'broken tests', 'Tests that should be reviewed');
  312. =cut
  313. sub createTestSuite {
  314. my ($self,$project_id,$name,$details) = @_;
  315. confess("Object methods must be called by an instance") unless ref($self);
  316. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  317. confess("Name must be a string") unless $self->_checkString($name);
  318. $details ||= 'res ipsa loquiter';
  319. confess("Project details must be a string") unless $self->_checkString($details);
  320. my $input = {
  321. name => $name,
  322. description => $details
  323. };
  324. my $result = $self->_doRequest('index.php?/api/v2/add_suite/'.$project_id,'POST',$input);
  325. return $result;
  326. }
  327. =head2 B<deleteTestSuite (suite_id)>
  328. Deletes specified testsuite.
  329. =over 4
  330. =item INTEGER C<SUITE ID> - ID of testsuite to delete.
  331. =back
  332. Returns BOOLEAN.
  333. $tl->deleteTestSuite(1);
  334. =cut
  335. sub deleteTestSuite {
  336. my ($self,$suite_id) = @_;
  337. confess("Object methods must be called by an instance") unless ref($self);
  338. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  339. my $result = $self->_doRequest('index.php?/api/v2/delete_suite/'.$suite_id,'POST');
  340. return $result;
  341. }
  342. =head2 B<getTestSuites (project_id)>
  343. Gets the testsuites for a project
  344. =over 4
  345. =item STRING C<PROJECT ID> - desired project's ID
  346. =back
  347. Returns ARRAYREF of testsuite definition HASHREFs, 0 on error.
  348. $suites = $tl->getTestSuites(123);
  349. =cut
  350. sub getTestSuites {
  351. my ($self,$proj) = @_;
  352. confess("Object methods must be called by an instance") unless ref($self);
  353. confess("Project ID must be integer") unless $self->_checkInteger($proj);
  354. return $self->_doRequest('index.php?/api/v2/get_suites/'.$proj);
  355. }
  356. =head2 B<getTestSuiteByName (project_id,testsuite_name)>
  357. Gets the testsuite that matches the given name inside of given project.
  358. =over 4
  359. =item STRING C<PROJECT ID> - ID of project holding this testsuite
  360. =item STRING C<TESTSUITE NAME> - desired parent testsuite name
  361. =back
  362. Returns desired testsuite definition HASHREF, false otherwise.
  363. $suites = $tl->getTestSuitesByName(321, 'hugSuite');
  364. =cut
  365. sub getTestSuiteByName {
  366. my ($self,$project_id,$testsuite_name) = @_;
  367. confess("Object methods must be called by an instance") unless ref($self);
  368. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  369. confess("Testsuite Name must be String") unless $self->_checkString($testsuite_name);
  370. #TODO cache
  371. my $suites = $self->getTestSuites($project_id);
  372. return -500 if !$suites || (reftype($suites) || 'undef') ne 'ARRAY'; #No suites for project, or no project
  373. foreach my $suite (@$suites) {
  374. return $suite if $suite->{'name'} eq $testsuite_name;
  375. }
  376. return 0; #Couldn't find it
  377. }
  378. =head2 B<getTestSuiteByID (testsuite_id)>
  379. Gets the testsuite with the given ID.
  380. =over 4
  381. =item STRING C<TESTSUITE_ID> - TestSuite ID.
  382. =back
  383. Returns desired testsuite definition HASHREF, false otherwise.
  384. $tests = $tl->getTestSuiteByID(123);
  385. =cut
  386. sub getTestSuiteByID {
  387. my ($self,$testsuite_id) = @_;
  388. confess("Object methods must be called by an instance") unless ref($self);
  389. confess("Testsuite ID must be integer") unless $self->_checkInteger($testsuite_id);
  390. my $result = $self->_doRequest('index.php?/api/v2/get_suite/'.$testsuite_id);
  391. return $result;
  392. }
  393. =head1 SECTION METHODS
  394. =head2 B<createSection(project_id,suite_id,name,[parent_id])>
  395. Creates a section.
  396. =over 4
  397. =item INTEGER C<PROJECT ID> - Parent Project ID.
  398. =item INTEGER C<SUITE ID> - Parent TestSuite ID.
  399. =item STRING C<NAME> - desired section name.
  400. =item INTEGER C<PARENT ID> (optional) - parent section id
  401. =back
  402. Returns new section definition HASHREF, false otherwise.
  403. $section = $tr->createSection(1,1,'nugs',1);
  404. =cut
  405. sub createSection {
  406. my ($self,$project_id,$suite_id,$name,$parent_id) = @_;
  407. confess("Object methods must be called by an instance") unless ref($self);
  408. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  409. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  410. confess("Section name must be string") unless $self->_checkString($name);
  411. confess("Parent section ID must be integer") unless !defined($parent_id) || $self->_checkInteger($parent_id);
  412. my $input = {
  413. name => $name,
  414. suite_id => $suite_id
  415. };
  416. $input->{'parent_id'} = $parent_id if $parent_id;
  417. my $result = $self->_doRequest('index.php?/api/v2/add_section/'.$project_id,'POST',$input);
  418. return $result;
  419. }
  420. =head2 B<deleteSection (section_id)>
  421. Deletes specified section.
  422. =over 4
  423. =item INTEGER C<SECTION ID> - ID of section to delete.
  424. =back
  425. Returns BOOLEAN.
  426. $tr->deleteSection(1);
  427. =cut
  428. sub deleteSection {
  429. my ($self,$section_id) = @_;
  430. confess("Object methods must be called by an instance") unless ref($self);
  431. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  432. my $result = $self->_doRequest('index.php?/api/v2/delete_section/'.$section_id,'POST');
  433. return $result;
  434. }
  435. =head2 B<getSections (project_id,suite_id)>
  436. Gets sections for a given project and suite.
  437. =over 4
  438. =item INTEGER C<PROJECT ID> - ID of parent project.
  439. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  440. =back
  441. Returns ARRAYREF of section definition HASHREFs.
  442. $tr->getSections(1,2);
  443. =cut
  444. sub getSections {
  445. my ($self,$project_id,$suite_id) = @_;
  446. confess("Object methods must be called by an instance") unless ref($self);
  447. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  448. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  449. return $self->_doRequest("index.php?/api/v2/get_sections/$project_id&suite_id=$suite_id");
  450. }
  451. =head2 B<getSectionByID (section_id)>
  452. Gets desired section.
  453. =over 4
  454. =item INTEGER C<PROJECT ID> - ID of parent project.
  455. =item INTEGER C<SUITE ID> - ID of suite to get sections for.
  456. =back
  457. Returns section definition HASHREF.
  458. $tr->getSectionByID(344);
  459. =cut
  460. sub getSectionByID {
  461. my ($self,$section_id) = @_;
  462. confess("Object methods must be called by an instance") unless ref($self);
  463. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  464. return $self->_doRequest("index.php?/api/v2/get_section/$section_id");
  465. }
  466. =head2 B<getSectionByName (project_id,suite_id,name)>
  467. Gets desired section.
  468. =over 4
  469. =item INTEGER C<PROJECT ID> - ID of parent project.
  470. =item INTEGER C<SUITE ID> - ID of suite to get section for.
  471. =item STRING C<NAME> - name of section to get
  472. =back
  473. Returns section definition HASHREF.
  474. $tr->getSectionByName(1,2,'nugs');
  475. =cut
  476. sub getSectionByName {
  477. my ($self,$project_id,$suite_id,$section_name) = @_;
  478. confess("Object methods must be called by an instance") unless ref($self);
  479. confess("Project ID must be an integer") unless $self->_checkInteger($project_id);
  480. confess("Suite ID must be an integer") unless $self->_checkInteger($suite_id);
  481. confess("Section Name must be a string") unless $self->_checkString($section_name);
  482. my $sections = $self->getSections($project_id,$suite_id);
  483. return -500 if !$sections || (reftype($sections) || 'undef') ne 'ARRAY';
  484. foreach my $sec (@$sections) {
  485. return $sec if $sec->{'name'} eq $section_name;
  486. }
  487. return 0;
  488. }
  489. =head1 CASE METHODS
  490. =head2 B<getCaseTypes ()>
  491. Gets possible case types.
  492. Returns ARRAYREF of case type definition HASHREFs.
  493. $tr->getCaseTypes();
  494. =cut
  495. sub getCaseTypes {
  496. my $self = shift;
  497. confess("Object methods must be called by an instance") unless ref($self);
  498. my $types = $self->_doRequest("index.php?/api/v2/get_case_types");
  499. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  500. $self->{'type_cache'} = $types if !$self->{'type_cache'}; #We can't change this with API, so assume it is static
  501. return $self->{'type_cache'};
  502. }
  503. =head2 B<getCaseTypeByName (name)>
  504. Gets case type by name.
  505. =over 4
  506. =item STRING C<NAME> - Name of desired case type
  507. =back
  508. Returns case type definition HASHREF.
  509. $tr->getCaseTypeByName();
  510. =cut
  511. sub getCaseTypeByName {
  512. #Useful for marking automated tests, etc
  513. my ($self,$name) = @_;
  514. confess("Object methods must be called by an instance") unless ref($self);
  515. confess("Case type must be string") unless $self->_checkString($name);
  516. my $types = $self->getCaseTypes();
  517. return -500 if !$types || (reftype($types) || 'undef') ne 'ARRAY';
  518. foreach my $type (@$types) {
  519. return $type if $type->{'name'} eq $name;
  520. }
  521. return 0;
  522. }
  523. =head2 B<createCase(section_id,title,type_id,options,extra_options)>
  524. Creates a test case.
  525. =over 4
  526. =item INTEGER C<SECTION ID> - Parent Project ID.
  527. =item STRING C<TITLE> - Case title.
  528. =item INTEGER C<TYPE_ID> (optional) - desired test type's ID. Defaults to whatever your TR install considers the default type.
  529. =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.
  530. =item HASHREF C<EXTRA OPTIONS> (optional) - contains priority_id, estimate, milestone_id and refs as possible keys. See TestRail API documentation for more info.
  531. =back
  532. Returns new case definition HASHREF, false otherwise.
  533. $custom_opts = {
  534. preconds => "Test harness installed",
  535. steps => "Do the needful",
  536. expected => "cubicle environment transforms into Dali painting"
  537. };
  538. $other_opts = {
  539. priority_id => 4,
  540. milestone_id => 666,
  541. estimate => '2m 45s',
  542. refs => ['TRACE-22','ON-166'] #ARRAYREF of bug IDs.
  543. }
  544. $case = $tr->createCase(1,'Do some stuff',3,$custom_opts,$other_opts);
  545. =cut
  546. sub createCase {
  547. my ($self,$section_id,$title,$type_id,$opts,$extras) = @_;
  548. confess("Object methods must be called by an instance") unless ref($self);
  549. confess("Section ID ($section_id) must be integer") unless $self->_checkInteger($section_id);
  550. confess("title must be string") unless $self->_checkString($title);
  551. confess("Type ID must be integer") unless !defined($type_id) || $self->_checkInteger($type_id);
  552. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') ne 'HASH';
  553. confess("Extras must be HASHREF") unless !defined($extras) || (reftype($extras) || 'undef') ne 'HASH';
  554. my $stuff = {
  555. title => $title,
  556. type_id => $type_id
  557. };
  558. #Handle sort of optional but baked in options
  559. if (defined($extras) && reftype($extras) eq 'HASH') {
  560. $stuff->{'priority_id'} = $extras->{'priority_id'} if defined($extras->{'priority_id'});
  561. $stuff->{'estimate'} = $extras->{'estimate'} if defined($extras->{'estimate'});
  562. $stuff->{'milestone_id'} = $extras->{'milestone_id'} if defined($extras->{'milestone_id'});
  563. $stuff->{'refs'} = join(',',@{$extras->{'refs'}}) if defined($extras->{'refs'});
  564. }
  565. #Handle custom fields
  566. if (defined($opts) && reftype($opts) eq 'HASH') {
  567. foreach my $key (keys(%$opts)) {
  568. $stuff->{"custom_$key"} = $opts->{$key};
  569. }
  570. }
  571. my $result = $self->_doRequest("index.php?/api/v2/add_case/$section_id",'POST',$stuff);
  572. return $result;
  573. }
  574. =head2 B<deleteCase (case_id)>
  575. Deletes specified section.
  576. =over 4
  577. =item INTEGER C<CASE ID> - ID of case to delete.
  578. =back
  579. Returns BOOLEAN.
  580. $tr->deleteCase(1324);
  581. =cut
  582. sub deleteCase {
  583. my ($self,$case_id) = @_;
  584. confess("Object methods must be called by an instance") unless ref($self);
  585. confess("Case ID must be integer") unless $self->_checkInteger($case_id);
  586. my $result = $self->_doRequest("index.php?/api/v2/delete_case/$case_id",'POST');
  587. return $result;
  588. }
  589. =head2 B<getCases (project_id,suite_id,section_id)>
  590. Gets cases for provided section.
  591. =over 4
  592. =item INTEGER C<PROJECT ID> - ID of parent project.
  593. =item INTEGER C<SUITE ID> - ID of parent suite.
  594. =item INTEGER C<SECTION ID> - ID of parent section
  595. =back
  596. Returns ARRAYREF of test case definition HASHREFs.
  597. $tr->getCases(1,2,3);
  598. =cut
  599. sub getCases {
  600. my ($self,$project_id,$suite_id,$section_id) = @_;
  601. confess("Object methods must be called by an instance") unless ref($self);
  602. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  603. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  604. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  605. my $url = "index.php?/api/v2/get_cases/$project_id&suite_id=$suite_id";
  606. $url .= "&section_id=$section_id" if $section_id;
  607. return $self->_doRequest($url);
  608. }
  609. =head2 B<getCaseByName (project_id,suite_id,section_id,name)>
  610. Gets case by name.
  611. =over 4
  612. =item INTEGER C<PROJECT ID> - ID of parent project.
  613. =item INTEGER C<SUITE ID> - ID of parent suite.
  614. =item INTEGER C<SECTION ID> - ID of parent section.
  615. =item STRING <NAME> - Name of desired test case.
  616. =back
  617. Returns test case definition HASHREF.
  618. $tr->getCaseByName(1,2,3,'nugs');
  619. =cut
  620. sub getCaseByName {
  621. my ($self,$project_id,$suite_id,$section_id,$name) = @_;
  622. confess("Object methods must be called by an instance") unless ref($self);
  623. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  624. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  625. confess("Section ID must be integer") unless $self->_checkInteger($section_id);
  626. confess("Test Case name must be string") unless $self->_checkString($name);
  627. my $cases = $self->getCases($project_id,$suite_id,$section_id);
  628. return -500 if !$cases || (reftype($cases) || 'undef') ne 'ARRAY';
  629. foreach my $case (@$cases) {
  630. return $case if $case->{'title'} eq $name;
  631. }
  632. return 0;
  633. }
  634. =head2 B<getCaseByID (case_id)>
  635. Gets case by ID.
  636. =over 4
  637. =item INTEGER C<CASE ID> - ID of case.
  638. =back
  639. Returns test case definition HASHREF.
  640. $tr->getCaseByID(1345);
  641. =cut
  642. sub getCaseByID {
  643. my ($self,$case_id) = @_;
  644. confess("Object methods must be called by an instance") unless ref($self);
  645. confess("Case ID must be integer") unless $self->_checkInteger($case_id);
  646. return $self->_doRequest("index.php?/api/v2/get_case/$case_id");
  647. }
  648. =head1 RUN METHODS
  649. =head2 B<createRun (project_id,suite_id,name,description,milestone_id,assigned_to_id,case_ids)>
  650. Create a run.
  651. =over 4
  652. =item INTEGER C<PROJECT ID> - ID of parent project.
  653. =item INTEGER C<SUITE ID> - ID of suite to base run on
  654. =item STRING C<NAME> - Name of run
  655. =item STRING C<DESCRIPTION> (optional) - Description of run
  656. =item INTEGER C<MILESTONE ID> (optional) - ID of milestone
  657. =item INTEGER C<ASSIGNED TO ID> (optional) - User to assign the run to
  658. =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.
  659. =back
  660. Returns run definition HASHREF.
  661. $tr->createRun(1,1345,'RUN AWAY','SO FAR AWAY',22,3,[3,4,5,6]);
  662. =cut
  663. #If you pass an array of case ids, it implies include_all is false
  664. sub createRun {
  665. my ($self,$project_id,$suite_id,$name,$desc,$milestone_id,$assignedto_id,$case_ids) = @_;
  666. confess("Object methods must be called by an instance") unless ref($self);
  667. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  668. confess("Suite ID must be integer") unless $self->_checkInteger($suite_id);
  669. confess("Name must be string") unless $self->_checkString($name);
  670. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  671. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  672. confess("Assigned To ID must be integer") unless !defined($assignedto_id) || $self->_checkInteger($assignedto_id);
  673. confess("Case IDs must be ARRAYREF") unless !defined($case_ids) || (reftype($case_ids) || 'undef') eq 'ARRAY';
  674. my $stuff = {
  675. suite_id => $suite_id,
  676. name => $name,
  677. description => $desc,
  678. milestone_id => $milestone_id,
  679. assignedto_id => $assignedto_id,
  680. include_all => defined($case_ids) ? 0 : 1,
  681. case_ids => $case_ids
  682. };
  683. my $result = $self->_doRequest("index.php?/api/v2/add_run/$project_id",'POST',$stuff);
  684. return $result;
  685. }
  686. =head2 B<deleteRun (run_id)>
  687. Deletes specified run.
  688. =over 4
  689. =item INTEGER C<RUN ID> - ID of run to delete.
  690. =back
  691. Returns BOOLEAN.
  692. $tr->deleteRun(1324);
  693. =cut
  694. sub deleteRun {
  695. my ($self,$run_id) = @_;
  696. confess("Object methods must be called by an instance") unless ref($self);
  697. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  698. my $result = $self->_doRequest("index.php?/api/v2/delete_run/$run_id",'POST');
  699. return $result;
  700. }
  701. =head2 B<getRuns (project_id)>
  702. Get all runs for specified project.
  703. =over 4
  704. =item INTEGER C<PROJECT_ID> - ID of parent project
  705. =back
  706. Returns ARRAYREF of run definition HASHREFs.
  707. $allRuns = $tr->getRuns(6969);
  708. =cut
  709. sub getRuns {
  710. my ($self,$project_id) = @_;
  711. confess("Object methods must be called by an instance") unless ref($self);
  712. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  713. return $self->_doRequest("index.php?/api/v2/get_runs/$project_id");
  714. }
  715. =head2 B<getRunByName (project_id,name)>
  716. Gets run by name.
  717. =over 4
  718. =item INTEGER C<PROJECT ID> - ID of parent project.
  719. =item STRING <NAME> - Name of desired run.
  720. =back
  721. Returns run definition HASHREF.
  722. $tr->getRunByName(1,'R2');
  723. =cut
  724. sub getRunByName {
  725. my ($self,$project_id,$name) = @_;
  726. confess("Object methods must be called by an instance") unless ref($self);
  727. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  728. confess("Run name must be string") unless $self->_checkString($name);
  729. my $runs = $self->getRuns($project_id);
  730. return -500 if !$runs || (reftype($runs) || 'undef') ne 'ARRAY';
  731. foreach my $run (@$runs) {
  732. return $run if $run->{'name'} eq $name;
  733. }
  734. return 0;
  735. }
  736. =head2 B<getRunByID (run_id)>
  737. Gets run by ID.
  738. =over 4
  739. =item INTEGER C<RUN ID> - ID of desired run.
  740. =back
  741. Returns run definition HASHREF.
  742. $tr->getRunByID(7779311);
  743. =cut
  744. sub getRunByID {
  745. my ($self,$run_id) = @_;
  746. confess("Object methods must be called by an instance") unless ref($self);
  747. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  748. return $self->_doRequest("index.php?/api/v2/get_run/$run_id");
  749. }
  750. =head1 RUN AS CHILD OF PLAN METHODS
  751. =head2 B<getChildRuns(plan)>
  752. Extract the child runs from a plan. Convenient, as the structure of this hash is deep, and correct error handling can be tedious.
  753. =over 4
  754. =item HASHREF C<PLAN> - Test Plan definition HASHREF returned by any of the PLAN methods below.
  755. =back
  756. Returns ARRAYREF of run definition HASHREFs. Returns 0 upon failure to extract the data.
  757. =cut
  758. sub getChildRuns {
  759. my ($self,$plan) = @_;
  760. confess("Object methods must be called by an instance") unless ref($self);
  761. confess("Plan must be HASHREF") unless defined($plan) && (reftype($plan) || 'undef') eq 'HASH';
  762. return 0 unless defined($plan->{'entries'}) && (reftype($plan->{'entries'}) || 'undef') eq 'ARRAY';
  763. return 0 unless defined($plan->{'entries'}->[0]->{'runs'}) && (reftype($plan->{'entries'}->[0]->{'runs'}) || 'undef') eq 'ARRAY';
  764. return $plan->{'entries'}->[0]->{'runs'};
  765. }
  766. =head2 B<getChildRunByName(plan,name,configurations)>
  767. =over 4
  768. =item HASHREF C<PLAN> - Test Plan definition HASHREF returned by any of the PLAN methods below.
  769. =item STRING C<NAME> - Name of run to search for within plan.
  770. =item ARRAYREF C<CONFIGURATIONS> (optional) - Names of configurations to filter runs by.
  771. =back
  772. Returns run definition HASHREF, or false if no such run is found.
  773. Convenience method using getChildRuns.
  774. =cut
  775. sub getChildRunByName {
  776. my ($self,$plan,$name,$configurations) = @_;
  777. confess("Object methods must be called by an instance") unless ref($self);
  778. confess("Plan must be HASHREF") unless defined($plan) && (reftype($plan) || 'undef') eq 'HASH';
  779. confess("Run name must be STRING") unless $self->_checkString($name);
  780. confess("Configurations must be ARRAYREF") unless !defined($configurations) || (reftype($configurations) || 'undef') eq 'ARRAY';
  781. my $runs = $self->getChildRuns($plan);
  782. return 0 if !$runs;
  783. my @pconfigs = ();
  784. #Figure out desired config IDs
  785. if (defined $configurations) {
  786. my $avail_configs = $self->getConfigurations($plan->{'project_id'});
  787. my ($cname);
  788. @pconfigs = map {$_->{'id'}} grep { $cname = $_->{'name'}; grep {$_ eq $cname} @$configurations } @$avail_configs; #Get a list of IDs from the names passed
  789. }
  790. foreach my $run (@$runs) {
  791. #Compare run config IDs against desired, invalidate run if all conditions not satisfied
  792. foreach my $cid (@{$run->{'config_ids'}}) {
  793. next unless List::Util::all {$_ eq $cid} @pconfigs;
  794. }
  795. return $run if $run->{name} eq $name;
  796. }
  797. return 0;
  798. }
  799. =head1 PLAN METHODS
  800. =head2 B<createPlan (project_id,name,description,milestone_id,entries)>
  801. Create a test plan.
  802. =over 4
  803. =item INTEGER C<PROJECT ID> - ID of parent project.
  804. =item STRING C<NAME> - Name of plan
  805. =item STRING C<DESCRIPTION> (optional) - Description of plan
  806. =item INTEGER C<MILESTONE_ID> (optional) - ID of milestone
  807. =item ARRAYREF C<ENTRIES> (optional) - New Runs to initially populate the plan with -- See TestRail API documentation for more advanced inputs here.
  808. =back
  809. Returns test plan definition HASHREF, or false on failure.
  810. $entries = {
  811. suite_id => 345,
  812. include_all => 1,
  813. assignedto_id => 1
  814. }
  815. $tr->createPlan(1,'Gosplan','Robo-Signed Soviet 5-year plan',22,$entries);
  816. =cut
  817. sub createPlan {
  818. my ($self,$project_id,$name,$desc,$milestone_id,$entries) = @_;
  819. confess("Object methods must be called by an instance") unless ref($self);
  820. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  821. confess("Plan name must be string") unless $self->_checkString($name);
  822. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  823. confess("Milestone ID must be integer") unless !defined($milestone_id) || $self->_checkInteger($milestone_id);
  824. confess("Entries must be ARRAYREF") unless !defined($entries) || (reftype($entries) || 'undef') eq 'ARRAY';
  825. my $stuff = {
  826. name => $name,
  827. description => $desc,
  828. milestone_id => $milestone_id,
  829. entries => $entries
  830. };
  831. my $result = $self->_doRequest("index.php?/api/v2/add_plan/$project_id",'POST',$stuff);
  832. return $result;
  833. }
  834. =head2 B<deletePlan (plan_id)>
  835. Deletes specified plan.
  836. =over 4
  837. =item INTEGER C<PLAN ID> - ID of plan to delete.
  838. =back
  839. Returns BOOLEAN.
  840. $tr->deletePlan(8675309);
  841. =cut
  842. sub deletePlan {
  843. my ($self,$plan_id) = @_;
  844. confess("Object methods must be called by an instance") unless ref($self);
  845. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  846. my $result = $self->_doRequest("index.php?/api/v2/delete_plan/$plan_id",'POST');
  847. return $result;
  848. }
  849. =head2 B<getPlans (project_id,get_runs)>
  850. Gets specified test plans.
  851. =over 4
  852. =item INTEGER C<PROJECT ID> - ID of parent project.
  853. =back
  854. Returns ARRAYREF of plan definition HASHREFs.
  855. $tr->getPlans(8);
  856. Does not contain any information about child test runs.
  857. Use getRunByID or getRunByName if you want that, in particular if you are interested in using getChildRunByName.
  858. =cut
  859. sub getPlans {
  860. my ($self,$project_id) = @_;
  861. confess("Object methods must be called by an instance") unless ref($self);
  862. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  863. return $self->_doRequest("index.php?/api/v2/get_plans/$project_id");
  864. }
  865. =head2 B<getPlanByName (project_id,name)>
  866. Gets specified plan by name.
  867. =over 4
  868. =item INTEGER C<PROJECT ID> - ID of parent project.
  869. =item STRING C<NAME> - Name of test plan.
  870. =back
  871. Returns plan definition HASHREF.
  872. $tr->getPlanByName(8,'GosPlan');
  873. =cut
  874. sub getPlanByName {
  875. my ($self,$project_id,$name) = @_;
  876. confess("Object methods must be called by an instance") unless ref($self);
  877. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  878. confess("Plan name must be string") unless $self->_checkString($name);
  879. my $plans = $self->getPlans($project_id);
  880. return -500 if !$plans || (reftype($plans) || 'undef') ne 'ARRAY';
  881. foreach my $plan (@$plans) {
  882. if ($plan->{'name'} eq $name) {
  883. return $self->getPlanByID($plan->{'id'});
  884. }
  885. }
  886. return 0;
  887. }
  888. =head2 B<getPlanByID (plan_id)>
  889. Gets specified plan by ID.
  890. =over 4
  891. =item INTEGER C<PLAN ID> - ID of plan.
  892. =back
  893. Returns plan definition HASHREF.
  894. $tr->getPlanByID(2);
  895. =cut
  896. sub getPlanByID {
  897. my ($self,$plan_id) = @_;
  898. confess("Object methods must be called by an instance") unless ref($self);
  899. confess("Plan ID must be integer") unless $self->_checkInteger($plan_id);
  900. return $self->_doRequest("index.php?/api/v2/get_plan/$plan_id");
  901. }
  902. =head1 MILESTONE METHODS
  903. =head2 B<createMilestone (project_id,name,description,due_on)>
  904. Create a milestone.
  905. =over 4
  906. =item INTEGER C<PROJECT ID> - ID of parent project.
  907. =item STRING C<NAME> - Name of milestone
  908. =item STRING C<DESCRIPTION> (optional) - Description of milestone
  909. =item INTEGER C<DUE_ON> - Date at which milestone should be completed. Unix Timestamp.
  910. =back
  911. Returns milestone definition HASHREF, or false on failure.
  912. $tr->createMilestone(1,'Patriotic victory of world perlism','Accomplish by Robo-Signed Soviet 5-year plan',time()+157788000);
  913. =cut
  914. sub createMilestone {
  915. my ($self,$project_id,$name,$desc,$due_on) = @_;
  916. confess("Object methods must be called by an instance") unless ref($self);
  917. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  918. confess("Name must be string") unless $self->_checkString($name);
  919. confess("Description must be string") unless !defined($desc) || $self->_checkString($desc);
  920. confess("Due on must be unix time stamp (integer)") unless !defined($due_on) || $self->_checkInteger($due_on);
  921. my $stuff = {
  922. name => $name,
  923. description => $desc,
  924. due_on => $due_on # unix timestamp
  925. };
  926. my $result = $self->_doRequest("index.php?/api/v2/add_milestone/$project_id",'POST',$stuff);
  927. return $result;
  928. }
  929. =head2 B<deleteMilestone (milestone_id)>
  930. Deletes specified milestone.
  931. =over 4
  932. =item INTEGER C<MILESTONE ID> - ID of milestone to delete.
  933. =back
  934. Returns BOOLEAN.
  935. $tr->deleteMilestone(86);
  936. =cut
  937. sub deleteMilestone {
  938. my ($self,$milestone_id) = @_;
  939. confess("Object methods must be called by an instance") unless ref($self);
  940. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  941. my $result = $self->_doRequest("index.php?/api/v2/delete_milestone/$milestone_id",'POST');
  942. return $result;
  943. }
  944. =head2 B<getMilestones (project_id)>
  945. Get milestones for some project.
  946. =over 4
  947. =item INTEGER C<PROJECT ID> - ID of parent project.
  948. =back
  949. Returns ARRAYREF of milestone definition HASHREFs.
  950. $tr->getMilestones(8);
  951. =cut
  952. sub getMilestones {
  953. my ($self,$project_id) = @_;
  954. confess("Object methods must be called by an instance") unless ref($self);
  955. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  956. return $self->_doRequest("index.php?/api/v2/get_milestones/$project_id");
  957. }
  958. =head2 B<getMilestoneByName (project_id,name)>
  959. Gets specified milestone by name.
  960. =over 4
  961. =item INTEGER C<PROJECT ID> - ID of parent project.
  962. =item STRING C<NAME> - Name of milestone.
  963. =back
  964. Returns milestone definition HASHREF.
  965. $tr->getMilestoneByName(8,'whee');
  966. =cut
  967. sub getMilestoneByName {
  968. my ($self,$project_id,$name) = @_;
  969. confess("Object methods must be called by an instance") unless ref($self);
  970. confess("Project ID must be integer") unless $self->_checkInteger($project_id);
  971. confess("Milestone name must be string") unless $self->_checkString($name);
  972. my $milestones = $self->getMilestones($project_id);
  973. return -500 if !$milestones || (reftype($milestones) || 'undef') ne 'ARRAY';
  974. foreach my $milestone (@$milestones) {
  975. return $milestone if $milestone->{'name'} eq $name;
  976. }
  977. return 0;
  978. }
  979. =head2 B<getMilestoneByID (milestone_id)>
  980. Gets specified milestone by ID.
  981. =over 4
  982. =item INTEGER C<MILESTONE ID> - ID of milestone.
  983. =back
  984. Returns milestone definition HASHREF.
  985. $tr->getMilestoneByID(2);
  986. =cut
  987. sub getMilestoneByID {
  988. my ($self,$milestone_id) = @_;
  989. confess("Object methods must be called by an instance") unless ref($self);
  990. confess("Milestone ID must be integer") unless $self->_checkInteger($milestone_id);
  991. return $self->_doRequest("index.php?/api/v2/get_milestone/$milestone_id");
  992. }
  993. =head1 TEST METHODS
  994. =head2 B<getTests (run_id)>
  995. Get tests for some run.
  996. =over 4
  997. =item INTEGER C<RUN ID> - ID of parent run.
  998. =back
  999. Returns ARRAYREF of test definition HASHREFs.
  1000. $tr->getTests(8);
  1001. =cut
  1002. sub getTests {
  1003. my ($self,$run_id) = @_;
  1004. confess("Object methods must be called by an instance") unless ref($self);
  1005. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  1006. return $self->_doRequest("index.php?/api/v2/get_tests/$run_id");
  1007. }
  1008. =head2 B<getTestByName (run_id,name)>
  1009. Gets specified test by name.
  1010. =over 4
  1011. =item INTEGER C<RUN ID> - ID of parent run.
  1012. =item STRING C<NAME> - Name of milestone.
  1013. =back
  1014. Returns test definition HASHREF.
  1015. $tr->getTestByName(36,'wheeTest');
  1016. =cut
  1017. sub getTestByName {
  1018. my ($self,$run_id,$name) = @_;
  1019. confess("Object methods must be called by an instance") unless ref($self);
  1020. confess("Run ID must be integer") unless $self->_checkInteger($run_id);
  1021. confess("Test name must be string") unless $self->_checkString($name);
  1022. my $tests = $self->getTests($run_id);
  1023. return -500 if !$tests || (reftype($tests) || 'undef') ne 'ARRAY';
  1024. foreach my $test (@$tests) {
  1025. return $test if $test->{'title'} eq $name;
  1026. }
  1027. return 0;
  1028. }
  1029. =head2 B<getTestByID (test_id)>
  1030. Gets specified test by ID.
  1031. =over 4
  1032. =item INTEGER C<TEST ID> - ID of test.
  1033. =back
  1034. Returns test definition HASHREF.
  1035. $tr->getTestByID(222222);
  1036. =cut
  1037. sub getTestByID {
  1038. my ($self,$test_id) = @_;
  1039. confess("Object methods must be called by an instance") unless ref($self);
  1040. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  1041. return $self->_doRequest("index.php?/api/v2/get_test/$test_id");
  1042. }
  1043. =head2 B<getTestResultFields()>
  1044. Gets custom fields that can be set for tests.
  1045. Returns ARRAYREF of result definition HASHREFs.
  1046. =cut
  1047. sub getTestResultFields {
  1048. my $self = shift;
  1049. confess("Object methods must be called by an instance") unless ref($self);
  1050. return $self->{'tr_fields'} if defined($self->{'tr_fields'}); #cache
  1051. $self->{'tr_fields'} = $self->_doRequest('index.php?/api/v2/get_result_fields');
  1052. return $self->{'tr_fields'};
  1053. }
  1054. =head2 B<getTestResultFieldByName(SYSTEM_NAME,PROJECT_ID)>
  1055. Gets a test result field by it's system name. Optionally filter by project ID.
  1056. =over 4
  1057. =item B<SYSTEM NAME> - STRING: system name of a result field.
  1058. =item B<PROJECT ID> - INTEGER (optional): Filter by whether or not the field is enabled for said project
  1059. =back
  1060. =cut
  1061. sub getTestResultFieldByName {
  1062. my ($self,$system_name,$project_id) = @_;
  1063. confess("Object methods must be called by an instance") unless ref($self);
  1064. confess("System name must be string") unless $self->_checkString($system_name);
  1065. my @candidates = grep {$_->{'name'} eq $system_name} @{$self->getTestResultFields()};
  1066. return 0 if !scalar(@candidates);
  1067. if (defined $project_id) {
  1068. @candidates = grep {
  1069. $_->{'configs'}->[0]->{'context'}->{'is_global'} ||
  1070. ( grep {$_ == $project_id} @{ $_->{'configs'}->[0]->{'context'}->{'project_ids'} } )
  1071. } @candidates;
  1072. }
  1073. return $candidates[0];
  1074. }
  1075. =head2 B<getPossibleTestStatuses()>
  1076. Gets all possible statuses a test can be set to.
  1077. Returns ARRAYREF of status definition HASHREFs.
  1078. =cut
  1079. sub getPossibleTestStatuses {
  1080. my $self = shift;
  1081. confess("Object methods must be called by an instance") unless ref($self);
  1082. return $self->_doRequest('index.php?/api/v2/get_statuses');
  1083. }
  1084. =head2 B<createTestResults(test_id,status_id,comment,options,custom_options)>
  1085. Creates a result entry for a test.
  1086. =over 4
  1087. =item INTEGER C<TEST_ID> - ID of desired test
  1088. =item INTEGER C<STATUS_ID> - ID of desired test result status
  1089. =item STRING C<COMMENT> (optional) - Any comments about this result
  1090. =item HASHREF C<OPTIONS> (optional) - Various "Baked-In" options that can be set for test results. See TR docs for more information.
  1091. =item HASHREF C<CUSTOM OPTIONS> (optional) - Options to set for custom fields. See buildStepResults for a simple way to post up custom steps.
  1092. =back
  1093. Returns result definition HASHREF.
  1094. $options = {
  1095. elapsed => '30m 22s',
  1096. defects => ['TSR-3','BOOM-44'],
  1097. version => '6969'
  1098. };
  1099. $custom_options = {
  1100. step_results => [
  1101. {
  1102. content => 'Step 1',
  1103. expected => "Bought Groceries",
  1104. actual => "No Dinero!",
  1105. status_id => 2
  1106. },
  1107. {
  1108. content => 'Step 2',
  1109. expected => 'Ate Dinner',
  1110. actual => 'Went Hungry',
  1111. status_id => 2
  1112. }
  1113. ]
  1114. };
  1115. $res = $tr->createTestResults(1,2,'Test failed because it was all like WAAAAAAA when I poked it',$options,$custom_options);
  1116. =cut
  1117. sub createTestResults {
  1118. my ($self,$test_id,$status_id,$comment,$opts,$custom_fields) = @_;
  1119. confess("Object methods must be called by an instance") unless ref($self);
  1120. confess("Test ID must be integer") unless $self->_checkInteger($test_id);
  1121. confess("Status ID must be integer") unless $self->_checkInteger($status_id);
  1122. confess("Comment must be string") unless !defined($comment) || $self->_checkString($comment);
  1123. confess("Options must be HASHREF") unless !defined($opts) || (reftype($opts) || 'undef') eq 'HASH';
  1124. confess("Custom Options must be HASHREF") unless !defined($custom_fields) || (reftype($custom_fields) || 'undef') eq 'HASH';
  1125. my $stuff = {
  1126. status_id => $status_id,
  1127. comment => $comment
  1128. };
  1129. #Handle options
  1130. if (defined($opts) && reftype($opts) eq 'HASH') {
  1131. $stuff->{'version'} = defined($opts->{'version'}) ? $opts->{'version'} : undef;
  1132. $stuff->{'elapsed'} = defined($opts->{'elapsed'}) ? $opts->{'elapsed'} : undef;
  1133. $stuff->{'defects'} = defined($opts->{'defects'}) ? join(',',@{$opts->{'defects'}}) : undef;
  1134. $stuff->{'assignedto_id'} = defined($opts->{'assignedto_id'}) ? $opts->{'assignedto_id'} : undef;
  1135. }
  1136. #Handle custom fields
  1137. if (defined($custom_fields) && reftype($custom_fields) eq 'HASH') {
  1138. foreach my $field (keys(%$custom_fields)) {
  1139. $stuff->{"custom_$field"} = $custom_fields->{$field};
  1140. }
  1141. }
  1142. return $self->_doRequest("index.php?/api/v2/add_result/$test_id",'POST',$stuff);
  1143. }
  1144. =head2 B<getTestResults(test_id,limit,offset)>
  1145. Get the recorded results for desired test, limiting output to 'limit' entries.
  1146. =over 4
  1147. =item INTEGER C<TEST_ID> - ID of desired test
  1148. =item POSITIVE INTEGER C<LIMIT> (OPTIONAL) - provide no more than this number of results.
  1149. =item INTEGER C<OFFSET> (OPTIONAL) - Offset to begin viewing result set at.
  1150. =back
  1151. Returns ARRAYREF of result definition HASHREFs.
  1152. =cut
  1153. sub getTestResults {
  1154. my ($self,$test_id,$limit,$offset) = @_;
  1155. confess("Object methods must be called by an instance") unless ref($self);
  1156. confess("Test ID must be positive integer") unless $self->_checkInteger($test_id);
  1157. confess("Result limitation must be positive integer") unless !defined($limit) || ($self->_checkInteger($limit) && $limit > 0);
  1158. confess("Result offset must be integer") unless !defined($offset) || $self->_checkInteger($offset);
  1159. my $url = "index.php?/api/v2/get_results/$test_id";
  1160. $url .= "&limit=$limit" if defined($limit);
  1161. $url .= "&offset=$offset" if defined($offset);
  1162. return $self->_doRequest($url);
  1163. }
  1164. =head1 CONFIGURATION METHODS
  1165. =head2 B<getConfigurations(project_id)>
  1166. Gets the available configurations for a project.
  1167. =over 4
  1168. =item INTEGER C<PROJECT_ID> - ID of relevant project
  1169. =back
  1170. Returns ARRAYREF of configuration definition HASHREFs.
  1171. =cut
  1172. sub getConfigurations {
  1173. my ($self,$project_id) = @_;
  1174. confess("Object methods must be called by an instance") unless ref($self);
  1175. confess("Test ID must be positive integer") unless $self->_checkInteger($project_id);
  1176. my $url = "index.php?/api/v2/get_configs/$project_id";
  1177. return $self->{'configurations'} if $self->{'configurations'}; #cache this since we can't change it with the API
  1178. $self->{'configurations'} = $self->_doRequest($url);
  1179. return $self->{'configurations'};
  1180. }
  1181. =head1 STATIC METHODS
  1182. =head2 B<buildStepResults(content,expected,actual,status_id)>
  1183. Convenience method to build the stepResult hashes seen in the custom options for getTestResults.
  1184. =over 4
  1185. =item STRING C<CONTENT> (optional) - The step itself.
  1186. =item STRING C<EXPECTED> (optional) - Expected result of test step.
  1187. =item STRING C<ACTUAL> (optional) - Actual result of test step
  1188. =item INTEGER C<STATUS ID> (optional) - Status ID of result
  1189. =back
  1190. =cut
  1191. #Convenience method for building stepResults
  1192. sub buildStepResults {
  1193. my ($content,$expected,$actual,$status_id) = @_;
  1194. return {
  1195. content => $content,
  1196. expected => $expected,
  1197. actual => $actual,
  1198. status_id => $status_id
  1199. };
  1200. }
  1201. #Type checks
  1202. sub _checkInteger {
  1203. shift;
  1204. my $integer = shift;
  1205. return ( defined $integer && looks_like_number($integer) && int($integer) == $integer );
  1206. }
  1207. sub _checkString {
  1208. shift;
  1209. my $str = shift;
  1210. return ( defined($str) && !ref($str) );
  1211. }
  1212. 1;
  1213. __END__
  1214. =head1 SEE ALSO
  1215. L<HTTP::Request>
  1216. L<LWP::UserAgent>
  1217. L<JSON::MaybeXS>
  1218. L<http://docs.gurock.com/testrail-api2/start>
  1219. =head1 SPECIAL THANKS
  1220. Thanks to cPanel Inc, for graciously funding the creation of this module.