index.php 3.7 KB

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