index.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. define('API_URL', 'https://git.giaever.org/api/v1');
  11. //define('API_TOKEN', '142efbfd6fbdf147f03d289f8b22a438eaa1b5d1');
  12. define('API_TOKEN', 'e14b9eff0749b6f0c4cadf4bb72b83d44578ae28');
  13. $client = new Gogs\API\Client(API_URL, API_TOKEN);
  14. try {
  15. $me = $client->user()->load();
  16. $tester = "tester";
  17. if (API_TOKEN == "142efbfd6fbdf147f03d289f8b22a438eaa1b5d1")
  18. $tester = "joachimmg";
  19. $repos = $me->repos()->load();
  20. $me->orgs()->load();
  21. /*
  22. $repo = $repos->get("gogs-php-api-client");
  23. var_dump($repo->branches()->load());
  24. */
  25. echo "\nNormal repo\n";
  26. foreach($repos->all() as $key => $repo)
  27. echo sprintf("* %s: %s\n", $key, $repo->name);
  28. echo "\nSorted created\n";
  29. foreach($repos->sort_by(Gogs\API\Request\Repos::SORT_CREATED)->all() as $key => $repo)
  30. echo sprintf("* %s: %s - %s\n", $repo->created_at, $key, $repo->name);
  31. echo "\nSorted created, then reversed\n";
  32. foreach($repos->sort_by(Gogs\API\Request\Repos::SORT_CREATED, true)->all() as $key => $repo)
  33. echo sprintf("* %s: %s - %s\n", $repo->created_at, $key, $repo->name);
  34. echo "\nSorted Normal, offset 1, limit 10\n";
  35. foreach($repos->offset(1)->limit(10)->all() as $key => $repo)
  36. echo sprintf("* %s: %s\n", $key, $repo->name);
  37. echo "\nNormal repo\n";
  38. foreach($repos->all() as $key => $repo)
  39. echo sprintf("* %s: %s\n", $key, $repo->name);
  40. echo "\nSearch for in loaded data for 'dns', limit 10\n";
  41. foreach($repos->search(array("name" => "dns", "limit" => 3))->all() as $key => $repo)
  42. echo sprintf("* %s: %s\n", $key, $repo->name);
  43. echo "\nSearch for in new data for 'dns', limit 10\n";
  44. foreach($me->repos()->search(array("name" => "dns", "limit" => 3))->all() as $key => $repo)
  45. echo sprintf("* %s: %s\n", $key, $repo->name);
  46. echo "\nUsers->search name 't', offset 1:\n";
  47. foreach($client->users()->search(array("name" => "to"))->offset(1)->all() as $key => $user)
  48. echo sprintf("* %s: %s\n", $key, $user->full_name);
  49. $user = $client->users()->get($tester);
  50. echo "\nUser '" . $user->username . "' public repos\n";
  51. foreach($user->repos()->load()->all() as $key => $repo)
  52. echo sprintf("* %s: %s\n", $key, $repo->name);
  53. echo "\nUser '" . $me->username . "' public repos \n";
  54. foreach($client->users()->get($me->username)->repos()->load()->all() as $key => $repo)
  55. echo sprintf("* %s: %s\n", $key, $repo->name);
  56. echo "\nUsers '" . $me->username . "' organizations\n";
  57. foreach($me->organizations()->load()->all() as $key => $org)
  58. echo sprintf("* %s: %s\n", $key, $org->full_name);
  59. echo "\nUser '" . $user->username . "' public organizations\n";
  60. foreach($user->organizations()->load()->all() as $key => $org) {
  61. echo sprintf("* %s: %s\n* Repositories:\n", $key, $org->full_name);
  62. foreach($org->repos()->load()->all() as $key => $repo)
  63. echo sprintf("#### %s: %s\n", $key, $repo->name);
  64. }
  65. echo "Create data under specified user";
  66. $repo = $repos->create(
  67. "test-gogs-api-repo-" . $repos->load()->len(),
  68. "This is test repo #" . $repos->load()->len() . " created with Gogs PHP API Client",
  69. false,
  70. true
  71. );
  72. echo "Created repo " . $repo->name . "\n";
  73. echo "\nLooking up repos of test-test-test-#\n";
  74. foreach($repos->search(array("name" => "test-gogs-api-repo-"))->sort_by()->all() as $key => $repo)
  75. echo sprintf("Deleting: '%s' %s\n", $repo->name, $repo->delete() ? "true" : "false");
  76. $orgs = $me->orgs()->load();
  77. /*
  78. try {
  79. echo "\nCreate organization\n";
  80. $org = $orgs->create(
  81. "test-" . $me->username . "-organization",
  82. $me->full_name . " Testing Organization"
  83. );
  84. echo "Organization '" . $org->username . "' created!";
  85. } catch (ApiException\NotAuthorizedException $e) {
  86. throw new ApiException\NotAuthorizedException("Creating organization", $e->getCode(), $e);
  87. } catch (ApiException\HTTPUnexpectedResponse $e) {
  88. echo $e->getResponse();
  89. }
  90. */
  91. echo "\nLooking up organizations of test-" . $me->username . "\n";
  92. foreach($orgs->search(array("name" => "test-" . $me->username))->all() as $key => $org)
  93. echo sprintf("* '%s': %s\n", $key, $org->username);
  94. $users = $client->users()->search(array("name" => "test-user"));
  95. $users->create(
  96. "test-user-" . $users->len(),
  97. "mytestuser" . $users->len() . "@gogitservice.joke"
  98. );
  99. foreach ($users->all() as $key => $user)
  100. echo sprintf("%s: delete %s\n", $key, $user->delete() ? "true" : "false");
  101. echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
  102. } catch (ApiException\NotAuthorizedException $e) {
  103. echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
  104. die("NOT AUTH: " . $e->getMessage() . "\nResponse: " . $e->getResponse() . "\n" . <<<EOF
  105. POSSIBLY WHAT YOU WANTED, CAUSE IT SEEMS LIKE YOUR
  106. AUTHORIZED USER IS TRYIGN TO PROCESS PARTS OF THE
  107. INDEX THAT ITS NOT AUTHORIZED TO DO, SUCH AS
  108. * Creating organizations
  109. * Creating users
  110. * More? Sure!
  111. EOF
  112. );
  113. } catch (ApiException\HTTPUnexpectedResponse $e) {
  114. echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
  115. die($e);
  116. } catch (Exception $e) {
  117. die($e);
  118. }
  119. ?>