index.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /*
  3. * TODO:
  4. *
  5. * Rewrite this. Shouldnt be a test file, like its now.
  6. *
  7. * */
  8. require "./src/gpac.php";
  9. use Gogs\Lib\Curl\Exception as ApiException;
  10. // API url
  11. define('API_URL', 'https://git.giaever.org/api/v1');
  12. // The token generated at Gogs
  13. define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
  14. // Edit this one to your authorized accounts password to create tokens.
  15. define('USR_PASS', "mypassword");
  16. // A known user (typically a test user) thats not the authorized one...
  17. define('KNOWN_USR', "tester");
  18. // Known word in repo to search for
  19. define("KNOWN_WORD", "dns");
  20. $client = new Gogs\API\Client(API_URL, API_TOKEN);
  21. try {
  22. /**
  23. * TESTING
  24. */
  25. $me = $client->user()->load();
  26. echo "Authorized user: '" . $me->username . "'\n";
  27. // Load every repo
  28. $repos = $me->repos()->load();
  29. // Loop through all of them in received order
  30. echo "\nNormal repo\n";
  31. foreach($repos->all() as $key => $repo)
  32. echo sprintf("* %s: %s\n", $key, $repo->name);
  33. // Loop through repos sorted on created_at date
  34. echo "\nSorted created\n";
  35. foreach($repos->sort_by(Gogs\API\Request\Repos::SORT_CREATED)->all() as $key => $repo)
  36. echo sprintf("* %s: %s - %s\n", $repo->created_at, $key, $repo->name);
  37. // Loop through repos sorted on created_at date, but ascending order
  38. echo "\nSorted created, then reversed\n";
  39. foreach($repos->sort_by(Gogs\API\Request\Repos::SORT_CREATED, true)->all() as $key => $repo)
  40. echo sprintf("* %s: %s - %s\n", $repo->created_at, $key, $repo->name);
  41. // Loop from offset 1 (skip fist) then 10 repos (11th repos returned)
  42. echo "\nSorted Normal, offset 1, limit 10\n";
  43. foreach($repos->offset(1)->limit(10)->all() as $key => $repo)
  44. echo sprintf("* %s: %s\n", $key, $repo->name);
  45. // Ensure repo order is still intact on $repos ... :)
  46. // Theres returned a copy of "collection" each "sort"
  47. echo "\nNormal repo\n";
  48. foreach($repos->all() as $key => $repo)
  49. echo sprintf("* %s: %s\n", $key, $repo->name);
  50. // Look for a common search word, but max 10 entries
  51. echo "\nSearch for in loaded data for '" . KNOWN_WORD . "', limit 10\n";
  52. foreach($repos->search(array("name" => KNOWN_WORD, "limit" => 3))->all() as $key => $repo)
  53. echo sprintf("* %s: %s\n", $key, $repo->name);
  54. // Search for the two first letters of known user.
  55. // NOTE! Several users may start on these letters,
  56. // and offset is 1 so another user that matches,
  57. // may be returned.
  58. $st = substr(KNOWN_USR, 0, 2);
  59. echo "\nUsers->search name '" . $st . "', offset 1:\n";
  60. foreach($client->users()->search(array("name" => $st))->offset(1)->all() as $key => $user)
  61. echo sprintf("* %s: %s\n", $key, $user->full_name);
  62. $user = $client->users()->get(KNOWN_USR);
  63. // Get public repos
  64. echo "\nUser '" . $user->username . "' public repos\n";
  65. foreach($user->repos()->load()->all() as $key => $repo)
  66. echo sprintf("* %s: %s\n", $key, $repo->name);
  67. // Get authorized user's public repos; bug!
  68. echo "\nUser '" . $me->username . "' public(nope, bug in Gogs....) repos \n";
  69. foreach($client->users()->get($me->username)->repos()->load()->all() as $key => $repo)
  70. echo sprintf("* %s: %s\n", $key, $repo->name);
  71. // Get public organizations
  72. echo "\nUser '" . $user->username . "' public organizations\n";
  73. foreach($user->organizations()->load()->all() as $key => $org) {
  74. echo sprintf("* %s: %s\n* Repositories:\n", $key, $org->full_name);
  75. // Organization repos? Yes sir!
  76. foreach($org->repos()->load()->all() as $key => $repo)
  77. echo sprintf("#### %s: %s\n", $key, $repo->name);
  78. }
  79. // Get authorized user's repos; BUG here too! :(
  80. echo "\nUser '" . $me->username . "' public(nope, bug in Gogs....) organizations\n";
  81. foreach($me->organizations()->load()->all() as $key => $org) {
  82. echo sprintf("* %s: %s\n", $key, $org->full_name);
  83. // Organization repos :)
  84. foreach($org->repos()->load()->all() as $key => $repo)
  85. echo sprintf("#### %s: %s\n", $key, $repo->name);
  86. }
  87. // Creating a test repo under authorized user
  88. echo "Create data under specified user\n";
  89. $repo = $repos->create(
  90. "test-gogs-api-repo-" . $repos->load()->len(),
  91. "This is test repo #" . $repos->load()->len() . " created with Gogs PHP API Client",
  92. false,
  93. true
  94. );
  95. echo "Created repo " . $repo->name . "\n";
  96. // Deleting this repo again.... And possibly others starting
  97. // with this bogus prefix.
  98. echo "\nLooking up repos of test-test-test-#\n";
  99. foreach($repos->search(array("name" => "test-gogs-api-repo-"))->sort_by()->all() as $key => $repo)
  100. echo sprintf("... and deleting test repo: '%s' %s\n", $repo->name, $repo->delete() ? "true" : "false");
  101. echo "\nMigrate repo 'gogs-php-api-client.git'\n";
  102. $mrepo = $repos->create()->migrate("https://git.giaever.org/joachimmg/gogs-php-api-client.git", "gogs-php-api-client-migrate");
  103. echo "Syncing repository '" . $mrepo->full_name . "': " . ($mrepo->sync() ? "true" : "false") . "\n";
  104. echo sprintf("Delete migrated repo: %s\n", $mrepo->delete());
  105. echo "\nMigrate repo (mirror) 'gogs-php-api-client.git'\n";
  106. $mrepo = $repos->create()->migrate(
  107. "https://git.giaever.org/joachimmg/gogs-php-api-client.git",
  108. "gogs-php-api-client-migrate-mirror",
  109. null, null,
  110. true
  111. );
  112. echo "Syncing repository '" . $mrepo->full_name . "': " . ($mrepo->sync() ? "true" : "false") . "\n";
  113. echo sprintf("Delete migrated repo: %s\n", $mrepo->delete());
  114. // Load all of my organizations.
  115. $orgs = $me->orgs()->load();
  116. /***
  117. * NOW WE STARTS WITH METHODS THAT REQUIRES A TOKEN
  118. * FOR AN AUTHORIZED USER THAT HAS ADMIN RIGHTS!
  119. *
  120. * Read exception carefully if you get one! ;)
  121. */
  122. /*
  123. * THIS IS LEFT OUT OF EVERY TEST, NO METHOD TO DELETE;
  124. * ...other than manually. And its so boring to do this all
  125. * the time....
  126. * Uncomment to test...
  127. try {
  128. echo "\nCreate organization\n";
  129. $org = $orgs->create(
  130. "test-" . $me->username . "-organization",
  131. $me->full_name . " Testing Organization"
  132. );
  133. echo "Organization '" . $org->username . "' created!";
  134. } catch (ApiException\NotAuthorizedException $e) {
  135. throw new ApiException\NotAuthorizedException("Creating organization", $e->getCode(), $e);
  136. } catch (ApiException\HTTPUnexpectedResponse $e) {
  137. echo $e->getResponse();
  138. }
  139. */
  140. // Look for a test organization
  141. // NOTE! Most likely not showing up unless you
  142. // uncomment stuff above.
  143. echo "\nLooking up organizations of test-" . $me->username . "\n";
  144. foreach($orgs->search(array("name" => "test-" . $me->username))->all() as $key => $org)
  145. echo sprintf("* '%s': %s\n", $key, $org->username);
  146. // Get users (without loading data!)
  147. $users = $client->users();
  148. // Create new user
  149. $nuser = $users->create(
  150. KNOWN_USR . "-" . $users->len(),
  151. KNOWN_USR . $users->len() . "@gogitservice.joke"
  152. );
  153. // Delete test users....
  154. // Note! As this Users object isnt loaded (->load())
  155. // the Users-Collection only contains 1 user
  156. // - the newly create one!
  157. echo "Delete user '" . $nuser->username . "\n";
  158. foreach ($users->all() as $key => $user)
  159. echo sprintf("%s: delete %s\n", $key, $user->delete() ? "true" : "false");
  160. echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
  161. } catch (ApiException\NotAuthorizedException $e) {
  162. echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
  163. die("NOT AUTH: " . $e->getMessage() . "\nResponse: " . $e->getResponse() . "\n" . <<<EOF
  164. POSSIBLY WHAT YOU WANTED, CAUSE IT SEEMS LIKE YOUR
  165. AUTHORIZED USER IS TRYIGN TO PROCESS PARTS OF THE
  166. INDEX THAT ITS NOT AUTHORIZED TO DO, SUCH AS
  167. * Creating organizations
  168. * Creating users
  169. * More? Sure!
  170. EOF
  171. );
  172. } catch (ApiException\HTTPUnexpectedResponse $e) {
  173. echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
  174. die($e);
  175. } catch (Exception $e) {
  176. die($e);
  177. }
  178. ?>