Browse Source

Added getter on Repos, with scoping client/user/org

Joachim M. Giæver 6 years ago
parent
commit
e3226a19cd
5 changed files with 71 additions and 7 deletions
  1. 5 3
      index.php
  2. 2 2
      src/API/Request/Base.php
  3. 28 0
      src/API/Request/Branches.php
  4. 35 1
      src/API/Request/Repos.php
  5. 1 1
      src/API/Request/Users.php

+ 5 - 3
index.php

@@ -27,7 +27,9 @@ try {
 
     $repos = $me->repos()->load();
 
-    echo "\nNormal repo\n";
+    $repo = $repos->get("gogs-php-api-client");
+
+    /*echo "\nNormal repo\n";
     foreach($repos->all() as $key => $repo)
         echo sprintf("* %s: %s\n", $key, $repo->name);
 
@@ -107,7 +109,7 @@ try {
         throw new ApiException\NotAuthorizedException("Creating organization", $e->getCode(), $e);
     } catch (ApiException\HTTPUnexpectedResponse $e) {
         echo $e->getResponse();
-    }*/
+    }
 
     echo "\nLooking up organizations of test-" . $me->username . "\n";
     foreach($orgs->search(array("name" => "test-" . $me->username))->all() as $key => $org)
@@ -124,7 +126,7 @@ try {
         echo sprintf("%s: delete %s\n", $key, $user->delete() ? "true" : "false");
 
 
-
+    */
     echo "\n\n\nLOG:\n" . join("\n", $client->get_log());
 
 } catch (ApiException\NotAuthorizedException $e) {

+ 2 - 2
src/API/Request/Base.php

@@ -114,8 +114,8 @@ namespace Gogs\API\Request {
          * @return null
          */
         public function get(string $s) {
-            if (!$this->set_scope("get"))
-                throw new Exception\NotImplementedException("::get:: Not implemented for class '" . get_class($this) . "'");
+            //if (!$this->set_scope("get"))
+            throw new Exception\NotImplementedException("::get:: Not implemented for class '" . get_class($this) . "'");
 
             return null;
         }

+ 28 - 0
src/API/Request/Branches.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Gogs\API\Request {
+
+    final class Brances extends Collection {
+        protected $repo;
+
+        public function __construct(string $api_url, string $api_token, Repo $repo) {
+            $this->repo = $repo;
+            parent::__construct($api_url, $api_token);
+        }
+
+        protected function set_scope(string $method) {
+            switch ($method) {
+            case "get":
+            case "load":
+                if ($owner == null)
+                    throw new Exception\InvalidMethodRequestException("Missing repository for branches");
+
+                $this->scope = sprintf("/repos/%s/%s/branches", $this->repo->owner, $this->repo->name);
+                return true;
+            }
+
+            return false;
+        }
+    }
+
+}

+ 35 - 1
src/API/Request/Repos.php

@@ -6,7 +6,7 @@ namespace Gogs\API\Request {
      * Repos is a collection of repos.
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1.1
+     * @version 0.1.2
      */
     final class Repos extends Collection {
 
@@ -53,6 +53,40 @@ namespace Gogs\API\Request {
             return true;
         }
 
+        /** 
+         * Get a single repository by name.
+         *
+         * If the `owner` is set, the name can be just the
+         * actual name of the repo 
+         * 
+         * @param string $name 
+         * @return 
+         */
+        public function get(string $name) {
+
+            if (isset($this->owner) && strpos($name, "/") === false )
+                $name = sprintf("%s/%s", $this->owner->username, $name);
+
+            if ($repo = $this->by_key($name))
+                return $repo;
+
+            $owner = !empty($this->owner) ? $this->owner : (
+                ($pos = strpos($name, "/")) !== false ? 
+                new User($this->url, $this->token, substr($name, 0, $pos)) : null
+            );
+
+            $repo = (new Repo(
+                $this->url, 
+                $this->token, 
+                $owner, 
+                ($pos = strpos($name, "/")) ? substr($name, $pos + 1) : $name
+            ))->load();
+
+            $this->add($repo, $repo->full_name);
+
+            return $repo;
+        }
+
         /** 
          * @see Collection
          */

+ 1 - 1
src/API/Request/Users.php

@@ -45,7 +45,7 @@ namespace Gogs\API\Request {
             return $user;
         }
 
-        public function get(string $s = "") {
+        public function get(string $s) {
 
             if ($this->by_key($s))
                 return $this->by_key($s);