index.php 8.1 KB

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