index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. try {
  13. $client = new Gogs\API\Client(API_URL, API_TOKEN);
  14. $me = $client->user()->load();
  15. $user_search = $client->users()->search(array(
  16. "name" => "tester2"
  17. ));
  18. if ($user_search->len() > 0)
  19. echo " * Found user(s) " . var_export($user_search, true);
  20. else
  21. echo " * User 'tester2' not found";
  22. try {
  23. echo " * Creating new repo under autorized user: " . $me->username;
  24. // Create new repo under authorized user
  25. $repo = $me->repos()->create(
  26. /*name*/ "gogs-php-api-client-test",
  27. /*desc*/ "Repository created from test file.",
  28. /*private*/ false,
  29. /*auto init*/ true,
  30. /*git ignore*/ "Vim",
  31. /*license*/ "MIT License"
  32. /* default read me */
  33. );
  34. echo "New repo '" . $repo->full_name . "' created!\n";
  35. } catch(ApiException\HTTPUnexpectedResponse $e) {
  36. // Repo exists, get it!
  37. $repo = $me->repo("gogs-php-api-client-test");
  38. // Delete it (it will now be created again on reload!)
  39. $repo->delete();
  40. echo "Repo '" . $repo->full_name . "' deleted\n";
  41. }
  42. // Loop through organizations
  43. foreach($me->organizations()->load()->all() as $org)
  44. var_dump("ORG", $org->username);
  45. echo sprintf(
  46. "org->username: %s\norg->org_username: %s\norg->user_username: %s\n",
  47. $org->username, $org->org_username, $org->user_username
  48. );
  49. // Requires ADMIN rights to the authenticated user
  50. #try {
  51. // Create new user "tester2"
  52. # $user = $client->users()->new("tester2", "git@git.giaever.org");
  53. # echo "New user '" . $user->uusername . "' created!\n";
  54. // And a new repository under this user
  55. # $repo = $user->repos()->new(
  56. # /*name*/ "gogs-php-api-client-auth-test",
  57. # /*desc*/ "Repository created from test file.",
  58. # /*private*/ false,
  59. # /*auto init*/ true,
  60. # /*git ignore*/ "Vim",
  61. # /*license*/ "MIT License"
  62. # /* default read me */
  63. # );
  64. # echo "New repo '" . $repo->rfull_name . "' created\n";
  65. # $org = $user->organizations()->new(
  66. # /*username*/ "tester-org",
  67. # /*full name*/ "Tester's organization",
  68. # /*desc*/ "Just a test organization",
  69. # /*website*/ "https://git.giaever.org/",
  70. # /*location*/ "Norway"
  71. # );
  72. #}catch (\Lib\NotAuthorizedException $e) {
  73. # echo "User (me) '" . $me->uusername . "' not authorized for this action: " . $e->getMessage();
  74. #} catch (\Lib\HTTPUnexpectedResponse $e) {
  75. // Most likely user exist; then delete it
  76. # $user = $client->users()->get("tester2");
  77. // Delete it's repositories
  78. # foreach ($user->repos()->load()->all() as $repo)
  79. # $repo->delete();
  80. # foreach ($user->organizations()->load()->all() as $org)
  81. # $org->delete();
  82. # $user->delete();
  83. # echo "User '" . $user->uusername . "' deleted!\n";
  84. #}
  85. /*var_dump($me->organizations()->load()->all()["FlyViking"]->repos()->create(
  86. "test-repo-for-org",
  87. "this is desc."
  88. ));*/
  89. } catch (ApiException\NotAuthorizedException $e) {
  90. die("NOT AUTH: " . $e->getMessage());
  91. } catch (ApiException\HTTPUnexpectedResponse $e) {
  92. die($e);
  93. } catch (Exception $e) {
  94. die($e);
  95. }
  96. ?>