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