Repo.php 8.6 KB

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