API.pm 30 KB

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