123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace Gogs\API\Request {
- /**
- * Stores data and methods related to a single repository.
- *
- * By now the following are supported:
- *
- * * GET `/repos/username/reponame`
- * * POST `/user/repos`
- * * POST `/admin/user/username/repos`
- * * POST `/org/orgname/repos`
- * * DELETE `/repos/username/reponame`
- *
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @version 0.1.4
- */
- final class Repo extends Base {
-
- public $repo_id;
- public $repo_owner;
- public $repo_name;
- public $repo_full_name;
- public $repo_description;
- public $repo_private;
- public $repo_fork;
- public $repo_parent;
- public $repo_empty;
- public $repo_mirror;
- public $repo_size;
- public $repo_html_url;
- public $repo_ssh_url;
- public $repo_clone_url;
- public $repo_website;
- public $repo_stars_count;
- public $repo_forks_count;
- public $repo_watchers_count;
- public $repo_open_issues_count;
- public $repo_default_branch;
- public $repo_created_at;
- public $repo_updated_at;
- public $repo_permissions;
-
- public function __construct(string $api_url, string $api_token, User $owner = null, string $name = null) {
- parent::__construct($api_url, $api_token);
- $this->owner = $owner;
- $this->name = $name;
- }
-
- protected function set_scope(string $method) {
- switch ($method) {
- case "create":
- if (empty($this->owner) || !$this->owner->authenticated() && empty($this->owner->username))
- throw new Exception\RequestErrorException("Missing userdata of unauthorized user 'username'");
- if ($this->owner instanceof Org)
- $this->scope = "/org/" . $this->owner->username . "/repos";
- elseif ($this->owner->authenticated())
- $this->scope = "/user/repos";
- else
- $this->scope = "/admin/users/" . $this->owner->username . "/repos";
- break;
- case "delete":
- if (empty($this->owner) || empty($this->owner->username))
- throw new Exception\RequestErrorException("Missing userdata 'username'");
- $this->scope = "/repos/" . $this->owner->username . "/" . $this->name;
- break;
- case "get":
- case "load":
- if ((empty($this->owner) || empty($this->owner->username)) && empty($this->full_name))
- throw new Exception\RequestErrorException("Missing userdata 'username' and/or 'full_name'");
- $this->scope = "/repos/" . ($this->owner ? $this->owner->username . "/" . $this->name : $this->full_name);
- break;
- case "migrate":
- $this->scope = "/repos/migrate";
- break;
- case "sync":
- if (empty($this->owner) || empty($this->owner->username))
- throw new Exception\RequestErrorException("Missing userdata 'username'");
- $this->scope = sprintf("/repos/%s/%s/mirror-sync", $this->owner->username, $this->name);
- break;
- default:
- return false;
- }
- return true;
- }
-
- public function branches() {
- return new Branches($this->url, $this->token, $this);
- }
-
- protected function json_set_property(\stdClass $obj) {
- foreach($obj as $key => $val) {
- if ($this->property_exists($key)) {
- switch ($key) {
- case "owner":
- if (!$this->owner) {
- $user = new User($this->url, $this->token);
- $user->json_set_property($val);
- $this->{$key} = $user;
- }
- break;
- default:
- $this->{$key} = $val;
- }
- }
- }
- $this->loaded = true;
- return true;
- }
-
- public function create(...$args) {
- $params = array();
- $this->set_param($params, "name", $args, 0, "string", null);
- $this->set_param($params, "description", $args, 1, "string", null);
- $this->set_param($params, "private", $args, 2, "bool", false);
- $this->set_param($params, "auto_init", $args, 3, "bool", false);
- $this->set_param($params, "gitignores", $args, 4, "string", null);
- $this->set_param($params, "license", $args, 5, "string", null);
- $this->set_param($params, "readme", $args, 6, "string", "Default");
-
- $this->filter_params($params);
- return parent::create($params);
- }
-
- public function migrate(...$args) {
- $params = array();
- if (empty($this->owner))
- throw new Exception\RequestErrorException("Missing required userdata 'uid' or owner must be set");
- $this->set_param($params, "clone_addr", $args, 0, "string", null, function(string $url) {
-
- });
- $this->set_param($params, "repo_name", $args, 1, "string", null);
- $this->set_param($params, "auth_username", $args, 2, "string", null);
- $this->set_param($params, "auth_password", $args, 3, "string", null);
- $this->set_param($params, "mirror", $args, 4, "bool", false);
- $this->set_param($params, "private", $args, 5, "bool", false);
- $this->set_param($params, "description", $args, 6, "string", null);
- $this->set_param($params, "uid", array(), 0, "int", $this->owner->id);
- $this->filter_params($params);
- $this->set_scope("migrate");
- $resp = parent::method_post($params);
- $this->json_set_property($this->json_decode($resp));
- return $this;
- }
-
- public function sync() {
- if ($this->mirror) {
- $this->set_scope("sync");
- $this->method_post();
- return true;
- }
- return false;
- }
- }
- }
- ?>
|