index.php 3.7 KB

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