Repo.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.4
  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. case "migrate":
  84. $this->scope = "/repos/migrate";
  85. break;
  86. default:
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Return branches for repository.
  93. *
  94. * @return Branches
  95. */
  96. public function branches() {
  97. return new Branches($this->url, $this->token, $this);
  98. }
  99. /**
  100. * Overrides Base method as this should set owner as well
  101. *
  102. * @see Base
  103. */
  104. protected function json_set_property(\stdClass $obj) {
  105. foreach($obj as $key => $val) {
  106. if ($this->property_exists($key)) {
  107. switch ($key) {
  108. case "owner":
  109. if (!$this->owner) {
  110. $user = new User($this->url, $this->token);
  111. $user->json_set_property($val);
  112. $this->{$key} = $user;
  113. }
  114. break;
  115. default:
  116. $this->{$key} = $val;
  117. }
  118. }
  119. }
  120. $this->loaded = true;
  121. return true;
  122. }
  123. /**
  124. * Create a new repo
  125. *
  126. * Valid paramters:
  127. *
  128. * 1. name, required
  129. * 2. description
  130. * 3. private (default: false)
  131. * 4. auto_init (default: false)
  132. * 5. gitignore
  133. * 6. license
  134. * 7. readme (default: "Default")
  135. *
  136. * This reflects the API v1 documentation, but is in an order
  137. * where the required fields are first.
  138. *
  139. * @param ...$args The parameter values
  140. * @return Repo
  141. */
  142. public function create(...$args) {
  143. $params = array();
  144. $this->set_param($params, "name", $args, 0, "string", null);
  145. $this->set_param($params, "description", $args, 1, "string", null);
  146. $this->set_param($params, "private", $args, 2, "bool", false);
  147. $this->set_param($params, "auto_init", $args, 3, "bool", false);
  148. $this->set_param($params, "gitignores", $args, 4, "string", null);
  149. $this->set_param($params, "license", $args, 5, "string", null);
  150. $this->set_param($params, "readme", $args, 6, "string", "Default");
  151. $this->filter_params($params);
  152. return parent::create($params);
  153. }
  154. /**
  155. * Migrate a repository from other Git hosting sources.
  156. *
  157. * Valid parameters:
  158. *
  159. * 1. clone_addr, required
  160. * 3. repo_name, required
  161. * 4. auth_username
  162. * 5. auth_password
  163. * 6. mirror (default: false)
  164. * 7. private (default: false)
  165. * 8. description
  166. *
  167. * **UID** will be set to `owner`. Either a User or an Organization.
  168. * **From API doc**: To migrate a repository for a organization,
  169. * the authenticated user must be a owner of the specified organization.
  170. *
  171. * This reflects the API v1 documentation, but is in an order
  172. * where the required fields as first.
  173. *
  174. * @throws Exception\RequestErrorException when owner not set
  175. * @param ...$args The parameter values
  176. * @return Repo
  177. */
  178. public function migrate(...$args) {
  179. $params = array();
  180. if (empty($this->owner))
  181. throw new Exception\RequestErrorException("Missing required userdata 'uid' or owner must be set");
  182. $this->set_param($params, "clone_addr", $args, 0, "string", null, function(string $url) {
  183. // @todo: URL/PATH validation here?
  184. });
  185. $this->set_param($params, "repo_name", $args, 1, "string", null);
  186. $this->set_param($params, "auth_username", $args, 2, "string", null);
  187. $this->set_param($params, "auth_password", $args, 3, "string", null);
  188. $this->set_param($params, "mirror", $args, 4, "bool", false);
  189. $this->set_param($params, "private", $args, 5, "bool", false);
  190. $this->set_param($params, "description", $args, 6, "string", null);
  191. $this->set_param($params, "uid", array(), 0, "int", $this->owner->id);
  192. $this->filter_params($params);
  193. $this->set_scope("migrate");
  194. $resp = parent::method_post($params);
  195. $this->json_set_property($this->json_decode($resp));
  196. return $this;
  197. }
  198. /**
  199. * Add repo to sync queue.
  200. *
  201. * Requires the repository to be a mirror.
  202. *
  203. * @return bool
  204. */
  205. public function sync() {
  206. if ($this->mirror) {
  207. $this->set_scope("mirror_sync");
  208. $this->method_post();
  209. return true;
  210. }
  211. return false;
  212. }
  213. }
  214. }
  215. ?>