Repo.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Stores data and methods related to a single repository.
  5. *
  6. * By now the following are supported:
  7. *
  8. * * GET `/repos/username/reponame`
  9. * * POST `/user/repos`
  10. * * POST `/admin/user/username/repos`
  11. * * POST `/org/orgname/repos`
  12. * * DELETE `/repos/username/reponame`
  13. *
  14. * @author Joachim M. Giaever (joachim[]giaever.org)
  15. * @version 0.1.3
  16. */
  17. final class Repo extends Base {
  18. public $repo_id;
  19. public $repo_owner;
  20. public $repo_name;
  21. public $repo_full_name;
  22. public $repo_description;
  23. public $repo_private;
  24. public $repo_fork;
  25. public $repo_parent;
  26. public $repo_empty;
  27. public $repo_mirror;
  28. public $repo_size;
  29. public $repo_html_url;
  30. public $repo_ssh_url;
  31. public $repo_clone_url;
  32. public $repo_website;
  33. public $repo_stars_count;
  34. public $repo_forks_count;
  35. public $repo_watchers_count;
  36. public $repo_open_issues_count;
  37. public $repo_default_branch;
  38. public $repo_created_at;
  39. public $repo_updated_at;
  40. public $repo_permissions;
  41. /**
  42. * Initialize a repo object.
  43. *
  44. * Note that the owner can also be an Org (organization),
  45. * or any other class that inherits a user.
  46. *
  47. * @see Base
  48. * @param User $owner The owner of the repo
  49. * @param string $name The repo name
  50. */
  51. public function __construct(string $api_url, string $api_token, User $owner = null, string $name = null) {
  52. parent::__construct($api_url, $api_token);
  53. $this->owner = $owner;
  54. $this->name = $name;
  55. }
  56. /**
  57. * @see Base
  58. * @throws Exception\RequestErrorException on missing repo or user data
  59. */
  60. protected function set_scope(string $method) {
  61. switch ($method) {
  62. case "create":
  63. if (!$this->owner->authenticated() && empty($this->owner->username))
  64. throw new Exception\RequestErrorException("Missing userdata of unauthorized user 'username'");
  65. if ($this->owner instanceof Org)
  66. $this->scope = "/org/" . $this->owner->username . "/repos";
  67. elseif ($this->owner->authenticated())
  68. $this->scope = "/user/repos";
  69. else
  70. $this->scope = "/admin/users/" . $this->owner->username . "/repos";
  71. break;
  72. case "delete":
  73. if (empty($this->owner->username))
  74. throw new Exception\RequestErrorException("Missing userdata 'username'");
  75. $this->scope = "/repos/" . $this->owner->username . "/" . $this->name;
  76. break;
  77. case "get":
  78. case "load":
  79. if (empty($this->owner->username) && empty($this->full_name))
  80. throw new Exception\RequestErrorException("Missing userdata 'username' and/or 'full_name'");
  81. $this->scope = "/repos/" . ($this->owner ? $this->owner->username . "/" . $this->name : $this->full_name);
  82. break;
  83. default:
  84. return false;
  85. }
  86. return true;
  87. }
  88. /**
  89. * Return branches for repository.
  90. *
  91. * @return Branches
  92. */
  93. public function branches() {
  94. return new Branches($this->url, $this->token, $this);
  95. }
  96. /**
  97. * Overrides Base method as this should set owner as well
  98. *
  99. * @see Base
  100. */
  101. protected function json_set_property(\stdClass $obj) {
  102. foreach($obj as $key => $val) {
  103. if ($this->property_exists($key)) {
  104. switch ($key) {
  105. case "owner":
  106. if (!$this->owner) {
  107. $user = new User($this->url, $this->token);
  108. $user->json_set_property($val);
  109. $this->{$key} = $user;
  110. }
  111. break;
  112. default:
  113. $this->{$key} = $val;
  114. }
  115. }
  116. }
  117. $this->loaded = true;
  118. return true;
  119. }
  120. /**
  121. * Create a new repo
  122. *
  123. * Valid paramters:
  124. *
  125. * 1. name
  126. * 2. description
  127. * 3. private (default: false)
  128. * 4. auto_init (default: false)
  129. * 5. gitignore
  130. * 6. license
  131. * 7. readme (default: "Default")
  132. *
  133. * This reflects the API v1 documentation, but is in an order
  134. * where the required fields are first.
  135. *
  136. * @param ...$args The parameter values
  137. * @return bool
  138. */
  139. public function create(...$args) {
  140. $params = array(
  141. "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  142. "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  143. "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
  144. "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
  145. "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  146. "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
  147. "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
  148. );
  149. $params = array_filter($params, function($val) {
  150. return $val != null;
  151. });
  152. parent::create($params);
  153. }
  154. }
  155. }
  156. ?>