123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?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.3
- */
- 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 (!$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->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->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;
- 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(
- "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
- "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
- "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
- "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
- "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
- "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
- "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
- );
- $params = array_filter($params, function($val) {
- return $val != null;
- });
- parent::create($params);
- }
- }
- }
- ?>
|